/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / instruments / types.ts
95 lines · 3.6 KBBlameHistoryRaw
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;
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;
25export interface NoteInstrument {
26 /** resolves when the instrument can play (e.g. an AudioWorklet has loaded) */
27 ready?: Promise<void>;
28 /** share of real time the audio thread spends on this instrument (0..1), if it can tell */
29 load?(): number;
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;
34 /** rendered-ahead instruments: polyphony allowed on this device, and what the sound asked for */
35 voices?(): { allowed: number; wanted: number };
36 play(ev: NoteEvent): NoteHandle | null;
37 dispose(): void;
40/** Map the drone's color (0..1) onto one instrument setting. from/to are 0..1 of that setting's range. */
41export interface TimbreMap {
42 target: string;
43 from: number;
44 to: number;
47export interface DroneInstrument {
48 /** resolves when the instrument can play */
49 ready?: Promise<void>;
50 /** share of real time the audio thread spends on this instrument (0..1), if it can tell */
51 load?(): number;
52 start(when: number, freq: number): void;
53 /** Stop with a release ending at `when`. */
54 stop(when: number, release: number): void;
55 /**
56 * Continuous controls, called often.
57 * level: linear gain 0..1; color: 0..1 timbre control (driven by energy).
58 */
59 set(
60 when: number,
61 controls: {
62 level: number;
63 color: number;
64 freq: number;
65 timbre?: TimbreMap[];
66 /** hold the output loudness steady while the timbre changes (instruments that support it) */
67 normalize?: boolean;
68 },
69 ): void;
70 /** current values of mapped settings, for the author readouts */
71 describe?(): string;
72 dispose(): void;
75export interface InstrumentOptions {
76 /** instrument-specific settings, e.g. a pasted modal-synth preset (JSON) */
77 custom?: string;
78 /** schedule each note's release when it is played (no final-note hold); for device checks */
79 immediateRelease?: boolean;
80 /**
81 * Effects: "own" (default: the instrument's own, e.g. modal-synth's reverb/delay), "none" (dry),
82 * or a shared reverb to send to (the instrument's reverb amount sets the send).
83 */
84 effects?: "own" | "none" | { send: AudioNode };
85 /** instrument settings to override (modal-synth parameters, e.g. { voices: 2 }) */
86 overrides?: Record<string, number | string | boolean>;
87 /**
88 * Drones: hold one note at `center` (MIDI) and glide its pitch with pitch bend over ±`range`
89 * semitones (≤ 24), instead of re-striking when the pitch changes.
90 */
91 glide?: { center: number; range: number };
94export type NoteInstrumentFactory = (ctx: AudioContext, out: AudioNode, opts: InstrumentOptions) => NoteInstrument;
95export type DroneInstrumentFactory = (ctx: AudioContext, out: AudioNode, opts: InstrumentOptions) => DroneInstrument;
↑↓ move↵openescclose