How to make speaker render silence output for 2 sec in ALSA ( Advanced Linux Sound Architecture)












0















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);
memset(tmp_buffer,0,size);

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

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

}
free(tmp_buffer);


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



How to render silence output to speaker for 2 sec?










share|improve this question

























  • 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
















0















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);
memset(tmp_buffer,0,size);

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

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

}
free(tmp_buffer);


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



How to render silence output to speaker for 2 sec?










share|improve this question

























  • 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














0












0








0








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);
memset(tmp_buffer,0,size);

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

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

}
free(tmp_buffer);


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



How to render silence output to speaker for 2 sec?










share|improve this question
















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);
memset(tmp_buffer,0,size);

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

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

}
free(tmp_buffer);


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



How to render silence output to speaker for 2 sec?







linux alsa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 15 at 1:09









JakeGould

32.2k1098141




32.2k1098141










asked Feb 15 at 1:04









Chakravarthi PradeepChakravarthi Pradeep

215




215













  • 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



















  • 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

















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







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





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





@CL,Sample rate 48000, channel is 2 and format is S16_LE.

– Chakravarthi Pradeep
Feb 15 at 8:47










1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























  • 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








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1405912%2fhow-to-make-speaker-render-silence-output-for-2-sec-in-alsa-advanced-linux-sou%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer
























  • 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


















1














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.






share|improve this answer
























  • 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
















1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 15 at 10:25









CL.CL.

1,321910




1,321910













  • 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





















  • 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



















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





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







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





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





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







thanks I understood (384000 /4) is number of frames.(pcm_handle, tmp_buffer, 96000)

– Chakravarthi Pradeep
Feb 17 at 9:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1405912%2fhow-to-make-speaker-render-silence-output-for-2-sec-in-alsa-advanced-linux-sou%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

Eduardo VII do Reino Unido