// The streaming system: messages between the page, the score worker, the synth workers and the // player worklet. Every time here is *piece time* (seconds from the start of the piece), never // AudioContext time: a performance is computed in the background as a function of piece time // alone, and the player maps it onto the audio clock, pausing whenever something isn't ready. // // score worker ──notes──▶ synth workers ──audio──▶ player worklet ──▶ effects ──▶ speakers // │ (or audio itself, e.g. the ensemble) ──────────▲ │ clock (playhead, how far ahead) // └──frames, events──▶ page (draws at the player's playhead) ◀──┘ status import type { Params as SynthParams } from "@modal-synth/engine/params.ts"; import type { Params } from "../core/params.ts"; /** Every performance renders at this rate on every device (the AudioContext runs at it too). */ export const SAMPLE_RATE = 48000; /** Synth rendering is done in blocks of this many frames, from piece frame 0 (deterministic). */ export const BLOCK = 128; /** Frames per audio chunk sent to the player. */ export const CHUNK = 2048; // ── score worker ── export interface ScoreInit { type: "init"; comp: string; // composition id P: Params; seed: number; /** piece time playback starts at */ start: number; /** piece time sound is computed from (start minus a pre-roll, so ringing notes are heard) */ from: number; /** one port per synth stem, in stem order (null for stems the score renders itself) */ synthPorts: (MessagePort | null)[]; /** the score's own link to the player: clock in, audio chunks (score stems) and the end out */ playerPort: MessagePort; } /** score worker → page */ export type ScoreOut = | { type: "frames"; track: number; stride: number; data: Float64Array } // [t, v…] per row | { type: "events"; events: VisEvent[] } | { type: "info"; t: number; info: Record } | { type: "end"; t: number } // the piece ends (everything faded) at piece time t | { type: "error"; message: string }; /** Something to draw at a moment: a crossing, a ring… (fields are up to the piece). */ export interface VisEvent { t: number; kind: string; [k: string]: number | string; } // ── synth workers ── export interface SynthInit { type: "init"; sound: SynthParams; /** first piece frame to render (block-aligned) */ fromFrame: number; /** first piece frame to send to the player (earlier frames are rendered silently) */ sendFrame: number; stem: number; scorePort: MessagePort; playerPort: MessagePort; /** testing: render this many times slower (simulates a slow device; ?slow=N in the page URL) */ slow?: number; } /** score → synth worker: timed instructions, in time order */ export type SynthCmd = | { t: number; type: "noteOn"; note: number; vel: number } /** `on`: when its note began; skipped if the same pitch was struck again since */ | { t: number; type: "noteOff"; note: number; on?: number } | { t: number; type: "params"; params: Record } // merged over the sound | { t: number; type: "bend"; value: number } /** output gain: move toward `value` with time constant `tau` */ | { t: number; type: "gain"; value: number; tau: number } /** the final note: from t, slow the decay so it rings `hold + fade` s, and fade out over the last `fade` s */ | { t: number; type: "hold"; hold: number; fade: number }; export type ScoreToSynth = | { type: "cmds"; cmds: SynthCmd[]; horizon: number } // every command before `horizon` has been sent | { type: "end"; t: number }; // render up to t, then stop // ── player ── /** producer (synth or score worker) → player */ export type ToPlayer = | { type: "chunk"; stem: number; frame: number; L: Float32Array; R: Float32Array } | { type: "done"; stem: number; frame: number } // this stem is silent from `frame` on | { type: "end"; frame: number }; // the piece ends here (from the score) /** player → producers, ~50 times a second */ export interface Clock { type: "clock"; playhead: number; // piece frame ahead: number; // frames to keep rendered ahead of the playhead } /** player → page */ export interface PlayerStatus { type: "status"; state: "buffering" | "playing" | "ended"; playhead: number; // piece frame at: number; // AudioContext frame (currentFrame) when the playhead was there /** 0..1: how full the buffer is toward resuming (while buffering) */ fill: number; rebuffers: number; /** seconds rendered ahead of the playhead (the least of all stems) */ ahead: number; } export interface PlayerOptions { stems: number; outputs: number; /** stem → output it plays on */ route: number[]; startFrame: number; /** the piece's likely end (frame), until the score knows it */ estimatedEnd: number; }