/ interactive_tutorials / hardening_softening
Sign in
interactive_tutorials / hardening_softening
hardening_softening / app / js / instruments.js
152 lines · 6.0 KBBlameHistoryRaw
1// Instruments for the lab slides. Each is a fixed linear filter applied to the
2// oscillator's sound u(t), y = h * u, built as a sum of resonances. A
3// resonance { f, tau, gain } has impulse response (2 gain / tau) e^(-t/tau)
4// sin(2 pi f t): a peak of height gain at f, about 1 / (pi tau) wide. The tuned
5// instruments are built on a base frequency (the oscillator's natural
6// frequency |f0|), the marimba and harp on a scale (SCALES) from it. The
7// whole filter is scaled so that its response peaks at 1 (between MIN_F and
8// MAX_F).
10const MIN_F = 50;
11const MAX_F = 4000;
12// Scales for the tuned instruments, as semitones above the base within an
13// octave (equal temperament).
14export const SCALES = {
15 'major-pentatonic': { label: 'Major pentatonic', steps: [0, 2, 4, 7, 9] },
16 'minor-pentatonic': { label: 'Minor pentatonic', steps: [0, 3, 5, 7, 10] },
17 major: { label: 'Major', steps: [0, 2, 4, 5, 7, 9, 11] },
18 minor: { label: 'Natural minor', steps: [0, 2, 3, 5, 7, 8, 10] },
19 'harmonic-minor': { label: 'Harmonic minor', steps: [0, 2, 3, 5, 7, 8, 11] },
20 blues: { label: 'Blues', steps: [0, 3, 5, 6, 7, 10] },
21 'whole-tone': { label: 'Whole tone', steps: [0, 2, 4, 6, 8, 10] },
22 chromatic: { label: 'Chromatic', steps: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] },
23};
24export const DEFAULT_SCALE = 'major-pentatonic';
26// the notes of a scale from a frequency, over a number of octaves
27const notesOf = (scaleName) => (from, octaves) => {
28 const { steps } = SCALES[scaleName] ?? SCALES[DEFAULT_SCALE];
29 const notes = [];
30 for (let o = 0; o < octaves; o++) for (const s of steps) notes.push(from * 2 ** (o + s / 12));
31 return notes;
32};
33const res = (f, tau, gain = 1) => ({ f, tau, gain });
35// a fixed pseudo-random sequence, so the metal plate is the same every time
36function seeded(seed) {
37 let s = seed;
38 return () => {
39 s = (s * 16807) % 2147483647;
40 return (s - 1) / 2147483646;
41 };
44export const INSTRUMENTS = {
45 raw: { label: 'Raw (the oscillator alone)', parts: () => [] },
46 wood: {
47 label: 'Wooden body',
48 parts: () => [[100, 0.08, 1], [205, 0.06, 0.9], [275, 0.05, 0.8], [390, 0.04, 0.7], [460, 0.04, 0.7],
49 [550, 0.035, 0.6], [700, 0.03, 0.5], [1100, 0.02, 0.4], [1500, 0.02, 0.3], [2500, 0.015, 0.2]].map(([f, t, g]) => res(f, t, g)),
50 },
51 plate: {
52 label: 'Metal plate',
53 // 96 inharmonic resonances spread from 100 to 4000 Hz, not tuned
54 parts: () => {
55 const rand = seeded(7);
56 return Array.from({ length: 96 }, (_, i) => {
57 const f = 100 * (4000 / 100) ** ((i + rand()) / 96);
58 return res(f, 0.3 + 0.9 * rand(), 1 / (1 + f / 800));
59 });
60 },
61 },
62 marimba: {
63 label: 'Marimba (bars tuned to f₀)',
64 tuned: true,
65 usesScale: true,
66 // bars on the scale from the base over two octaves, each with the
67 // partials of a marimba bar at 1, 3.93 and 9.2 times its pitch
68 parts: (b, scale) => scale(b, 2).flatMap((f) => [res(f, 0.6, 1), res(3.93 * f, 0.25, 0.3), res(9.2 * f, 0.1, 0.1)]),
69 },
70 harp: {
71 label: 'Harp (strings tuned to f₀)',
72 tuned: true,
73 // strings on the scale from an octave below the base, each with
74 // its first six harmonics, higher ones fainter and shorter
75 usesScale: true,
76 parts: (b, scale) => scale(b / 2, 3).flatMap((f) => [1, 2, 3, 4, 5, 6].map((n) => res(n * f, 1.5 / Math.sqrt(n), 1 / n))),
77 },
78 bell: {
79 label: 'Bell (tuned to f₀)',
80 tuned: true,
81 // church bell partials: hum, prime, tierce (minor third), quint, nominal, ...
82 parts: (b) => [[0.5, 2, 0.8], [1, 1.8, 1], [1.2, 1.5, 0.8], [1.5, 1.3, 0.6], [2, 1.1, 0.7], [2.5, 0.9, 0.4],
83 [2.67, 0.8, 0.4], [3, 0.7, 0.3], [4, 0.5, 0.25], [5.33, 0.4, 0.2], [6.67, 0.3, 0.15]].map(([r, t, g]) => res(b * r, t, g)),
84 },
85};
87const base = (b) => (b > 0 ? b : 220);
88const partsOf = (name, b, scaleName) => (INSTRUMENTS[name] ?? INSTRUMENTS.raw).parts(base(b), notesOf(scaleName));
90// the unscaled frequency response at f (Hz), as [re, im]
91function rawResponse(parts, f) {
92 const w = 2 * Math.PI * f;
93 let re = 0;
94 let im = 0;
95 for (const p of parts) {
96 // (2 gain / tau) wk / ((1/tau + i w)^2 + wk^2): of size gain at f = fk
97 const wk = 2 * Math.PI * p.f;
98 const a = 1 / p.tau;
99 const dr = a * a - w * w + wk * wk;
100 const di = 2 * a * w;
101 const n = (2 * a * wk * p.gain) / (dr * dr + di * di);
102 re += n * dr;
103 im -= n * di;
104 }
105 return [re, im];
108const GRID = Array.from({ length: 800 }, (_, i) => MIN_F * (MAX_F / MIN_F) ** (i / 799));
110// the factor that makes the response peak at 1: searched on a grid and at
111// every resonance (narrow peaks fall between grid points)
112function peakScale(parts) {
113 let peak = 0;
114 const fs = [...GRID, ...parts.map((p) => p.f)];
115 for (const f of fs) peak = Math.max(peak, Math.hypot(...rawResponse(parts, f)));
116 return peak > 0 ? 1 / peak : 1;
119// |H(f)| of an instrument tuned to base b, scaled to peak at 1, at the frequencies fs
120export function responseMagnitude(name, b, fs, scaleName = DEFAULT_SCALE) {
121 const parts = partsOf(name, b, scaleName);
122 const k = peakScale(parts);
123 return fs.map((f) => k * Math.hypot(...rawResponse(parts, f)));
126// The impulse response h, sampled at rate sr and scaled to peak at 1 (null for
127// raw), long enough for its slowest resonance to fall by e^5 (about 43 dB).
128export function impulseResponse(name, b, sr, scaleName = DEFAULT_SCALE) {
129 const parts = partsOf(name, b, scaleName).filter((p) => p.f < 0.45 * sr);
130 if (!parts.length) return null;
131 const longest = Math.max(...parts.map((p) => p.tau));
132 const n = Math.max(1, Math.round(5 * longest * sr));
133 const h = new Float32Array(n);
134 for (const p of parts) {
135 // A r^i sin(w i) by its recurrence y[i] = 2 r cos(w) y[i-1] - r^2 y[i-2]
136 const r = Math.exp(-1 / (p.tau * sr));
137 const w = (2 * Math.PI * p.f) / sr;
138 const c1 = 2 * r * Math.cos(w);
139 const c2 = -r * r;
140 let y2 = 0;
141 let y1 = ((2 * p.gain) / (p.tau * sr)) * r * Math.sin(w);
142 for (let i = 1; i < n; i++) {
143 h[i] += y1;
144 const y = c1 * y1 + c2 * y2;
145 y2 = y1;
146 y1 = y;
147 }
148 }
149 const k = peakScale(parts);
150 for (let i = 0; i < n; i++) h[i] *= k;
151 return h;
↑↓ move↵openescclose