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

address review (claude): novelty floor for section boundaries; honor pre-aborted decode signal

[._.]/ Adam Eivy325ced0b8053
diff --git a/server/services/musicVideo/audioAnalysis.js b/server/services/musicVideo/audioAnalysis.js
index a79cddfbf3..139fc5f3f7 100644
--- a/server/services/musicVideo/audioAnalysis.js
+++ b/server/services/musicVideo/audioAnalysis.js
@@ -72,6 +72,10 @@ const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
*/
export async function decodeAudioToPcm(audioPath, { signal } = {}) {
if (typeof audioPath !== 'string' || !audioPath) return null;
+ // A listener added to an already-aborted signal never fires, so without this
+ // guard a pre-cancelled request (plausible off the request lifecycle, under a
+ // render queue) would still spawn ffmpeg and decode the whole track.
+ if (signal?.aborted) return null;
const ffmpeg = await findFfmpeg();
if (!ffmpeg) return null;
@@ -272,11 +276,23 @@ function segmentSections(energy, fps, durationSec) {
const novelty = new Float32Array(winCount);
for (let w = 1; w < winCount; w++) novelty[w] = Math.abs(profile[w] - profile[w - 1]);
+ // A boundary must reflect a real energy change, not just be the highest of an
+ // essentially-flat profile. Without this floor, silence / sustained drones /
+ // an even click track (all near-zero novelty) still get carved into the
+ // maximum evenly-spaced sections purely as a spacing artifact. Require the
+ // jump to clear a small fraction of the track's mean window energy; flat
+ // input then has no qualifying boundary and falls through to one section.
+ let meanProfile = 0;
+ for (let w = 0; w < winCount; w++) meanProfile += profile[w];
+ meanProfile /= winCount;
+ const noveltyFloor = Math.max(1e-9, meanProfile * 0.05);
+
const minWin = Math.max(1, Math.round(MIN_SECTION_SEC / SECTION_WINDOW_SEC));
- // Rank candidate boundaries by novelty, greedily accept those that respect
- // the minimum-section spacing, up to MAX_SECTIONS - 1 internal boundaries.
+ // Rank candidate boundaries by novelty, greedily accept those that clear the
+ // floor and respect the minimum-section spacing, up to MAX_SECTIONS - 1
+ // internal boundaries.
const ranked = Array.from({ length: winCount }, (_, w) => w)
- .filter((w) => w >= minWin && w <= winCount - minWin)
+ .filter((w) => w >= minWin && w <= winCount - minWin && novelty[w] >= noveltyFloor)
.sort((a, b) => novelty[b] - novelty[a]);
const boundaries = [];
for (const w of ranked) {
diff --git a/server/services/musicVideo/audioAnalysis.test.js b/server/services/musicVideo/audioAnalysis.test.js
index 7d200e11bb..f93c8b07d9 100644
--- a/server/services/musicVideo/audioAnalysis.test.js
+++ b/server/services/musicVideo/audioAnalysis.test.js
@@ -112,6 +112,17 @@ describe('analyzePcm', () => {
}
});
+ it('does not shatter a long flat/structureless track into spurious sections', () => {
+ // A 40s sustained tone at constant amplitude has no energy novelty, so it
+ // must yield a single section — not the maximum evenly-spaced boundaries.
+ const n = ANALYSIS_SAMPLE_RATE * 40;
+ const samples = new Float32Array(n);
+ for (let i = 0; i < n; i++) samples[i] = 0.3 * Math.sin((2 * Math.PI * 220 * i) / ANALYSIS_SAMPLE_RATE);
+ const { sections } = analyzePcm(samples, ANALYSIS_SAMPLE_RATE);
+ expect(sections.length).toBe(1);
+ expect(sections[0].startSec).toBe(0);
+ });
+
it('reports no tempo for silence but still spans sections', () => {
const samples = new Float32Array(ANALYSIS_SAMPLE_RATE * 12); // 12s of zeros
const result = analyzePcm(samples, ANALYSIS_SAMPLE_RATE);