Files
karaoke-gen/frontend/lib/audio-data.ts
T
Andrew Beveridge ☄️andClaude Fable 5 1755fa138c fix(review): correct vocal waveform time drift on 44.1kHz audio devices (#938)
## Summary

Fixes the vocal waveform in the lyrics-review timeline editor appearing
shifted ~0.4–0.5s late relative to the audio in some situations
(reported on job 0eebb248, segment 25 at ~171s).

**Root cause:** the client-side peak envelope in
`frontend/lib/audio-data.ts` floored samples-per-bucket to an integer.
At a 44.1kHz `AudioContext` (device-dependent — common on Macs and audio
interfaces) the exact stride is 110.25 samples but was floored to 110,
making the envelope's clock run ~0.23% fast. The waveform drifted late
by ~2.3ms per second of song — ~0.4s by the 3-minute mark. 48kHz devices
divide evenly (120) and were unaffected, which is why the bug only
appeared "in some situations".

## Changes

- Bucket boundaries are now computed from the fractional stride
(`Math.floor(bucket * samplesPerBucket)`), keeping every bucket within
one bucket (2.5ms, non-accumulating, sub-pixel) of its nominal 1/400s
position at any sample rate.
- Peak computation extracted into an exported pure `computePeaks()` so
it's unit-testable without a browser `AudioContext`.
- 7 new unit tests in `frontend/lib/__tests__/audio-data.test.ts`,
including a 44.1kHz drift regression test that fails against the old
floored-stride code (impulse landed 27 buckets / 67ms late at 29.5s).
- Version bump 0.198.4.

## Testing

- `make test`: all backend tests + 92 frontend suites (1158 tests) pass.
- New regression tests verified to fail against the pre-fix
implementation.
- Local 8-angle code review + CodeRabbit CLI review: no findings.

@coderabbitai ignore

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-24 16:50:33 -04:00

99 lines
3.8 KiB
TypeScript

export interface AudioData {
duration: number
// Downsampled absolute-amplitude envelope: one peak per bucket. Storing the
// envelope instead of the raw PCM keeps a whole vocal track in ~hundreds of KB
// (peaksPerSecond * duration * 4 bytes) rather than the tens of MB a full
// Float32Array of decoded samples would occupy, which noticeably slows the app.
peaks: Float32Array
peaksPerSecond: number
}
// Resolution of the precomputed peak envelope. High enough to stay crisp when a
// short segment is zoomed across the full timeline width, low enough to keep the
// whole track tiny in memory. 400/s over a 4-minute song ≈ 96k floats (~384 KB).
const PEAKS_PER_SECOND = 400
// The vocals endpoint returns 202 while audio separation is still running (the
// user can open lyrics review before the vocal stem has been uploaded). Callers
// should catch this and retry after a delay rather than giving up.
export class AudioNotReadyError extends Error {
constructor() {
super('Vocals audio not ready yet (separation in progress)')
this.name = 'AudioNotReadyError'
}
}
// Minimal structural subset of AudioBuffer, so the peak math is testable
// without constructing a real (browser-only) AudioContext.
export interface DecodedAudioLike {
duration: number
length: number
numberOfChannels: number
getChannelData(channel: number): Float32Array
}
export function computePeaks(decoded: DecodedAudioLike): AudioData {
const { duration, numberOfChannels, length } = decoded
const bucketCount = Math.max(1, Math.ceil(duration * PEAKS_PER_SECOND))
// Fractional on purpose: at a 44.1 kHz AudioContext this is 110.25. Flooring
// it to an integer stride made the envelope's clock run ~0.23% fast, so the
// waveform drifted visibly late as the song progressed (~0.4s by the 3-minute
// mark). With floored per-bucket boundaries the remaining error is bounded by
// the ceil() rounding of bucketCount: under one bucket (2.5 ms) anywhere in
// the track, non-accumulating and sub-pixel at any timeline zoom.
const samplesPerBucket = length / bucketCount
const peaks = new Float32Array(bucketCount)
// Collapse all channels into a single envelope by taking the maximum
// absolute sample value in each bucket. Iterate per bucket (not per sample)
// so we avoid a division + Math.floor on every one of the ~millions of
// samples, which would otherwise freeze the main thread while loading.
// The raw decoded buffer is released when this function returns; only the
// compact `peaks` array is retained.
for (let channelIdx = 0; channelIdx < numberOfChannels; channelIdx++) {
const channelData = decoded.getChannelData(channelIdx)
for (let bucket = 0; bucket < bucketCount; bucket++) {
const start = Math.floor(bucket * samplesPerBucket)
if (start >= length) break
const end = bucket === bucketCount - 1 ? length : Math.min(length, Math.floor((bucket + 1) * samplesPerBucket))
let peak = peaks[bucket]
for (let i = start; i < end; i++) {
const amplitude = Math.abs(channelData[i])
if (amplitude > peak) {
peak = amplitude
}
}
peaks[bucket] = peak
}
}
return {
duration,
peaks,
peaksPerSecond: PEAKS_PER_SECOND
}
}
export async function fetchAudioData(url: string): Promise<AudioData> {
// fetch() resolves even for 4xx/5xx; the vocals endpoint 404s when a job has
// no vocal stem. Fail explicitly so the caller sees "no vocals" rather than an
// opaque decodeAudioData error on the JSON/HTML error body.
const response = await fetch(url)
if (response.status === 202) {
throw new AudioNotReadyError()
}
if (!response.ok) {
throw new Error(`Failed to fetch vocals audio: ${response.status}`)
}
const arrayBuffer = await response.arrayBuffer()
const context = new AudioContext()
try {
const decoded = await context.decodeAudioData(arrayBuffer)
return computePeaks(decoded)
} finally {
context.close()
}
}