topical media & game development
mobile-game-present-lib-quintus-audio.js / js
/*global Quintus:false */
Quintus.Audio = function(Q) {
Q.audio = {
channels: [],
channelMax: Q.options.channelMax || 10,
active: {}
};
Q.enableSound = function() {
var hasTouch = !!('ontouchstart' in window);
if(!hasTouch) {
Q.audio.enableDesktopSound();
} else {
Q.audio.enableMobileSound();
}
return Q;
};
// Dummy methods
Q.play = function() {};
Q.audioSprites = function() {};
Q.audio.enableWebAudioSound = function() {
Q.audioContext = initializeNewWebAudioContext();
Q.loadAssetAudio = function(key,src,callback,errorCallback) {
var snd = new Audio(),
baseName = Q._removeExtension(src),
extension = null,
filename = null;
/* Find a supported type */
extension =
Q._detect(Q.options.audioSupported,
function(extension) {
return snd.canPlayType(Q.audioMimeTypes[extension]) ?
extension : null;
});
/* No supported audio = trigger ok callback anyway */
if(!extension) {
callback(key,null);
return;
}
Q.audioContext.loadSound( Q.options.audioPath + baseName + "." + extension, baseName);
callback(key,true);
return true;
};
Q.play = function(s) {
Q.audioContext.playSound(s);
}
};
Q.audio.enableDesktopSound = function() {
for (var i=0;i<Q.audio.channelMax;i++) {
Q.audio.channels[i] = {};
Q.audio.channels[i]['channel'] = new Audio();
Q.audio.channels[i]['finished'] = -1;
}
Q.play = function(s,debounce) {
if(Q.audio.active[s]) { return; }
if(debounce) {
Q.audio.active[s] = true;
setTimeout(function() {
delete Q.audio.active[s];
},debounce);
}
for (var i=0;i<Q.audio.channels.length;i++) {
var now = new Date();
if (Q.audio.channels[i]['finished'] < now.getTime()) {
Q.audio.channels[i]['finished'] = now.getTime() + Q.asset(s).duration*1000;
Q.audio.channels[i]['channel'].src = Q.asset(s).src;
Q.audio.channels[i]['channel'].load();
Q.audio.channels[i]['channel'].play();
break;
}
}
};
};
Q.audio.enableMobileSound = function() {
var isiOS = navigator.userAgent.match(/iPad|iPod|iPhone/i) != null;
Q.audioSprites = function(asset) {
if(Q._isString(asset)) { asset = Q.asset(asset); }
Q.audio.spriteFile = asset['resources'][0].replace(/\.[a-z]+/,"");
if(Q.audio.sprites && Q.audio.sprites[sound]) {
var startTime = Q.audio.sprites[sound].start - 0.05,
endDelay = Q.audio.sprites[sound].end - startTime;
Q.audio.sheet.currentTime = startTime;
Q.audio.sheet.play();
clearTimeout(Q.audio.silenceTimer);
Q.audio.silenceTimer = setTimeout(Q.audio.timer,endDelay*1000 + 500);
}
};
};
};
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.