Create AudioContext only when needed

This commit is contained in:
Fedor Indutny 2023-02-08 07:05:50 -08:00 committed by GitHub
parent ee55014049
commit 27d44a746c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View file

@ -26,8 +26,7 @@ export type Contents = {
// and instantiate these inside of `GlobalAudioProvider`. (We may wish to keep
// `audioContext` global, however, as the browser limits the number that can be
// created.)
const audioContext = new AudioContext();
void audioContext.suspend();
let audioContext: AudioContext | undefined;
const waveformCache: WaveformCache = new LRU({
max: MAX_WAVEFORM_COUNT,
@ -106,6 +105,11 @@ async function doComputePeaks(
return emptyResult;
}
if (!audioContext) {
audioContext = new AudioContext();
await audioContext.suspend();
}
const data = await audioContext.decodeAudioData(raw);
// Compute RMS peaks