2012-02-02 70 views
3

我正在使用SDL audio播放聲音。我可以暫停回調嗎?

SDL_LockAudio講述了這樣:

不要在回調函數中調用這個,否則你會導致死鎖。

但是,SDL_PauseAudio不說,而是告訴:

此功能暫停和取消暫停音頻回調處理

我的混頻器的回調看起來是這樣的:

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庫(這是最可能是錯誤的,谷歌搜索後這個消息),我想應該有可能改變我的代碼修復,或者至少避免underrun。

所以,問題是:我可以暫停自己的回調?它能引起我看到的不足嗎?

回答

2

最後我想通了。

當在回調中調用SDL_PauseAudio(1);時,SDL將切換到另一個回調(僅將零置入音頻流中)。該函數被調用後,回調將結束執行。

因此,從回調調用此函數是安全的。