// Instrument interfaces. Real instruments drop in by implementing these and // registering in ./index.ts; compositions pick them by name. export interface NoteEvent { /** AudioContext time */ when: number; freq: number; /** dynamics, 0..1 (overall level is set by the composition's mixer, not here) */ velocity: number; /** -1..1 */ pan: number; /** seconds; how long the note rings (instrument may interpret freely) */ duration: number; } /** Control over a note after it has been scheduled. */ export interface NoteHandle { /** * Stop the note's natural decay at `from` (AudioContext time), sustain its level for * `holdLen` seconds, then fade out over `fadeLen` seconds. Used for the final note of a piece. */ hold(from: number, holdLen: number, fadeLen: number): void; } export interface NoteInstrument { /** resolves when the instrument can play (e.g. an AudioWorklet has loaded) */ ready?: Promise; /** share of real time the audio thread spends on this instrument (0..1), if it can tell */ load?(): number; /** rendered-ahead instruments: every note before `time` (AudioContext time) has been played */ horizon?(time: number): void; /** rendered-ahead instruments: how often playback ran out of rendered audio */ underruns?(): number; play(ev: NoteEvent): NoteHandle | null; dispose(): void; } /** Map the drone's color (0..1) onto one instrument setting. from/to are 0..1 of that setting's range. */ export interface TimbreMap { target: string; from: number; to: number; } export interface DroneInstrument { /** resolves when the instrument can play */ ready?: Promise; /** share of real time the audio thread spends on this instrument (0..1), if it can tell */ load?(): number; start(when: number, freq: number): void; /** Stop with a release ending at `when`. */ stop(when: number, release: number): void; /** * Continuous controls, called often. * level: linear gain 0..1; color: 0..1 timbre control (driven by energy). */ set( when: number, controls: { level: number; color: number; freq: number; timbre?: TimbreMap[]; /** hold the output loudness steady while the timbre changes (instruments that support it) */ normalize?: boolean; }, ): void; /** current values of mapped settings, for the author readouts */ describe?(): string; dispose(): void; } export interface InstrumentOptions { /** instrument-specific settings, e.g. a pasted modal-synth preset (JSON) */ custom?: string; /** schedule each note's release when it is played (no final-note hold); for device checks */ immediateRelease?: boolean; /** * Effects: "own" (default: the instrument's own, e.g. modal-synth's reverb/delay), "none" (dry), * or a shared reverb to send to (the instrument's reverb amount sets the send). */ effects?: "own" | "none" | { send: AudioNode }; /** instrument settings to override (modal-synth parameters, e.g. { voices: 2 }) */ overrides?: Record; /** * Drones: hold one note at `center` (MIDI) and glide its pitch with pitch bend over ±`range` * semitones (≤ 24), instead of re-striking when the pitch changes. */ glide?: { center: number; range: number }; } export type NoteInstrumentFactory = (ctx: AudioContext, out: AudioNode, opts: InstrumentOptions) => NoteInstrument; export type DroneInstrumentFactory = (ctx: AudioContext, out: AudioNode, opts: InstrumentOptions) => DroneInstrument;