/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / stream / protocol.ts
120 lines · 4.9 KBBlameHistoryRaw
1// The streaming system: messages between the page, the score worker, the synth workers and the
2// player worklet. Every time here is *piece time* (seconds from the start of the piece), never
3// AudioContext time: a performance is computed in the background as a function of piece time
4// alone, and the player maps it onto the audio clock, pausing whenever something isn't ready.
5//
6// score worker ──notes──▶ synth workers ──audio──▶ player worklet ──▶ effects ──▶ speakers
7// │ (or audio itself, e.g. the ensemble) ──────────▲ │ clock (playhead, how far ahead)
8// └──frames, events──▶ page (draws at the player's playhead) ◀──┘ status
10import type { Params as SynthParams } from "@modal-synth/engine/params.ts";
11import type { Params } from "../core/params.ts";
13/** Every performance renders at this rate on every device (the AudioContext runs at it too). */
14export const SAMPLE_RATE = 48000;
15/** Synth rendering is done in blocks of this many frames, from piece frame 0 (deterministic). */
16export const BLOCK = 128;
17/** Frames per audio chunk sent to the player. */
18export const CHUNK = 2048;
20// ── score worker ──
22export interface ScoreInit {
23 type: "init";
24 comp: string; // composition id
25 P: Params;
26 seed: number;
27 /** piece time playback starts at */
28 start: number;
29 /** piece time sound is computed from (start minus a pre-roll, so ringing notes are heard) */
30 from: number;
31 /** one port per synth stem, in stem order (null for stems the score renders itself) */
32 synthPorts: (MessagePort | null)[];
33 /** the score's own link to the player: clock in, audio chunks (score stems) and the end out */
34 playerPort: MessagePort;
37/** score worker → page */
38export type ScoreOut =
39 | { type: "frames"; track: number; stride: number; data: Float64Array } // [t, v…] per row
40 | { type: "events"; events: VisEvent[] }
41 | { type: "info"; t: number; info: Record<string, string> }
42 | { type: "end"; t: number } // the piece ends (everything faded) at piece time t
43 | { type: "error"; message: string };
45/** Something to draw at a moment: a crossing, a ring… (fields are up to the piece). */
46export interface VisEvent {
47 t: number;
48 kind: string;
49 [k: string]: number | string;
52// ── synth workers ──
54export interface SynthInit {
55 type: "init";
56 sound: SynthParams;
57 /** first piece frame to render (block-aligned) */
58 fromFrame: number;
59 /** first piece frame to send to the player (earlier frames are rendered silently) */
60 sendFrame: number;
61 stem: number;
62 scorePort: MessagePort;
63 playerPort: MessagePort;
64 /** testing: render this many times slower (simulates a slow device; ?slow=N in the page URL) */
65 slow?: number;
68/** score → synth worker: timed instructions, in time order */
69export type SynthCmd =
70 | { t: number; type: "noteOn"; note: number; vel: number }
71 /** `on`: when its note began; skipped if the same pitch was struck again since */
72 | { t: number; type: "noteOff"; note: number; on?: number }
73 | { t: number; type: "params"; params: Record<string, number | string | boolean> } // merged over the sound
74 | { t: number; type: "bend"; value: number }
75 /** output gain: move toward `value` with time constant `tau`; `normalize` holds the loudness steady */
76 | { t: number; type: "gain"; value: number; tau: number; normalize?: boolean }
77 /** the final note: from t, slow the decay so it rings `hold + fade` s, and fade out over the last `fade` s */
78 | { t: number; type: "hold"; hold: number; fade: number };
80export type ScoreToSynth =
81 | { type: "cmds"; cmds: SynthCmd[]; horizon: number } // every command before `horizon` has been sent
82 | { type: "end"; t: number }; // render up to t, then stop
84// ── player ──
86/** producer (synth or score worker) → player */
87export type ToPlayer =
88 | { type: "chunk"; stem: number; frame: number; L: Float32Array; R: Float32Array }
89 | { type: "done"; stem: number; frame: number } // this stem is silent from `frame` on
90 | { type: "end"; frame: number }; // the piece ends here (from the score)
92/** player → producers, ~50 times a second */
93export interface Clock {
94 type: "clock";
95 playhead: number; // piece frame
96 ahead: number; // frames to keep rendered ahead of the playhead
99/** player → page */
100export interface PlayerStatus {
101 type: "status";
102 state: "buffering" | "playing" | "ended";
103 playhead: number; // piece frame
104 at: number; // AudioContext frame (currentFrame) when the playhead was there
105 /** 0..1: how full the buffer is toward resuming (while buffering) */
106 fill: number;
107 rebuffers: number;
108 /** seconds rendered ahead of the playhead (the least of all stems) */
109 ahead: number;
112export interface PlayerOptions {
113 stems: number;
114 outputs: number;
115 /** stem → output it plays on */
116 route: number[];
117 startFrame: number;
118 /** the piece's likely end (frame), until the score knows it */
119 estimatedEnd: number;
↑↓ move↵openescclose