// What a composition's score implements, and the sink it writes to. A score runs in the score // worker: it simulates the piece in piece time and emits everything the performance needs. // It must be deterministic (same params + seed → same output everywhere): use physics/dmath.ts, // never Math.sin/cos/exp/log/pow/hypot or Math.random. import type { Params } from "../core/params.ts"; import type { SynthCmd, VisEvent } from "./protocol.ts"; /** A note whose release can still be moved (the final-note hold). */ export interface NoteRef { stem: number; t: number; off: SynthCmd; } export interface ScoreSink { /** false while fast-forwarding silently to the start point (then nothing is emitted) */ readonly emitting: boolean; /** one row of drawing data on a track (rows in time order) */ frame(track: number, t: number, values: ArrayLike): void; event(e: VisEvent): void; /** author readouts, as of piece time t */ info(t: number, info: Record): void; /** a note on a synth stem: pitch as MIDI (fractional ok), velocity 0..1, length in s */ note(stem: number, t: number, midi: number, vel: number, dur: number): NoteRef | null; /** let a note ring: its release moves to the end of the fade (see SynthCmd "hold") */ hold(ref: NoteRef, at: number, hold: number, fade: number): void; /** any other timed command for a synth stem (drones: gain, params, …) */ cmd(stem: number, c: SynthCmd): void; /** audio the score renders itself, for a "score" stem: consecutive samples from `from` */ audio(stem: number, L: Float32Array, R: Float32Array): void; /** the piece ends (everything faded) at piece time t */ end(t: number): void; } export interface PieceScore { /** simulate up to piece time t, emitting through the sink */ advance(t: number): void; /** piece time simulated so far */ readonly time: number; } export type ScoreFactory = (P: Params, seed: number, sink: ScoreSink) => PieceScore;