Create AudioContext only when needed
This commit is contained in:
parent
ee55014049
commit
27d44a746c
2 changed files with 14 additions and 3 deletions
|
@ -26,8 +26,7 @@ export type Contents = {
|
||||||
// and instantiate these inside of `GlobalAudioProvider`. (We may wish to keep
|
// and instantiate these inside of `GlobalAudioProvider`. (We may wish to keep
|
||||||
// `audioContext` global, however, as the browser limits the number that can be
|
// `audioContext` global, however, as the browser limits the number that can be
|
||||||
// created.)
|
// created.)
|
||||||
const audioContext = new AudioContext();
|
let audioContext: AudioContext | undefined;
|
||||||
void audioContext.suspend();
|
|
||||||
|
|
||||||
const waveformCache: WaveformCache = new LRU({
|
const waveformCache: WaveformCache = new LRU({
|
||||||
max: MAX_WAVEFORM_COUNT,
|
max: MAX_WAVEFORM_COUNT,
|
||||||
|
@ -106,6 +105,11 @@ async function doComputePeaks(
|
||||||
return emptyResult;
|
return emptyResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!audioContext) {
|
||||||
|
audioContext = new AudioContext();
|
||||||
|
await audioContext.suspend();
|
||||||
|
}
|
||||||
|
|
||||||
const data = await audioContext.decodeAudioData(raw);
|
const data = await audioContext.decodeAudioData(raw);
|
||||||
|
|
||||||
// Compute RMS peaks
|
// Compute RMS peaks
|
||||||
|
|
|
@ -11,7 +11,7 @@ export type SoundOpts = {
|
||||||
export class Sound {
|
export class Sound {
|
||||||
static sounds = new Map();
|
static sounds = new Map();
|
||||||
|
|
||||||
private readonly context = new AudioContext();
|
private static context: AudioContext | undefined;
|
||||||
|
|
||||||
private readonly loop: boolean;
|
private readonly loop: boolean;
|
||||||
|
|
||||||
|
@ -59,6 +59,13 @@ export class Sound {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private get context(): AudioContext {
|
||||||
|
if (!Sound.context) {
|
||||||
|
Sound.context = new AudioContext();
|
||||||
|
}
|
||||||
|
return Sound.context;
|
||||||
|
}
|
||||||
|
|
||||||
static async loadSoundFile(src: string): Promise<ArrayBuffer> {
|
static async loadSoundFile(src: string): Promise<ArrayBuffer> {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue