From 7b05a18f599670ceec2229681ff3e09a000bac82 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Thu, 22 Feb 2024 20:31:35 +0000 Subject: [PATCH 1/2] For older Safari be defensive about speech We'll aim to increase the sample rate but this will at least mean the simulator isn't put in a bad state. --- src/jshal.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) 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) ); }, From 2246983760c6d77c9578932273ecd3276dae4811 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Thu, 22 Feb 2024 20:50:11 +0000 Subject: [PATCH 2/2] Experiment - why do we recreate this? --- src/board/audio/index.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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(); }