-
Notifications
You must be signed in to change notification settings - Fork 3
/
AudioPlayer.js
440 lines (371 loc) · 15.2 KB
/
AudioPlayer.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
// Audio player implemented using AudioBuffers. Tracks are resampled and mixed
// Manually to hardware specs to guarantee seamless playback between consecutive
// Audiobuffers.
import {roundSampleTime, ensureArray} from "util";
import {AudioParam, AudioContext, performance} from "platform/platform";
import {PLAYER_READY_EVENT_NAME} from "audio/backend/AudioPlayerBackend";
import WorkerFrontend from "WorkerFrontend";
import AudioPlayerSourceNode from "audio/frontend/AudioPlayerSourceNode";
import {WEB_AUDIO_BLOCK_SIZE,
SUSTAINED_BUFFERED_AUDIO_RATIO,
MIN_SUSTAINED_AUDIO_SECONDS} from "audio/frontend/buffering";
const SUSPEND_AUDIO_CONTEXT_AFTER_SECONDS = 20;
const MAX_DIFFERENT_AUDIO_BUFFER_KEYS = 10;
// TODO Make end user configurable
const SEEK_FADE_TIME = 0.2;
const TRACK_CHANGE_FADE_TIME = 0.2;
const PAUSE_RESUME_FADE_TIME = 0.4;
const MUTE_UNMUTE_FADE_TIME = 0.4;
if (!AudioContext.prototype.suspend) {
AudioContext.prototype.suspend = function() {
return Promise.resolve();
};
}
if (!AudioContext.prototype.resume) {
AudioContext.prototype.resume = function() {
return Promise.resolve();
};
}
function NativeGetOutputTimestamp() {
return this._audioContext.getOutputTimestamp();
}
function PolyfillGetOutputTimestamp() {
return {
contextTime: this._audioContext.currentTime,
performanceTime: performance.now()
};
}
function cancelAndHoldStandardImpl(audioParam, value) {
return audioParam.cancelAndHoldAtTime(value);
}
function cancelAndHoldNonStandardImpl(audioParam, value) {
return audioParam.cancelValuesAndHoldAtTime(value);
}
function cancelAndHoldPolyfillImpl(audioParam, value) {
const currentValue = audioParam.value;
audioParam.cancelScheduledValues(value);
audioParam.setValueAtTime(currentValue, value);
}
export const cancelAndHold = typeof AudioParam.prototype.cancelAndHoldAtTime === `function` ? cancelAndHoldStandardImpl :
typeof AudioParam.prototype.cancelValuesAndHoldAtTime === `function` ? cancelAndHoldNonStandardImpl :
cancelAndHoldPolyfillImpl;
function audioBufferCacheKey(channelCount, sampleRate) {
return `${channelCount}-${sampleRate}`;
}
function lruCmp(a, b) {
return b.lastUsed - a.lastUsed;
}
export default class AudioPlayer extends WorkerFrontend {
constructor(deps) {
super(PLAYER_READY_EVENT_NAME, deps.workerWrapper);
this.page = deps.page;
this.env = deps.env;
this.db = deps.db;
this.timers = deps.timers;
this.dbValues = deps.dbValues;
this.effectPreferencesBindingContext = deps.effectPreferencesBindingContext;
this.applicationPreferencesBindingContext = deps.applicationPreferencesBindingContext;
this._audioContext = null;
this._unprimedAudioContext = null;
this._silentBuffer = null;
this._outputSampleRate = -1;
this._outputChannelCount = -1;
this._scheduleAheadTime = -1;
this._sourceNode = new AudioPlayerSourceNode(this);
this._bufferFrameCount = 0;
this._playedAudioBuffersNeededForVisualization = 0;
this._audioBufferTime = -1;
this._audioBufferCache = new Map();
this._audioBufferCacheKeys = [];
this._suspensionTimeoutMs = SUSPEND_AUDIO_CONTEXT_AFTER_SECONDS * 1000;
this._currentStateModificationAction = null;
this._lastAudioContextRefresh = 0;
this._targetBufferLengthSeconds = -1;
this._sustainedBufferedAudioSeconds = -1;
this._sustainedBufferCount = -1;
this._minBuffersToRequest = -1;
this._playbackStoppedTime = performance.now();
this._suspend = this._suspend.bind(this);
this._suspensionTimeoutId = this.page.setTimeout(this._suspend, this._suspensionTimeoutMs);
this.effectPreferencesBindingContext.on(`change`, async () => {
await this.ready();
this._updateBackendConfig({
effects: ensureArray(this.effectPreferencesBindingContext.getAudioPlayerEffects()),
crossfadeDuration: this.getCrossfadeDuration()
});
});
this.applicationPreferencesBindingContext.on(`change`, async () => {
await this.ready();
const preferences = this.applicationPreferencesBindingContext.preferences();
this._setBufferSize(preferences.getBufferLengthMilliSeconds());
this._updateBackendConfig({
loudnessNormalization: preferences.getEnableLoudnessNormalization(),
silenceTrimming: preferences.getEnableSilenceTrimming()
});
});
this.page.addDocumentListener(`touchend`, this._touchended.bind(this), true);
this.getOutputTimestamp = typeof AudioContext.prototype.getOutputTimestamp === `function` ? NativeGetOutputTimestamp
: PolyfillGetOutputTimestamp;
this._resetAudioContext();
this._initBackend();
}
getCrossfadeDuration() {
return this.effectPreferencesBindingContext.getCrossfadeDuration();
}
/* eslint-disable class-methods-use-this */
getPauseResumeFadeTime() {
return PAUSE_RESUME_FADE_TIME;
}
getSeekFadeTime() {
return SEEK_FADE_TIME;
}
getMuteUnmuteFadeTime() {
return MUTE_UNMUTE_FADE_TIME;
}
getTrackChangeFadeTime() {
return TRACK_CHANGE_FADE_TIME;
}
_bufferFrameCountForSampleRate(sampleRate) {
return this._targetBufferLengthSeconds * sampleRate;
}
getTargetBufferLengthSeconds() {
return this._targetBufferLengthSeconds;
}
getSustainedBufferedAudioSeconds() {
return this._sustainedBufferedAudioSeconds;
}
getSustainedBufferCount() {
return this._sustainedBufferCount;
}
getMinBuffersToRequest() {
return this._minBuffersToRequest;
}
shouldSkipSilence() {
return this.applicationPreferencesBindingContext.getPreference(`enableSilenceTrimming`);
}
async _setBufferSize(bufferLengthMilliSecondsPreference) {
if (this._targetBufferLengthSeconds / 1000 === bufferLengthMilliSecondsPreference) {
return;
}
const sampleRate = this._outputSampleRate;
const channelCount = this._outputChannelCount;
this._targetBufferLengthSeconds = bufferLengthMilliSecondsPreference / 1000;
this._sustainedBufferedAudioSeconds = Math.max(MIN_SUSTAINED_AUDIO_SECONDS,
this._targetBufferLengthSeconds * SUSTAINED_BUFFERED_AUDIO_RATIO);
this._sustainedBufferCount = Math.ceil(this._sustainedBufferedAudioSeconds / this._targetBufferLengthSeconds);
this._minBuffersToRequest = Math.ceil(this._sustainedBufferCount / 4);
this._bufferFrameCount = this._bufferFrameCountForSampleRate(this._outputSampleRate);
this._audioBufferTime = this._bufferFrameCount / this._outputSampleRate;
this._playedAudioBuffersNeededForVisualization = Math.ceil(this.getAudioLatency() / this._audioBufferTime) + 1;
if (!this._silentBuffer) {
this._silentBuffer = this._audioContext.createBuffer(channelCount, this._bufferFrameCount, sampleRate);
}
await this._updateBackendConfig({bufferTime: this._audioBufferTime});
}
/* eslint-enable class-methods-use-this */
receiveMessage(event) {
const {methodName, args, transferList} = event.data;
if (!this[methodName]) {
this._sourceNode[methodName](args, transferList);
} else {
this[methodName](args, transferList);
}
}
async _updateBackendConfig(config) {
await this.ready();
this._message(`audioConfiguration`, config);
}
async _initBackend() {
await this.ready();
const preferences = this.applicationPreferencesBindingContext.preferences();
this._updateBackendConfig({
loudnessNormalization: preferences.getEnableLoudnessNormalization(),
silenceTrimming: preferences.getEnableSilenceTrimming(),
effects: ensureArray(this.effectPreferencesBindingContext.getAudioPlayerEffects()),
crossfadeDuration: this.getCrossfadeDuration()
});
}
_audioContextChanged(oldAudioContextTime) {
const {_audioContext} = this;
const {channelCount} = _audioContext.destination;
const {sampleRate} = _audioContext;
if (this._setAudioOutputParameters({channelCount, sampleRate})) {
this._setBufferSize(this.applicationPreferencesBindingContext.preferences().getBufferLengthMilliSeconds());
}
this._sourceNode.adoptNewAudioContext(_audioContext, oldAudioContextTime);
}
_setAudioOutputParameters({sampleRate, channelCount}) {
let changed = false;
if (this._outputSampleRate !== sampleRate) {
this._outputSampleRate = sampleRate;
changed = true;
}
if (this._outputChannelCount !== channelCount) {
this._outputChannelCount = channelCount;
changed = true;
}
this._scheduleAheadTime = Math.max(this._scheduleAheadTime,
roundSampleTime(WEB_AUDIO_BLOCK_SIZE * 8, sampleRate) / sampleRate);
return changed;
}
getAudioLatency() {
return (this._audioContext.baseLatency || 0) +
(this._audioContext.outputLatency || 0);
}
getScheduleAheadTime() {
return this._scheduleAheadTime;
}
createAudioBuffer(channelCount, length, sampleRate) {
const key = audioBufferCacheKey(channelCount, sampleRate);
const {_audioBufferCacheKeys, _audioBufferCache} = this;
const lastUsed = performance.now();
let keyExists = false;
for (let i = 0; i < _audioBufferCacheKeys.length; ++i) {
if (_audioBufferCacheKeys[i].key === key) {
_audioBufferCacheKeys[i].lastUsed = lastUsed;
keyExists = true;
break;
}
}
if (!keyExists) {
const entry = {key, lastUsed};
if (_audioBufferCacheKeys.length >= MAX_DIFFERENT_AUDIO_BUFFER_KEYS) {
_audioBufferCacheKeys.sort(lruCmp);
const removedKey = _audioBufferCacheKeys.pop().key;
_audioBufferCache.delete(removedKey);
}
_audioBufferCacheKeys.push(entry);
_audioBufferCache.set(key, []);
}
const audioBuffers = _audioBufferCache.get(key);
if (!audioBuffers.length) {
return this._audioContext.createBuffer(channelCount, length, sampleRate);
} else {
while (audioBuffers.length > 0) {
const audioBuffer = audioBuffers.pop();
if (audioBuffer.length === length) {
return audioBuffer;
}
}
return this._audioContext.createBuffer(channelCount, length, sampleRate);
}
}
freeAudioBuffer(audioBuffer) {
const {numberOfChannels, sampleRate} = audioBuffer;
const key = audioBufferCacheKey(numberOfChannels, sampleRate);
this._audioBufferCache.get(key).push(audioBuffer);
}
async _touchended() {
if (this._unprimedAudioContext) {
const audioCtx = this._unprimedAudioContext;
try {
await audioCtx.resume();
} catch (e) {
// Noop
}
const source = audioCtx.createBufferSource();
source.buffer = this._silentBuffer;
source.connect(audioCtx.destination);
source.start(0);
this._unprimedAudioContext = null;
}
}
_suspend() {
if (this._audioContext.state === `suspended`) return Promise.resolve();
if (!this._currentStateModificationAction) {
this._currentStateModificationAction = {
type: `suspend`,
promise: (async () => {
try {
await Promise.resolve(this._audioContext.suspend());
} finally {
this._currentStateModificationAction = null;
}
})()
};
return this._currentStateModificationAction.promise;
} else if (this._currentStateModificationAction.type === `resume`) {
this._currentStateModificationAction.promise = (async () => {
try {
try {
await this._currentStateModificationAction.promise;
} finally {
await this._suspend();
}
} finally {
this._currentStateModificationAction = null;
}
})();
}
return this._currentStateModificationAction.promise;
}
_resetAudioContext() {
const oldAudioContextTime = this._audioContext ? this._audioContext.currentTime : 0;
try {
if (this._audioContext) {
this._audioContext.close();
}
} catch (e) {
// NOOP
} finally {
this._audioContext = null;
}
this._audioContext = new AudioContext({latencyHint: `playback`});
this._unprimedAudioContext = this._audioContext;
this._audioContextChanged(oldAudioContextTime);
this.emit(`audioContextReset`, this);
}
_clearSuspensionTimer() {
this._playbackStoppedTime = -1;
this.page.clearTimeout(this._suspensionTimeoutId);
this._suspensionTimeoutId = -1;
}
_message(action, args) {
this.postMessage({action, args});
}
getCurrentTime() {
return this._audioContext.currentTime;
}
getAudioContext() {
return this._audioContext;
}
resume() {
if (this._audioContext.state === `running`) {
if (this._playbackStoppedTime !== -1 &&
performance.now() - this._playbackStoppedTime > this._suspensionTimeoutMs) {
this._playbackStoppedTime = -1;
this.emit(`audioContextSuspend`, this);
this._resetAudioContext();
}
return;
}
// Reset AudioContext as it's probably ruined despite of suspension efforts.
if (!this._currentStateModificationAction) {
this._resetAudioContext();
} else if (this._currentStateModificationAction.type === `suspend`) {
this._currentStateModificationAction = null;
this._resetAudioContext();
}
}
playbackStopped() {
this._clearSuspensionTimer();
this._playbackStoppedTime = performance.now();
this._suspensionTimeoutId = this.page.setTimeout(this._suspend, this._suspensionTimeoutMs);
}
playbackStarted() {
this._clearSuspensionTimer();
}
getMaximumSeekTime(duration) {
return Math.max(0, duration - (this._audioBufferTime + (2048 / this._audioContext.sampleRate)));
}
getBufferDuration() {
return this._audioBufferTime;
}
createSourceNode() {
return this._sourceNode;
}
ping() {
this.timers.tick();
this._message(`ping`, {});
}
}