From 8be5e9d2ebf8088998f8996f93c7d9cf7aa47b5d Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Thu, 30 May 2024 16:25:15 -0400 Subject: [PATCH] fixed --- packages/superdough/superdough.mjs | 24 ++++++++++++------------ website/src/repl/Repl.jsx | 11 ++++++++--- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index 018caa9a..e50f6bb3 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -27,6 +27,7 @@ export function getSound(s) { export const resetLoadedSounds = () => soundMap.set({}); let audioContext; +export const isAudioInitialized = () => audioContext != null; export const setDefaultAudioContext = () => { audioContext = new AudioContext(); @@ -37,17 +38,12 @@ export const getAudioContext = () => { if (!audioContext) { return setDefaultAudioContext(); } + return audioContext; }; -let workletsLoading; - -function loadWorklets() { - if (workletsLoading) { - return workletsLoading; - } - workletsLoading = getAudioContext().audioWorklet.addModule(workletsUrl); - return workletsLoading; +export async function loadWorklets() { + return await getAudioContext().audioWorklet.addModule(workletsUrl); } // this function should be called on first user interaction (to avoid console warning) @@ -56,11 +52,15 @@ export async function initAudio(options = {}) { if (typeof window !== 'undefined') { await getAudioContext().resume(); if (!disableWorklets) { - await loadWorklets().catch((err) => { - console.warn('could not load AudioWorklet effects coarse, crush and shape', err); - }); + await loadWorklets() + .catch((err) => { + console.warn('could not load AudioWorklet effects', err); + }) + .finally(() => { + logger('audio worklets loaded'); + }); } else { - console.log('disableWorklets: AudioWorklet effects coarse, crush and shape are skipped!'); + logger('disableWorklets: AudioWorklet effects skipped!'); } } } diff --git a/website/src/repl/Repl.jsx b/website/src/repl/Repl.jsx index d709d058..2dedbd1c 100644 --- a/website/src/repl/Repl.jsx +++ b/website/src/repl/Repl.jsx @@ -10,10 +10,11 @@ import cx from '@src/cx.mjs'; import { transpiler } from '@strudel/transpiler'; import { getAudioContext, - initAudioOnFirstClick, + initAudio, webaudioOutput, resetGlobalEffects, resetLoadedSounds, + isAudioInitialized, } from '@strudel/webaudio'; import { defaultAudioDeviceName } from '../settings.mjs'; import { getAudioDevices, setAudioDevice } from './util.mjs'; @@ -44,7 +45,6 @@ const { latestCode } = settingsMap.get(); let modulesLoading, presets, drawContext, clearCanvas, isIframe; if (typeof window !== 'undefined') { - initAudioOnFirstClick(); modulesLoading = loadModules(); presets = prebake(); drawContext = getDrawContext(); @@ -83,11 +83,16 @@ export function Repl({ embedded = false }) { onUpdateState: (state) => { setReplState({ ...state }); }, - onToggle: (playing) => { + onToggle: async (playing) => { if (!playing) { clearHydra(); } }, + beforeEval: async () => { + if (!isAudioInitialized()) { + await initAudio(); + } + }, afterEval: (all) => { const { code } = all; setLatestCode(code);