Skip to content

Fix Safari AudioContext initialisation #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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