/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / instruments / types.ts
71 lines · 2.3 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 play(ev: NoteEvent): NoteHandle | null;
29 dispose(): void;
32/** Map the drone's color (0..1) onto one instrument setting. from/to are 0..1 of that setting's range. */
33export interface TimbreMap {
34 target: string;
35 from: number;
36 to: number;
39export interface DroneInstrument {
40 /** resolves when the instrument can play */
41 ready?: Promise<void>;
42 start(when: number, freq: number): void;
43 /** Stop with a release ending at `when`. */
44 stop(when: number, release: number): void;
45 /**
46 * Continuous controls, called often.
47 * level: linear gain 0..1; color: 0..1 timbre control (driven by energy).
48 */
49 set(
50 when: number,
51 controls: {
52 level: number;
53 color: number;
54 freq: number;
55 timbre?: TimbreMap[];
56 /** hold the output loudness steady while the timbre changes (instruments that support it) */
57 normalize?: boolean;
58 },
59 ): void;
60 /** current values of mapped settings, for the author readouts */
61 describe?(): string;
62 dispose(): void;
65export interface InstrumentOptions {
66 /** instrument-specific settings, e.g. a pasted modal-synth preset (JSON) */
67 custom?: string;
70export type NoteInstrumentFactory = (ctx: AudioContext, out: AudioNode, opts: InstrumentOptions) => NoteInstrument;
71export type DroneInstrumentFactory = (ctx: AudioContext, out: AudioNode, opts: InstrumentOptions) => DroneInstrument;
↑↓ move↵openescclose