C++ 我可以从回调本身暂停回调吗?
我习惯于播放声音 告诉我们: 不要从回调函数调用此函数,否则将导致死锁 但是,它并没有这样说,而是告诉我们: 此函数用于暂停和取消音频回调处理 我的混音器回调如下所示:C++ 我可以从回调本身暂停回调吗?,c++,audio,sdl,fedora,alsa,C++,Audio,Sdl,Fedora,Alsa,我习惯于播放声音 告诉我们: 不要从回调函数调用此函数,否则将导致死锁 但是,它并没有这样说,而是告诉我们: 此函数用于暂停和取消音频回调处理 我的混音器回调如下所示: void AudioPlaybackCallback( void *, core::bty::UInt8 *stream, int len ) { // number of bytes left to play in the current sample const int thisSample
void AudioPlaybackCallback( void *, core::bty::UInt8 *stream, int len )
{
// number of bytes left to play in the current sample
const int thisSampleLeft = currentSample.dataLength - currentSample.dataPos;
// number of bytes that will be sent to the audio stream
const int amountToPlay = std::min( thisSampleLeft, len );
if ( amountToPlay > 0 )
{
SDL_MixAudio( stream,
currentSample.data + currentSample.dataPos,
amountToPlay,
currentSample.volume );
// update the current sample
currentSample.dataPos += amountToPlay;
}
else
{
if ( PlayingQueue::QueueHasElements() )
{
// update the current sample
currentSample = PlayingQueue::QueuePop();
}
else
{
// since the current sample finished, and there are no more samples to
// play, pause the playback
SDL_PauseAudio( 1 );
}
}
}
PlayingQueue
是一个类,它提供对静态std::queue
对象的访问。没什么特别的
这很好,直到我们决定更新SDL和alsa库(现在已经没有回头路了)。从那时起,我在我的日志中看到:
ALSA lib pcm.c:7316:(snd_pcm_recover)发生欠运行
如果我假设SDL或alsa库中没有bug(谷歌搜索此消息后,这很可能是错误的),那么我想应该可以将代码更改为修复,或者至少避免运行不足
所以,问题是:我可以暂停回调本身吗?这会导致我看到的跑步不足吗?我终于明白了
当SDL_PauseAudio(1)时
在回调中被调用,然后SDL将切换到另一个回调(它只是将零放入音频流)。调用函数后,回调将完成执行
因此,从回调调用此函数是安全的