//"use strict";
var LibraryOpenAL = {
$AL__deps: ['$Browser'],
$AL: {
contexts: [],
currentContext: null,
QUEUE_INTERVAL: 25,
QUEUE_LOOKAHEAD: 100
},
alcProcessContext: function(context) {},
alcSuspendContext: function(context) {},
alcMakeContextCurrent: function(context) {
if (context == 0) {
AL.currentContext = null;
return 0;
} else {
AL.currentContext = AL.contexts[context - 1];
return 1;
}
},
alcGetContextsDevice: function(context) {
if (context <= AL.contexts.length && context > 0) {
// Returns the only one audio device
return 1;
}
return 0;
},
alcGetCurrentContext: function() {
for (var i = 0; i < AL.contexts.length; ++i) {
if (AL.contexts[i] == AL.currentContext) {
return i + 1;
}
}
return 0;
},
alcDestroyContext: function(context) {
// Stop playback, etc
clearInterval(context.interval);
},
alcCloseDevice: function(device) {
// Stop playback, etc
},
alcOpenDevice: function(deviceName) {
if (typeof(AudioContext) == "function" ||
typeof(webkitAudioContext) == "function") {
return 1; // non-null pointer -- we just simulate one device
} else {
return 0;
}
},
alcCreateContext__deps: ['updateSources'],
alcCreateContext: function(device, attrList) {
if (device != 1) {
return 0;
}
if (attrList) {
#if OPENAL_DEBUG
console.log("The attrList argument of alcCreateContext is not supported yet");
#endif
return 0;
}
var ctx;
try {
ctx = new AudioContext();
} catch (e) {
try {
ctx = new webkitAudioContext();
} catch (e) {}
}
if (ctx) {
var context = {
ctx: ctx,
err: 0,
src: [],
buf: [],
interval: setInterval(function() { _updateSources(context); }, AL.QUEUE_INTERVAL)
};
AL.contexts.push(context);
return AL.contexts.length;
} else {
return 0;
}
},
updateSources__deps: ['updateSource'],
updateSources: function(context) {
for (var i = 0; i < context.src.length; i++) {
_updateSource(context.src[i]);
}
},
updateSource__deps: ['setSourceState'],
updateSource: function(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) {
_setSourceState(src, 0x1012 /* AL_PLAYING */);
} else {
_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