Howl = function (o) { var self = this; // Throw an error if no source is provided. if (!o.src || o.src.length === 0) { console.error('An array of source files must be passed with any new Howl.'); return; } self.init(o); }
n/a
Howl = function (o) { var self = this; // Throw an error if no source is provided. if (!o.src || o.src.length === 0) { console.error('An array of source files must be passed with any new Howl.'); return; } self.init(o); }
n/a
_cleanBuffer = function (node) { var self = this; if (self._scratchBuffer) { node.bufferSource.onended = null; node.bufferSource.disconnect(0); try { node.bufferSource.buffer = self._scratchBuffer; } catch(e) {} } node.bufferSource = null; return self; }
...
if (typeof sound._node.bufferSource.stop === 'undefined') {
sound._node.bufferSource.noteOff(0);
} else {
sound._node.bufferSource.stop(0);
}
// Clean up the buffer source.
self._cleanBuffer(sound._node);
} else if (!isNaN(sound._node.duration) || sound._node.duration === Infinity) {
sound._node.pause();
}
}
}
// Fire the pause event, unless `true` is passed as the 2nd argument.
...
_clearTimer = function (id) { var self = this; if (self._endTimers[id]) { clearTimeout(self._endTimers[id]); delete self._endTimers[id]; } return self; }
...
playWebAudio();
} else {
// Wait for the audio to load and then begin playback.
var event = !isRunning && self._state === 'loaded' ? 'resume' : 'load';
self.once(event, playWebAudio, isRunning ? sound._id : null);
// Cancel the end timer.
self._clearTimer(sound._id);
}
} else {
// Fire this when the sound is ready to play to begin HTML5 Audio playback.
var playHtml5 = function() {
node.currentTime = seek;
node.muted = sound._muted || self._muted || Howler._muted || node.muted;
node.volume = sound._volume * Howler.volume();
...
_drain = function () { var self = this; var limit = self._pool; var cnt = 0; var i = 0; // If there are less sounds than the max pool size, we are done. if (self._sounds.length < limit) { return; } // Count the number of inactive sounds. for (i=0; i<self._sounds.length; i++) { if (self._sounds[i]._ended) { cnt++; } } // Remove excess inactive sounds, going in reverse order. for (i=self._sounds.length - 1; i>=0; i--) { if (cnt <= limit) { return; } if (self._sounds[i]._ended) { // Disconnect the audio source when using Web Audio. if (self._webAudio && self._sounds[i]._node) { self._sounds[i]._node.disconnect(0); } // Remove sounds until we have the pool size. self._sounds.splice(i, 1); cnt--; } } }
...
/**
* Return an inactive sound from the pool or create a new one.
* @return {Sound} Sound playback object.
*/
_inactiveSound: function() {
var self = this;
self._drain();
// Find the first inactive node to recycle.
for (var i=0; i<self._sounds.length; i++) {
if (self._sounds[i]._ended) {
return self._sounds[i].reset();
}
}
...
_emit = function (event, id, msg) { var self = this; var events = self['_on' + event]; // Loop through event store and fire all functions. for (var i=events.length-1; i>=0; i--) { if (!events[i].id || events[i].id === id || event === 'load') { setTimeout(function(fn) { fn.call(this, id, msg); }.bind(self, events[i].fn), 0); // If this event was setup with `once`, remove it. if (events[i].once) { self.off(event, events[i].fn, events[i].id); } } } return self; }
...
} else if (self.state === 'suspended') {
self.state = 'resuming';
self.ctx.resume().then(function() {
self.state = 'running';
// Emit to all Howls that the audio has resumed.
for (var i=0; i<self._howls.length; i++) {
self._howls[i]._emit('resume');
}
});
if (self._suspendTimer) {
clearTimeout(self._suspendTimer);
self._suspendTimer = null;
}
...
_ended = function (sound) { var self = this; var sprite = sound._sprite; // Should this sound loop? var loop = !!(sound._loop || self._sprite[sprite][2]); // Fire the ended event. self._emit('end', sound._id); // Restart the playback for HTML5 Audio loop. if (!self._webAudio && loop) { self.stop(sound._id, true).play(sound._id); } // Restart this timer if on a Web Audio loop. if (self._webAudio && loop) { self._emit('play', sound._id); sound._seek = sound._start || 0; sound._rateSeek = 0; sound._playStart = Howler.ctx.currentTime; var timeout = ((sound._stop - sound._start) * 1000) / Math.abs(sound._rate); self._endTimers[sound._id] = setTimeout(self._ended.bind(self, sound), timeout); } // Mark the node as paused. if (self._webAudio && !loop) { sound._paused = true; sound._ended = true; sound._seek = sound._start || 0; sound._rateSeek = 0; self._clearTimer(sound._id); // Clean up the buffer source. self._cleanBuffer(sound._node); // Attempt to auto-suspend AudioContext if no sounds are still playing. Howler._autoSuspend(); } // When using a sprite, end the track. if (!self._webAudio && !loop) { self.stop(sound._id); } return self; }
n/a
_getSoundIds = function (id) { var self = this; if (typeof id === 'undefined') { var ids = []; for (var i=0; i<self._sounds.length; i++) { ids.push(self._sounds[i]._id); } return ids; } else { return [id]; } }
...
self.masterGain.gain.value = vol;
}
// Loop through and change volume for all HTML5 audio nodes.
for (var i=0; i<self._howls.length; i++) {
if (!self._howls[i]._webAudio) {
// Get all of the sounds in this Howl group.
var ids = self._howls[i]._getSoundIds();
// Loop through all sounds and change the volumes.
for (var j=0; j<ids.length; j++) {
var sound = self._howls[i]._soundById(ids[j]);
if (sound && sound._node) {
sound._node.volume = sound._volume * vol;
...
_inactiveSound = function () { var self = this; self._drain(); // Find the first inactive node to recycle. for (var i=0; i<self._sounds.length; i++) { if (self._sounds[i]._ended) { return self._sounds[i].reset(); } } // If no inactive node was found, create a new one. return new Sound(self); }
...
sprite = null;
} else {
id = null;
}
}
// Get the selected node, or get one from the pool.
var sound = id ? self._soundById(id) : self._inactiveSound();
// If the sound doesn't exist, do nothing.
if (!sound) {
return null;
}
// Select the sprite definition.
...
_loadQueue = function () { var self = this; if (self._queue.length > 0) { var task = self._queue[0]; // don't move onto the next task until this one is done self.once(task.event, function() { self._queue.shift(); self._loadQueue(); }); task.action(); } return self; }
...
if (self._queue.length > 0) {
var task = self._queue[0];
// don't move onto the next task until this one is done
self.once(task.event, function() {
self._queue.shift();
self._loadQueue();
});
task.action();
}
return self;
},
...
_refreshBuffer = function (sound) { var self = this; // Setup the buffer source for playback. sound._node.bufferSource = Howler.ctx.createBufferSource(); sound._node.bufferSource.buffer = cache[self._src]; // Connect to the correct node. if (sound._panner) { sound._node.bufferSource.connect(sound._panner); } else { sound._node.bufferSource.connect(sound._node); } // Setup looping and playback rate. sound._node.bufferSource.loop = sound._loop; if (sound._loop) { sound._node.bufferSource.loopStart = sound._start || 0; sound._node.bufferSource.loopEnd = sound._stop; } sound._node.bufferSource.playbackRate.value = sound._rate; return self; }
...
sound._loop = !!(sound._loop || self._sprite[sprite][2]);
// Begin the actual playback.
var node = sound._node;
if (self._webAudio) {
// Fire this when the sound is ready to play to begin Web Audio playback.
var playWebAudio = function() {
self._refreshBuffer(sound);
// Setup the playback params.
var vol = (sound._muted || self._muted) ? 0 : sound._volume;
node.gain.setValueAtTime(vol, Howler.ctx.currentTime);
sound._playStart = Howler.ctx.currentTime;
// Play the sound using the supported method.
...
_soundById = function (id) { var self = this; // Loop through all sounds and find the one with this ID. for (var i=0; i<self._sounds.length; i++) { if (id === self._sounds[i]._id) { return self._sounds[i]; } } return null; }
...
for (var i=0; i<self._howls.length; i++) {
if (!self._howls[i]._webAudio) {
// Get all of the sounds in this Howl group.
var ids = self._howls[i]._getSoundIds();
// Loop through all sounds and change the volumes.
for (var j=0; j<ids.length; j++) {
var sound = self._howls[i]._soundById(ids[j]);
if (sound && sound._node) {
sound._node.volume = sound._volume * vol;
}
}
}
}
...
_stopFade = function (id) { var self = this; var sound = self._soundById(id); if (sound && sound._interval) { if (self._webAudio) { sound._node.gain.cancelScheduledValues(Howler.ctx.currentTime); } clearInterval(sound._interval); sound._interval = null; self._emit('fade', id); } return self; }
...
if (sound && !sound._paused) {
// Reset the seek position.
sound._seek = self.seek(ids[i]);
sound._rateSeek = 0;
sound._paused = true;
// Stop currently running fades.
self._stopFade(ids[i]);
if (sound._node) {
if (self._webAudio) {
// make sure the sound has been created
if (!sound._node.bufferSource) {
return self;
}
...
duration = function (id) { var self = this; var duration = self._duration; // If we pass an ID, get the sound and return the sprite length. var sound = self._soundById(id); if (sound) { duration = self._sprite[sound._sprite][1] / 1000; } return duration; }
n/a
fade = function (from, to, len, id) { var self = this; var diff = Math.abs(from - to); var dir = from > to ? 'out' : 'in'; var steps = diff / 0.01; var stepLen = (steps > 0) ? len / steps : len; // Since browsers clamp timeouts to 4ms, we need to clamp our steps to that too. if (stepLen < 4) { steps = Math.ceil(steps / (4 / stepLen)); stepLen = 4; } // If the sound hasn't loaded, add it to the load queue to fade when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'fade', action: function() { self.fade(from, to, len, id); } }); return self; } // Set the volume to the start position. self.volume(from, id); // Fade the volume of one or all sounds. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { // Get the sound. var sound = self._soundById(ids[i]); // Create a linear fade or fall back to timeouts with HTML5 Audio. if (sound) { // Stop the previous fade if no sprite is being used (otherwise, volume handles this). if (!id) { self._stopFade(ids[i]); } // If we are using Web Audio, let the native methods do the actual fade. if (self._webAudio && !sound._muted) { var currentTime = Howler.ctx.currentTime; var end = currentTime + (len / 1000); sound._volume = from; sound._node.gain.setValueAtTime(from, currentTime); sound._node.gain.linearRampToValueAtTime(to, end); } var vol = from; sound._interval = setInterval(function(soundId, sound) { // Update the volume amount, but only if the volume should change. if (steps > 0) { vol += (dir === 'in' ? 0.01 : -0.01); } // Make sure the volume is in the right bounds. vol = Math.max(0, vol); vol = Math.min(1, vol); // Round to within 2 decimal points. vol = Math.round(vol * 100) / 100; // Change the volume. if (self._webAudio) { if (typeof id === 'undefined') { self._volume = vol; } sound._volume = vol; } else { self.volume(vol, soundId, true); } // When the fade is complete, stop it and fire event. if ((to < from && vol <= to) || (to > from && vol >= to)) { clearInterval(sound._interval); sound._interval = null; self.volume(to, soundId); self._emit('fade', soundId); } }.bind(self, ids[i], sound), stepLen); } } return self; }
...
sound.pause(id);
```
- The deprecated `fadeIn` and `fadeOut` methods have been removed in favor of the single `fade` method.
```javascript
// Fade in a sound.
sound.fade(0, 1, 1000);
// Fade out the sound once the previous fade has ended.
sound.once('fade', function(){
sound.fade(1, 0, 1000);
});
```
...
init = function (o) { var self = this; // Setup user-defined default properties. self._orientation = o.orientation || [1, 0, 0]; self._stereo = o.stereo || null; self._pos = o.pos || null; self._pannerAttr = { coneInnerAngle: typeof o.coneInnerAngle !== 'undefined' ? o.coneInnerAngle : 360, coneOuterAngle: typeof o.coneOuterAngle !== 'undefined' ? o.coneOuterAngle : 360, coneOuterGain: typeof o.coneOuterGain !== 'undefined' ? o.coneOuterGain : 0, distanceModel: typeof o.distanceModel !== 'undefined' ? o.distanceModel : 'inverse', maxDistance: typeof o.maxDistance !== 'undefined' ? o.maxDistance : 10000, panningModel: typeof o.panningModel !== 'undefined' ? o.panningModel : 'HRTF', refDistance: typeof o.refDistance !== 'undefined' ? o.refDistance : 1, rolloffFactor: typeof o.rolloffFactor !== 'undefined' ? o.rolloffFactor : 1 }; // Setup event listeners. self._onstereo = o.onstereo ? [{fn: o.onstereo}] : []; self._onpos = o.onpos ? [{fn: o.onpos}] : []; self._onorientation = o.onorientation ? [{fn: o.onorientation}] : []; // Complete initilization with howler.js core's init function. return _super.call(this, o); }
...
/***************************************************************************/
/**
* Create the global controller. All contained methods and properties apply
* to all sounds that are currently playing or will be in the future.
*/
var HowlerGlobal = function() {
this.init();
};
HowlerGlobal.prototype = {
/**
* Initialize the global Howler object.
* @return {Howler}
*/
init: function() {
...
load = function () { var self = this; var url = null; // If no audio is available, quit immediately. if (Howler.noAudio) { self._emit('loaderror', null, 'No audio support.'); return; } // Make sure our source is in an array. if (typeof self._src === 'string') { self._src = [self._src]; } // Loop through the sources and pick the first one that is compatible. for (var i=0; i<self._src.length; i++) { var ext, str; if (self._format && self._format[i]) { // If an extension was specified, use that instead. ext = self._format[i]; } else { // Make sure the source is a string. str = self._src[i]; if (typeof str !== 'string') { self._emit('loaderror', null, 'Non-string found in selected audio sources - ignoring.'); continue; } // Extract the file extension from the URL or base64 data URI. ext = /^data:audio\/([^;,]+);/i.exec(str); if (!ext) { ext = /\.([^.]+)$/.exec(str.split('?', 1)[0]); } if (ext) { ext = ext[1].toLowerCase(); } } // Log a warning if no extension was found. if (!ext) { console.warn('No file extension was found. Consider using the "format" property or specify an extension.'); } // Check if this extension is available. if (ext && Howler.codecs(ext)) { url = self._src[i]; break; } } if (!url) { self._emit('loaderror', null, 'No codec support for selected audio sources.'); return; } self._src = url; self._state = 'loading'; // If the hosting page is HTTPS and the source isn't, // drop down to HTML5 Audio to avoid Mixed Content errors. if (window.location.protocol === 'https:' && url.slice(0, 5) === 'http:') { self._html5 = true; self._webAudio = false; } // Create a new sound object and add it to the pool. new Sound(self); // Load and decode the audio data for playback. if (self._webAudio) { loadBuffer(self); } return self; }
...
self.play();
}
});
}
// Load the source file unless otherwise specified.
if (self._preload) {
self.load();
}
return self;
},
/**
* Load the audio file.
...
loop = function () { var self = this; var args = arguments; var loop, id, sound; // Determine the values for loop and id. if (args.length === 0) { // Return the grou's loop value. return self._loop; } else if (args.length === 1) { if (typeof args[0] === 'boolean') { loop = args[0]; self._loop = loop; } else { // Return this sound's loop value. sound = self._soundById(parseInt(args[0], 10)); return sound ? sound._loop : false; } } else if (args.length === 2) { loop = args[0]; id = parseInt(args[1], 10); } // If no id is passed, get all ID's to be looped. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { sound = self._soundById(ids[i]); if (sound) { sound._loop = loop; if (self._webAudio && sound._node && sound._node.bufferSource) { sound._node.bufferSource.loop = loop; if (loop) { sound._node.bufferSource.loopStart = sound._start || 0; sound._node.bufferSource.loopEnd = sound._stop; } } } } return self; }
n/a
mute = function (muted, id) { var self = this; // If the sound hasn't loaded, add it to the load queue to mute when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'mute', action: function() { self.mute(muted, id); } }); return self; } // If applying mute/unmute to all sounds, update the group's value. if (typeof id === 'undefined') { if (typeof muted === 'boolean') { self._muted = muted; } else { return self._muted; } } // If no id is passed, get all ID's to be muted. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { // Get the sound. var sound = self._soundById(ids[i]); if (sound) { sound._muted = muted; if (self._webAudio && sound._node) { sound._node.gain.setValueAtTime(muted ? 0 : sound._volume, Howler.ctx.currentTime); } else if (sound._node) { sound._node.muted = Howler._muted ? true : muted; } self._emit('mute', sound._id); } } return self; }
...
sound.seek(10);
```
- `mute` and `unmute` have been consolidated into `mute`.
```javascript
// Mute a sound (or all sounds).
sound.mute(true);
Howler.mute(true);
// Unmute a sound (or all sounds).
sound.mute(false);
Howler.mute(false);
```
...
off = function (event, fn, id) { var self = this; var events = self['_on' + event]; var i = 0; if (fn) { // Loop through event store and remove the passed function. for (i=0; i<events.length; i++) { if (fn === events[i].fn && id === events[i].id) { events.splice(i, 1); break; } } } else if (event) { // Clear out all events of this type. self['_on' + event] = []; } else { // Clear out all events of every type. var keys = Object.keys(self); for (i=0; i<keys.length; i++) { if ((keys[i].indexOf('_on') === 0) && Array.isArray(self[keys[i]])) { self[keys[i]] = []; } } } return self; }
...
- Audio on Chrome for Android no longer gets stuck after a period of inactivity.
- Crash in iOS <9 webview.
- Bug in iOS that can cause audio distortion when opening/closing browser.
- Only setup AudioContext after first `Howl` is setup so that background audio on mobile devices behaves as expected.
## 1.1.29 (January 22, 2016)
- `ADDED` Error messages added onto each `loaderror` event (thanks Philip Silva).
- `FIXED` Various edge-case bugs by no longer comparing functions by string in `.off()` (
thanks richard-livingston).
- `FIXED` Edge case where multiple overlapping instances of the same sound won't all fire `end` (thanks richard-livingston).
- `FIXED` `end` event now fires correctly when changing the `rate` of a sound.
## 1.1.28 (October 22, 2015)
- `FIXED` Typo with iOS enabler that was preventing it from working.
## 1.1.27 (October 2, 2015)
...
on = function (event, fn, id, once) { var self = this; var events = self['_on' + event]; if (typeof fn === 'function') { events.push(once ? {id: id, fn: fn, once: once} : {id: id, fn: fn}); } return self; }
...
// Clear listener after first call.
sound.once('load', function(){
sound.play();
});
// Fires when the sound finishes playing.
sound.on('end', function(){
console.log('Finished!');
});
```
##### Control multiple sounds:
```javascript
var sound = new Howl({
...
once = function (event, fn, id) { var self = this; // Setup the event listener. self.on(event, fn, id, 1); return self; }
...
- The deprecated `fadeIn` and `fadeOut` methods have been removed in favor of the single `fade` method.
```javascript
// Fade in a sound.
sound.fade(0, 1, 1000);
// Fade out the sound once the previous fade has ended.
sound.once('fade', function(){
sound.fade(1, 0, 1000);
});
```
### New Features
- Lots of general code cleanup, simplification and reorganization.
- Howler.js is now modularized. The core represents the initial goal for howler.js with 100% compatibility across HTML5 Audio and
Web Audio. The spatial plugin adds spatial and stereo support through Web Audio API.
...
orientation = function (x, y, z, id) { var self = this; // Stop right here if not using Web Audio. if (!self._webAudio) { return self; } // If the sound hasn't loaded, add it to the load queue to change orientation when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'orientation', action: function() { self.orientation(x, y, z, id); } }); return self; } // Set the defaults for optional 'y' & 'z'. y = (typeof y !== 'number') ? self._orientation[1] : y; z = (typeof z !== 'number') ? self._orientation[2] : z; // Setup the group's spatial orientation if no ID is passed. if (typeof id === 'undefined') { // Return the group's spatial orientation if no parameters are passed. if (typeof x === 'number') { self._orientation = [x, y, z]; } else { return self._orientation; } } // Change the spatial orientation of one or all sounds in group. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { // Get the sound. var sound = self._soundById(ids[i]); if (sound) { if (typeof x === 'number') { sound._orientation = [x, y, z]; if (sound._node) { // Check if there is a panner setup and create a new one if not. if (!sound._panner) { // Make sure we have a position to setup the node with. if (!sound._pos) { sound._pos = self._pos || [0, 0, -0.5]; } setupPanner(sound, 'spatial'); } sound._panner.setOrientation(x, y, z); } self._emit('orientation', sound._id); } else { return sound._orientation; } } } return self; }
...
}
// If the sound hasn't loaded, add it to the load queue to change orientation when capable.
if (self._state !== 'loaded') {
self._queue.push({
event: 'orientation',
action: function() {
self.orientation(x, y, z, id);
}
});
return self;
}
// Set the defaults for optional 'y' & 'z'.
...
pannerAttr = function () { var self = this; var args = arguments; var o, id, sound; // Stop right here if not using Web Audio. if (!self._webAudio) { return self; } // Determine the values based on arguments. if (args.length === 0) { // Return the group's panner attribute values. return self._pannerAttr; } else if (args.length === 1) { if (typeof args[0] === 'object') { o = args[0]; // Set the grou's panner attribute values. if (typeof id === 'undefined') { self._pannerAttr = { coneInnerAngle: typeof o.coneInnerAngle !== 'undefined' ? o.coneInnerAngle : self._coneInnerAngle, coneOuterAngle: typeof o.coneOuterAngle !== 'undefined' ? o.coneOuterAngle : self._coneOuterAngle, coneOuterGain: typeof o.coneOuterGain !== 'undefined' ? o.coneOuterGain : self._coneOuterGain, distanceModel: typeof o.distanceModel !== 'undefined' ? o.distanceModel : self._distanceModel, maxDistance: typeof o.maxDistance !== 'undefined' ? o.maxDistance : self._maxDistance, panningModel: typeof o.panningModel !== 'undefined' ? o.panningModel : self._panningModel, refDistance: typeof o.refDistance !== 'undefined' ? o.refDistance : self._refDistance, rolloffFactor: typeof o.rolloffFactor !== 'undefined' ? o.rolloffFactor : self._rolloffFactor }; } } else { // Return this sound's panner attribute values. sound = self._soundById(parseInt(args[0], 10)); return sound ? sound._pannerAttr : self._pannerAttr; } } else if (args.length === 2) { o = args[0]; id = parseInt(args[1], 10); } // Update the values of the specified sounds. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { sound = self._soundById(ids[i]); if (sound) { // Merge the new values into the sound. var pa = sound._pannerAttr; pa = { coneInnerAngle: typeof o.coneInnerAngle !== 'undefined' ? o.coneInnerAngle : pa.coneInnerAngle, coneOuterAngle: typeof o.coneOuterAngle !== 'undefined' ? o.coneOuterAngle : pa.coneOuterAngle, coneOuterGain: typeof o.coneOuterGain !== 'undefined' ? o.coneOuterGain : pa.coneOuterGain, distanceModel: typeof o.distanceModel !== 'undefined' ? o.distanceModel : pa.distanceModel, maxDistance: typeof o.maxDistance !== 'undefined' ? o.maxDistance : pa.maxDistance, panningModel: typeof o.panningModel !== 'undefined' ? o.panningModel : pa.panningModel, refDistance: typeof o.refDistance !== 'undefined' ? o.refDistance : pa.refDistance, rolloffFactor: typeof o.rolloffFactor !== 'undefined' ? o.rolloffFactor : pa.rolloffFactor }; // Update the panner values or create a new panner if none exists. var panner = sound._panner; if (panner) { panner.coneInnerAngle = pa.coneInnerAngle; panner.coneOuterAngle = pa.coneOuterAngle; panner.coneOuterGain = pa.coneOuterGain; panner.distanceModel = pa.distanceModel; panner.maxDistance = pa.maxDistance; panner.panningModel = pa.panningModel; panner.refDistance = pa.refDistance; panner.rolloffFactor = pa.rolloffFactor; } else { // Make sure we have a position to setup the node with. if (!sound._pos) { sound._pos = self._pos || [0, 0, -0.5]; } // Create a new panner node. setupPanner(sound, 'spatial'); } } } return self; }
n/a
pause = function (id) { var self = this; // If the sound hasn't loaded, add it to the load queue to pause when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'pause', action: function() { self.pause(id); } }); return self; } // If no id is passed, get all ID's to be paused. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { // Clear the end timer. self._clearTimer(ids[i]); // Get the sound. var sound = self._soundById(ids[i]); if (sound && !sound._paused) { // Reset the seek position. sound._seek = self.seek(ids[i]); sound._rateSeek = 0; sound._paused = true; // Stop currently running fades. self._stopFade(ids[i]); if (sound._node) { if (self._webAudio) { // make sure the sound has been created if (!sound._node.bufferSource) { return self; } if (typeof sound._node.bufferSource.stop === 'undefined') { sound._node.bufferSource.noteOff(0); } else { sound._node.bufferSource.stop(0); } // Clean up the buffer source. self._cleanBuffer(sound._node); } else if (!isNaN(sound._node.duration) || sound._node.duration === Infinity) { sound._node.pause(); } } } // Fire the pause event, unless `true` is passed as the 2nd argument. if (!arguments[1]) { self._emit('pause', sound ? sound._id : null); } } return self; }
...
- The `play` method no longer takes a callback and immediately returns the playback sound id (this means you can no longer chain
onto the `play` method, but all others work the same).
```javascript
// Get sound id for a specific playback.
var id = sound.play();
// Pause this playback.
sound.pause(id);
```
- The deprecated `fadeIn` and `fadeOut` methods have been removed in favor of the single `fade` method.
```javascript
// Fade in a sound.
sound.fade(0, 1, 1000);
...
play = function (sprite, internal) { var self = this; var id = null; // Determine if a sprite, sound id or nothing was passed if (typeof sprite === 'number') { id = sprite; sprite = null; } else if (typeof sprite === 'string' && self._state === 'loaded' && !self._sprite[sprite]) { // If the passed sprite doesn't exist, do nothing. return null; } else if (typeof sprite === 'undefined') { // Use the default sound sprite (plays the full audio length). sprite = '__default'; // Check if there is a single paused sound that isn't ended. // If there is, play that sound. If not, continue as usual. var num = 0; for (var i=0; i<self._sounds.length; i++) { if (self._sounds[i]._paused && !self._sounds[i]._ended) { num++; id = self._sounds[i]._id; } } if (num === 1) { sprite = null; } else { id = null; } } // Get the selected node, or get one from the pool. var sound = id ? self._soundById(id) : self._inactiveSound(); // If the sound doesn't exist, do nothing. if (!sound) { return null; } // Select the sprite definition. if (id && !sprite) { sprite = sound._sprite || '__default'; } // If we have no sprite and the sound hasn't loaded, we must wait // for the sound to load to get our audio's duration. if (self._state !== 'loaded' && !self._sprite[sprite]) { self._queue.push({ event: 'play', action: function() { self.play(self._soundById(sound._id) ? sound._id : undefined); } }); return sound._id; } // Don't play the sound if an id was passed and it is already playing. if (id && !sound._paused) { // Trigger the play event, in order to keep iterating through queue. if (!internal) { setTimeout(function() { self._emit('play', sound._id); }, 0); } return sound._id; } // Make sure the AudioContext isn't suspended, and resume it if it is. if (self._webAudio) { Howler._autoResume(); } // Determine how long to play for and where to start playing. var seek = Math.max(0, sound._seek > 0 ? sound._seek : self._sprite[sprite][0] / 1000); var duration = Math.max(0, ((self._sprite[sprite][0] + self._sprite[sprite][1]) / 1000) - seek); var timeout = (duration * 1000) / Math.abs(sound._rate); // Update the parameters of the sound sound._paused = false; sound._ended = false; sound._sprite = sprite; sound._seek = seek; sound._start = self._sprite[sprite][0] / 1000; sound._stop = (self._sprite[sprite][0] + self._sprite[sprite][1]) / 1000; sound._loop = !!(sound._loop || self._sprite[sprite][2]); // Begin the actual playback. var node = sound._node; if (self._webAudio) { // Fire this when the sound is ready to play to begin Web Audio playback. var playWebAudio = function() { self._refreshBuffer(sound); // Setup the playback params. var vol = (sound._muted || self._muted) ? 0 : sound._volume; node.gain.setValueAtTime(vol, Howler.ctx.currentTime); sound._playStart = Howler.ctx.currentTime; // Play the sound using the supported method. if (typeof node.bufferSource.start === 'undefined') { sound._loop ? node.bufferSource.noteGrainOn(0, seek, 86400) : node.bufferSource.noteGrainOn(0, seek, duration); } else { sound._loop ? node.bufferSource.start(0, seek, 86400) : node.bufferSource.start(0, seek, duration); } // Start a new timer if none is present. if (timeout !== Infinity) { self._endTimers[sound._id] = setTimeout(self._ended.bind(self, sound), timeout); } if (!internal) { setTimeout(function() { self._emit('play', sound._id); }, 0); } }; var isRunning = (Howler.state === 'running'); if (self._state === 'loaded' && isRunning) { playWebAudio(); } else { // Wait for the audio to load and then begin playback. var event = !isRunning && self._state === 'loaded' ? 'resume' : 'load'; self.once(event, playWebAudio, isRunn ...
...
Howler.mute(false);
```
- The `play` method no longer takes a callback and immediately returns the playback sound id (this means you can no longer chain
onto the `play` method, but all others work the same).
```javascript
// Get sound id for a specific playback.
var id = sound.play();
// Pause this playback.
sound.pause(id);
```
- The deprecated `fadeIn` and `fadeOut` methods have been removed in favor of the single `fade` method.
...
playing = function (id) { var self = this; // Check the passed sound ID (if any). if (typeof id === 'number') { var sound = self._soundById(id); return sound ? !sound._paused : false; } // Otherwise, loop through all sounds and check if any are playing. for (var i=0; i<self._sounds.length; i++) { if (!self._sounds[i]._paused) { return true; } } return false; }
...
// Get the sound.
var sound = self._soundById(id);
if (sound) {
if (typeof seek === 'number' && seek >= 0) {
// Pause the sound and update position for restarting playback.
var playing = self.playing(id);
if (playing) {
self.pause(id, true);
}
// Move the position of the track and cancel timer.
sound._seek = seek;
sound._ended = false;
...
pos = function (x, y, z, id) { var self = this; // Stop right here if not using Web Audio. if (!self._webAudio) { return self; } // If the sound hasn't loaded, add it to the load queue to change position when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'pos', action: function() { self.pos(x, y, z, id); } }); return self; } // Set the defaults for optional 'y' & 'z'. y = (typeof y !== 'number') ? 0 : y; z = (typeof z !== 'number') ? -0.5 : z; // Setup the group's spatial position if no ID is passed. if (typeof id === 'undefined') { // Return the group's spatial position if no parameters are passed. if (typeof x === 'number') { self._pos = [x, y, z]; } else { return self._pos; } } // Change the spatial position of one or all sounds in group. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { // Get the sound. var sound = self._soundById(ids[i]); if (sound) { if (typeof x === 'number') { sound._pos = [x, y, z]; if (sound._node) { // Check if there is a panner setup and create a new one if not. if (!sound._panner || sound._panner.pan) { setupPanner(sound, 'spatial'); } sound._panner.setPosition(x, y, z); } self._emit('pos', sound._id); } else { return sound._pos; } } } return self; }
...
}
// If the sound hasn't loaded, add it to the load queue to change position when capable.
if (self._state !== 'loaded') {
self._queue.push({
event: 'pos',
action: function() {
self.pos(x, y, z, id);
}
});
return self;
}
// Set the defaults for optional 'y' & 'z'.
...
rate = function () { var self = this; var args = arguments; var rate, id; // Determine the values based on arguments. if (args.length === 0) { // We will simply return the current rate of the first node. id = self._sounds[0]._id; } else if (args.length === 1) { // First check if this is an ID, and if not, assume it is a new rate value. var ids = self._getSoundIds(); var index = ids.indexOf(args[0]); if (index >= 0) { id = parseInt(args[0], 10); } else { rate = parseFloat(args[0]); } } else if (args.length === 2) { rate = parseFloat(args[0]); id = parseInt(args[1], 10); } // Update the playback rate or return the current value. var sound; if (typeof rate === 'number') { // If the sound hasn't loaded, add it to the load queue to change playback rate when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'rate', action: function() { self.rate.apply(self, args); } }); return self; } // Set the group rate. if (typeof id === 'undefined') { self._rate = rate; } // Update one or all volumes. id = self._getSoundIds(id); for (var i=0; i<id.length; i++) { // Get the sound. sound = self._soundById(id[i]); if (sound) { // Keep track of our position when the rate changed and update the playback // start position so we can properly adjust the seek position for time elapsed. sound._rateSeek = self.seek(id[i]); sound._playStart = self._webAudio ? Howler.ctx.currentTime : sound._playStart; sound._rate = rate; // Change the playback rate. if (self._webAudio && sound._node && sound._node.bufferSource) { sound._node.bufferSource.playbackRate.value = rate; } else if (sound._node) { sound._node.playbackRate = rate; } // Reset the timers. var seek = self.seek(id[i]); var duration = ((self._sprite[sound._sprite][0] + self._sprite[sound._sprite][1]) / 1000) - seek; var timeout = (duration * 1000) / Math.abs(sound._rate); // Start a new end timer if sound is already playing. if (self._endTimers[id[i]] || !sound._paused) { self._clearTimer(id[i]); self._endTimers[id[i]] = setTimeout(self._ended.bind(self, sound), timeout); } self._emit('rate', sound._id); } } } else { sound = self._soundById(id); return sound ? sound._rate : self._rate; } return self; }
...
// Play returns a uniqe Sound ID that can be passed
// into any method on Howl to control that specific sound.
var id1 = sound.play();
var id2 = sound.play();
// Fade out the first sound and speed up the second.
sound.fade(1, 0, 1000, id1);
sound.rate(1.5, id2);
```
More in-depth examples (with accompanying live demos) can be found in the [examples directory](https://github.com/goldfire/howler
.js/tree/master/examples).
## Core
...
seek = function () { var self = this; var args = arguments; var seek, id; // Determine the values based on arguments. if (args.length === 0) { // We will simply return the current position of the first node. id = self._sounds[0]._id; } else if (args.length === 1) { // First check if this is an ID, and if not, assume it is a new seek position. var ids = self._getSoundIds(); var index = ids.indexOf(args[0]); if (index >= 0) { id = parseInt(args[0], 10); } else { id = self._sounds[0]._id; seek = parseFloat(args[0]); } } else if (args.length === 2) { seek = parseFloat(args[0]); id = parseInt(args[1], 10); } // If there is no ID, bail out. if (typeof id === 'undefined') { return self; } // If the sound hasn't loaded, add it to the load queue to seek when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'seek', action: function() { self.seek.apply(self, args); } }); return self; } // Get the sound. var sound = self._soundById(id); if (sound) { if (typeof seek === 'number' && seek >= 0) { // Pause the sound and update position for restarting playback. var playing = self.playing(id); if (playing) { self.pause(id, true); } // Move the position of the track and cancel timer. sound._seek = seek; sound._ended = false; self._clearTimer(id); // Restart the playback if the sound was playing. if (playing) { self.play(id, true); } // Update the seek position for HTML5 Audio. if (!self._webAudio && sound._node) { sound._node.currentTime = seek; } self._emit('seek', id); } else { if (self._webAudio) { var realTime = self.playing(id) ? Howler.ctx.currentTime - sound._playStart : 0; var rateSeek = sound._rateSeek ? sound._rateSeek - sound._seek : 0; return sound._seek + (rateSeek + realTime * Math.abs(sound._rate)); } else { return sound._node.currentTime; } } } return self; }
...
### Breaking Changes
- The `buffer` option is now named `html5`. Use this to force HTML5 Audio usage.
- The `urls` option is now named `src` to specify the audio file(s) to play.
- The `pos` method has been renamed to `seek`.
```javascript
// Change the seek position of a sound (in seconds).
sound.seek(10);
```
- `mute` and `unmute` have been consolidated into `mute`.
```javascript
// Mute a sound (or all sounds).
sound.mute(true);
...
state = function () { return this._state; }
n/a
stereo = function (pan, id) { var self = this; // Stop right here if not using Web Audio. if (!self._webAudio) { return self; } // If the sound hasn't loaded, add it to the load queue to change stereo pan when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'stereo', action: function() { self.stereo(pan, id); } }); return self; } // Check for PannerStereoNode support and fallback to PannerNode if it doesn't exist. var pannerType = (typeof Howler.ctx.createStereoPanner === 'undefined') ? 'spatial' : 'stereo'; // Setup the group's stereo panning if no ID is passed. if (typeof id === 'undefined') { // Return the group's stereo panning if no parameters are passed. if (typeof pan === 'number') { self._stereo = pan; self._pos = [pan, 0, 0]; } else { return self._stereo; } } // Change the streo panning of one or all sounds in group. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { // Get the sound. var sound = self._soundById(ids[i]); if (sound) { if (typeof pan === 'number') { sound._stereo = pan; sound._pos = [pan, 0, 0]; if (sound._node) { // If we are falling back, make sure the panningModel is equalpower. sound._pannerAttr.panningModel = 'equalpower'; // Check if there is a panner setup and create a new one if not. if (!sound._panner || !sound._panner.pan) { setupPanner(sound, pannerType); } if (pannerType === 'spatial') { sound._panner.setPosition(pan, 0, 0); } else { sound._panner.pan.value = pan; } } self._emit('stereo', sound._id); } else { return sound._stereo; } } } return self; }
...
// Stop right here if not using Web Audio.
if (!self.ctx || !self.ctx.listener) {
return self;
}
// Loop through all Howls and update their stereo panning.
for (var i=self._howls.length-1; i>=0; i--) {
self._howls[i].stereo(pan);
}
return self;
};
/**
* Get/set the position of the listener in 3D cartesian space. Sounds using
...
stop = function (id, internal) { var self = this; // If the sound hasn't loaded, add it to the load queue to stop when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'stop', action: function() { self.stop(id); } }); return self; } // If no id is passed, get all ID's to be stopped. var ids = self._getSoundIds(id); for (var i=0; i<ids.length; i++) { // Clear the end timer. self._clearTimer(ids[i]); // Get the sound. var sound = self._soundById(ids[i]); if (sound) { // Reset the seek position. sound._seek = sound._start || 0; sound._rateSeek = 0; sound._paused = true; sound._ended = true; // Stop currently running fades. self._stopFade(ids[i]); if (sound._node) { if (self._webAudio) { // make sure the sound has been created if (!sound._node.bufferSource) { if (!internal) { self._emit('stop', sound._id); } return self; } if (typeof sound._node.bufferSource.stop === 'undefined') { sound._node.bufferSource.noteOff(0); } else { sound._node.bufferSource.stop(0); } // Clean up the buffer source. self._cleanBuffer(sound._node); } else if (!isNaN(sound._node.duration) || sound._node.duration === Infinity) { sound._node.currentTime = sound._start || 0; sound._node.pause(); } } } if (sound && !internal) { self._emit('stop', sound._id); } } return self; }
...
if (!sound._node.bufferSource) {
return self;
}
if (typeof sound._node.bufferSource.stop === 'undefined') {
sound._node.bufferSource.noteOff(0);
} else {
sound._node.bufferSource.stop(0);
}
// Clean up the buffer source.
self._cleanBuffer(sound._node);
} else if (!isNaN(sound._node.duration) || sound._node.duration === Infinity) {
sound._node.pause();
}
...
unload = function () { var self = this; // Stop playing any active sounds. var sounds = self._sounds; for (var i=0; i<sounds.length; i++) { // Stop the sound if it is currently playing. if (!sounds[i]._paused) { self.stop(sounds[i]._id); } // Remove the source or disconnect. if (!self._webAudio) { // Set the source to 0-second silence to stop any downloading. sounds[i]._node.src = 'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBIAAAABAAEARKwAAIhYAQACABAAAABkYXRhAgAAAAEA'; // Remove any event listeners. sounds[i]._node.removeEventListener('error', sounds[i]._errorFn, false); sounds[i]._node.removeEventListener(Howler._canPlayEvent, sounds[i]._loadFn, false); } // Empty out all of the nodes. delete sounds[i]._node; // Make sure all timers are cleared out. self._clearTimer(sounds[i]._id); // Remove the references in the global Howler object. var index = Howler._howls.indexOf(self); if (index >= 0) { Howler._howls.splice(index, 1); } } // Delete this sound from the cache (if no other Howl is using it). var remCache = true; for (i=0; i<Howler._howls.length; i++) { if (Howler._howls[i]._src === self._src) { remCache = false; break; } } if (cache && remCache) { delete cache[self._src]; } // Clear global errors. Howler.noAudio = false; // Clear out `self`. self._state = 'unloaded'; self._sounds = []; self = null; return null; }
...
* Unload and destroy all currently loaded Howl objects.
* @return {Howler}
*/
unload: function() {
var self = this || Howler;
for (var i=self._howls.length-1; i>=0; i--) {
self._howls[i].unload();
}
// Create a new AudioContext to make sure it is fully reset.
if (self.usingWebAudio && self.ctx && typeof self.ctx.close !== 'undefined') {
self.ctx.close();
self.ctx = null;
setupAudioContext();
...
volume = function () { var self = this; var args = arguments; var vol, id; // Determine the values based on arguments. if (args.length === 0) { // Return the value of the groups' volume. return self._volume; } else if (args.length === 1 || args.length === 2 && typeof args[1] === 'undefined') { // First check if this is an ID, and if not, assume it is a new volume. var ids = self._getSoundIds(); var index = ids.indexOf(args[0]); if (index >= 0) { id = parseInt(args[0], 10); } else { vol = parseFloat(args[0]); } } else if (args.length >= 2) { vol = parseFloat(args[0]); id = parseInt(args[1], 10); } // Update the volume or return the current volume. var sound; if (typeof vol !== 'undefined' && vol >= 0 && vol <= 1) { // If the sound hasn't loaded, add it to the load queue to change volume when capable. if (self._state !== 'loaded') { self._queue.push({ event: 'volume', action: function() { self.volume.apply(self, args); } }); return self; } // Set the group volume. if (typeof id === 'undefined') { self._volume = vol; } // Update one or all volumes. id = self._getSoundIds(id); for (var i=0; i<id.length; i++) { // Get the sound. sound = self._soundById(id[i]); if (sound) { sound._volume = vol; // Stop currently running fades. if (!args[2]) { self._stopFade(id[i]); } if (self._webAudio && sound._node && !sound._muted) { sound._node.gain.setValueAtTime(vol, Howler.ctx.currentTime); } else if (sound._node && !sound._muted) { sound._node.volume = vol * Howler.volume(); } self._emit('volume', sound._id); } } } else { sound = id ? self._soundById(id) : self._sounds[0]; return sound ? sound._volume : 0; } return self; }
...
self._clearTimer(sound._id);
}
} else {
// Fire this when the sound is ready to play to begin HTML5 Audio playback.
var playHtml5 = function() {
node.currentTime = seek;
node.muted = sound._muted || self._muted || Howler._muted || node.muted;
node.volume = sound._volume * Howler.volume();
node.playbackRate = sound._rate;
node.play();
// Setup the new end timer.
if (timeout !== Infinity) {
self._endTimers[sound._id] = setTimeout(self._ended.bind(self, sound), timeout);
}
...