diff --git a/src/samples/sample-utils.ts b/src/samples/sample-utils.ts index 8364938..90347e8 100644 --- a/src/samples/sample-utils.ts +++ b/src/samples/sample-utils.ts @@ -8,3 +8,7 @@ export const createAudioContext = () => { } return audioContext!; } + +export const resetAudioContext = () => { + audioContext = null; +} diff --git a/src/scenes/DrumsScene.ts b/src/scenes/DrumsScene.ts index 2189f44..6339cc6 100644 --- a/src/scenes/DrumsScene.ts +++ b/src/scenes/DrumsScene.ts @@ -9,6 +9,7 @@ import {playRide} from '../samples/ride.ts'; import {playTom2High} from '../samples/tom-high.ts'; import {playTom1Low} from '../samples/tom-low.ts'; import {FontFamily} from '../fonts.ts'; +import {resetAudioContext} from '../samples/sample-utils.ts'; // loop let isRecording = false; @@ -74,7 +75,14 @@ const instrumentToSample: Record void> = { const playInstrument = (instrument: Instrument) => { console.log(`Playing ${instrument}`); - instrumentToSample[instrument](); + try { + instrumentToSample[instrument](); + } catch(e){ + console.error(`Error playing ${instrument}`, e); + resetAudioContext(); + instrumentToSample[instrument](); + } + if (isRecording) { const time = Date.now() - startRecordingTime; loop.push({