From a8c613b7eb46df2c3bdfa39b2930434306225cc4 Mon Sep 17 00:00:00 2001 From: Ignacio Ortega Date: Thu, 23 Oct 2025 12:45:44 +0200 Subject: [PATCH] Refactor sound stopping and triggering logic in SoundsTab component --- .../src/repl/components/panel/SoundsTab.jsx | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/website/src/repl/components/panel/SoundsTab.jsx b/website/src/repl/components/panel/SoundsTab.jsx index 363f6b98..a9f10bf1 100644 --- a/website/src/repl/components/panel/SoundsTab.jsx +++ b/website/src/repl/components/panel/SoundsTab.jsx @@ -62,11 +62,9 @@ export function SoundsTab() { // stop current sound on mouseup useEvent('mouseup', () => { - const t = trigRef.current; + const ref = trigRef.current; trigRef.current = undefined; - t?.then((ref) => { - ref?.stop(getAudioContext().currentTime + 0.01); - }); + ref?.stop?.(getAudioContext().currentTime + 0.01); }); return (
@@ -124,12 +122,19 @@ export function SoundsTab() { duration: 0.5, }; soundPreviewIdx++; - const time = ctx.currentTime + 0.05; const onended = () => trigRef.current?.node?.disconnect(); - trigRef.current = Promise.resolve(onTrigger(time, params, onended)); - trigRef.current.then((ref) => { - connectToDestination(ref?.node); - }); + try { + // Pre-load the sample by calling onTrigger with a future time + // This triggers the loading but schedules playback for later + const time = ctx.currentTime + 0.5; // Give 500ms for loading + const ref = await onTrigger(time, params, onended); + trigRef.current = ref; + if (ref?.node) { + connectToDestination(ref.node); + } + } catch (err) { + console.warn('Failed to trigger sound:', err); + } }} > {' '}