延迟后snd_pcm_writei无法正常工作

时间:2021-01-13 01:19:14

I was trying to generate a beep sound using ALSA. 2 beep sounds should be generated, the 2nd one after a delay of 1 second. My code looks like the following.

我试图用ALSA产生一个哔声。应该产生2声蜂鸣声,第二声延迟1秒后。我的代码如下所示。

if ((err = snd_pcm_prepare (playback_handle)) < 0) {
    printf("cannot prepare audio interface for use \n");
    return -1;
}


for (i = 0; i < TABLE_SIZE; i ++){
    samples[i] = 100*sin(2.0*pi*f*(i/44100.0));
    if ((err = snd_pcm_writei (playback_handle, &samples[i], 4)) != 4) {
        printf("write to audio interface failed \n");
        return -1;
    }
}

//snd_pcm_drain(playback_handle);

printf("ending beep !!! \n");
sleep(1);

if ((err = snd_pcm_prepare (playback_handle)) < 0) {
    printf("cannot prepare audio interface for use \n");
    return -1;
}
for (i = 0; i < TABLE_SIZE; i ++){
    samples[i] = 100*sin(2.0*pi*f*(i/44100.0));
    if ((err = snd_pcm_writei (playback_handle, &samples[i], 4)) != 4) {
        printf("write to audio interface failed \n");
        return -1;
    }
}

Here the first beep is heard properly but the second beep is not OK. Can anyone help me with this issue.

在这里听到第一声嘟嘟声,但第二声蜂鸣声不正常。任何人都可以帮我解决这个问题。

1 个解决方案

#1


0  

snd_pcm_write*() just writes samples to the buffer, but returns immediately when all the samples are in the buffer.

snd_pcm_write *()只是将样本写入缓冲区,但是当所有样本都在缓冲区中时立即返回。

To wait until all samples in the buffer have been played, call snd_pcm_drain().

要等到缓冲区中的所有样本都已播放,请调用snd_pcm_drain()。

#1


0  

snd_pcm_write*() just writes samples to the buffer, but returns immediately when all the samples are in the buffer.

snd_pcm_write *()只是将样本写入缓冲区,但是当所有样本都在缓冲区中时立即返回。

To wait until all samples in the buffer have been played, call snd_pcm_drain().

要等到缓冲区中的所有样本都已播放,请调用snd_pcm_drain()。