1// The energy hum: one drone instrument whose timbre follows the energy, with fade-in at the start
2// and a fade-out that ends with the piece. Shared by the compositions.
3import { dbToGain } from "../../core/audio.ts";
4import type { Params } from "../../core/params.ts";
5import { DRONE_INSTRUMENTS } from "../../instruments/index.ts";
6import type { DroneInstrument } from "../../instruments/types.ts";
7import { midiToFreq } from "../../music/scales.ts";
9export class Hum {
10 inst: DroneInstrument | null = null;
11 private key = "";
13 /** Create the instrument (or switch it, crossfading, if the setting changed while playing). */
14 ensure(ctx: AudioContext, out: AudioNode, P: Params, playing: boolean) {
15 const name = P.humInstrument as string;
16 const key = name === "modal: custom" ? `${name}\n${P.humCustom}` : name;
17 if (this.inst && this.key === key) return;
18 const old = this.inst;
19 this.key = key;
20 const h = DRONE_INSTRUMENTS[name](ctx, out, { custom: P.humCustom as string });
21 this.inst = h;
22 if (old && playing) {
23 const t = ctx.currentTime;
24 old.stop(t + 0.5, 0.5);
25 window.setTimeout(() => old.dispose(), 1500);
26 void (h.ready ?? Promise.resolve()).then(() => h === this.inst && h.start(ctx.currentTime + 0.05, midiToFreq(P.humPitch as number)));
27 }
28 }
30 get ready(): Promise<void> | undefined {
31 return this.inst?.ready;
32 }
34 start(ctx: AudioContext, P: Params) {
35 this.inst?.start(ctx.currentTime + 0.05, midiToFreq(P.humPitch as number));
36 }
38 /**
39 * e: normalized energy now; t: piece time; end: piece time everything has faded, or null while
40 * still running (the fade-out only begins once the end is known).
41 */
42 update(ctx: AudioContext, P: Params, e: number, t: number, end: number | null) {
43 if (!this.inst) return;
44 const peak = P.energyPeak as number;
45 const rel = Math.max(0, Math.min(1, e / peak));
46 const e0 = Math.min(P.energyStart as number, 0.999 * peak);
47 const colorBase =
48 P.humColorMode === "log" ? Math.max(0, Math.min(1, Math.log(Math.max(e, 1e-12) / e0) / Math.log(peak / e0))) : rel;
49 const fadeIn = P.humFadeIn as number;
50 const fadeOut = P.humFadeOut as number;
51 const fade = Math.min(1, fadeIn > 0 ? t / fadeIn : 1, end !== null && fadeOut > 0 ? (end - t) / fadeOut : 1);
52 const follow = P.humFollow as number;
53 this.inst.set(ctx.currentTime, {
54 level: dbToGain(P.humLevel as number) * Math.max(0, fade) * (1 - follow + follow * rel),
55 color: Math.pow(colorBase, P.humColorCurve as number),
56 freq: midiToFreq(P.humPitch as number),
57 normalize: P.humNormalize as boolean,
58 timbre: [1, 2]
59 .filter((i) => P[`humTarget${i}`] !== "none")
60 .map((i) => ({ target: P[`humTarget${i}`] as string, from: P[`humFrom${i}`] as number, to: P[`humTo${i}`] as number })),
61 });
62 }
64 describe(): string {
65 return this.inst?.describe?.() || "–";
66 }
68 /** Release: fade out if it was sounding, then dispose. */
69 release(ctx: AudioContext, wasPlaying: boolean) {
70 const h = this.inst;
71 if (!h) return;
72 if (wasPlaying) h.stop(ctx.currentTime + 0.3, 0.3); // a hum is only started once playing
73 window.setTimeout(() => h.dispose(), 600);
74 this.inst = null;
75 this.key = "";
76 }
77}