// How many ringing bottom notes can this device render? Instruments like Chaos gong are costly // per ringing voice (strongly coupled modes can't be culled), so measure one voice's cost in a // render worker (as playback would run it) and allow as many voices as fit the budget. import { DEFAULTS } from "@modal-synth/engine/params.ts"; import { PRESETS } from "@modal-synth/presets.ts"; import type { Params } from "../../core/params.ts"; import { parseModalPreset } from "../../instruments/modal.ts"; import type { BenchNote, FromWorker, ToWorker } from "../../instruments/stream/protocol.ts"; const BUDGET = 0.6; // render time / audio time allowed for the busiest worker export interface VoiceCapacity { cap: number; // voices allowed perVoice: number; // measured load of one ringing voice } function sound(P: Params): Params | null { const name = P.bottomInstrument as string; if (!name.startsWith("modal: ")) return null; const p = name === "modal: custom" ? parseModalPreset(P.modalCustom as string) : PRESETS.find((x) => `modal: ${x.name}` === name)?.p; return { ...DEFAULTS, ...(p ?? {}) }; } export async function overlapCapacity(P: Params, sampleRate: number, wanted: number): Promise { const s = sound(P); if (!s) return { cap: wanted, perVoice: 0 }; const key = `double-pendulum:capacity:04-crash:${JSON.stringify({ v: 1, sr: sampleRate, i: P.bottomInstrument, c: P.modalCustom })}`; try { const c = localStorage.getItem(key); if (c) { const r = JSON.parse(c) as VoiceCapacity; return { ...r, cap: Math.max(1, Math.min(wanted, r.perVoice > 0 ? Math.floor(BUDGET / r.perVoice) : wanted)) }; } } catch { /* no storage */ } // one voice, struck repeatedly so it always rings (each strike replaces the last) const notes: BenchNote[] = []; for (let t = 0; t < 2; t += 0.7) notes.push({ time: t, note: 31 + Math.round(t * 10), vel: 0.8, len: 0.5 }); const perVoice = await new Promise((resolve) => { const w = new Worker(new URL("../../instruments/stream/renderWorker.ts", import.meta.url), { type: "module" }); w.onmessage = (e: MessageEvent) => { if (e.data.type === "bench") { resolve(e.data.load); w.terminate(); } }; const m: ToWorker = { type: "bench", sampleRate, params: { ...s, voices: 1 }, notes, seconds: 2, warmup: 0.5 }; w.postMessage(m); }); try { localStorage.setItem(key, JSON.stringify({ cap: 0, perVoice })); } catch { /* no storage */ } return { cap: Math.max(1, Math.min(wanted, Math.floor(BUDGET / perVoice))), perVoice }; } export const OVERLAP_BUDGET = BUDGET;