/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / instruments / placeholders.ts
112 lines · 3.9 KBBlameHistoryRaw
1// Placeholder instruments: deliberately plain, to be replaced later.
2import { fadeCurve } from "./fade.ts";
3import type { DroneInstrument, NoteEvent, NoteHandle, NoteInstrument } from "./types.ts";
5/** Sine + soft octave triangle with a percussive exponential decay. */
6export function placeholderTone(ctx: AudioContext, out: AudioNode): NoteInstrument {
7 return {
8 play({ when, freq, velocity, pan, duration }: NoteEvent): NoteHandle | null {
9 if (velocity <= 0) return null;
10 const t0 = Math.max(when, ctx.currentTime);
11 const end = t0 + duration;
12 const peak = 0.3 * velocity;
13 const ta = t0 + 0.004;
14 const env = ctx.createGain();
15 env.gain.setValueAtTime(0, t0);
16 env.gain.linearRampToValueAtTime(peak, ta);
17 env.gain.exponentialRampToValueAtTime(0.0001, end);
18 const panner = ctx.createStereoPanner();
19 panner.pan.value = Math.max(-1, Math.min(1, pan));
20 env.connect(panner).connect(out);
22 const a = ctx.createOscillator();
23 a.frequency.value = freq;
24 a.connect(env);
25 const b = ctx.createOscillator();
26 b.type = "triangle";
27 b.frequency.value = freq * 2;
28 const bg = ctx.createGain();
29 bg.gain.value = 0.08 + 0.15 * velocity; // a touch brighter when louder
30 b.connect(bg).connect(env);
32 for (const o of [a, b]) {
33 o.start(t0);
34 o.stop(end + 0.05);
35 }
36 b.onended = () => panner.disconnect();
38 return {
39 hold(from, holdLen, fadeLen) {
40 const t = Math.max(from, ta, ctx.currentTime);
41 if (t >= end) return;
42 // level of the exponential decay at t
43 const v = peak * Math.pow(0.0001 / peak, (t - ta) / (end - ta));
44 const g = env.gain;
45 if (typeof g.cancelAndHoldAtTime === "function") g.cancelAndHoldAtTime(t);
46 else g.cancelScheduledValues(t);
47 g.setValueAtTime(v, t);
48 g.setValueCurveAtTime(fadeCurve(v), t + Math.max(holdLen, 0.005), fadeLen);
49 const stopAt = t + holdLen + fadeLen + 0.05;
50 a.stop(stopAt);
51 b.stop(stopAt);
52 },
53 };
54 },
55 dispose() {},
56 };
59/**
60 * Hum: sub sine + two detuned saws through a low-pass. `color` (0..1) opens the
61 * filter and adds resonance, so the timbre brightens with energy at fixed pitch.
62 */
63export function placeholderHum(ctx: AudioContext, out: AudioNode): DroneInstrument {
64 const gain = ctx.createGain();
65 gain.gain.value = 0;
66 const filter = ctx.createBiquadFilter();
67 filter.type = "lowpass";
68 filter.frequency.value = 120;
69 filter.Q.value = 0.7;
70 filter.connect(gain).connect(out);
72 const sub = ctx.createOscillator();
73 const subGain = ctx.createGain();
74 subGain.gain.value = 0.5;
75 sub.connect(subGain).connect(gain); // sub bypasses the filter: the stable core of the hum
77 const saws = [-6, 6].map((cents) => {
78 const o = ctx.createOscillator();
79 o.type = "sawtooth";
80 o.detune.value = cents;
81 const g = ctx.createGain();
82 g.gain.value = 0.25;
83 o.connect(g).connect(filter);
84 return o;
85 });
86 const oscs = [sub, ...saws];
88 return {
89 start(when, freq) {
90 sub.frequency.value = freq;
91 for (const o of saws) o.frequency.value = freq * 2;
92 for (const o of oscs) o.start(when);
93 },
94 stop(when, release) {
95 gain.gain.cancelScheduledValues(when - release);
96 gain.gain.setTargetAtTime(0, when - release, release / 4);
97 for (const o of oscs) o.stop(when + 0.1);
98 },
99 set(when, { level, color, freq }) {
100 const tc = 0.08;
101 gain.gain.setTargetAtTime(level, when, tc);
102 sub.frequency.setTargetAtTime(freq, when, tc);
103 for (const o of saws) o.frequency.setTargetAtTime(freq * 2, when, tc);
104 const c = Math.max(0, Math.min(1, color));
105 filter.frequency.setTargetAtTime(freq * 2 * Math.pow(2, 6 * c), when, tc);
106 filter.Q.setTargetAtTime(0.7 + 5 * c * c, when, tc);
107 },
108 dispose() {
109 gain.disconnect();
110 },
111 };
↑↓ move↵openescclose