Merge pull request 'Bug Fix: Retries for sounds tab' (#1754) from glossing/sounds-tab-preview into main
Reviewed-on: https://codeberg.org/uzu/strudel/pulls/1754
This commit is contained in:
commit
6a1c6081e0
2 changed files with 36 additions and 17 deletions
|
|
@ -263,8 +263,8 @@ let audioReady;
|
||||||
export async function initAudioOnFirstClick(options) {
|
export async function initAudioOnFirstClick(options) {
|
||||||
if (!audioReady) {
|
if (!audioReady) {
|
||||||
audioReady = new Promise((resolve) => {
|
audioReady = new Promise((resolve) => {
|
||||||
document.addEventListener('click', async function listener() {
|
document.addEventListener('mousedown', async function listener() {
|
||||||
document.removeEventListener('click', listener);
|
document.removeEventListener('mousedown', listener);
|
||||||
await initAudio(options);
|
await initAudio(options);
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,10 @@ import { prebake } from '@src/repl/prebake.mjs';
|
||||||
const getSamples = (samples) =>
|
const getSamples = (samples) =>
|
||||||
Array.isArray(samples) ? samples.length : typeof samples === 'object' ? Object.values(samples).length : 1;
|
Array.isArray(samples) ? samples.length : typeof samples === 'object' ? Object.values(samples).length : 1;
|
||||||
|
|
||||||
|
function wait(ms) {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
export function SoundsTab() {
|
export function SoundsTab() {
|
||||||
const sounds = useStore(soundMap);
|
const sounds = useStore(soundMap);
|
||||||
|
|
||||||
|
|
@ -56,9 +60,7 @@ export function SoundsTab() {
|
||||||
|
|
||||||
// holds mutable ref to current triggered sound
|
// holds mutable ref to current triggered sound
|
||||||
const trigRef = useRef();
|
const trigRef = useRef();
|
||||||
|
const numRef = useRef(0);
|
||||||
// Used to cycle through sound previews on banks with multiple sounds
|
|
||||||
let soundPreviewIdx = 0;
|
|
||||||
|
|
||||||
// stop current sound on mouseup
|
// stop current sound on mouseup
|
||||||
useEvent('mouseup', () => {
|
useEvent('mouseup', () => {
|
||||||
|
|
@ -66,6 +68,14 @@ export function SoundsTab() {
|
||||||
trigRef.current = undefined;
|
trigRef.current = undefined;
|
||||||
ref?.stop?.(getAudioContext().currentTime + 0.01);
|
ref?.stop?.(getAudioContext().currentTime + 0.01);
|
||||||
});
|
});
|
||||||
|
useEvent('keydown', (e) => {
|
||||||
|
if (!isNaN(Number(e.key))) {
|
||||||
|
numRef.current = Number(e.key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
useEvent('keyup', (e) => {
|
||||||
|
numRef.current = 0;
|
||||||
|
});
|
||||||
return (
|
return (
|
||||||
<div id="sounds-tab" className="px-4 flex gap-2 flex-col w-full h-full text-foreground">
|
<div id="sounds-tab" className="px-4 flex gap-2 flex-col w-full h-full text-foreground">
|
||||||
<Textbox placeholder="Search" value={search} onChange={(v) => setSearch(v)} />
|
<Textbox placeholder="Search" value={search} onChange={(v) => setSearch(v)} />
|
||||||
|
|
@ -115,25 +125,34 @@ export function SoundsTab() {
|
||||||
const params = {
|
const params = {
|
||||||
note: ['synth', 'soundfont'].includes(data.type) ? 'a3' : undefined,
|
note: ['synth', 'soundfont'].includes(data.type) ? 'a3' : undefined,
|
||||||
s: name,
|
s: name,
|
||||||
n: soundPreviewIdx,
|
n: numRef.current,
|
||||||
clip: 1,
|
clip: 1,
|
||||||
release: 0.5,
|
release: 0.5,
|
||||||
sustain: 1,
|
sustain: 1,
|
||||||
duration: 0.5,
|
duration: 0.5,
|
||||||
};
|
};
|
||||||
soundPreviewIdx++;
|
|
||||||
const onended = () => trigRef.current?.node?.disconnect();
|
const onended = () => trigRef.current?.node?.disconnect();
|
||||||
try {
|
// Attempt to play the sample and retry every 200ms until 10 attempts have been reached
|
||||||
// Pre-load the sample by calling onTrigger with a future time
|
let errMsg;
|
||||||
// This triggers the loading but schedules playback for later
|
for (let attempt = 0; attempt < 10; attempt++) {
|
||||||
const time = ctx.currentTime + 0.05;
|
try {
|
||||||
const ref = await onTrigger(time, params, onended);
|
// Pre-load the sample by calling onTrigger with a future time
|
||||||
trigRef.current = ref;
|
// This triggers the loading but schedules playback for later
|
||||||
if (ref?.node) {
|
const time = ctx.currentTime + 0.05; // Give 50ms for loading
|
||||||
connectToDestination(ref.node);
|
const ref = await onTrigger(time, params, onended);
|
||||||
|
trigRef.current = ref;
|
||||||
|
if (ref?.node) {
|
||||||
|
connectToDestination(ref.node);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
errMsg = err;
|
||||||
|
}
|
||||||
|
if (attempt == 9) {
|
||||||
|
console.warn('Failed to trigger sound after 10 attempts' + (errMsg ? `: ${errMsg}` : ''));
|
||||||
|
} else {
|
||||||
|
await wait(200);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
console.warn('Failed to trigger sound:', err);
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue