forked from Stinkstudios/sono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsono.js
More file actions
313 lines (271 loc) · 6.12 KB
/
sono.js
File metadata and controls
313 lines (271 loc) · 6.12 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
import 'core-js/fn/object/assign';
import context from './context';
import Effects from './effects';
import file from './utils/file';
import Group from './group';
import Loader from './utils/loader';
import log from './utils/log';
import pageVisibility from './utils/pageVisibility';
import Sound from './sound';
import SoundGroup from './utils/sound-group';
import touchLock from './utils/touchLock';
import utils from './utils/utils';
const VERSION = '2.1.6';
const bus = new Group(context, context.destination);
/*
* Get Sound by id
*/
function get(id) {
return bus.find(id);
}
/*
* Create group
*/
function group(sounds) {
const soundGroup = new SoundGroup(context, bus.gain);
if (sounds) {
sounds.forEach((sound) => soundGroup.add(sound));
}
return soundGroup;
}
/*
* Loading
*/
function add(config) {
const src = file.getSupportedFile(config.src || config.url || config.data || config);
const sound = new Sound(Object.assign({}, config || {}, {
src,
context,
destination: bus.gain
}));
sound.isTouchLocked = isTouchLocked;
if (config) {
sound.id = config.id || config.name || '';
sound.loop = !!config.loop;
sound.volume = config.volume;
sound.effects = config.effects || [];
}
bus.add(sound);
return sound;
}
function queue(config, loaderGroup) {
const sound = add(config).prepare();
if (loaderGroup) {
loaderGroup.add(sound.loader);
}
return sound;
}
function load(config) {
const src = config.src || config.url || config.data || config;
let sound, loader;
if (file.containsURL(src)) {
sound = queue(config);
loader = sound.loader;
} else if (Array.isArray(src) && file.containsURL(src[0].src || src[0].url)) {
sound = [];
loader = new Loader.Group();
src.forEach((url) => sound.push(queue(url, loader)));
} else {
const errorMessage = 'sono.load: No audio file URLs found in config.';
if (config.onError) {
config.onError('[ERROR] ' + errorMessage);
} else {
throw new Error(errorMessage);
}
return null;
}
if (config.onProgress) {
loader.on('progress', (progress) => config.onProgress(progress));
}
if (config.onComplete) {
loader.once('complete', () => {
loader.off('progress');
config.onComplete(sound);
});
}
loader.once('error', err => {
loader.off('error');
if (config.onError) {
config.onError(err);
} else {
console.error(err);
}
});
loader.start();
return sound;
}
/*
* Create Sound
*
* Accepted values for param config:
* Object config e.g. { id:'foo', url:['foo.ogg', 'foo.mp3'] }
* Array (of files e.g. ['foo.ogg', 'foo.mp3'])
* ArrayBuffer
* HTMLMediaElement
* Filename string (e.g. 'foo.ogg')
* Oscillator type string (i.e. 'sine', 'square', 'sawtooth', 'triangle')
*/
function create(config) {
// try to load if config contains URLs
if (file.containsURL(config)) {
return load(config);
}
const sound = add(config);
sound.data = config.data || config;
return sound;
}
/*
* Destroy
*/
function destroy(soundOrId) {
bus.find(soundOrId, (sound) => sound.destroy());
return sono;
}
function destroyAll() {
bus.destroy();
return sono;
}
/*
* Controls
*/
function mute() {
bus.mute();
return sono;
}
function unMute() {
bus.unMute();
return sono;
}
function fade(volume, duration) {
bus.fade(volume, duration);
return sono;
}
function pauseAll() {
bus.pause();
return sono;
}
function resumeAll() {
bus.resume();
return sono;
}
function stopAll() {
bus.stop();
return sono;
}
function play(id, delay, offset) {
bus.find(id, (sound) => sound.play(delay, offset));
return sono;
}
function pause(id) {
bus.find(id, (sound) => sound.pause());
return sono;
}
function stop(id) {
bus.find(id, (sound) => sound.stop());
return sono;
}
/*
* Mobile touch lock
*/
let isTouchLocked = touchLock(context, () => {
isTouchLocked = false;
bus.sounds.forEach(sound => (sound.isTouchLocked = false));
});
/*
* Page visibility events
*/
const pageHiddenPaused = [];
// pause currently playing sounds and store refs
function onHidden() {
bus.sounds.forEach(sound => {
if (sound.playing) {
sound.pause();
pageHiddenPaused.push(sound);
}
});
}
// play sounds that got paused when page was hidden
function onShown() {
while (pageHiddenPaused.length) {
pageHiddenPaused.pop().play();
}
}
const pageVis = pageVisibility(onHidden, onShown);
function register(name, fn, attachTo = Effects.prototype) {
attachTo[name] = fn;
sono[name] = fn;
return fn;
}
const sono = {
canPlay: file.canPlay,
context,
create,
createGroup: group,
createSound: create,
destroyAll,
destroy,
effects: bus.effects,
extensions: file.extensions,
fade,
file,
gain: bus.gain,
getOfflineContext: utils.getOfflineContext,
get,
getSound: get,
group,
hasWebAudio: !context.isFake,
isSupported: file.extensions.length > 0,
load,
log: () => log(sono),
mute,
pause,
pauseAll,
play,
register,
resumeAll,
stop,
stopAll,
unMute,
utils,
VERSION,
get effects() {
return bus.effects;
},
set effects(value) {
bus.effects.removeAll().add(value);
},
get fx() {
return this.effects;
},
set fx(value) {
this.effects = value;
},
get isTouchLocked() {
return isTouchLocked;
},
get playInBackground() {
return !pageVis.enabled;
},
set playInBackground(value) {
pageVis.enabled = !value;
if (!value) {
onShown();
}
},
get sounds() {
return bus.sounds.slice(0);
},
get volume() {
return bus.volume;
},
set volume(value) {
bus.volume = value;
},
// expose for unit testing
__test: {
Effects,
Group,
Sound
}
};
export default sono;