Skip to content

Commit

Permalink
Fix Safari AudioContext initialisation. (#99)
Browse files Browse the repository at this point in the history
Current version seem to require the user interaction to be on the stack
when creating/resuming the context.

Switch to a single context and resume it if it starts suspended.

Add some clean-up so we can keep the single context for the lifetime of
the app (across stop/start).

Closes #97
  • Loading branch information
microbit-matt-hillsdon authored Feb 6, 2023
1 parent fc2093b commit a3e92f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
26 changes: 21 additions & 5 deletions src/board/audio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ export class Audio {
defaultAudioCallback,
speechAudioCallback,
}: AudioOptions) {
this.context = new AudioContext({
// The highest rate is the sound expression synth.
sampleRate: 44100,
});

if (!this.context) {
throw new Error("Context must be pre-created from a user event");
}
this.muteNode = this.context.createGain();
this.muteNode.gain.setValueAtTime(
this.muted ? 0 : 1,
Expand Down Expand Up @@ -62,6 +60,16 @@ export class Audio {
);
}

async createAudioContextFromUserInteraction(): Promise<void> {
this.context = new AudioContext({
// The highest rate is the sound expression synth.
sampleRate: 44100,
});
if (this.context.state === "suspended") {
return this.context.resume();
}
}

playSoundExpression(expr: string) {
const soundEffects = parseSoundEffects(replaceBuiltinSound(expr));
const onDone = () => {
Expand Down Expand Up @@ -138,6 +146,9 @@ export class Audio {

boardStopped() {
this.stopOscillator();
this.speech?.dispose();
this.soundExpression?.dispose();
this.default?.dispose();
}

private stopOscillator() {
Expand Down Expand Up @@ -188,4 +199,9 @@ class BufferedAudio {
}
source.start(startTime);
}

dispose() {
// Prevent calls into WASM when the buffer nodes finish.
this.callback = () => {};
}
}
10 changes: 7 additions & 3 deletions src/board/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,10 @@ export class Board {
this.initializePlayButton();
// We start stopped.
this.displayStoppedState();
this.playButton.addEventListener("click", () =>
this.notifications.onRequestFlash()
);
this.playButton.addEventListener("click", async () => {
await this.audio.createAudioContextFromUserInteraction();
this.notifications.onRequestFlash();
});

this.updateTranslationsInternal();
this.notifications.onReady(this.getState());
Expand Down Expand Up @@ -481,6 +482,9 @@ export class Board {
* @returns A promise that resolves when the simulator is stopped.
*/
async stop(brief: boolean = false): Promise<void> {
// Preemptively stop audio so that we don't call into WASM for more data
this.audio.boardStopped();

if (this.panicTimeout) {
clearTimeout(this.panicTimeout);
this.panicTimeout = null;
Expand Down

0 comments on commit a3e92f7

Please sign in to comment.