Skip to content

Commit

Permalink
fix(audiocontext): tentative: re-recreate audio context when failing
Browse files Browse the repository at this point in the history
  • Loading branch information
domi7777 committed Oct 11, 2024
1 parent c556441 commit 7972171
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/samples/sample-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export const createAudioContext = () => {
}
return audioContext!;
}

export const resetAudioContext = () => {
audioContext = null;
}
10 changes: 9 additions & 1 deletion src/scenes/DrumsScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,7 +75,14 @@ const instrumentToSample: Record<Instrument, () => 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({
Expand Down

0 comments on commit 7972171

Please sign in to comment.