Phase 1: offline audio pipeline

Whole-track analysis into a frame-indexed FeatureTrack. Nothing reads a
live AnalyserNode: realtime preview maps currentTime to a frame index,
export counts frames, both read the same rows.

- fft.js: radix-2 with precomputed tables, allocated once per track
- analyze.js: STFT at hop 1/60s with CENTRED windows (a window that starts
  at the timestamp reports energy arriving up to 23ms later, which reads as
  visuals lagging the music). Energy features normalised against the track's
  own percentiles; absolute stats kept in summary for the look generator.
- tempo.js: autocorrelation + grid F-measure, beat grid, downbeats
- segment.js: self-similarity novelty, boundaries snapped to the bar grid
- FeatureTrack: assembles everything, plus the lookahead fields. buildSlope
  rises through the bars leading into a higher-energy section, so a build can
  ramp into the drop rather than react after it lands.
- clicktrack.js: mixes clicks onto the detected grid for validation by ear

Three real bugs found and fixed by the tests:
- 174 BPM read as 87. Mean-energy-per-beat scores a half-tempo grid
  identically to the true one; only an F-measure penalises the missed
  onsets via recall.
- 90 BPM read as 180. Offbeat hi-hats make a double-tempo grid score
  perfectly on both precision and recall, so the grid is now interpreted
  metrically afterwards: a systematic strong/weak alternation means the
  real beat is every other grid point.
- Beat grid drifted ~30ms over 30s from integer-frame offsets. Onset peaks
  are now parabolically interpolated and the grid least-squares fitted.

Gates: 11/11 node tests against synthetic ground truth (tempo within 2%
across 90-174 BPM, beat alignment under half a frame, segmentation within
2s of a known boundary, graceful on silence, 6-minute analysis in 1.0s);
5/5 browser checks including audio-driven vs fixed-step frame parity.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Dejvino
2026-08-05 11:10:01 +02:00
co-authored by Claude Opus 5
parent 7e31c19d6e
commit cb320557e6
10 changed files with 1731 additions and 1 deletions
+47
View File
@@ -0,0 +1,47 @@
// File -> AudioBuffer. Decoding is done once, up front, and the PCM is kept:
// the analysis pass, the seed hash, the click track and the export mux all read
// the same decoded samples.
let sharedContext = null;
function context() {
if (!sharedContext) {
sharedContext = new (window.AudioContext || window.webkitAudioContext)();
}
return sharedContext;
}
export async function decodeFile(file) {
const arrayBuffer = await file.arrayBuffer();
return decodeArrayBuffer(arrayBuffer);
}
export async function decodeArrayBuffer(arrayBuffer) {
const ctx = context();
// decodeAudioData detaches the buffer, so hand it a copy if the caller may
// still need the original bytes.
return await ctx.decodeAudioData(arrayBuffer.slice(0));
}
/** Interleaved mono mixdown. Used for the content hash that seeds the look. */
export function monoSamples(audioBuffer) {
const channels = audioBuffer.numberOfChannels;
const left = audioBuffer.getChannelData(0);
if (channels === 1) return left;
const right = audioBuffer.getChannelData(1);
const out = new Float32Array(left.length);
for (let i = 0; i < left.length; i++) out[i] = (left[i] + right[i]) * 0.5;
return out;
}
export function audioContext() {
return context();
}
/** Human-readable duration, used by the transport display. */
export function formatTime(seconds) {
if (!isFinite(seconds) || seconds < 0) return '0:00';
const m = Math.floor(seconds / 60);
const s = Math.floor(seconds % 60);
return `${m}:${String(s).padStart(2, '0')}`;
}