forked from Wizcorp/boombox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
368 lines (288 loc) · 7.36 KB
/
index.js
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
require('soundmanager2');
var soundManager = window.soundManager;
var sm2Loaded = false; // Indicates whether SM2 has been loaded or not.
var queue = []; // Queue where we stuff adds and plays before SM2 has been loaded.
var channels = {}; // Maps of the created channels where sounds are played
var settings = {}; // General settings for each channel (only volume at the moment)
var sounds = {}; // list of available sounds
var boombox;
var volumeTransitions = {
none: function (soundId, volume, cb) {
var sound = sounds[soundId];
clearInterval(sound.interval);
sound.isInTransition = false;
sound.setVolume(volume);
if (cb) {
cb(soundId);
}
},
fadeTo: function (soundId, volume, cb) {
var time = 500;
var step = 50;
var sound = sounds[soundId];
sound.transitionCallBack = cb;
sound.isInTransition = true;
var diff = volume - sound.volume;
var volumeStep = step * diff / time;
clearInterval(sound.interval);
sound.interval = setInterval(function () {
var newVolume = sound.volume + volumeStep;
newVolume = volumeStep > 0 ? Math.min(newVolume, volume) : Math.max(newVolume, volume);
sound.setVolume(newVolume);
if (newVolume === volume) {
clearInterval(sound.interval);
sound.isInTransition = false;
sound.transitionCallBack = null;
if (cb) {
cb(soundId);
}
}
}, step);
}
};
function loopSound() {
this.play({
onfinish: loopSound
});
}
function deleteSound() {
this.setVolume(0);
delete this.channel[this.id];
this.channel = false;
}
function playSound(channelName, soundId, params) {
params = params || {};
var channel = channels[channelName];
var sound = sounds[soundId];
sound.channel = channel;
if (params.restart) {
sound.setPosition(0);
}
var options = { onstop: deleteSound };
options.onload = function () {
if (sound.playState !== 1) {
sound.play();
}
};
options.onfinish = function () {
if (params.loop) {
return loopSound.call(sound);
}
deleteSound.call(sound);
if (typeof params.onfinish === 'function') {
params.onfinish();
}
};
channel[soundId] = sound.play(options);
}
function stopSound(id) {
var sound = sounds[id];
if (sound) {
sound.stop();
}
}
function add(id, url) {
//Check if SM2 is loaded. If not, stuff it in a queue so we can set up the audio later.
if (!sm2Loaded) {
queue.push({
action: 'add',
id: id,
url: url
});
return;
}
sounds[id] = soundManager.createSound({
id: id,
url: url,
volume: 0,
autoPlay: false,
autoLoad: false
});
sounds[id].channel = false;
}
//Set up the sound manager.
soundManager.setup({
url: '/assets/swf/default/',
onready: function () {
sm2Loaded = true;
for (var i = 0, l = queue.length; i < l; i++) {
var queued = queue[i];
if (queued.action === 'add') {
add(queued.id, queued.url);
} else if (queued.action === 'play') {
boombox.play(queued.channel, queued.id, queued.params);
}
}
}
});
function loadSettings() {
var loadedSettings = localStorage.getItem('boomBox');
if (!loadedSettings) {
return;
}
try {
settings = JSON.parse(loadedSettings);
} catch (e) {
console.error('could not load the settings');
}
}
loadSettings();
function BoomBox() {
}
BoomBox.prototype.saveSettings = function () {
try {
localStorage.setItem('boomBox', JSON.stringify(settings));
} catch (e) {
console.error('could not save the settings');
}
};
BoomBox.prototype.getChannelVolume = function (channelName) {
if (!settings[channelName]) {
return 0;
}
return settings[channelName].volume;
};
BoomBox.prototype.addChannel = function (name, volume) {
if (!channels[name]) {
channels[name] = {};
settings[name] = settings[name] || { volume: volume };
}
};
BoomBox.prototype.add = function (name, url) {
if (sounds[name]) {
return;
}
add(name, url);
};
function skipTransition(sound) {
clearInterval(sound.interval);
sound.isInTransition = false;
if (sound.transitionCallBack) {
sound.transitionCallBack(sound.id);
sound.transitionCallBack = null;
}
}
BoomBox.prototype.play = function (channelName, id, params) {
params = params || {};
var channel = channels[channelName];
if (!channel) {
console.error('unknown channel', channelName);
return;
}
var sound;
// if the sound is already playing and we don't want to restart it
if (channel[id] && !params.restart) {
sound = sounds[id];
// the sound volume is not in transition (fade) or it has
// just been started (fade in): we don't restart the sound.
if (sound.isStarting || !sound.isInTransition) {
return;
}
// the sound volume is in transition. we skip the transition and start the new requested one.
skipTransition(sound);
}
if (!sounds[id]) {
if (!params.path) {
return console.error('the sound', id, 'does not exist.');
}
add(id, params.path);
}
if (!sm2Loaded) {
return queue.push({
action: 'play',
id: id,
channel: channelName,
params: params
});
}
var transitionFn;
if (!params.hasOwnProperty('stopAll') || params.stopAll) {
transitionFn = volumeTransitions[params.stopTransition || 'fadeTo'];
for (var fadeId in channel) {
if (fadeId !== id) {
transitionFn(fadeId, 0, stopSound);
}
}
}
sound = sounds[id];
transitionFn = volumeTransitions[params.transition || 'fadeTo'];
if (!sound) {
return console.error('the sound', id, 'does not exist.');
}
var volume = params.volume || settings[channelName].volume;
sound.isStarting = true;
transitionFn(id, volume, function () {
sound.isStarting = false;
});
playSound(channelName, id, params);
};
BoomBox.prototype.stopChannel = function (channelName, params) {
var soundList = [];
for (var id in channels[channelName]) {
soundList.push(id);
}
this.stop(soundList, params);
};
BoomBox.prototype.stop = function (soundList, params) {
params = params || {};
if (typeof soundList === 'string') {
soundList = [soundList];
}
var transition = params.transition || 'fadeTo';
for (var i = 0, len = soundList.length; i < len; i += 1) {
var soundId = soundList[i];
var sound = sounds[soundId];
if (sound && sound.channel) {
volumeTransitions[transition](soundId, 0, stopSound);
}
}
};
/**
* Mute audio on all channels.
*/
BoomBox.prototype.muteAll = function () {
soundManager.mute();
};
BoomBox.prototype.mute = function (channelName, params) {
params = params || {};
var channel = channels[channelName];
var transition = params.transition || 'fadeTo';
for (var id in channel) {
volumeTransitions[transition](id, 0);
}
};
/**
* Unmute audio on all channels
*/
BoomBox.prototype.unmuteAll = function () {
soundManager.unmute();
};
BoomBox.prototype.unmute = function (channelName, params) {
params = params || {};
var channel = channels[channelName];
var transition = params.transition || 'fadeTo';
for (var id in channel) {
volumeTransitions[transition](id, settings[channelName].volume);
}
};
BoomBox.prototype.setVolume = function (channelName, volume) {
if (volume > 100 || volume < 0) {
return console.error('Volume needs to be a number between 0 and 100');
}
settings[channelName].volume = volume;
var channel = channels[channelName];
for (var id in channel) {
channel[id].setVolume(volume);
}
};
BoomBox.prototype.isMuted = function () {
return soundManager.muted;
};
BoomBox.prototype.toggleMuteAll = function () {
if (soundManager.muted) {
soundManager.unmuteAll();
} else {
soundManager.muteAll();
}
};
module.exports = boombox = new BoomBox();