diff --git a/src/board/audio/index.ts b/src/board/audio/index.ts index 3b14dd53..9bb7f5f9 100644 --- a/src/board/audio/index.ts +++ b/src/board/audio/index.ts @@ -67,10 +67,12 @@ export class Audio { } async createAudioContextFromUserInteraction(): Promise { - this.context = new (window.AudioContext || window.webkitAudioContext)({ - // The highest rate is the sound expression synth. - sampleRate: 44100, - }); + this.context = + this.context ?? + new (window.AudioContext || window.webkitAudioContext)({ + // The highest rate is the sound expression synth. + sampleRate: 44100, + }); if (this.context.state === "suspended") { return this.context.resume(); } diff --git a/src/jshal.js b/src/jshal.js index 39d3cf08..3cc18cd1 100644 --- a/src/jshal.js +++ b/src/jshal.js @@ -240,14 +240,22 @@ mergeInto(LibraryManager.library, { /** @type {number} */ buf, /** @type {number} */ num_samples ) { + /** @type {AudioBuffer | undefined} */ let webAudioBuffer; + try { + // @ts-expect-error + webAudioBuffer = Module.board.audio.speech.createBuffer(num_samples); + } catch (e) { + // Swallow error on older Safari to keep the sim in a good state. + // @ts-expect-error + if (e.name === "NotSupportedError") { + return; + } else { + throw e; + } + } // @ts-expect-error Module.board.audio.speech.writeData( - Module.conversions.convertAudioBuffer( - Module.HEAPU8, - buf, - // @ts-expect-error - Module.board.audio.speech.createBuffer(num_samples) - ) + Module.conversions.convertAudioBuffer(Module.HEAPU8, buf, webAudioBuffer) ); },