From 84c042143b57382e07d1af137d47fd36288b34dd Mon Sep 17 00:00:00 2001 From: Jukka Jylänki Date: Sun, 16 Jun 2013 15:09:33 +0300 Subject: Improve the Web Audio callback buffer scheduling logic to stay ahead of buffer underflow by at least more than one sample block. --- src/library_sdl.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/library_sdl.js') diff --git a/src/library_sdl.js b/src/library_sdl.js index 79468af8..55e9aa49 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -1595,13 +1595,17 @@ var LibrarySDL = { var curtime = SDL.audioContext['currentTime']; if (curtime > SDL.audio.nextPlayTime && SDL.audio.nextPlayTime != 0) { console.log('warning: Audio callback had starved sending audio by ' + (curtime - SDL.audio.nextPlayTime) + ' seconds.'); - // Immediately queue up an extra buffer to force the sound feeding to be ahead by one sample block: - Browser.safeSetTimeout(SDL.audio.caller, 1); } var playtime = Math.max(curtime, SDL.audio.nextPlayTime); SDL.audio.soundSource[SDL.audio.nextSoundSource]['start'](playtime); - SDL.audio.nextPlayTime = playtime + SDL.audio.soundSource[SDL.audio.nextSoundSource]['buffer']['duration']; + var buffer_duration = SDL.audio.soundSource[SDL.audio.nextSoundSource]['buffer']['duration']; + SDL.audio.nextPlayTime = playtime + buffer_duration; SDL.audio.nextSoundSource = (SDL.audio.nextSoundSource + 1) % 4; + // Make sure we are always more than one sample block ahead of the current time to avoid starving. + if (curtime + buffer_duration + buffer_duration >= SDL.audio.nextPlayTime) { + // Immediately queue up an extra buffer to force the sound feeding to be ahead by more than one sample block: + Browser.safeSetTimeout(SDL.audio.caller, 1); + } } catch(e) { console.log('Web Audio API error playing back audio: ' + e.toString()); } -- cgit v1.2.3-18-g5258