-
Notifications
You must be signed in to change notification settings - Fork 15
/
sound.js
458 lines (378 loc) · 11.7 KB
/
sound.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import context from './context';
import BufferSource from './source/buffer-source';
import Effects from './effects';
import Emitter from './utils/emitter';
import file from './utils/file';
import firefox from './utils/firefox';
import utils from './utils/utils';
import isSafeNumber from './utils/isSafeNumber';
import Loader from './utils/loader';
import AudioSource from './source/audio-source';
import MediaSource from './source/media-source';
import MicrophoneSource from './source/microphone-source';
import OscillatorSource from './source/oscillator-source';
export default class Sound extends Emitter {
constructor(config) {
super();
this.id = config.id || null;
this._context = config.context || context;
this._destination = config.destination || this._context.destination;
this._effects = new Effects(this._context);
this._gain = this._context.createGain();
this._config = config;
this._data = null;
this._fadeTimeout = null;
this._isTouchLocked = false;
this._loader = null;
this._loop = false;
this._offset = 0;
this._playbackRate = 1;
this._playWhenReady = null;
this._source = null;
this._wave = null;
this._userData = {};
this._effects.setDestination(this._gain);
this._gain.connect(this._destination);
this._onEnded = this._onEnded.bind(this);
this._onLoad = this._onLoad.bind(this);
this._onLoadError = this._onLoadError.bind(this);
}
prepare(newConfig = null, force = false) {
const skipLoad = !force && !this._source && !!this._config.deferLoad;
if (newConfig) {
const configSrc = file.getSrc(newConfig);
const src = file.getSupportedFile(configSrc) || this._config.src;
this._config = Object.assign(this._config, newConfig, {src});
}
if (this._source && this._data && this._data.tagName) {
this._source.load(this._config.src);
} else {
this._loader = new Loader(this._config.src, skipLoad);
this._loader.audioContext = !!this._config.asMediaElement || this._context.isFake ? null : this._context;
this._loader.isTouchLocked = this._isTouchLocked;
this._loader.once('loaded', this._onLoad);
this._loader.once('error', this._onLoadError);
}
return this;
}
load(config = null) {
this.stop();
this._source = null;
if (!config || file.containsURL(config)) {
if (this._loader) {
this._loader.destroy();
}
this.prepare(config, true);
this._loader.start();
} else {
this.data = config.data || config;
}
return this;
}
play(delay, offset) {
if (!this._source || this._isTouchLocked) {
this._playWhenReady = () => {
if (this._source) {
this.play(delay, offset);
}
};
if (!!this._config.deferLoad) {
if (!this._loader) {
this.prepare(null, true);
}
this._loader.start(true);
}
return this;
}
this._playWhenReady = null;
this._effects.setSource(this._source.sourceNode);
if (this._offset && typeof offset === 'undefined') {
offset = this._offset;
this._offset = 0;
}
this._source.play(delay, offset);
if (this._source.hasOwnProperty('volume')) {
this._source.volume = this._gain.gain.value;
}
if (this._source.hasOwnProperty('loop')) {
this._source.loop = this._loop;
}
this.emit('play', this);
return this;
}
pause() {
this._source && this._source.pause();
this.emit('pause', this);
return this;
}
stop(delay) {
this._source && this._source.stop(delay || 0);
this.emit('stop', this);
return this;
}
seek(value) {
this.currentTime = value;
return this;
}
fade(volume, duration = 1) {
if (!this._source) {
return this;
}
const param = this._gain.gain;
if (this._context && !this._context.isFake && !firefox) {
const time = this._context.currentTime;
param.cancelScheduledValues(time);
param.setValueAtTime(param.value, time);
param.linearRampToValueAtTime(volume, time + duration);
} else {
this._fadePolyfill(volume, duration);
}
this.emit('fade', this, volume);
return this;
}
_fadePolyfill(toVolume, duration) {
const ramp = (value, step) => {
this._fadeTimeout = window.setTimeout(() => {
this.volume = this.volume + (value - this.volume) * 0.2;
if (Math.abs(this.volume - value) > 0.05) {
ramp(value, step);
return;
}
this.volume = value;
}, step * 1000);
};
window.clearTimeout(this._fadeTimeout);
ramp(toVolume, duration / 10);
return this;
}
unload() {
this._source && this._source.destroy();
this._loader && this._loader.destroy();
this._data = null;
this._playWhenReady = null;
this._source = null;
this._loader = null;
this._config.deferLoad = true;
this.emit('unload', this);
}
reload() {
return this.load(null, true);
}
destroy() {
this._source && this._source.destroy();
this._effects && this._effects.destroy();
this._gain && this._gain.disconnect();
if (this._loader) {
this._loader.off('loaded');
this._loader.off('error');
this._loader.destroy();
}
this._gain = null;
this._context = null;
this._destination = null;
this._data = null;
this._playWhenReady = null;
this._source = null;
this._effects = null;
this._loader = null;
this._config = null;
this.emit('destroy', this);
this.off();
}
waveform(length) {
if (!this._wave) {
this._wave = utils.waveform();
}
if (!this._data) {
this.once('ready', () => this._wave(this._data, length));
}
return this._wave(this._data, length);
}
get context() {
return this._context;
}
get currentTime() {
return this._source ? this._source.currentTime : this._offset;
}
set currentTime(value) {
if (this._source) {
const playing = this._source.playing;
this._source.stop();
this._source.currentTime = value;
if (playing) {
this.play(0, value);
}
} else {
this._offset = value;
}
}
get data() {
return this._data;
}
set data(value) {
if (!value) {
return;
}
this._data = value;
this._createSource(value);
}
get duration() {
return this._source ? this._source.duration : 0;
}
get effects() {
return this._effects._nodes;
}
set effects(value) {
this._effects.removeAll().add(value);
}
get fx() {
return this.effects;
}
set fx(value) {
this.effects = value;
}
get ended() {
return !!this._source && this._source.ended;
}
get frequency() {
return this._source ? this._source.frequency : 0;
}
set frequency(value) {
if (this._source && this._source.hasOwnProperty('frequency')) {
this._source.frequency = value;
}
}
get gain() {
return this._gain;
}
// for media element source
get groupVolume() {
return this._source.groupVolume;
}
set groupVolume(value) {
if (this._source && this._source.hasOwnProperty('groupVolume')) {
this._source.groupVolume = value;
}
}
set isTouchLocked(value) {
this._isTouchLocked = value;
if (this._loader) {
this._loader.isTouchLocked = value;
}
if (!value && this._playWhenReady) {
this._playWhenReady();
}
}
get loader() {
return this._loader;
}
get loop() {
return this._loop;
}
set loop(value) {
this._loop = !!value;
if (this._source && this._source.hasOwnProperty('loop') && this._source.loop !== this._loop) {
this._source.loop = this._loop;
}
}
get singlePlay() {
return this._config.singlePlay;
}
set singlePlay(value) {
this._config.singlePlay = value;
this._source.singlePlay = value;
}
get config() {
return this._config;
}
get paused() {
return !!this._source && this._source.paused;
}
get playing() {
return !!this._source && this._source.playing;
}
get playbackRate() {
return this._playbackRate;
}
set playbackRate(value) {
this._playbackRate = value;
if (this._source) {
this._source.playbackRate = value;
}
}
get progress() {
return this._source ? this._source.progress || 0 : 0;
}
get sourceInfo() {
return this._source && this._source.info ? this._source.info : {};
}
get sourceNode() {
return this._source ? this._source.sourceNode : null;
}
get volume() {
return this._gain.gain.value;
}
set volume(value) {
if (!isSafeNumber(value)) {
return;
}
window.clearTimeout(this._fadeTimeout);
value = Math.min(Math.max(value, 0), 1);
const param = this._gain.gain;
const time = this._context.currentTime;
param.cancelScheduledValues(time);
param.value = value;
if (!firefox) {
param.setValueAtTime(value, time);
}
if (this._source && this._source.hasOwnProperty('volume')) {
this._source.volume = value;
}
}
get userData() {
return this._userData;
}
_createSource(data) {
const isAudioBuffer = file.isAudioBuffer(data);
if (isAudioBuffer || file.isMediaElement(data)) {
const Fn = isAudioBuffer ? BufferSource : MediaSource;
this._source = new AudioSource(Fn, data, this._context, this._onEnded);
this._source.singlePlay = !!this._config.singlePlay;
this._source.playbackRate = this._playbackRate;
this._source.currentTime = this._offset;
} else if (file.isMediaStream(data)) {
this._source = new MicrophoneSource(data, this._context);
} else if (file.isOscillatorType((data && data.type) || data)) {
this._source = new OscillatorSource(data.type || data, this._context);
} else {
throw new Error('Cannot detect data type: ' + data);
}
this._effects.setSource(this._source.sourceNode);
this.emit('ready', this);
if (this._playWhenReady) {
this._playWhenReady();
}
}
_onEnded() {
this.emit('ended', this);
}
_onLoad(data) {
this._data = data;
this.emit('loaded', this);
this._createSource(data);
}
_onLoadError(err) {
if (this.listenerCount('error')) {
this.emit('error', this, err);
return;
}
console.error('Sound load error', this._loader.url);
}
}
// expose for unit tests
Sound.__source = {
BufferSource,
MediaSource,
MicrophoneSource,
OscillatorSource
};