-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
520 lines (414 loc) · 13.5 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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
(function(factory) {
// Establish the root object, `window` (`self`) in the browser, or `global` on the server.
// We use `self` instead of `window` for `WebWorker` support.
var root = (typeof self == 'object' && self.self === self && self) ||
(typeof global == 'object' && global.global === global && global);
// Node.js, CommonJS, or ES6
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = factory(root, exports);
// Finally, as a browser global.
} else {
root.Gapless = factory(root, {});
}
})(function(root, Gapless) {
const PRELOAD_NUM_TRACKS = 2;
const isBrowser = typeof window !== 'undefined';
const audioContext = isBrowser ? new (window.AudioContext || window.webkitAudioContext)() : null;
const GaplessPlaybackType = {
HTML5: 'HTML5',
WEBAUDIO: 'WEBAUDIO'
};
const GaplessPlaybackLoadingState = {
NONE: 'NONE',
LOADING: 'LOADING',
LOADED: 'LOADED'
};
class Queue {
constructor(props = {}) {
const {
tracks = [],
onProgress,
onEnded,
onPlayNextTrack,
onPlayPreviousTrack,
onStartNewTrack,
webAudioIsDisabled = false
} = props;
this.props = {
onProgress,
onEnded,
onPlayNextTrack,
onPlayPreviousTrack,
onStartNewTrack
};
this.state = {
volume: 1,
currentTrackIdx: 0,
webAudioIsDisabled
};
this.Track = Track;
this.tracks = tracks.map((trackUrl, idx) =>
new Track({
trackUrl,
idx,
queue: this
})
);
}
addTrack({ trackUrl, metadata = {} }) {
this.tracks.push(
new Track({
trackUrl,
metadata,
idx: this.tracks.length,
queue: this
})
);
}
removeTrack(track) {
const index = this.tracks.indexOf(track);
return this.tracks.splice(index, 1);
}
togglePlayPause() {
if (this.currentTrack) this.currentTrack.togglePlayPause();
}
play() {
if (this.currentTrack) this.currentTrack.play();
}
pause() {
if (this.currentTrack) this.currentTrack.pause();
}
playPrevious() {
this.resetCurrentTrack();
if (--this.state.currentTrackIdx < 0) this.state.currentTrackIdx = 0;
this.resetCurrentTrack();
this.play();
if (this.props.onStartNewTrack) this.props.onStartNewTrack(this.currentTrack);
if (this.props.onPlayPreviousTrack) this.props.onPlayPreviousTrack(this.currentTrack);
}
playNext() {
this.resetCurrentTrack();
this.state.currentTrackIdx++;
this.resetCurrentTrack();
this.play();
if (this.props.onStartNewTrack) this.props.onStartNewTrack(this.currentTrack);
if (this.props.onPlayNextTrack) this.props.onPlayNextTrack(this.currentTrack);
}
resetCurrentTrack() {
if (this.currentTrack) {
this.currentTrack.seek(0)
this.currentTrack.pause();
}
}
pauseAll() {
Object.values(this.tracks).map(track => {
track.pause();
});
}
gotoTrack(idx, playImmediately = false) {
this.pauseAll();
this.state.currentTrackIdx = idx;
this.resetCurrentTrack();
if (playImmediately) {
this.play();
if (this.props.onStartNewTrack) this.props.onStartNewTrack(this.currentTrack);
}
}
loadTrack(idx, loadHTML5) {
// only preload if song is within the next 2
if (this.state.currentTrackIdx + PRELOAD_NUM_TRACKS <= idx) return;
const track = this.tracks[idx];
if (track) track.preload(loadHTML5);
}
setProps(obj = {}) {
this.props = Object.assign(this.props, obj);
}
onEnded() {
if (this.props.onEnded) this.props.onEnded();
}
onProgress(track) {
if (this.props.onProgress) this.props.onProgress(track);
}
get currentTrack() {
return this.tracks[this.state.currentTrackIdx];
}
get nextTrack() {
return this.tracks[this.state.currentTrackIdx + 1];
}
disableWebAudio() {
this.state.webAudioIsDisabled = true;
}
setVolume(nextVolume) {
if (nextVolume < 0) nextVolume = 0;
else if (nextVolume > 1) nextVolume = 1;
this.state.volume = nextVolume;
this.tracks.map(track => track.setVolume(nextVolume));
}
}
class Track {
constructor({ trackUrl, queue, idx, metadata }) {
// playback type state
this.playbackType = GaplessPlaybackType.HTML5;
this.webAudioLoadingState = GaplessPlaybackLoadingState.NONE;
this.loadedHEAD = false;
// basic inputs from Queue
this.idx = idx;
this.queue = queue;
this.trackUrl = trackUrl;
this.metadata = metadata;
this.onEnded = this.onEnded.bind(this);
this.onProgress = this.onProgress.bind(this);
// HTML5 Audio
this.audio = new Audio();
this.audio.onerror = this.audioOnError;
this.audio.onended = this.onEnded;
this.audio.controls = false;
this.audio.volume = queue.state.volume;
this.audio.preload = 'none';
this.audio.src = trackUrl;
// this.audio.onprogress = () => this.debug(this.idx, this.audio.buffered)
if (queue.state.webAudioIsDisabled) return;
// WebAudio
this.audioContext = audioContext;
this.gainNode = this.audioContext.createGain();
this.gainNode.gain.value = queue.state.volume;
this.webAudioStartedPlayingAt = 0;
this.webAudioPausedDuration = 0;
this.webAudioPausedAt = 0;
this.audioBuffer = null;
this.bufferSourceNode = this.audioContext.createBufferSource();
this.bufferSourceNode.onended = this.onEnded;
}
// private functions
loadHEAD(cb) {
if (this.loadedHEAD) return cb();
const options = {
method: 'HEAD'
};
fetch(this.trackUrl, options)
.then(res => {
if (res.redirected) {
this.trackUrl = res.url;
}
this.loadedHEAD = true;
cb();
})
}
loadBuffer(cb) {
if (this.webAudioLoadingState !== GaplessPlaybackLoadingState.NONE) return;
this.webAudioLoadingState = GaplessPlaybackLoadingState.LOADING;
fetch(this.trackUrl)
.then(res => res.arrayBuffer())
.then(res =>
this.audioContext.decodeAudioData(res, buffer => {
this.debug('finished downloading track');
this.webAudioLoadingState = GaplessPlaybackLoadingState.LOADED;
this.bufferSourceNode.buffer = this.audioBuffer = buffer;
this.bufferSourceNode.connect(this.gainNode);
// try to preload next track
this.queue.loadTrack(this.idx + 1);
// if we loaded the active track, switch to web audio
if (this.isActiveTrack) this.switchToWebAudio();
cb && cb(buffer);
})
)
.catch(e => this.debug('caught fetch error', e));
}
switchToWebAudio() {
// if we've switched tracks, don't switch to web audio
if (!this.isActiveTrack) return;
this.debug('switch to web audio', this.currentTime, this.isPaused, this.audio.duration - this.audioBuffer.duration);
// if currentTime === 0, this is a new track, so play it
// otherwise we're hitting this mid-track which may
// happen in the middle of a paused track
this.bufferSourceNode.playbackRate.value = this.currentTime !== 0 && this.isPaused ? 0 : 1;
this.connectGainNode();
this.webAudioStartedPlayingAt = this.audioContext.currentTime - this.currentTime;
// slight blip, could be improved
this.bufferSourceNode.start(0, this.currentTime);
this.audio.pause();
this.playbackType = GaplessPlaybackType.WEBAUDIO;
}
// public-ish functions
pause() {
if (this.isUsingWebAudio) {
if (this.bufferSourceNode.playbackRate.value === 0) return;
this.webAudioPausedAt = this.audioContext.currentTime;
this.bufferSourceNode.playbackRate.value = 0;
this.gainNode.disconnect(this.audioContext.destination);
}
else {
this.audio.pause();
}
}
play() {
this.debug('play');
if (this.audioBuffer) {
// if we've already set up the buffer just set playbackRate to 1
if (this.isUsingWebAudio) {
if (this.bufferSourceNode.playbackRate.value === 1) return;
if (this.webAudioPausedAt) {
this.webAudioPausedDuration += this.audioContext.currentTime - this.webAudioPausedAt;
}
// use seek to avoid bug where track wouldn't play properly
// if paused for longer than length of track
// TODO: fix bug -- must be related to bufferSourceNode
this.seek(this.currentTime);
// was paused, now force play
this.connectGainNode();
this.bufferSourceNode.playbackRate.value = 1;
this.webAudioPausedAt = 0;
}
// otherwise set the bufferSourceNode buffer and switch to WebAudio
else {
this.switchToWebAudio();
}
// Try to preload the next track
this.queue.loadTrack(this.idx + 1);
}
else {
this.audio.preload = 'auto';
this.audio.play();
if (!this.queue.state.webAudioIsDisabled) this.loadHEAD(() => this.loadBuffer());
}
this.onProgress();
}
togglePlayPause() {
this.isPaused ? this.play() : this.pause();
}
preload(HTML5) {
this.debug('preload', HTML5);
if (HTML5 && this.audio.preload !== 'auto') {
this.audio.preload = 'auto';
}
else if (!this.audioBuffer && !this.queue.state.webAudioIsDisabled) {
this.loadHEAD(() => this.loadBuffer());
}
}
// TODO: add checks for to > duration or null or negative (duration - to)
seek(to = 0) {
if (this.isUsingWebAudio) {
this.seekBufferSourceNode(to);
}
else {
this.audio.currentTime = to;
}
this.onProgress();
}
seekBufferSourceNode(to) {
const wasPaused = this.isPaused;
this.bufferSourceNode.onended = null;
this.bufferSourceNode.stop();
this.bufferSourceNode = this.audioContext.createBufferSource();
this.bufferSourceNode.buffer = this.audioBuffer;
this.bufferSourceNode.connect(this.gainNode);
this.bufferSourceNode.onended = this.onEnded;
this.webAudioStartedPlayingAt = this.audioContext.currentTime - to;
this.webAudioPausedDuration = 0;
this.bufferSourceNode.start(0, to);
if (wasPaused) {
this.connectGainNode();
this.pause();
}
}
connectGainNode() {
this.gainNode.connect(this.audioContext.destination);
}
// basic event handlers
audioOnError(e) {
this.debug('audioOnError', e);
}
onEnded() {
this.debug('onEnded');
this.queue.playNext();
this.queue.onEnded();
}
onProgress() {
if (!this.isActiveTrack) return;
const isWithinLastTwentyFiveSeconds = (this.duration - this.currentTime) <= 25;
const nextTrack = this.queue.nextTrack;
// if in last 25 seconds and next track hasn't loaded yet
// start loading next track's HTML5
if (isWithinLastTwentyFiveSeconds && nextTrack && !nextTrack.isLoaded) {
this.queue.loadTrack(this.idx + 1, true);
}
this.queue.onProgress(this);
// if we're paused, we still want to send one final onProgress call
// and then bow out, hence this being at the end of the function
if (this.isPaused) return;
// this.debug(this.currentTime, this.duration);
window.requestAnimationFrame(this.onProgress);
// setTimeout(this.onProgress, 33.33); // 30fps
}
setVolume(nextVolume) {
this.audio.volume = nextVolume;
this.gainNode.gain.value = nextVolume;
}
// getter helpers
get isUsingWebAudio() {
return this.playbackType === GaplessPlaybackType.WEBAUDIO;
}
get isPaused() {
if (this.isUsingWebAudio) {
return this.bufferSourceNode.playbackRate.value === 0;
}
else {
return this.audio.paused;
}
}
get currentTime() {
if (this.isUsingWebAudio) {
return this.audioContext.currentTime - this.webAudioStartedPlayingAt - this.webAudioPausedDuration;
}
else {
return this.audio.currentTime;
}
}
get duration() {
if (this.isUsingWebAudio) {
return this.audioBuffer.duration;
}
else {
return this.audio.duration;
}
}
get isActiveTrack() {
return this.queue.currentTrack === this;
}
get isLoaded() {
return this.webAudioLoadingState === GaplessPlaybackLoadingState.LOADED;
}
get state() {
return {
playbackType: this.playbackType,
webAudioLoadingState: this.webAudioLoadingState
};
}
get completeState() {
return {
playbackType: this.playbackType,
webAudioLoadingState: this.webAudioLoadingState,
isPaused: this.isPaused,
currentTime: this.currentTime,
duration: this.duration,
idx: this.idx
};
}
// debug helper
debug(first, ...args) {
console.log(`${this.idx}:${first}`, ...args, this.state);
}
// just a helper to quick jump to the end of a track for testing
seekToEnd() {
if (this.isUsingWebAudio) {
this.seekBufferSourceNode(this.audioBuffer.duration - 6);
}
else {
this.audio.currentTime = this.audio.duration - 6;
}
}
}
Gapless.Queue = Queue;
Gapless.Track = Track;
return Gapless;
});