//"use strict";
var LibraryOpenAL = {
$AL__deps: ['$Browser'],
$AL: {
contexts: [],
currentContext: null,
QUEUE_INTERVAL: 25,
QUEUE_LOOKAHEAD: 100,
updateSources: function updateSources(context) {
for (var i = 0; i < context.src.length; i++) {
AL.updateSource(context.src[i]);
}
},
updateSource: function updateSource(src) {
#if OPENAL_DEBUG
var idx = AL.currentContext.src.indexOf(src);
#endif
if (src.state !== 0x1012 /* AL_PLAYING */) {
return;
}
var currentTime = AL.currentContext.ctx.currentTime;
var startTime = src.bufferPosition;
for (var i = src.buffersPlayed; i < src.queue.length; i++) {
var entry = src.queue[i];
var startOffset = startTime - currentTime;
var endTime = startTime + entry.buffer.duration;
// Clean up old buffers.
if (currentTime >= endTime) {
// Update our location in the queue.
src.bufferPosition = endTime;
src.buffersPlayed = i + 1;
// Stop / restart the source when we hit the end.
if (src.buffersPlayed >= src.queue.length) {
if (src.loop) {
AL.setSourceState(src, 0x1012 /* AL_PLAYING */);
} else {
AL.setSourceState(src, 0x1014 /* AL_STOPPED */);
}
}
}
// Process all buffers that'll be played before the next tick.
else if (startOffset < (AL.QUEUE_LOOKAHEAD / 1000) && !entry.src) {
// If the start offset is negative, we need to offset the actual buffer.
var offset = Math.abs(Math.min(startOffset, 0));
entry.src = AL.currentContext.ctx.createBufferSource();
entry.src.buffer = entry.buffer;
entry.src.connect(src.gain);
entry.src.start(startTime, offset);
#if OPENAL_DEBUG
console.log('updateSource queuing buffer ' + i + ' for source ' + idx + ' at ' + startTime + ' (offset by ' + offset + ')');
#endif
}
startTime = endTime;
}
},
setSourceState: function setSourceState(src, state) {
#if OPENAL_DEBUG
var idx = AL.currentContext.src.indexOf(src);
#endif
if (state === 0x1012 /* AL_PLAYING */) {
if (src.state !== 0x1013 /* AL_PAUSED */) {
src.state = 0x1012 /* AL_PLAYING */;
// Reset our position.
src.bufferPosition = AL.currentContext.ctx.currentTime;
src.buffersPlayed = 0;
#if OPENAL_DEBUG
console.log(