5d7ea1acrash: fixed-pitch 'bottom notes' (no longer called crashes)Jeremy Magland 1// How many ringing bottom notes can this device render? Instruments like Chaos gong are costly
f7d91baAdd composition 4: crashJeremy Magland 2// per ringing voice (strongly coupled modes can't be culled), so measure one voice's cost in a
3// render worker (as playback would run it) and allow as many voices as fit the budget.
5import { DEFAULTS } from "@modal-synth/engine/params.ts";
6import { PRESETS } from "@modal-synth/presets.ts";
7import type { Params } from "../../core/params.ts";
8import { parseModalPreset } from "../../instruments/modal.ts";
9import type { BenchNote, FromWorker, ToWorker } from "../../instruments/stream/protocol.ts";
11const BUDGET = 0.6; // render time / audio time allowed for the busiest worker
13export interface VoiceCapacity {
14 cap: number; // voices allowed
15 perVoice: number; // measured load of one ringing voice
16}
18function sound(P: Params): Params | null {
5d7ea1acrash: fixed-pitch 'bottom notes' (no longer called crashes)Jeremy Magland 19 const name = P.bottomInstrument as string;
21 const p = name === "modal: custom" ? parseModalPreset(P.modalCustom as string) : PRESETS.find((x) => `modal: ${x.name}` === name)?.p;
22 return { ...DEFAULTS, ...(p ?? {}) };
23}
5d7ea1acrash: fixed-pitch 'bottom notes' (no longer called crashes)Jeremy Magland 25export async function overlapCapacity(P: Params, sampleRate: number, wanted: number): Promise<VoiceCapacity> {
27 if (!s) return { cap: wanted, perVoice: 0 };
5d7ea1acrash: fixed-pitch 'bottom notes' (no longer called crashes)Jeremy Magland 28 const key = `double-pendulum:capacity:04-crash:${JSON.stringify({ v: 1, sr: sampleRate, i: P.bottomInstrument, c: P.modalCustom })}`;
30 const c = localStorage.getItem(key);
31 if (c) {
32 const r = JSON.parse(c) as VoiceCapacity;
33 return { ...r, cap: Math.max(1, Math.min(wanted, r.perVoice > 0 ? Math.floor(BUDGET / r.perVoice) : wanted)) };
34 }
35 } catch {
36 /* no storage */
37 }
38 // one voice, struck repeatedly so it always rings (each strike replaces the last)
39 const notes: BenchNote[] = [];
40 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 });
41 const perVoice = await new Promise<number>((resolve) => {
42 const w = new Worker(new URL("../../instruments/stream/renderWorker.ts", import.meta.url), { type: "module" });
43 w.onmessage = (e: MessageEvent<FromWorker>) => {
44 if (e.data.type === "bench") {
45 resolve(e.data.load);
46 w.terminate();
47 }
48 };
49 const m: ToWorker = { type: "bench", sampleRate, params: { ...s, voices: 1 }, notes, seconds: 2, warmup: 0.5 };
50 w.postMessage(m);
51 });
52 try {
53 localStorage.setItem(key, JSON.stringify({ cap: 0, perVoice }));
54 } catch {
55 /* no storage */
56 }
57 return { cap: Math.max(1, Math.min(wanted, Math.floor(BUDGET / perVoice))), perVoice };
58}
5d7ea1acrash: fixed-pitch 'bottom notes' (no longer called crashes)Jeremy Magland 60export const OVERLAP_BUDGET = BUDGET;