Attempt system for sounds tab previews
This commit is contained in:
parent
ea7dc7a732
commit
8144ec46bd
2 changed files with 28 additions and 13 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);
|
||||||
|
|
||||||
|
|
@ -121,19 +125,30 @@ export function SoundsTab() {
|
||||||
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.5; // Give 500ms for loading
|
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);
|
||||||
|
soundPreviewIdx++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log(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