73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 1// Instrument interfaces. Real instruments drop in by implementing these and
2// registering in ./index.ts; compositions pick them by name.
4export interface NoteEvent {
5 /** AudioContext time */
6 when: number;
7 freq: number;
8 /** dynamics, 0..1 (overall level is set by the composition's mixer, not here) */
9 velocity: number;
10 /** -1..1 */
11 pan: number;
12 /** seconds; how long the note rings (instrument may interpret freely) */
13 duration: number;
14}
16/** Control over a note after it has been scheduled. */
17export interface NoteHandle {
18 /**
19 * Stop the note's natural decay at `from` (AudioContext time), sustain its level for
20 * `holdLen` seconds, then fade out over `fadeLen` seconds. Used for the final note of a piece.
21 */
22 hold(from: number, holdLen: number, fadeLen: number): void;
23}
25export interface NoteInstrument {
26 /** resolves when the instrument can play (e.g. an AudioWorklet has loaded) */
27 ready?: Promise<void>;
8a22646orbits: limit the orbit count to what the device can play liveJeremy Magland 28 /** share of real time the audio thread spends on this instrument (0..1), if it can tell */
29 load?(): number;
216afddorbits: render instruments ahead in Web WorkersJeremy Magland 30 /** rendered-ahead instruments: every note before `time` (AudioContext time) has been played */
31 horizon?(time: number): void;
32 /** rendered-ahead instruments: how often playback ran out of rendered audio */
33 underruns?(): number;
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 34 play(ev: NoteEvent): NoteHandle | null;
35 dispose(): void;
36}
38/** Map the drone's color (0..1) onto one instrument setting. from/to are 0..1 of that setting's range. */
39export interface TimbreMap {
40 target: string;
41 from: number;
42 to: number;
43}
45export interface DroneInstrument {
46 /** resolves when the instrument can play */
47 ready?: Promise<void>;
8a22646orbits: limit the orbit count to what the device can play liveJeremy Magland 48 /** share of real time the audio thread spends on this instrument (0..1), if it can tell */
49 load?(): number;
73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 50 start(when: number, freq: number): void;
51 /** Stop with a release ending at `when`. */
52 stop(when: number, release: number): void;
53 /**
54 * Continuous controls, called often.
55 * level: linear gain 0..1; color: 0..1 timbre control (driven by energy).
56 */
57 set(
58 when: number,
59 controls: {
60 level: number;
61 color: number;
62 freq: number;
63 timbre?: TimbreMap[];
64 /** hold the output loudness steady while the timbre changes (instruments that support it) */
65 normalize?: boolean;
66 },
67 ): void;
68 /** current values of mapped settings, for the author readouts */
69 describe?(): string;
70 dispose(): void;
71}
73export interface InstrumentOptions {
74 /** instrument-specific settings, e.g. a pasted modal-synth preset (JSON) */
75 custom?: string;
8a22646orbits: limit the orbit count to what the device can play liveJeremy Magland 76 /** schedule each note's release when it is played (no final-note hold); for device checks */
77 immediateRelease?: boolean;
79e1f25orbits: lighter on phones (faster engine, shared reverb, cached drawing)Jeremy Magland 78 /**
79 * Effects: "own" (default: the instrument's own, e.g. modal-synth's reverb/delay), "none" (dry),
80 * or a shared reverb to send to (the instrument's reverb amount sets the send).
81 */
82 effects?: "own" | "none" | { send: AudioNode };
85export type NoteInstrumentFactory = (ctx: AudioContext, out: AudioNode, opts: InstrumentOptions) => NoteInstrument;
86export type DroneInstrumentFactory = (ctx: AudioContext, out: AudioNode, opts: InstrumentOptions) => DroneInstrument;