I want make the audio speaker on a computer silence for 2 sec by writing 0's data to "default" device using ALSA (Advanced Linux Sound Architecture). Below is code snippet.

int val
int size = 24000;
u_char *tmp_buffer = (u_char *)malloc(size);

if ((val = snd_pcm_writei(pcm_handle, tmp_buffer, size)) == -EPIPE)

else if (val < 0)
    qDebug()<<"ERROR. Can't write to PCM device " << snd_strerror(val);


With the above code, it render as "chuck chuck .." noise not silence .

How to render silence output to speaker for 2 sec?

  • Wouldn't leaving the speaker off and playing nothing be quieter than trying to play what ends up being "white noise"? PS. If you're asking about programming specifically, stackoverflow.com is great – Xen2050 Feb 15 at 3:22
  • What are the sample format, rate, and channels? – CL. Feb 15 at 7:54
  • @CL,Sample rate 48000, channel is 2 and format is S16_LE. – Chakravarthi Pradeep Feb 15 at 8:47

With a sample rate of 48000 Hz, two seconds need 96000 frames, so that is the number you need to give to snd_pcm_writei().

With four bytes per frame, the size of the temporary buffer must be 384000.

  • snd_pcm_writei(pcm_handle, tmp_buffer, 384000) for 2 sec this will render silence or noise to speaker...? – Chakravarthi Pradeep Feb 16 at 3:00
  • 384000 frames would be wrong. – CL. Feb 16 at 9:47
  • I understand that temp buffer size should be 384000 (48000*2*2*2) but what should be number of frames for snd_pcm_writei? – Chakravarthi Pradeep Feb 16 at 10:54
  • The size of one frame is four bytes. – CL. Feb 16 at 16:33
  • thanks I understood (384000 /4) is number of frames.(pcm_handle, tmp_buffer, 96000) – Chakravarthi Pradeep Feb 17 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.