aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJukka Jylänki <jujjyl@gmail.com>2013-06-16 15:09:33 +0300
committerAlon Zakai <alonzakai@gmail.com>2013-09-24 16:56:44 -0700
commit84c042143b57382e07d1af137d47fd36288b34dd (patch)
treef560ae552389234c9e1c5f411afaea5d77e9d6fa
parente17329cd47c7c02658355112f7b43a28b693f229 (diff)
Improve the Web Audio callback buffer scheduling logic to stay ahead of buffer underflow by at least more than one sample block.
-rw-r--r--src/library_sdl.js10
1 files changed, 7 insertions, 3 deletions
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());
}