1 | "use strict";
|
2 |
|
3 | window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function SoundLoader() {
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 | this.sounds = {};
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 | this.totalSounds = 0;
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | this.loadedSounds = 0;
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 | this.muted = false;
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 | this.looping = {};
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 | this.context = new window.AudioContext();
|
48 |
|
49 | this.gainNode = this.context.createGain();
|
50 | this.gainNode.connect(this.context.destination);
|
51 | this.volume = this.gainNode.gain.value;
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | SoundLoader.prototype.load = function(name, path) {
|
59 | var self = this;
|
60 |
|
61 | if (this.totalSounds === 0) {
|
62 |
|
63 |
|
64 | var firstTouchHandler = function() {
|
65 | window.removeEventListener("click", firstTouchHandler);
|
66 | window.removeEventListener("keydown", firstTouchHandler);
|
67 | window.removeEventListener("touchstart", firstTouchHandler);
|
68 |
|
69 | var source = self.context.createOscillator();
|
70 | source.connect(self.gainNode);
|
71 | source.start(0);
|
72 | source.stop(0);
|
73 |
|
74 | if (self.firstPlay) {
|
75 | self.play(self.firstPlay, self.firstPlayLoop);
|
76 | } else {
|
77 | self.firstPlay = "workaround";
|
78 | }
|
79 | };
|
80 | window.addEventListener("click", firstTouchHandler);
|
81 | window.addEventListener("keydown", firstTouchHandler);
|
82 | window.addEventListener("touchstart", firstTouchHandler);
|
83 | }
|
84 |
|
85 | this.totalSounds++;
|
86 |
|
87 | var request = new XMLHttpRequest();
|
88 | request.open("GET", path, true);
|
89 | request.responseType = "arraybuffer";
|
90 | request.addEventListener("readystatechange", function() {
|
91 | if (request.readyState !== 4) {
|
92 | return;
|
93 | }
|
94 | if (request.status !== 200 && request.status !== 0) {
|
95 | console.error("Error loading sound " + path);
|
96 | return;
|
97 | }
|
98 | self.context.decodeAudioData(request.response, function(buffer) {
|
99 | self.sounds[name] = buffer;
|
100 | self.loadedSounds++;
|
101 | }, function(err) {
|
102 | console.error("Error decoding audio data for " + path + ": " + err);
|
103 | });
|
104 | });
|
105 | request.addEventListener("error", function() {
|
106 | console.error("Error loading sound " + path);
|
107 | });
|
108 | try {
|
109 | request.send();
|
110 | } catch (e) {
|
111 | console.error("Error loading sound", path, e);
|
112 | }
|
113 | };
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | SoundLoader.prototype.allLoaded = function() {
|
119 | return this.totalSounds === this.loadedSounds;
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | SoundLoader.prototype.play = function(name, loop) {
|
127 | if (loop && this.looping[name]) {
|
128 | return;
|
129 | }
|
130 | if (!this.firstPlay) {
|
131 |
|
132 | this.firstPlay = name;
|
133 | this.firstPlayLoop = loop;
|
134 | return;
|
135 | }
|
136 | var snd = this.sounds[name];
|
137 | if (snd === undefined) {
|
138 | console.error("Unknown sound: " + name);
|
139 | }
|
140 | var source = this.context.createBufferSource();
|
141 | source.buffer = snd;
|
142 | source.connect(this.gainNode);
|
143 | if (loop) {
|
144 | source.loop = true;
|
145 | this.looping[name] = source;
|
146 | }
|
147 | source.start(0);
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | SoundLoader.prototype.stop = function(name) {
|
154 | if (!this.looping[name]) {
|
155 | return;
|
156 | }
|
157 | this.looping[name].stop();
|
158 | delete this.looping[name];
|
159 | };
|
160 |
|
161 |
|
162 |
|
163 | SoundLoader.prototype.mute = function() {
|
164 | this.gainNode.gain.value = 0;
|
165 | this.muted = true;
|
166 | };
|
167 |
|
168 |
|
169 |
|
170 | SoundLoader.prototype.unmute = function() {
|
171 | this.gainNode.gain.value = this.volume;
|
172 | this.muted = false;
|
173 | };
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | SoundLoader.prototype.setVolume = function(gain) {
|
179 | this.volume = gain;
|
180 | this.gainNode.gain = gain;
|
181 | this.muted = false;
|
182 | };
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | SoundLoader.prototype.isMuted = function() {
|
188 | return this.muted;
|
189 | };
|
190 |
|
191 | function AudioTagSoundLoader() {
|
192 | this.sounds = {};
|
193 | this.totalSounds = 0;
|
194 | this.loadedSounds = 0;
|
195 | this.muted = false;
|
196 | this.looping = {};
|
197 | this.volume = new Audio().volume;
|
198 | }
|
199 | AudioTagSoundLoader.prototype.load = function(name, path) {
|
200 | this.totalSounds++;
|
201 |
|
202 | var audio = new Audio();
|
203 | var self = this;
|
204 | audio.addEventListener("error", function() {
|
205 | console.error("Error loading sound " + path);
|
206 | });
|
207 | audio.addEventListener("canplaythrough", function() {
|
208 | self.sounds[name] = audio;
|
209 | self.loadedSounds++;
|
210 | });
|
211 | audio.volume = this.volume;
|
212 | audio.src = path;
|
213 | audio.load();
|
214 | };
|
215 | AudioTagSoundLoader.prototype.allLoaded = function() {
|
216 | return this.totalSounds === this.loadedSounds;
|
217 | };
|
218 | AudioTagSoundLoader.prototype.play = function(name, loop) {
|
219 | if (loop && this.looping[name]) {
|
220 | return;
|
221 | }
|
222 | var snd = this.sounds[name];
|
223 | if (snd === undefined) {
|
224 | console.error("Unknown sound: " + name);
|
225 | }
|
226 | if (loop) {
|
227 | snd.loop = true;
|
228 | this.looping[name] = snd;
|
229 | }
|
230 | snd.play();
|
231 | };
|
232 | AudioTagSoundLoader.prototype.stop = function(name) {
|
233 | var snd = this.looping[name];
|
234 | if (!snd) {
|
235 | return;
|
236 | }
|
237 | snd.loop = false;
|
238 | snd.pause();
|
239 | snd.currentTime = 0;
|
240 | delete this.looping[name];
|
241 | };
|
242 | function setAudioTagVolume(sounds, gain) {
|
243 | for (var name in sounds) {
|
244 | if (sounds.hasOwnProperty(name)) {
|
245 | sounds[name].volume = gain;
|
246 | }
|
247 | }
|
248 | }
|
249 | AudioTagSoundLoader.prototype.mute = function() {
|
250 | setAudioTagVolume(this.sounds, 0);
|
251 | this.muted = true;
|
252 | };
|
253 | AudioTagSoundLoader.prototype.unmute = function() {
|
254 | setAudioTagVolume(this.sounds, this.volume);
|
255 | this.muted = false;
|
256 | };
|
257 | AudioTagSoundLoader.prototype.setVolume = function(gain) {
|
258 | this.volume = gain;
|
259 | setAudioTagVolume(this.sounds, gain);
|
260 | this.muted = false;
|
261 | };
|
262 | AudioTagSoundLoader.prototype.isMuted = function() {
|
263 | return this.muted;
|
264 | };
|
265 |
|
266 |
|
267 | function FakeSoundLoader() {}
|
268 | FakeSoundLoader.prototype.load = function() {};
|
269 | FakeSoundLoader.prototype.allLoaded = function() { return true; };
|
270 | FakeSoundLoader.prototype.play = function() {};
|
271 | FakeSoundLoader.prototype.stop = function() {};
|
272 | FakeSoundLoader.prototype.mute = function() {};
|
273 | FakeSoundLoader.prototype.unmute = function() {};
|
274 | FakeSoundLoader.prototype.setVolume = function() {};
|
275 | FakeSoundLoader.prototype.isMuted = function() {
|
276 | return true;
|
277 | };
|
278 |
|
279 | if (window.AudioContext) {
|
280 | module.exports = SoundLoader;
|
281 | } else if (window.Audio) {
|
282 | module.exports = AudioTagSoundLoader;
|
283 | } else {
|
284 | console.log("This browser doesn't support the Web Audio API or the HTML5 audio tag.");
|
285 | module.exports = FakeSoundLoader;
|
286 | }
|