|
| 1 | +/* |
| 2 | + * DefaultPlugin |
| 3 | + * Visit http://createjs.com/ for documentation, updates and examples. |
| 4 | + * |
| 5 | + * |
| 6 | + * Copyright (c) 2012 gskinner.com, inc. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person |
| 9 | + * obtaining a copy of this software and associated documentation |
| 10 | + * files (the "Software"), to deal in the Software without |
| 11 | + * restriction, including without limitation the rights to use, |
| 12 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the |
| 14 | + * Software is furnished to do so, subject to the following |
| 15 | + * conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be |
| 18 | + * included in all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 28 | + */ |
| 29 | + |
| 30 | +// namespace: |
| 31 | +this.createjs = this.createjs || {}; |
| 32 | + |
| 33 | +(function () { |
| 34 | + "use strict"; |
| 35 | + /** |
| 36 | + * A default plugin class used as a base for all other plugins. |
| 37 | + * @class DefaultPlugin |
| 38 | + * @constructor |
| 39 | + * @since 0.5.3 |
| 40 | + */ |
| 41 | + |
| 42 | + var DefaultPlugin = function () { |
| 43 | + this._init(); |
| 44 | + }; |
| 45 | + var p = DefaultPlugin.prototype; |
| 46 | + DefaultPlugin.prototype.constructor = DefaultPlugin; |
| 47 | + |
| 48 | + /** |
| 49 | + * The capabilities of the plugin. This is generated via the {{#crossLink "DefaultPlugin/_generateCapabilities:method"}}{{/crossLink}} |
| 50 | + * method and is used internally. |
| 51 | + * @property _capabilities |
| 52 | + * @type {Object} |
| 53 | + * @default null |
| 54 | + * @protected |
| 55 | + * @static |
| 56 | + */ |
| 57 | + p._capabilities = null; |
| 58 | + |
| 59 | + /** |
| 60 | + * Object hash indexed by the source URI of each file to indicate if an audio source has begun loading, |
| 61 | + * is currently loading, or has completed loading. Can be used to store non boolean data after loading |
| 62 | + * is complete (for example arrayBuffers for web audio). |
| 63 | + * @property _audioSources |
| 64 | + * @type {Object} |
| 65 | + * @protected |
| 66 | + */ |
| 67 | + p._audioSources = null; |
| 68 | + |
| 69 | + /** |
| 70 | + * The internal master volume value of the plugin. |
| 71 | + * @property _volume |
| 72 | + * @type {Number} |
| 73 | + * @default 1 |
| 74 | + * @protected |
| 75 | + */ |
| 76 | + p._volume = 1; |
| 77 | + |
| 78 | + /** |
| 79 | + * A reference to a loader class used by a plugin that must be set. |
| 80 | + * @type {Object} |
| 81 | + * @protected |
| 82 | + */ |
| 83 | + p._loader; |
| 84 | + |
| 85 | + /** |
| 86 | + * A reference to a SoundInstance class used by a plugin that must be set. |
| 87 | + * @type {Object} |
| 88 | + * @protected; |
| 89 | + */ |
| 90 | + p._soundInstance; |
| 91 | + |
| 92 | + /** |
| 93 | + * An initialization function run by the constructor |
| 94 | + * @method _init |
| 95 | + * @protected |
| 96 | + */ |
| 97 | + p._init = function () { |
| 98 | + this._capabilities = s._capabilities; |
| 99 | + this._audioSources = {}; |
| 100 | + }; |
| 101 | + |
| 102 | + /** |
| 103 | + * Pre-register a sound for preloading and setup. This is called by {{#crossLink "Sound"}}{{/crossLink}}. |
| 104 | + * Note all plugins provide a <code>Loader</code> instance, which <a href="http://preloadjs.com" target="_blank">PreloadJS</a> |
| 105 | + * can use to assist with preloading. |
| 106 | + * @method register |
| 107 | + * @param {String} src The source of the audio |
| 108 | + * @param {Number} instances The number of concurrently playing instances to allow for the channel at any time. |
| 109 | + * Note that not every plugin will manage this value. |
| 110 | + * @return {Object} A result object, containing a "tag" for preloading purposes. |
| 111 | + */ |
| 112 | + p.register = function (src, instances) { |
| 113 | + this._audioSources[src] = true; |
| 114 | + if (!this._loader) {return;} |
| 115 | + var loader = {tag: new this._loader(src, this)}; |
| 116 | + return loader; |
| 117 | + }; |
| 118 | + |
| 119 | + /** |
| 120 | + * Checks if preloading has started for a specific source. If the source is found, we can assume it is loading, |
| 121 | + * or has already finished loading. |
| 122 | + * @method isPreloadStarted |
| 123 | + * @param {String} src The sound URI to check. |
| 124 | + * @return {Boolean} |
| 125 | + */ |
| 126 | + p.isPreloadStarted = function (src) { |
| 127 | + return (this._audioSources[src] != null); |
| 128 | + }; |
| 129 | + |
| 130 | + /** |
| 131 | + * Checks if preloading has finished for a specific source. |
| 132 | + * @method isPreloadComplete |
| 133 | + * @param {String} src The sound URI to load. |
| 134 | + * @return {Boolean} |
| 135 | + */ |
| 136 | + p.isPreloadComplete = function (src) { |
| 137 | + return (!(this._audioSources[src] == null || this._audioSources[src] == true)); |
| 138 | + }; |
| 139 | + |
| 140 | + /** |
| 141 | + * Remove a sound added using {{#crossLink "WebAudioPlugin/register"}}{{/crossLink}}. Note this does not cancel a preload. |
| 142 | + * @method removeSound |
| 143 | + * @param {String} src The sound URI to unload. |
| 144 | + */ |
| 145 | + p.removeSound = function (src) { |
| 146 | + delete(this._audioSources[src]); |
| 147 | + }; |
| 148 | + |
| 149 | + /** |
| 150 | + * Remove all sounds added using {{#crossLink "WebAudioPlugin/register"}}{{/crossLink}}. Note this does not cancel a preload. |
| 151 | + * @method removeAllSounds |
| 152 | + * @param {String} src The sound URI to unload. |
| 153 | + */ |
| 154 | + p.removeAllSounds = function () { |
| 155 | + this._audioSources = {}; |
| 156 | + }; |
| 157 | + |
| 158 | + /** |
| 159 | + * Handles internal preload completion. |
| 160 | + * @method _handlePreloadComplete |
| 161 | + * @protected |
| 162 | + */ |
| 163 | + p._handlePreloadComplete = function (loader) { |
| 164 | + createjs.Sound._sendFileLoadEvent(loader.src); |
| 165 | + loader.cleanUp(); |
| 166 | + }; |
| 167 | + |
| 168 | + /** |
| 169 | + * Internally preload a sound. |
| 170 | + * @method preload |
| 171 | + * @param {String} src The sound URI to load. |
| 172 | + * @param {Object} tag An HTML audio tag used to load src. |
| 173 | + */ |
| 174 | + p.preload = function (src, tag) { |
| 175 | + this._audioSources[src] = true; |
| 176 | + if (!this._loader) {return;} |
| 177 | + var loader = new this._loader(src, this); |
| 178 | + loader.onload = this._handlePreloadComplete; |
| 179 | + loader.load(); |
| 180 | + }; |
| 181 | + |
| 182 | + /** |
| 183 | + * Create a sound instance. If the sound has not been preloaded, it is internally preloaded here. |
| 184 | + * @method create |
| 185 | + * @param {String} src The sound source to use. |
| 186 | + * @param {Number} startTime Audio sprite property used to apply an offset, in milliseconds. |
| 187 | + * @param {Number} duration Audio sprite property used to set the time the clip plays for, in milliseconds. |
| 188 | + * @return {SoundInstance} A sound instance for playback and control. |
| 189 | + */ |
| 190 | + p.create = function (src, startTime, duration) { |
| 191 | + if (!this.isPreloadStarted(src)) {this.preload(src);} |
| 192 | + return new this._soundInstance(src, startTime, duration, this); |
| 193 | + }; |
| 194 | + |
| 195 | + /** |
| 196 | + * Set the master volume of the plugin, which affects all SoundInstances. |
| 197 | + * @method setVolume |
| 198 | + * @param {Number} value The volume to set, between 0 and 1. |
| 199 | + * @return {Boolean} If the plugin processes the setVolume call (true). The Sound class will affect all the |
| 200 | + * instances manually otherwise. |
| 201 | + */ |
| 202 | + p.setVolume = function (value) { |
| 203 | + this._volume = value; |
| 204 | + this._updateVolume(); |
| 205 | + return true; |
| 206 | + }; |
| 207 | + |
| 208 | + /** |
| 209 | + * Set the gain value for master audio. Should not be called externally. |
| 210 | + * @method _updateVolume |
| 211 | + * @protected |
| 212 | + */ |
| 213 | + p._updateVolume = function () { |
| 214 | + // Plugin Specific code |
| 215 | + }; |
| 216 | + |
| 217 | + /** |
| 218 | + * Get the master volume of the plugin, which affects all SoundInstances. |
| 219 | + * @method getVolume |
| 220 | + * @return The volume level, between 0 and 1. |
| 221 | + */ |
| 222 | + p.getVolume = function () { |
| 223 | + return this._volume; |
| 224 | + }; |
| 225 | + |
| 226 | + /** |
| 227 | + * Mute all sounds via the plugin. |
| 228 | + * @method setMute |
| 229 | + * @param {Boolean} value If all sound should be muted or not. Note that plugin-level muting just looks up |
| 230 | + * the mute value of Sound {{#crossLink "Sound/getMute"}}{{/crossLink}}, so this property is not used here. |
| 231 | + * @return {Boolean} If the mute call succeeds. |
| 232 | + */ |
| 233 | + p.setMute = function (value) { |
| 234 | + this._updateVolume(); |
| 235 | + return true; |
| 236 | + }; |
| 237 | + |
| 238 | + p.toString = function () { |
| 239 | + return "[DefaultAudioPlugin]"; |
| 240 | + }; |
| 241 | + |
| 242 | + createjs.DefaultPlugin = DefaultPlugin; |
| 243 | + |
| 244 | +}()); |
0 commit comments