-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsound_loader.js.html
More file actions
337 lines (305 loc) · 10.5 KB
/
sound_loader.js.html
File metadata and controls
337 lines (305 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: sound_loader.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: sound_loader.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
window.AudioContext = window.AudioContext || window.webkitAudioContext;
/**
* Loads sound files and lets you know when they're all available. An instance of SoundLoader is available as {@link Splat.Game#sounds}.
* This implementation uses the Web Audio API, and if that is not available it automatically falls back to the HTML5 &lt;audio&gt; tag.
* @constructor
*/
function SoundLoader() {
/**
* The key-value object that stores named sounds.
* @member {object}
* @private
*/
this.sounds = {};
/**
* The total number of sounds to be loaded.
* @member {number}
* @private
*/
this.totalSounds = 0;
/**
* The number of sounds that have loaded completely.
* @member {number}
* @private
*/
this.loadedSounds = 0;
/**
* A flag signifying if sounds have been muted through {@link SoundLoader#mute}.
* @member {boolean}
* @private
*/
this.muted = false;
/**
* A key-value object that stores named looping sounds.
* @member {object}
* @private
*/
this.looping = {};
/**
* The Web Audio API AudioContext
* @member {external:AudioContext}
* @private
*/
this.context = new window.AudioContext();
this.gainNode = this.context.createGain();
this.gainNode.connect(this.context.destination);
this.volume = this.gainNode.gain.value;
}
/**
* Load an audio file.
* @param {string} name The name you want to use when you {@link SoundLoader#play} the sound.
* @param {string} path The path of the sound file.
*/
SoundLoader.prototype.load = function(name, path) {
var self = this;
if (this.totalSounds === 0) {
// safari on iOS mutes sounds until they're played in response to user input
// play a dummy sound on first touch
var firstTouchHandler = function() {
window.removeEventListener("click", firstTouchHandler);
window.removeEventListener("keydown", firstTouchHandler);
window.removeEventListener("touchstart", firstTouchHandler);
var source = self.context.createOscillator();
source.connect(self.gainNode);
source.start(0);
source.stop(0);
if (self.firstPlay) {
self.play(self.firstPlay, self.firstPlayLoop);
} else {
self.firstPlay = "workaround";
}
};
window.addEventListener("click", firstTouchHandler);
window.addEventListener("keydown", firstTouchHandler);
window.addEventListener("touchstart", firstTouchHandler);
}
this.totalSounds++;
var request = new XMLHttpRequest();
request.open("GET", path, true);
request.responseType = "arraybuffer";
request.addEventListener("readystatechange", function() {
if (request.readyState !== 4) {
return;
}
if (request.status !== 200 && request.status !== 0) {
console.error("Error loading sound " + path);
return;
}
self.context.decodeAudioData(request.response, function(buffer) {
self.sounds[name] = buffer;
self.loadedSounds++;
}, function(err) {
console.error("Error decoding audio data for " + path + ": " + err);
});
});
request.addEventListener("error", function() {
console.error("Error loading sound " + path);
});
try {
request.send();
} catch (e) {
console.error("Error loading sound", path, e);
}
};
/**
* Test if all sounds have loaded.
* @returns {boolean}
*/
SoundLoader.prototype.allLoaded = function() {
return this.totalSounds === this.loadedSounds;
};
/**
* Play a sound.
* @param {string} name The name given to the sound during {@link SoundLoader#load}
* @param {boolean} [loop=false] A flag denoting whether the sound should be looped. To stop a looped sound use {@link SoundLoader#stop}.
*/
SoundLoader.prototype.play = function(name, loop) {
if (loop && this.looping[name]) {
return;
}
if (!this.firstPlay) {
// let the iOS user input workaround handle it
this.firstPlay = name;
this.firstPlayLoop = loop;
return;
}
var snd = this.sounds[name];
if (snd === undefined) {
console.error("Unknown sound: " + name);
}
var source = this.context.createBufferSource();
source.buffer = snd;
source.connect(this.gainNode);
if (loop) {
source.loop = true;
this.looping[name] = source;
}
source.start(0);
};
/**
* Stop playing a sound. This currently only stops playing a sound that was looped earlier, and doesn't stop a sound mid-play. Patches welcome.
* @param {string} name The name given to the sound during {@link SoundLoader#load}
*/
SoundLoader.prototype.stop = function(name) {
if (!this.looping[name]) {
return;
}
this.looping[name].stop(0);
delete this.looping[name];
};
/**
* Silence all sounds. Sounds keep playing, but at zero volume. Call {@link SoundLoader#unmute} to restore the previous volume level.
*/
SoundLoader.prototype.mute = function() {
this.gainNode.gain.value = 0;
this.muted = true;
};
/**
* Restore volume to whatever value it was before {@link SoundLoader#mute} was called.
*/
SoundLoader.prototype.unmute = function() {
this.gainNode.gain.value = this.volume;
this.muted = false;
};
/**
* Set the volume of all sounds.
* @param {number} gain The desired volume level. A number between 0.0 and 1.0, with 0.0 being silent, and 1.0 being maximum volume.
*/
SoundLoader.prototype.setVolume = function(gain) {
this.volume = gain;
this.gainNode.gain = gain;
this.muted = false;
};
/**
* Test if the volume is currently muted.
* @return {boolean} True if the volume is currently muted.
*/
SoundLoader.prototype.isMuted = function() {
return this.muted;
};
function AudioTagSoundLoader() {
this.sounds = {};
this.totalSounds = 0;
this.loadedSounds = 0;
this.muted = false;
this.looping = {};
this.volume = new Audio().volume;
}
AudioTagSoundLoader.prototype.load = function(name, path) {
this.totalSounds++;
var audio = new Audio();
var self = this;
audio.addEventListener("error", function() {
console.error("Error loading sound " + path);
});
audio.addEventListener("canplaythrough", function() {
self.sounds[name] = audio;
self.loadedSounds++;
});
audio.volume = this.volume;
audio.src = path;
audio.load();
};
AudioTagSoundLoader.prototype.allLoaded = function() {
return this.totalSounds === this.loadedSounds;
};
AudioTagSoundLoader.prototype.play = function(name, loop) {
if (loop && this.looping[name]) {
return;
}
var snd = this.sounds[name];
if (snd === undefined) {
console.error("Unknown sound: " + name);
}
if (loop) {
snd.loop = true;
this.looping[name] = snd;
}
snd.play();
};
AudioTagSoundLoader.prototype.stop = function(name) {
var snd = this.looping[name];
if (!snd) {
return;
}
snd.loop = false;
snd.pause();
snd.currentTime = 0;
delete this.looping[name];
};
function setAudioTagVolume(sounds, gain) {
for (var name in sounds) {
if (sounds.hasOwnProperty(name)) {
sounds[name].volume = gain;
}
}
}
AudioTagSoundLoader.prototype.mute = function() {
setAudioTagVolume(this.sounds, 0);
this.muted = true;
};
AudioTagSoundLoader.prototype.unmute = function() {
setAudioTagVolume(this.sounds, this.volume);
this.muted = false;
};
AudioTagSoundLoader.prototype.setVolume = function(gain) {
this.volume = gain;
setAudioTagVolume(this.sounds, gain);
this.muted = false;
};
AudioTagSoundLoader.prototype.isMuted = function() {
return this.muted;
};
function FakeSoundLoader() {}
FakeSoundLoader.prototype.load = function() {};
FakeSoundLoader.prototype.allLoaded = function() { return true; };
FakeSoundLoader.prototype.play = function() {};
FakeSoundLoader.prototype.stop = function() {};
FakeSoundLoader.prototype.mute = function() {};
FakeSoundLoader.prototype.unmute = function() {};
FakeSoundLoader.prototype.setVolume = function() {};
FakeSoundLoader.prototype.isMuted = function() {
return true;
};
if (window.AudioContext) {
module.exports = SoundLoader;
} else if (window.Audio) {
module.exports = AudioTagSoundLoader;
} else {
console.log("This browser doesn't support the Web Audio API or the HTML5 audio tag.");
module.exports = FakeSoundLoader;
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-buffer.html">buffer</a></li><li><a href="module-KeyMap.html">KeyMap</a></li></ul><h3>Externals</h3><ul><li><a href="external-AudioContext.html">AudioContext</a></li><li><a href="external-canvas.html">canvas</a></li><li><a href="external-CanvasRenderingContext2D.html">CanvasRenderingContext2D</a></li><li><a href="external-image.html">image</a></li></ul><h3>Classes</h3><ul><li><a href="Accelerometer.html">Accelerometer</a></li><li><a href="Animation.html">Animation</a></li><li><a href="AnimationLoader.html">AnimationLoader</a></li><li><a href="FontLoader.html">FontLoader</a></li><li><a href="ImageLoader.html">ImageLoader</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="SceneManager.html">SceneManager</a></li><li><a href="SoundLoader.html">SoundLoader</a></li><li><a href="Splat.AnimatedEntity.html">AnimatedEntity</a></li><li><a href="Splat.AStar.html">AStar</a></li><li><a href="Splat.BinaryHeap.html">BinaryHeap</a></li><li><a href="Splat.Camera.html">Camera</a></li><li><a href="Splat.Entity.html">Entity</a></li><li><a href="Splat.EntityBoxCamera.html">EntityBoxCamera</a></li><li><a href="Splat.Game.html">Game</a></li><li><a href="Splat.math.Random.html">Random</a></li><li><a href="Splat.NinePatch.html">NinePatch</a></li><li><a href="Splat.Scene.html">Scene</a></li><li><a href="Splat.Timer.html">Timer</a></li></ul><h3>Namespaces</h3><ul><li><a href="Splat.html">Splat</a></li><li><a href="Splat.ads.html">ads</a></li><li><a href="Splat.leaderboards.html">leaderboards</a></li><li><a href="Splat.math.html">math</a></li><li><a href="Splat.saveData.html">saveData</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a> on Thu Sep 17 2015 23:22:42 GMT-0400 (EDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>