fix(core): stop \b from missing underscore-separated names, guard clipsOverlap's negative duration

\b treats `_` as a word character, so \bbed\b never matched bed_01,
music_bed_loop, or theme_song, and \bvo\b/\bvox\b/\btts\b had the same gap —
an underscore-separated bed classified as "unknown" and could end up offered
as its own carve source. Replaced the short hints with a boundary that
actually excludes letters and digits on both sides.

clipsOverlap computed end = start + duration without guarding sign, so a
negative duration put end before start — an interval that does not describe
anything, and one specific case showed it silently dropping a real overlap
(a shorter, earlier broken end rejected a clip that genuinely contained the
point). Duration clamps to zero instead: a clip cannot un-play time, and a
zero-length clip at its start is the sane reading of "duration nobody wrote
down as positive."

Review by Miga (PR #3212).
This commit is contained in:
Vance Ingalls
2026-08-13 00:42:18 -07:00
parent cc631a28d1
commit 009fb88239
2 changed files with 77 additions and 4 deletions
+54
View File
@@ -7,6 +7,7 @@ import {
carveBandsToChain,
carveProfile,
classifyAudioName,
clipsOverlap,
mixCarveSources,
couldBeCarveSource,
DEFAULT_CARVE,
@@ -494,6 +495,59 @@ describe("classifyAudioName", () => {
expect(couldBeCarveSource("music-bed")).toBe(false);
expect(couldBeCarveSource("sfx-explosion")).toBe(false);
});
it("treats underscores as separators, not word characters, for short hints", () => {
// `\b` treats `_` as a word character, so `\bbed\b` used to miss `bed_01` —
// an underscore-separated bed classified as "unknown" and could end up
// offered as its own carve source.
expect(classifyAudioName("bed_01")).toBe("music");
expect(classifyAudioName("my_bed")).toBe("music");
expect(classifyAudioName("music_bed_loop")).toBe("music");
expect(classifyAudioName("theme_song")).toBe("music");
expect(classifyAudioName("vo_take3")).toBe("voice");
expect(classifyAudioName("main_vox")).toBe("voice");
expect(couldBeCarveSource("bed_01")).toBe(false);
});
});
describe("clipsOverlap", () => {
it("overlaps when the spans genuinely share time", () => {
expect(clipsOverlap({ start: 0, duration: 5 }, { start: 3, duration: 5 })).toBe(true);
});
it("does not overlap when one span ends before the other starts", () => {
expect(clipsOverlap({ start: 0, duration: 5 }, { start: 5, duration: 5 })).toBe(false);
expect(clipsOverlap({ start: 10, duration: 5 }, { start: 0, duration: 5 })).toBe(false);
});
it("does not overlap two clips that only touch at an edge", () => {
// Half-open: an end exactly at the other's start shares no time to carve.
expect(clipsOverlap({ start: 0, duration: 5 }, { start: 5, duration: 5 })).toBe(false);
});
it("treats a null or undefined duration as unbounded", () => {
expect(clipsOverlap({ start: 0, duration: null }, { start: 100, duration: 1 })).toBe(true);
expect(clipsOverlap({ start: 0 }, { start: 100, duration: 1 })).toBe(true);
// Symmetric: the unbounded span can be on either side.
expect(clipsOverlap({ start: 100, duration: 1 }, { start: 0, duration: undefined })).toBe(true);
});
it("gives a zero-duration clip a single instant, not a span", () => {
expect(clipsOverlap({ start: 5, duration: 0 }, { start: 5, duration: 5 })).toBe(false);
expect(clipsOverlap({ start: 5, duration: 0 }, { start: 4, duration: 5 })).toBe(true);
});
it("clamps a negative duration to zero rather than inverting the interval", () => {
// The regression: end = start + duration puts a negative-duration clip's
// end BEFORE its start, and end(a) is what the other clip's start gets
// compared against — so a smaller (earlier) broken end silently rejects
// real overlaps too. {start:10, duration:-5} clamped is a zero-length
// clip AT t=10, which genuinely sits inside {start:6, duration:20}'s
// [6, 26) span; the unclamped math missed it (end(a) came out to 5).
expect(clipsOverlap({ start: 10, duration: -5 }, { start: 6, duration: 20 })).toBe(true);
// And it stays correct where it isn't inside anything.
expect(clipsOverlap({ start: 10, duration: -5 }, { start: 20, duration: 5 })).toBe(false);
});
});
describe("mixCarveSources", () => {
+23 -4
View File
@@ -105,11 +105,26 @@ export type HfAudioNameKind = "voice" | "music" | "sfx" | "unknown";
/** Short, deliberately dull effects. Nothing here is ever a voiceover. */
const SFX_NAME =
/sfx|foley|whoosh|impact|riser|stinger|swoosh|thud|boom|click|ding|beep|ambien|room[-_ ]?tone/i;
// `\b` treats `_` as a word character, so `\bbed\b` does not match `bed_01`
// or `music_bed_loop` — exactly the separator an asset name is likely to use.
// These short words need a boundary that actually excludes letters and
// digits on both sides; everything else here is long enough that a
// substring match is already the intent (`music` inside `bgmusic` is fine).
const NOT_WORD = "(?<![a-z0-9])";
const NOT_WORD_END = "(?![a-z0-9])";
const wordish = (term: string): string => `${NOT_WORD}${term}${NOT_WORD_END}`;
/** A bed, which is the thing being carved rather than the thing carving it. */
const MUSIC_NAME = /music|bgm|\bbed\b|soundtrack|score|\bsong\b|theme|instrumental|track\d/i;
const MUSIC_NAME = new RegExp(
`music|bgm|${wordish("bed")}|soundtrack|score|${wordish("song")}|theme|instrumental|track\\d`,
"i",
);
/** Speech. */
const VOICE_NAME =
/voice|\bvo\b|\bvox\b|narrat|speech|dialog|monolog|announce|\btts\b|talk|interview|podcast|recap|script/i;
const VOICE_NAME = new RegExp(
`voice|${wordish("vo")}|${wordish("vox")}|narrat|speech|dialog|monolog|announce|` +
`${wordish("tts")}|talk|interview|podcast|recap|script`,
"i",
);
/**
* Classify a track from its id and filename together.
@@ -152,7 +167,11 @@ export interface HfClipSpan {
export function clipsOverlap(a: HfClipSpan, b: HfClipSpan): boolean {
const end = (clip: HfClipSpan): number =>
typeof clip.duration === "number" && Number.isFinite(clip.duration)
? clip.start + clip.duration
? // Negative is clamped to zero-length rather than passed through: a
// clip cannot un-play time, and letting it through inverts the
// interval (end before start), which reads as overlapping everything
// it is nowhere near.
clip.start + Math.max(0, clip.duration)
: Number.POSITIVE_INFINITY;
return a.start < end(b) && b.start < end(a);
}