X-Git-Url: http://105106.c2e0p.group/sound.git/blobdiff_plain/45ddec7e27af3ded3ebb81d2d2200e6331deb8f1..aad8eacf291f9ec284ec61b7e3bf5547adb03b41:/sound.js diff --git a/sound.js b/sound.js index c4f9c0e..2303927 100644 --- a/sound.js +++ b/sound.js @@ -1,11 +1,33 @@ -function Sound() { +/* Copyright (c) 2014 Jer Noble + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +function Sound(src) { + + if (Sound.audioContext === undefined) { + var AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext; + Sound.audioContext = new AudioContext(); + } - if (Sound.audioContext === undefined) - Sound.audioContext = new webkitAudioContext(); - - this._src = null; this._networkState = this.NETWORK.EMPTY; - this._preload = true; + this._preload = this.PRELOAD.AUTO; this._buffered = {}; this._readyState = this.READY.NOTHING; this._seeking = false; @@ -15,7 +37,7 @@ function Sound() { this._played = {}; this._seekable = {}; this._ended = false; - this._autoplay = false; + this._autoplay = true; this._loop = false; this._volume = 1; this._muted = false; @@ -26,10 +48,14 @@ function Sound() { this.gainNode = null; this.ajax = null; - this.eventListeners = { }; - this.shouldBePlaying = 0; + this.eventListeners = { }; this.startTime = 0; this.nextStartTime = 0; + + this.autoplaying = false; + this.delayingTheLoadEvent = false; + + this.setSrc(src); } Sound.prototype = { @@ -57,6 +83,12 @@ Sound.prototype = { ENOUGH_DATA: 4, }, + PRELOAD: { + NONE: 0, + METADATA: 1, + AUTO: 2, + }, + load: function() { if (this.ajax) this.ajax.abort(); @@ -74,9 +106,21 @@ Sound.prototype = { this.setPlaybackRate(this.defaultPlaybackRate); this._error = null; - this._autoplay = true; + this.autoplaying = true; this.stopInternal(); + this.selectResource(); + + }, + + selectResource: function() { + this._networkState = this.NETWORK.NO_SOURCE; + this.delayingTheLoadEvent = true; + + setTimeout(this.selectResourceAsync.bind(this), 0); + }, + + selectResourceAsync: function() { if (!this._src) { this._networkState = this.NETWORK.EMPTY; return; @@ -85,6 +129,17 @@ Sound.prototype = { this._networkState = this.NETWORK.LOADING; this.dispatchEventAsync(new CustomEvent('loadstart')); + setTimeout(this.fetchResource(), 0); + }, + + fetchResource: function() { + if (this._preload === this.PRELOAD.NONE) { + this._networkState = this.NETWORK.IDLE; + this.dispatchEventAsync(new CustomEvent('suspend')); + this.delayingTheLoadEvent = false; + return; + } + this.ajax = new XMLHttpRequest(); this.ajax.open("GET", this._src, true); this.ajax.responseType = "arraybuffer"; @@ -92,18 +147,21 @@ Sound.prototype = { if (!this.ajax.response) return; + this._networkState = this.NETWORK.IDLE; + this.dispatchEventAsync(new CustomEvent('suspend')); this.setReadyState(this.READY.FUTURE_DATA); try { Sound.audioContext.decodeAudioData( - this.ajax.response, + this.ajax.response, function(buffer) { this.buffer = buffer; - if (this.shouldBePlaying) + if (this.autoplaying && this._paused && this._autoplay) this.play(); - }.bind(this), - function(error) { - console.log("Error in creating buffer for sound '" + this._src + "': " + error); + this.dispatchEventAsync(new CustomEvent('canplaythrough')); + }.bind(this), + function(error) { + console.log("Error in creating buffer for sound '" + this._src + "': " + error); }.bind(this) ); } catch(exception) { @@ -113,12 +171,21 @@ Sound.prototype = { this.ajax.onprogress = function() { this.dispatchEventAsync(new CustomEvent('progress')); }.bind(this); + this.ajax.onerror = function() { + this.error = { code: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED }; + this._networkState = this.NETWORK.NO_SOURCE; + this.dispatchEventAsync(new CustomEvent('error')); + this.delayingTheLoadEvent = false; + }.bind(this); this.ajax.send(); }, play: function() { + if (this._networkState === this.NETWORK.EMPTY) + this.loadResource(); + if (!this.buffer) { - this.shouldBePlaying = true; + this.autoplaying = true; return; } @@ -159,6 +226,9 @@ Sound.prototype = { pause: function() { + if (this._networkState === this.NETWORK.EMPTY) + this.loadResource(); + this._autoplay = false; if (!this._paused) { @@ -243,7 +313,7 @@ Sound.prototype = { setSrc: function(src) { this._src = src; - if (this._autoplay && this._src != null) + if (this._src) this.load(); }, @@ -264,21 +334,30 @@ Sound.prototype = { }, getPreload: function() { - if (!this._preload) - return 'none'; - return 'auto'; + switch (this._preload) { + case this.PRELOAD.NONE: return 'none'; + case this.PRELOAD.METADATA: return 'metadata'; + case this.PRELOAD.AUTO: return 'auto'; + default: return ''; + } }, setPreload: function(preload) { switch (preload) { - case 'none': - this._preload = false; - break; - default: - this._preload = true; - if (!this.buffer) - load(); - break; + default: + case 'none': + this._preload = this.PRELOAD.NONE; + break; + case 'metadata': + this._preload = this.PRELOAD.METADATA; + if (this._networkState === this.NETWORK.EMPTY) + this.load(); + break; + case 'auto': + this._preload = this.PRELOAD.auto; + if (this._networkState === this.NETWORK.EMPTY) + this.load(); + break; } }, @@ -288,7 +367,7 @@ Sound.prototype = { return this.nextStartTime + Sound.audioContext.currentTIme - this.startTime; }, - setCurrentTime: function(time) { + setCurrentTime: function(time) { this.nextStartTime = time; this.dispatchEventAsync(new CustomEvent('timeupdate')); if (!this.node) @@ -327,7 +406,7 @@ Sound.prototype = { }, setVolume: function(volume) { - if (this._volume == volume) + if (this._volume === volume) return; this._volume = volume; @@ -342,7 +421,7 @@ Sound.prototype = { }, setMuted: function(muted) { - if (this._muted == muted) + if (this._muted === muted) return; this._muted = muted; @@ -357,11 +436,11 @@ Sound.prototype = { }, setAutoplay: function(autoplay) { - if (this._autoplay == autoplay) + if (this._autoplay === autoplay) return; this._autoplay = autoplay; - if (this._autoplay && this._src != null) + if (this._autoplay && this._networkState === this.NETWORK.EMPTY) this.load(); }, @@ -469,8 +548,17 @@ Object.defineProperty(Sound.prototype, 'defaultMuted', { set: Sound.prototype.setDefaultMuted, }); +Object.defineProperty(Sound.prototype, 'preload', { + get: Sound.prototype.getPreload, + set: Sound.prototype.setPreload, +}); + document.createElement = function(elementName) { if (elementName === "Audio" || elementName === "audio") return new Sound(); return Document.prototype.createElement.call(this, elementName); +}; + +window.Audio = function(src) { + return new Sound(src); }; \ No newline at end of file