Skip to content

Commit 99048fe

Browse files
committed
fix(cli): retry incomplete stem separations
1 parent 08f3491 commit 99048fe

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎packages/cli/src/audio/read/read.test.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { join } from "node:path";
66
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
77
import { findFfBinary } from "@hyperframes/parsers/ff-binaries";
88
import { readAudio } from "./read.js";
9-
import { fetchStems } from "./stems.js";
9+
import { cachedStems, fetchStems } from "./stems.js";
1010

1111
const RATE = 22_050;
1212
const dir = mkdtempSync(join(tmpdir(), "hf-audio-read-"));
@@ -168,6 +168,8 @@ describe.skipIf(!ffmpeg)("readAudio", () => {
168168
stemFiles.drums = wav("drums.wav", 8, boom);
169169
stemFiles.piano = wav("piano.wav", 8, bed);
170170
stemFiles.bass = wav("bass.wav", 8, () => 0);
171+
stemFiles.guitar = stemFiles.bass;
172+
stemFiles.other = stemFiles.bass;
171173
stemFiles["../../escape"] = stemFiles.bass;
172174
const path = wav("episode.wav", 8, (t) => voiced(t) + boom(t) + bed(t));
173175
vi.stubEnv("HEYGEN_API_KEY", "test-key");
@@ -180,7 +182,7 @@ describe.skipIf(!ffmpeg)("readAudio", () => {
180182
expect(polls).toBe(1);
181183
expect(analysis).toMatchObject({
182184
separation: "stems",
183-
stems: ["bass", "drums", "piano", "vocals"],
185+
stems: ["bass", "drums", "guitar", "other", "piano", "vocals"],
184186
warnings: [expect.stringContaining("no transcript")],
185187
});
186188
expect(analysis.voice!.source).toBe("activity");
@@ -196,6 +198,32 @@ describe.skipIf(!ffmpeg)("readAudio", () => {
196198
expect(stemCalls).toBe(1);
197199
});
198200

201+
it("falls back on partial stems and retries instead of caching unmeasured layers as silence", async () => {
202+
for (const name of Object.keys(stemFiles)) delete stemFiles[name];
203+
stemFiles.vocals = wav("partial-vocals.wav", 2, voiced);
204+
const path = wav("partial.wav", 2, voiced);
205+
vi.stubEnv("HEYGEN_API_KEY", "test-key");
206+
const cacheDir = join(dir, "cache-partial");
207+
const partial = await readAudio(path, "partial.wav", { cacheDir, quality: "best" });
208+
expect(partial.analysis).toMatchObject({ separation: "mix", effects: null, music: null });
209+
expect(partial.analysis.warnings.join("\n")).toContain("incomplete separation: missing drums");
210+
const callsBefore = stemCalls;
211+
const silence = wav("partial-silence.wav", 2, () => 0);
212+
for (const name of ["drums", "bass", "guitar", "piano", "other"]) stemFiles[name] = silence;
213+
const complete = await readAudio(path, "partial.wav", { cacheDir, quality: "best" });
214+
expect(complete.cached).toBe(false);
215+
expect(complete.analysis.separation).toBe("stems");
216+
expect(stemCalls).toBe(callsBefore + 1);
217+
218+
const stemDir = join(cacheDir, "stems", complete.analysis.sha256.slice(0, 12));
219+
writeFileSync(join(stemDir, "stems.json"), JSON.stringify({ vocals: "vocals.audio" }));
220+
expect(cachedStems(stemDir)).toBeNull();
221+
const repaired = await readAudio(path, "partial.wav", { cacheDir, quality: "best" });
222+
expect(repaired.cached).toBe(false);
223+
expect(stemCalls).toBe(callsBefore + 2);
224+
expect(Object.keys(cachedStems(stemDir)!)).toHaveLength(6);
225+
});
226+
199227
it.each(["/put", "/v3/audio/stems"])(
200228
"aborts a stalled %s request at the separation deadline",
201229
async (endpoint) => {

‎packages/cli/src/audio/read/read.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export async function readAudio(
194194
quality === "best" &&
195195
(cachedStems(stemDir) !== null || typeof (await stemsAccess()) !== "string");
196196
if (
197+
(entry.analysis.separation !== "stems" || cachedStems(stemDir) !== null) &&
197198
entry.analysis.version === READ_VERSION &&
198199
entry.transcriptKey === transcriptKey &&
199200
!stemsNowPossible

‎packages/cli/src/audio/read/stems.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export function cachedStems(dir: string): Record<string, string> | null {
5858
const done = join(dir, DONE);
5959
if (!existsSync(done)) return null;
6060
const names = JSON.parse(readFileSync(done, "utf8")) as Record<string, string>;
61-
const paths = Object.fromEntries(Object.entries(names).map(([n, f]) => [n, join(dir, f)]));
61+
if (!STEM_NAMES.every((name) => typeof names[name] === "string")) return null;
62+
const paths = Object.fromEntries(STEM_NAMES.map((name) => [name, join(dir, names[name]!)]));
6263
return Object.values(paths).every((p) => existsSync(p)) ? paths : null;
6364
}
6465

@@ -134,8 +135,11 @@ export async function fetchStems(
134135
`stem separation failed${current.error?.message ? `: ${current.error.message}` : ""}`,
135136
);
136137
const stems = current.stems ?? [];
137-
if (!stems.some((s) => s.name === VOICE_STEM))
138-
throw new Error("the stems service returned no vocals stem");
138+
const missing = STEM_NAMES.filter((name) => !stems.some((stem) => stem.name === name));
139+
if (missing.length)
140+
throw new Error(
141+
`the stems service returned an incomplete separation: missing ${missing.join(", ")}`,
142+
);
139143
mkdirSync(dir, { recursive: true });
140144
const names: Record<string, string> = {};
141145
for (const name of STEM_NAMES) {

0 commit comments

Comments
 (0)