123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044 |
- (function() {
- // setup
- var cache = {};
- // setup the audio context
- var ctx = null,
- usingWebAudio = true,
- noAudio = false;
- if (typeof AudioContext !== 'undefined') {
- ctx = new AudioContext();
- } else if (typeof webkitAudioContext !== 'undefined') {
- ctx = new webkitAudioContext();
- } else if (typeof Audio !== 'undefined') {
- usingWebAudio = false;
- try {
- new Audio();
- } catch(e) {
- noAudio = true;
- }
- } else {
- usingWebAudio = false;
- noAudio = true;
- }
- // create a master gain node
- if (usingWebAudio) {
- var masterGain = (typeof ctx.createGain === 'undefined') ? ctx.createGainNode() : ctx.createGain();
- masterGain.gain.value = 1;
- masterGain.connect(ctx.destination);
- }
- // create global controller
- var HowlerGlobal = function() {
- this._volume = 1;
- this._muted = false;
- this.usingWebAudio = usingWebAudio;
- this._howls = [];
- };
- HowlerGlobal.prototype = {
- volume: function(vol) {
- var self = this;
- // make sure volume is a number
- vol = parseFloat(vol);
- if (vol && vol >= 0 && vol <= 1) {
- self._volume = vol;
- if (usingWebAudio) {
- masterGain.gain.value = vol;
- }
- // loop through cache and change volume of all nodes that are using HTML5 Audio
- for (var key in self._howls) {
- if (self._howls.hasOwnProperty(key) && self._howls[key]._webAudio === false) {
- // loop through the audio nodes
- for (var i=0; i<self._howls[key]._audioNode.length; i++) {
- self._howls[key]._audioNode[i].volume = self._howls[key]._volume * self._volume;
- }
- }
- }
- return self;
- }
- // return the current global volume
- return (usingWebAudio) ? masterGain.gain.value : self._volume;
- },
- mute: function() {
- this._setMuted(true);
- return this;
- },
-
- unmute: function() {
- this._setMuted(false);
- return this;
- },
-
- _setMuted: function(muted) {
- var self = this;
- self._muted = muted;
- if (usingWebAudio) {
- masterGain.gain.value = muted ? 0 : self._volume;
- }
- for (var key in self._howls) {
- if (self._howls.hasOwnProperty(key) && self._howls[key]._webAudio === false) {
- // loop through the audio nodes
- for (var i=0; i<self._howls[key]._audioNode.length; i++) {
- self._howls[key]._audioNode[i].muted = muted;
- }
- }
- }
- }
- };
- var Howler = new HowlerGlobal();
- // check for browser codec support
- var audioTest = null;
- if (!noAudio) {
- audioTest = new Audio();
- var codecs = {
- mp3: !!audioTest.canPlayType('audio/mpeg;').replace(/^no$/,''),
- opus: !!audioTest.canPlayType('audio/ogg; codecs="opus"').replace(/^no$/,''),
- ogg: !!audioTest.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/,''),
- wav: !!audioTest.canPlayType('audio/wav; codecs="1"').replace(/^no$/,''),
- m4a: !!(audioTest.canPlayType('audio/x-m4a;') || audioTest.canPlayType('audio/aac;')).replace(/^no$/,''),
- webm: !!audioTest.canPlayType('audio/webm; codecs="vorbis"').replace(/^no$/,'')
- };
- }
- // setup the audio object
- var Howl = function(o) {
- var self = this;
- // setup the defaults
- self._autoplay = o.autoplay || false;
- self._buffer = o.buffer || false;
- self._duration = o.duration || 0;
- self._format = o.format || null;
- self._loop = o.loop || false;
- self._loaded = false;
- self._sprite = o.sprite || {};
- self._src = o.src || '';
- self._pos3d = o.pos3d || [0, 0, -0.5];
- self._volume = o.volume || 1;
- self._urls = o.urls || [];
- self._rate = o.rate || 1;
- // setup event functions
- self._onload = [o.onload || function() {}];
- self._onloaderror = [o.onloaderror || function() {}];
- self._onend = [o.onend || function() {}];
- self._onpause = [o.onpause || function() {}];
- self._onplay = [o.onplay || function() {}];
- self._onendTimer = [];
- // Web Audio or HTML5 Audio?
- self._webAudio = usingWebAudio && !self._buffer;
- // check if we need to fall back to HTML5 Audio
- self._audioNode = [];
- if (self._webAudio) {
- self._setupAudioNode();
- }
- // add this to an array of Howl's to allow global control
- Howler._howls.push(self);
- // load the track
- self.load();
- };
- // setup all of the methods
- Howl.prototype = {
-
- load: function() {
- var self = this,
- url = null;
- // if no audio is available, quit immediately
- if (noAudio) {
- self.on('loaderror');
- return;
- }
- var canPlay = {
- mp3: codecs.mp3,
- opus: codecs.opus,
- ogg: codecs.ogg,
- wav: codecs.wav,
- m4a: codecs.m4a,
- weba: codecs.webm
- };
- // loop through source URLs and pick the first one that is compatible
- for (var i=0; i<self._urls.length; i++) {
- var ext;
- if (self._format) {
- // use specified audio format if available
- ext = self._format;
- } else {
- // figure out the filetype (whether an extension or base64 data)
- ext = self._urls[i].toLowerCase().match(/.+\.([^?]+)(\?|$)/);
- ext = (ext && ext.length >= 2) ? ext[1] : self._urls[i].toLowerCase().match(/data\:audio\/([^?]+);/)[1];
- }
- if (canPlay[ext]) {
- url = self._urls[i];
- break;
- }
- }
- if (!url) {
- self.on('loaderror');
- return;
- }
- self._src = url;
- if (self._webAudio) {
- loadBuffer(self, url);
- } else {
- var newNode = new Audio();
- self._audioNode.push(newNode);
- // setup the new audio node
- newNode.src = url;
- newNode._pos = 0;
- newNode.preload = 'auto';
- newNode.volume = (Howler._muted) ? 0 : self._volume * Howler.volume();
-
- // add this sound to the cache
- cache[url] = self;
- // setup the event listener to start playing the sound
- // as soon as it has buffered enough
- var listener = function() {
- self._duration = newNode.duration;
- // setup a sprite if none is defined
- if (Object.getOwnPropertyNames(self._sprite).length === 0) {
- self._sprite = {_default: [0, self._duration * 1000]};
- }
- if (!self._loaded) {
- self._loaded = true;
- self.on('load');
- }
- if (self._autoplay) {
- self.play();
- }
- // clear the event listener
- newNode.removeEventListener('canplaythrough', listener, false);
- };
- newNode.addEventListener('canplaythrough', listener, false);
- newNode.load();
- }
- return self;
- },
- urls: function(urls) {
- var self = this;
- if (urls) {
- self.stop();
- self._urls = (typeof urls === 'string') ? [urls] : urls;
- self._loaded = false;
- self.load();
- return self;
- } else {
- return self._urls;
- }
- },
-
- play: function(sprite, callback) {
- var self = this;
- // if no sprite was passed but a callback was, update the variables
- if (typeof sprite === 'function') {
- callback = sprite;
- }
- // use the default sprite if none is passed
- if (!sprite || typeof sprite === 'function') {
- sprite = '_default';
- }
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('load', function() {
- self.play(sprite, callback);
- });
- return self;
- }
- // if the sprite doesn't exist, play nothing
- if (!self._sprite[sprite]) {
- if (typeof callback === 'function') callback();
- return self;
- }
- // get the node to playback
- self._inactiveNode(function(node) {
- // persist the sprite being played
- node._sprite = sprite;
- // determine where to start playing from
- var pos = (node._pos > 0) ? node._pos : self._sprite[sprite][0] / 1000,
- duration = self._sprite[sprite][1] / 1000 - node._pos;
- // determine if this sound should be looped
- var loop = !!(self._loop || self._sprite[sprite][2]);
- // set timer to fire the 'onend' event
- var soundId = (typeof callback === 'string') ? callback : Math.round(Date.now() * Math.random()) + '',
- timerId;
- (function() {
- var data = {
- id: soundId,
- sprite: sprite,
- loop: loop
- };
- timerId = setTimeout(function() {
- // if looping, restart the track
- if (!self._webAudio && loop) {
- self.stop(data.id, data.timer).play(sprite, data.id);
- }
- // set web audio node to paused at end
- if (self._webAudio && !loop) {
- self._nodeById(data.id).paused = true;
- }
- // end the track if it is HTML audio and a sprite
- if (!self._webAudio && !loop) {
- self.stop(data.id, data.timer);
- }
- // fire ended event
- self.on('end', soundId);
- }, duration * 1000);
- // store the reference to the timer
- self._onendTimer.push(timerId);
- // remember which timer to cancel
- data.timer = self._onendTimer[self._onendTimer.length - 1];
- })();
- if (self._webAudio) {
- var loopStart = self._sprite[sprite][0] / 1000,
- loopEnd = self._sprite[sprite][1] / 1000;
- // set the play id to this node and load into context
- node.id = soundId;
- node.paused = false;
- refreshBuffer(self, [loop, loopStart, loopEnd], soundId);
- self._playStart = ctx.currentTime;
- node.gain.value = self._volume;
- if (typeof node.bufferSource.start === 'undefined') {
- node.bufferSource.noteGrainOn(0, pos, duration);
- } else {
- node.bufferSource.start(0, pos, duration);
- }
- } else {
- if (node.readyState === 4) {
- node.id = soundId;
- node.currentTime = pos;
- node.muted = Howler._muted;
- node.volume = self._volume * Howler.volume();
- setTimeout(function() { node.play(); }, 0);
- } else {
- self._clearEndTimer(timerId);
- (function(){
- var sound = self,
- playSprite = sprite,
- fn = callback,
- newNode = node;
- var listener = function() {
- sound.play(playSprite, fn);
- // clear the event listener
- newNode.removeEventListener('canplaythrough', listener, false);
- };
- newNode.addEventListener('canplaythrough', listener, false);
- })();
- return self;
- }
- }
- // fire the play event and send the soundId back in the callback
- self.on('play');
- if (typeof callback === 'function') callback(soundId);
- return self;
- });
- return self;
- },
-
- pause: function(id, timerId) {
- var self = this;
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('play', function() {
- self.pause(id);
- });
- return self;
- }
- // clear 'onend' timer
- self._clearEndTimer(timerId || 0);
- var activeNode = (id) ? self._nodeById(id) : self._activeNode();
- if (activeNode) {
- activeNode._pos = self.pos(null, id);
- if (self._webAudio) {
- // make sure the sound has been created
- if (!activeNode.bufferSource) {
- return self;
- }
- activeNode.paused = true;
- if (typeof activeNode.bufferSource.stop === 'undefined') {
- activeNode.bufferSource.noteOff(0);
- } else {
- activeNode.bufferSource.stop(0);
- }
- } else {
- activeNode.pause();
- }
- }
- self.on('pause');
- return self;
- },
- stop: function(id, timerId) {
- var self = this;
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('play', function() {
- self.stop(id);
- });
- return self;
- }
- // clear 'onend' timer
- self._clearEndTimer(timerId || 0);
- var activeNode = (id) ? self._nodeById(id) : self._activeNode();
- if (activeNode) {
- activeNode._pos = 0;
- if (self._webAudio) {
- // make sure the sound has been created
- if (!activeNode.bufferSource) {
- return self;
- }
- activeNode.paused = true;
- if (typeof activeNode.bufferSource.stop === 'undefined') {
- activeNode.bufferSource.noteOff(0);
- } else {
- activeNode.bufferSource.stop(0);
- }
- } else {
- activeNode.pause();
- activeNode.currentTime = 0;
- }
- }
- return self;
- },
- mute: function(id) {
- var self = this;
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('play', function() {
- self.mute(id);
- });
- return self;
- }
- var activeNode = (id) ? self._nodeById(id) : self._activeNode();
- if (activeNode) {
- if (self._webAudio) {
- activeNode.gain.value = 0;
- } else {
- activeNode.volume = 0;
- }
- }
- return self;
- },
- unmute: function(id) {
- var self = this;
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('play', function() {
- self.unmute(id);
- });
- return self;
- }
- var activeNode = (id) ? self._nodeById(id) : self._activeNode();
- if (activeNode) {
- if (self._webAudio) {
- activeNode.gain.value = self._volume;
- } else {
- activeNode.volume = self._volume;
- }
- }
- return self;
- },
- volume: function(vol, id) {
- var self = this;
- // make sure volume is a number
- vol = parseFloat(vol);
- if (vol >= 0 && vol <= 1) {
- self._volume = vol;
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('play', function() {
- self.volume(vol, id);
- });
- return self;
- }
- var activeNode = (id) ? self._nodeById(id) : self._activeNode();
- if (activeNode) {
- if (self._webAudio) {
- activeNode.gain.value = vol;
- } else {
- activeNode.volume = vol * Howler.volume();
- }
- }
- return self;
- } else {
- return self._volume;
- }
- },
- loop: function(loop) {
- var self = this;
- if (typeof loop === 'boolean') {
- self._loop = loop;
- return self;
- } else {
- return self._loop;
- }
- },
- sprite: function(sprite) {
- var self = this;
- if (typeof sprite === 'object') {
- self._sprite = sprite;
- return self;
- } else {
- return self._sprite;
- }
- },
- pos: function(pos, id) {
- var self = this;
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('load', function() {
- self.pos(pos);
- });
- return typeof pos === 'number' ? self : self._pos || 0;
- }
- // make sure we are dealing with a number for pos
- pos = parseFloat(pos);
- var activeNode = (id) ? self._nodeById(id) : self._activeNode();
- if (activeNode) {
- if (self._webAudio) {
- if (pos >= 0) {
- activeNode._pos = pos;
- self.pause(id).play(activeNode._sprite, id);
- return self;
- } else {
- return activeNode._pos + (ctx.currentTime - self._playStart);
- }
- } else {
- if (pos >= 0) {
- activeNode.currentTime = pos;
- return self;
- } else {
- return activeNode.currentTime;
- }
- }
- } else if (pos >= 0) {
- return self;
- } else {
- // find the first inactive node to return the pos for
- for (var i=0; i<self._audioNode.length; i++) {
- if (self._audioNode[i].paused && self._audioNode[i].readyState === 4) {
- return (self._webAudio) ? self._audioNode[i]._pos : self._audioNode[i].currentTime;
- }
- }
- }
- },
- pos3d: function(x, y, z, id) {
- var self = this;
- // set a default for the optional 'y' & 'z'
- y = (typeof y === 'undefined' || !y) ? 0 : y;
- z = (typeof z === 'undefined' || !z) ? -0.5 : z;
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('play', function() {
- self.pos3d(x, y, z, id);
- });
- return self;
- }
- if (x >= 0 || x < 0) {
- if (self._webAudio) {
- var activeNode = (id) ? self._nodeById(id) : self._activeNode();
- if (activeNode) {
- self._pos3d = [x, y, z];
- activeNode.panner.setPosition(x, y, z);
- }
- }
- } else {
- return self._pos3d;
- }
- return self;
- },
- fade: function(from, to, len, callback, id) {
- var self = this,
- diff = Math.abs(from - to),
- dir = from > to ? 'down' : 'up',
- steps = diff / 0.01,
- stepTime = len / steps;
- // if the sound hasn't been loaded, add it to the event queue
- if (!self._loaded) {
- self.on('load', function() {
- self.fade(from, to, len, callback, id);
- });
- return self;
- }
- // set the volume to the start position
- self.volume(from, id);
- for (var i=1; i<=steps; i++) {
- (function() {
- var change = self._volume + (dir === 'up' ? 0.01 : -0.01) * i,
- vol = Math.round(1000 * change) / 1000,
- toVol = to;
- setTimeout(function() {
- self.volume(vol, id);
- if (vol === toVol) {
- if (callback) callback();
- }
- }, stepTime * i);
- })();
- }
- },
- fadeIn: function(to, len, callback) {
- return this.volume(0).play().fade(0, to, len, callback);
- },
-
- fadeOut: function(to, len, callback, id) {
- var self = this;
- return self.fade(self._volume, to, len, function() {
- if (callback) callback();
- self.pause(id);
- // fire ended event
- self.on('end');
- }, id);
- },
- _nodeById: function(id) {
- var self = this,
- node = self._audioNode[0];
- // find the node with this ID
- for (var i=0; i<self._audioNode.length; i++) {
- if (self._audioNode[i].id === id) {
- node = self._audioNode[i];
- break;
- }
- }
- return node;
- },
-
- _activeNode: function() {
- var self = this,
- node = null;
- // find the first playing node
- for (var i=0; i<self._audioNode.length; i++) {
- if (!self._audioNode[i].paused) {
- node = self._audioNode[i];
- break;
- }
- }
- // remove excess inactive nodes
- self._drainPool();
- return node;
- },
- _inactiveNode: function(callback) {
- var self = this,
- node = null;
- // find first inactive node to recycle
- for (var i=0; i<self._audioNode.length; i++) {
- if (self._audioNode[i].paused && self._audioNode[i].readyState === 4) {
- callback(self._audioNode[i]);
- node = true;
- break;
- }
- }
- // remove excess inactive nodes
- self._drainPool();
- if (node) {
- return;
- }
- // create new node if there are no inactives
- var newNode;
- if (self._webAudio) {
- newNode = self._setupAudioNode();
- callback(newNode);
- } else {
- self.load();
- newNode = self._audioNode[self._audioNode.length - 1];
- newNode.addEventListener('loadedmetadata', function() {
- callback(newNode);
- });
- }
- },
- /**
- * If there are more than 5 inactive audio nodes in the pool, clear out the rest.
- */
- _drainPool: function() {
- var self = this,
- inactive = 0,
- i;
- // count the number of inactive nodes
- for (i=0; i<self._audioNode.length; i++) {
- if (self._audioNode[i].paused) {
- inactive++;
- }
- }
- // remove excess inactive nodes
- for (i=self._audioNode.length-1; i>=0; i--) {
- if (inactive <= 5) {
- break;
- }
- if (self._audioNode[i].paused) {
- // disconnect the audio source if using Web Audio
- if (self._webAudio) {
- self._audioNode[i].disconnect(0);
- }
- inactive--;
- self._audioNode.splice(i, 1);
- }
- }
- },
- _clearEndTimer: function(timerId) {
- var self = this,
- timer = self._onendTimer.indexOf(timerId);
- // make sure the timer gets cleared
- timer = timer >= 0 ? timer : 0;
- if (self._onendTimer[timer]) {
- clearTimeout(self._onendTimer[timer]);
- self._onendTimer.splice(timer, 1);
- }
- },
- _setupAudioNode: function() {
- var self = this,
- node = self._audioNode,
- index = self._audioNode.length;
- // create gain node
- node[index] = (typeof ctx.createGain === 'undefined') ? ctx.createGainNode() : ctx.createGain();
- node[index].gain.value = self._volume;
- node[index].paused = true;
- node[index]._pos = 0;
- node[index].readyState = 4;
- node[index].connect(masterGain);
- // create the panner
- node[index].panner = ctx.createPanner();
- node[index].panner.setPosition(self._pos3d[0], self._pos3d[1], self._pos3d[2]);
- node[index].panner.connect(node[index]);
- return node[index];
- },
- on: function(event, fn) {
- var self = this,
- events = self['_on' + event];
- if (typeof fn === "function") {
- events.push(fn);
- } else {
- for (var i=0; i<events.length; i++) {
- if (fn) {
- events[i].call(self, fn);
- } else {
- events[i].call(self);
- }
- }
- }
- return self;
- },
- off: function(event, fn) {
- var self = this,
- events = self['_on' + event],
- fnString = fn.toString();
- // loop through functions in the event for comparison
- for (var i=0; i<events.length; i++) {
- if (fnString === events[i].toString()) {
- events.splice(i, 1);
- break;
- }
- }
- return self;
- },
-
- unload: function() {
- var self = this;
- // stop playing any active nodes
- var nodes = self._audioNode;
- for (var i=0; i<self._audioNode.length; i++) {
- self.stop(nodes[i].id);
- if (!self._webAudio) {
- // remove the source if using HTML5 Audio
- nodes[i].src = '';
- } else {
- // disconnect the output from the master gain
- nodes[i].disconnect(0);
- }
- }
- // remove the reference in the global Howler object
- var index = Howler._howls.indexOf(self);
- if (index) {
- Howler._howls.splice(index, 1);
- }
- // delete this sound from the cache
- delete cache[self._src];
- self = null;
- }
- };
- // only define these functions when using WebAudio
- if (usingWebAudio) {
- var loadBuffer = function(obj, url) {
- // check if the buffer has already been cached
- if (url in cache) {
- // set the duration from the cache
- obj._duration = cache[url].duration;
- // load the sound into this object
- loadSound(obj);
- } else {
- // load the buffer from the URL
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url, true);
- xhr.responseType = 'arraybuffer';
- xhr.onload = function() {
- // decode the buffer into an audio source
- ctx.decodeAudioData(xhr.response, function(buffer) {
- if (buffer) {
- cache[url] = buffer;
- loadSound(obj, buffer);
- }
- });
- };
- xhr.onerror = function() {
- // if there is an error, switch the sound to HTML Audio
- if (obj._webAudio) {
- obj._buffer = true;
- obj._webAudio = false;
- obj._audioNode = [];
- delete obj._gainNode;
- obj.load();
- }
- };
- try {
- xhr.send();
- } catch (e) {
- xhr.onerror();
- }
- }
- };
- var loadSound = function(obj, buffer) {
- // set the duration
- obj._duration = (buffer) ? buffer.duration : obj._duration;
- // setup a sprite if none is defined
- if (Object.getOwnPropertyNames(obj._sprite).length === 0) {
- obj._sprite = {_default: [0, obj._duration * 1000]};
- }
- // fire the loaded event
- if (!obj._loaded) {
- obj._loaded = true;
- obj.on('load');
- }
- if (obj._autoplay) {
- obj.play();
- }
- };
- var refreshBuffer = function(obj, loop, id) {
- // determine which node to connect to
- var node = obj._nodeById(id);
- // setup the buffer source for playback
- node.bufferSource = ctx.createBufferSource();
- node.bufferSource.buffer = cache[obj._src];
- node.bufferSource.connect(node.panner);
- node.bufferSource.loop = loop[0];
- if (loop[0]) {
- node.bufferSource.loopStart = loop[1];
- node.bufferSource.loopEnd = loop[1] + loop[2];
- }
- node.bufferSource.playbackRate.value = obj._rate;
- };
- }
- /**
- * Add support for AMD (Asynchronous Module Definition) libraries such as require.js.
- */
- if (typeof define === 'function' && define.amd) {
- define(function() {
- return {
- Howler: Howler,
- Howl: Howl
- };
- });
- }
-
- // define globally in case AMD is not available or available but not used
- window.Howler = Howler;
- window.Howl = Howl;
-
- })();
|