|
| 1 | +/** |
| 2 | + * Synthetic verification of the HumNote DSP pipeline (run with bun): |
| 3 | + * bun /home/z/eval-dsp/dsp-test.ts |
| 4 | + * Emulates MicRecorder frames (2048 window / 40 ms hop) over generated audio |
| 5 | + * and checks note segmentation for voice-like, whistle-like, vibrato, glide, |
| 6 | + * creak and noise-only material. |
| 7 | + */ |
| 8 | +import { analyzeRecording } from '../src/lib/audio/analysis' |
| 9 | +import { computeRms } from '../src/lib/audio/notes' |
| 10 | +import { PitchFrame } from '../src/lib/audio/types' |
| 11 | +import { yin } from '../src/lib/audio/yin' |
| 12 | + |
| 13 | +const SR = 44100 |
| 14 | +const WINDOW = 2048 |
| 15 | +const HOP = Math.round(0.04 * SR) |
| 16 | + |
| 17 | +interface NoteSpec { |
| 18 | + f0: number |
| 19 | + dur: number |
| 20 | + harmonics: number[] |
| 21 | + vibratoDepth?: number // fraction of f0 |
| 22 | + vibratoHz?: number |
| 23 | + glideTo?: number // linear pitch glide target (Hz) across the note |
| 24 | +} |
| 25 | + |
| 26 | +function generate(notes: NoteSpec[], gapSec: number, noiseRms: number, breathRms: number): Float32Array { |
| 27 | + const chunks: Float32Array[] = [] |
| 28 | + const gapLen = Math.round(gapSec * SR) |
| 29 | + chunks.push(new Float32Array(gapLen)) |
| 30 | + for (const n of notes) { |
| 31 | + const len = Math.round(n.dur * SR) |
| 32 | + const chunk = new Float32Array(len) |
| 33 | + const phase = new Float64Array(n.harmonics.length) |
| 34 | + for (let i = 0; i < len; i++) { |
| 35 | + const t = i / SR |
| 36 | + const prog = t / n.dur |
| 37 | + const vib = 1 + (n.vibratoDepth ?? 0) * Math.sin(2 * Math.PI * (n.vibratoHz ?? 5.5) * t) |
| 38 | + const glide = n.glideTo ? Math.pow(n.glideTo / n.f0, prog) : 1 |
| 39 | + let s = 0 |
| 40 | + for (let h = 0; h < n.harmonics.length; h++) { |
| 41 | + phase[h] += (2 * Math.PI * n.f0 * (h + 1) * vib * glide) / SR |
| 42 | + s += n.harmonics[h] * Math.sin(phase[h]) |
| 43 | + } |
| 44 | + chunk[i] = s * 0.25 |
| 45 | + } |
| 46 | + chunks.push(chunk) |
| 47 | + const gap = new Float32Array(gapLen) |
| 48 | + for (let i = 0; i < gapLen; i++) { |
| 49 | + const inBreath = Math.abs(i - gapLen / 2) < gapLen * 0.2 |
| 50 | + const amp = inBreath ? breathRms : noiseRms |
| 51 | + gap[i] = (Math.random() * 2 - 1) * amp * Math.SQRT1_2 |
| 52 | + } |
| 53 | + chunks.push(gap) |
| 54 | + } |
| 55 | + chunks.push(new Float32Array(gapLen)) |
| 56 | + const total = chunks.reduce((m, c) => m + c.length, 0) |
| 57 | + const out = new Float32Array(total) |
| 58 | + let off = 0 |
| 59 | + for (const c of chunks) { |
| 60 | + out.set(c, off) |
| 61 | + off += c.length |
| 62 | + } |
| 63 | + for (let i = 0; i < out.length; i++) out[i] += (Math.random() * 2 - 1) * noiseRms * Math.SQRT1_2 |
| 64 | + return out |
| 65 | +} |
| 66 | + |
| 67 | +function framesFrom(samples: Float32Array): PitchFrame[] { |
| 68 | + const frames: PitchFrame[] = [] |
| 69 | + for (let start = 0; start + WINDOW <= samples.length; start += HOP) { |
| 70 | + const buf = samples.slice(start, start + WINDOW) |
| 71 | + const t = start / SR |
| 72 | + const rms = computeRms(buf) |
| 73 | + const { freq, prob } = yin(buf, SR) |
| 74 | + frames.push({ t, freq, rms, prob }) |
| 75 | + } |
| 76 | + return frames |
| 77 | +} |
| 78 | + |
| 79 | +function runCase(name: string, samples: Float32Array, expectedMidi: number[], maxExtra: number) { |
| 80 | + const frames = framesFrom(samples) |
| 81 | + const { rawSegments } = analyzeRecording(frames) |
| 82 | + const got = rawSegments.map((s) => s.midi) |
| 83 | + const pass = |
| 84 | + got.length >= expectedMidi.length - maxExtra && |
| 85 | + got.length <= expectedMidi.length + maxExtra && |
| 86 | + expectedMidi.every((m) => got.includes(m)) |
| 87 | + console.log( |
| 88 | + `${pass ? '✅' : '❌'} ${name}: got [${got.join(', ')}] expected [${expectedMidi.join(', ')}] (${rawSegments.length} segments)` |
| 89 | + ) |
| 90 | + return pass |
| 91 | +} |
| 92 | + |
| 93 | +// ── 1. voice-like: 4 notes, harmonics, vibrato, breath bursts ────────────── |
| 94 | +const voice = generate( |
| 95 | + [ |
| 96 | + { f0: 220.0, dur: 0.4, harmonics: [1, 0.5, 0.35, 0.2, 0.12], vibratoDepth: 0.004 }, |
| 97 | + { f0: 261.63, dur: 0.4, harmonics: [1, 0.5, 0.35, 0.2, 0.12], vibratoDepth: 0.004 }, |
| 98 | + { f0: 329.63, dur: 0.4, harmonics: [1, 0.5, 0.35, 0.2, 0.12], vibratoDepth: 0.004 }, |
| 99 | + { f0: 392.0, dur: 0.4, harmonics: [1, 0.5, 0.35, 0.2, 0.12], vibratoDepth: 0.004 }, |
| 100 | + ], |
| 101 | + 0.14, |
| 102 | + 0.01, |
| 103 | + 0.05 |
| 104 | +) |
| 105 | + |
| 106 | +// ── 2. whistle-like: high sine + strong 2nd harmonic, deep vibrato ───────── |
| 107 | +const whistle = generate( |
| 108 | + [ |
| 109 | + { f0: 1046.5, dur: 0.45, harmonics: [1, 0.4, 0.12], vibratoDepth: 0.008, vibratoHz: 6 }, |
| 110 | + { f0: 1318.5, dur: 0.45, harmonics: [1, 0.4, 0.12], vibratoDepth: 0.008, vibratoHz: 6 }, |
| 111 | + { f0: 880.0, dur: 0.45, harmonics: [1, 0.4, 0.12], vibratoDepth: 0.008, vibratoHz: 6 }, |
| 112 | + ], |
| 113 | + 0.12, |
| 114 | + 0.008, |
| 115 | + 0.04 |
| 116 | +) |
| 117 | + |
| 118 | +// ── 3. one long legato note with heavy vibrato must not shatter ──────────── |
| 119 | +const legato = generate( |
| 120 | + [{ f0: 220.0, dur: 1.4, harmonics: [1, 0.5, 0.3], vibratoDepth: 0.012, vibratoHz: 5.5 }], |
| 121 | + 0.2, |
| 122 | + 0.01, |
| 123 | + 0.05 |
| 124 | +) |
| 125 | + |
| 126 | +// ── 4. noise only ─────────────────────────────────────────────────────────── |
| 127 | +const noiseOnly = generate([], 0.5, 0.05, 0.08) |
| 128 | + |
| 129 | +// ── 5. male voice low hum: A2 C3 E3 G3, rich glottal stack ──────────────── |
| 130 | +const maleHarmonics = [1, 0.6, 0.4, 0.25, 0.15, 0.1] |
| 131 | +const male = generate( |
| 132 | + [ |
| 133 | + { f0: 110.0, dur: 0.45, harmonics: maleHarmonics, vibratoDepth: 0.005 }, |
| 134 | + { f0: 130.81, dur: 0.45, harmonics: maleHarmonics, vibratoDepth: 0.005 }, |
| 135 | + { f0: 164.81, dur: 0.45, harmonics: maleHarmonics, vibratoDepth: 0.005 }, |
| 136 | + { f0: 196.0, dur: 0.45, harmonics: maleHarmonics, vibratoDepth: 0.005 }, |
| 137 | + ], |
| 138 | + 0.14, |
| 139 | + 0.01, |
| 140 | + 0.05 |
| 141 | +) |
| 142 | + |
| 143 | +// ── 6. creaky male tone: AM at f0/2 — the classic octave-down trap ───────── |
| 144 | +const creak = generate([{ f0: 110.0, dur: 0.8, harmonics: [1, 0.55, 0.35, 0.2, 0.12] }], 0.2, 0.01, 0.04) |
| 145 | +for (let i = 0; i < creak.length; i++) { |
| 146 | + creak[i] *= 1 + 0.35 * Math.cos((2 * Math.PI * 55 * i) / SR) |
| 147 | +} |
| 148 | + |
| 149 | +// ── 7. sub-harmonic garbage: a genuine 55 Hz tone folds up to A2 ─────────── |
| 150 | +const subGarbage = generate([{ f0: 55.0, dur: 0.6, harmonics: [1, 0.5] }], 0.2, 0.01, 0.04) |
| 151 | + |
| 152 | +// ── 8. octave-up trap: weak fundamental, strong 2nd harmonic (D4-ish) ────── |
| 153 | +const weakF0 = generate([{ f0: 233.08, dur: 0.7, harmonics: [0.12, 1, 0.3] }], 0.2, 0.01, 0.04) |
| 154 | + |
| 155 | +// ── 9. glide (portamento) must not produce phantom notes ─────────────────── |
| 156 | +const glide = generate([{ f0: 220.0, dur: 0.9, harmonics: [1, 0.5, 0.3], glideTo: 440 }], 0.2, 0.01, 0.04) |
| 157 | + |
| 158 | +let ok = true |
| 159 | +ok = runCase('voice (A3 C4 E4 G4)', voice, [57, 60, 64, 67], 1) && ok |
| 160 | +ok = runCase('whistle (C6 E6 A5)', whistle, [84, 88, 81], 1) && ok |
| 161 | +ok = runCase('legato vibrato (1 note)', legato, [57], 0) && ok |
| 162 | +ok = runCase('noise only', noiseOnly, [], 0) && ok |
| 163 | +ok = runCase('male low hum (A2 C3 E3 G3)', male, [45, 48, 52, 55], 1) && ok |
| 164 | +ok = runCase('creaky A2 (AM at f0/2)', creak, [45], 0) && ok |
| 165 | +ok = runCase('sub-harmonic fold (55 Hz → A2)', subGarbage, [45], 0) && ok |
| 166 | +ok = runCase('weak fundamental D3 (no octave-up)', weakF0, [58], 0) && ok |
| 167 | +ok = runCase('glide A3→A4 (no phantom notes)', glide, [], 0) && ok |
| 168 | + |
| 169 | +// ── direct YIN checks: pure low tones must not be read an octave down ───── |
| 170 | +function yinFreqOf(f0: number, harmonics: number[]): number { |
| 171 | + const buf = new Float32Array(WINDOW) |
| 172 | + for (let i = 0; i < WINDOW; i++) { |
| 173 | + let s = 0 |
| 174 | + for (let h = 0; h < harmonics.length; h++) { |
| 175 | + s += harmonics[h] * Math.sin((2 * Math.PI * f0 * (h + 1) * i) / SR) |
| 176 | + } |
| 177 | + buf[i] = s * 0.25 |
| 178 | + } |
| 179 | + return yin(buf, SR).freq |
| 180 | +} |
| 181 | +for (const [name, f0, harmonics] of [ |
| 182 | + ['A2 110 Hz clean', 110.0, [1, 0.5, 0.35, 0.2]], |
| 183 | + ['E2 82.4 Hz clean', 82.41, [1, 0.5, 0.3]], |
| 184 | + ['G3 196 Hz clean', 196.0, [1, 0.5, 0.35, 0.2, 0.12]], |
| 185 | +] as const) { |
| 186 | + const got = yinFreqOf(f0, [...harmonics]) |
| 187 | + const cents = Math.abs((1200 * Math.log2(got / f0)) | 0) |
| 188 | + const pass = cents < 30 |
| 189 | + console.log( |
| 190 | + `${pass ? '✅' : '❌'} yin ${name}: ${got.toFixed(1)} Hz (${cents} cents off) — must not be f0/2` |
| 191 | + ) |
| 192 | + ok = ok && pass |
| 193 | +} |
| 194 | + |
| 195 | +const frames = framesFrom(voice) |
| 196 | +const t0 = performance.now() |
| 197 | +for (let i = 0; i < 10; i++) analyzeRecording(frames) |
| 198 | +console.log(`perf: analyzeRecording(6s take) ×10 → ${((performance.now() - t0) / 10).toFixed(1)} ms`) |
| 199 | +process.exit(ok ? 0 : 1) |
0 commit comments