Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: a sustained local loudness change marks a take's window changed (#9266)

Adam Eivy0ab292403fa2
diff --git a/server/lib/audioFingerprint.js b/server/lib/audioFingerprint.js
index 122d537949..7986d9c550 100644
--- a/server/lib/audioFingerprint.js
+++ b/server/lib/audioFingerprint.js
@@ -36,6 +36,11 @@ const COMPARE_FLOOR_DB = -60;
const MIN_SHAPE_STD_DB = 1;
export const FINGERPRINT_MIN_CORRELATION = 0.98;
export const FINGERPRINT_MAX_MEAN_DELTA_DB = 1.5;
+// A window-wide mean dilutes a short local change (a silenced second of a
+// 30 s steady window moves the mean ~1.3 dB), so a quarter second of frames
+// that each moved by 10 dB or more is a change on its own.
+const LOCAL_CHANGE_DB = 10;
+const LOCAL_CHANGE_FRAMES = 25;
/**
* Decode `path` to 8 kHz mono and return its whole 100 Hz RMS envelope as an
@@ -111,7 +116,8 @@ const decodeFingerprint = (fingerprint) => {
* Does `envelope` (a whole song) still carry the audio a stored fingerprint
* recorded? Returns `{ same, correlation, meanDeltaDb }`; `same` is false when
* the fingerprint is unreadable or the song no longer reaches the window.
- * Pearson correlation below 0.98 or a mean |ΔdB| above 1.5 dB is a change.
+ * Pearson correlation below 0.98, a mean |ΔdB| above 1.5 dB, or a quarter
+ * second moved by 10 dB or more is a change.
*/
export function compareWindowFingerprint(fingerprint, envelope) {
const stored = decodeFingerprint(fingerprint);
@@ -122,12 +128,17 @@ export function compareWindowFingerprint(fingerprint, envelope) {
let sumA = 0;
let sumB = 0;
let sumDelta = 0;
+ let run = 0;
+ let localChange = false;
for (let i = 0; i < n; i++) {
a[i] = Math.max(COMPARE_FLOOR_DB, stored[i]);
b[i] = Math.max(COMPARE_FLOOR_DB, envelope[fingerprint.startFrame + i]);
sumA += a[i];
sumB += b[i];
- sumDelta += Math.abs(a[i] - b[i]);
+ const delta = Math.abs(a[i] - b[i]);
+ sumDelta += delta;
+ run = delta >= LOCAL_CHANGE_DB ? run + 1 : 0;
+ if (run >= LOCAL_CHANGE_FRAMES) localChange = true;
}
const meanA = sumA / n;
const meanB = sumB / n;
@@ -142,6 +153,6 @@ export function compareWindowFingerprint(fingerprint, envelope) {
const shaped = Math.sqrt(varA / n) >= MIN_SHAPE_STD_DB && Math.sqrt(varB / n) >= MIN_SHAPE_STD_DB;
const correlation = shaped ? cov / Math.sqrt(varA * varB) : null;
const meanDeltaDb = sumDelta / n;
- const same = meanDeltaDb <= FINGERPRINT_MAX_MEAN_DELTA_DB && (correlation == null || correlation >= FINGERPRINT_MIN_CORRELATION);
+ const same = !localChange && meanDeltaDb <= FINGERPRINT_MAX_MEAN_DELTA_DB && (correlation == null || correlation >= FINGERPRINT_MIN_CORRELATION);
return { same, correlation, meanDeltaDb };
}
diff --git a/server/lib/audioFingerprint.test.js b/server/lib/audioFingerprint.test.js
index 783dcc1a46..6bf9d68120 100644
--- a/server/lib/audioFingerprint.test.js
+++ b/server/lib/audioFingerprint.test.js
@@ -19,7 +19,7 @@ afterAll(() => rm(dir, { recursive: true, force: true }));
// A "sung" line: a 220 Hz tone whose loudness steps every 250 ms through a
// fixed pattern, so the window has a real envelope shape to correlate.
// `silence` blanks [startSec, endSec) — the changed bar of a new mix.
-function songWav({ silence = null } = {}) {
+function songWav({ silence = null, steady = false } = {}) {
const samples = SONG_SEC * RATE;
const buf = Buffer.alloc(44 + samples * 2);
buf.write('RIFF', 0); buf.writeUInt32LE(36 + samples * 2, 4); buf.write('WAVE', 8);
@@ -30,7 +30,7 @@ function songWav({ silence = null } = {}) {
for (let i = 0; i < samples; i++) {
const t = i / RATE;
const muted = silence && t >= silence.startSec && t < silence.endSec;
- const amp = muted ? 0 : levels[Math.floor(t * 4) % levels.length];
+ const amp = muted ? 0 : steady ? 0.3 : levels[Math.floor(t * 4) % levels.length];
buf.writeInt16LE(Math.round(amp * 30000 * Math.sin(2 * Math.PI * 220 * t)), 44 + i * 2);
}
return buf;
@@ -64,6 +64,21 @@ describe.skipIf(!ffmpeg)('audio window fingerprints', () => {
expect(compareWindowFingerprint(fp, await computeRmsEnvelope(inside)).same).toBe(false);
}, 30_000);
+ it('catches a short local change in a window with no loudness shape to correlate', async () => {
+ const master = join(dir, 'steady.wav');
+ await writeFile(master, songWav({ steady: true }));
+ const fp = windowFingerprint(await computeRmsEnvelope(master), { startSec: 0, endSec: SONG_SEC });
+ const reencoded = join(dir, 'steady.m4a');
+ execFileSync(ffmpeg, ['-v', 'error', '-i', master, '-c:a', 'aac', '-b:a', '128k', '-y', reencoded]);
+ expect(compareWindowFingerprint(fp, await computeRmsEnvelope(reencoded)).same).toBe(true);
+ // A quarter second blanked moves the window mean under 1.5 dB, but not unnoticed.
+ const dropped = join(dir, 'steady-dropped.wav');
+ await writeFile(dropped, songWav({ steady: true, silence: { startSec: 4, endSec: 4.27 } }));
+ const verdict = compareWindowFingerprint(fp, await computeRmsEnvelope(dropped));
+ expect(verdict.meanDeltaDb).toBeLessThan(1.5);
+ expect(verdict.same).toBe(false);
+ }, 30_000);
+
it('treats a song that no longer reaches the window, or an unreadable fingerprint, as changed', async () => {
const master = join(dir, 'short-master.wav');
await writeFile(master, songWav());