@@ -6,7 +6,7 @@ import { join } from "node:path";
66import { afterAll , afterEach , beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
77import { findFfBinary } from "@hyperframes/parsers/ff-binaries" ;
88import { readAudio } from "./read.js" ;
9- import { fetchStems } from "./stems.js" ;
9+ import { cachedStems , fetchStems } from "./stems.js" ;
1010
1111const RATE = 22_050 ;
1212const 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 ) => {
0 commit comments