mirror of
https://codeberg.org/uzu/strudel
synced 2026-09-05 06:57:35 -04:00
first draft for sound preloading
This commit is contained in:
@@ -46,8 +46,8 @@ export function repl({
|
||||
|
||||
logger(`[eval] code updated`);
|
||||
pattern = editPattern?.(pattern) || pattern;
|
||||
await afterEval?.({ code, pattern });
|
||||
scheduler.setPattern(pattern, autostart);
|
||||
afterEval?.({ code, pattern });
|
||||
return pattern;
|
||||
} catch (err) {
|
||||
// console.warn(`[repl] eval error: ${err.message}`);
|
||||
|
||||
@@ -50,13 +50,13 @@ function useStrudel({
|
||||
setCode(code);
|
||||
await beforeEval?.();
|
||||
},
|
||||
afterEval: (res) => {
|
||||
afterEval: async (res) => {
|
||||
const { pattern: _pattern, code } = res;
|
||||
setActiveCode(code);
|
||||
setPattern(_pattern);
|
||||
setEvalError();
|
||||
setSchedulerError();
|
||||
afterEval?.(res);
|
||||
await afterEval?.(res);
|
||||
},
|
||||
onToggle: (v) => {
|
||||
setStarted(v);
|
||||
|
||||
@@ -170,7 +170,7 @@ export const webaudioOutput = async (hap, deadline, hapDuration, cps) => {
|
||||
sourceNode = source(t, hap.value, hapDuration);
|
||||
} else if (getSound(s)) {
|
||||
const { onTrigger } = getSound(s);
|
||||
const soundHandle = await onTrigger(t, hap.value, onended);
|
||||
const soundHandle = await onTrigger(t, { ...hap.value, s }, onended);
|
||||
if (soundHandle) {
|
||||
sourceNode = soundHandle.node;
|
||||
soundHandle.stop(t + hapDuration);
|
||||
|
||||
@@ -118,11 +118,39 @@ export function Repl({ embedded = false }) {
|
||||
cleanupUi();
|
||||
cleanupDraw();
|
||||
},
|
||||
afterEval: ({ code }) => {
|
||||
afterEval: async ({ code, pattern }) => {
|
||||
// preload sounds
|
||||
const t = scheduler.getTime();
|
||||
const lookahead = 16;
|
||||
const upcoming = pattern
|
||||
.queryArc(t, t + lookahead)
|
||||
.filter((h) => h.value.s)
|
||||
.map((h) => `${h.value.bank ? `${h.value.bank}_` : ''}${h.value.s}:${h.value.n || 0}`)
|
||||
.filter((v, i, all) => all.indexOf(v) === i);
|
||||
// console.log('now preloading sounds:', upcoming);
|
||||
const preload = upcoming.map(async (v) => {
|
||||
const [s, n] = v.split(':');
|
||||
const sound = soundMap.value[s];
|
||||
if (!sound) {
|
||||
throw new Error(`[preload] error: sound not found: "${s}:${n}"`);
|
||||
}
|
||||
// TODO: only preload if not already preloaded...
|
||||
// TODO: add sound.preload interface that only loads the sample, without creating a buffersource
|
||||
return sound.onTrigger(getAudioContext().currentTime + 0.5, { s, n }, () => {
|
||||
// console.log('onended');
|
||||
});
|
||||
});
|
||||
await Promise.all(preload);
|
||||
// console.log('preloading done');
|
||||
|
||||
setPending(false);
|
||||
setLatestCode(code);
|
||||
window.location.hash = '#' + encodeURIComponent(btoa(code));
|
||||
},
|
||||
onEvalError: (err) => {
|
||||
console.log('errr');
|
||||
setPending(false);
|
||||
},
|
||||
onToggle: (play) => !play && cleanupDraw(false),
|
||||
drawContext,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user