1- import { existsSync , readFileSync } from "node:fs" ;
1+ import { existsSync } from "node:fs" ;
22import { resolve } from "node:path" ;
33import { test , expect } from "@playwright/test" ;
44
55const MODELS_DIR = resolve ( import . meta. dirname , "../../../data/onnx-split" ) ;
66const MODEL = resolve ( MODELS_DIR , "htdemucs.onnx" ) ;
77const FIXTURE = resolve ( import . meta. dirname , "../../../fixtures/sine-2s.wav" ) ;
8- const NATIVE_OUTPUT = resolve (
9- import . meta. dirname ,
10- "../../../data/output-split" ,
11- ) ;
12-
13- function readFloatWav ( buffer : Buffer ) {
14- const dataOffset = buffer . indexOf ( "data" ) + 8 ;
15- expect ( dataOffset ) . toBeGreaterThan ( 7 ) ;
16- const samples = new Float32Array ( ( buffer . length - dataOffset ) / 4 ) ;
17- for ( let index = 0 ; index < samples . length ; index ++ ) {
18- samples [ index ] = buffer . readFloatLE ( dataOffset + index * 4 ) ;
19- }
20- return samples ;
21- }
228
239test ( "separates a clip fully client-side" , async ( { page } ) => {
2410 expect ( existsSync ( MODEL ) , `model missing at ${ MODEL } ` ) . toBe ( true ) ;
2511 expect ( existsSync ( FIXTURE ) , `fixture missing at ${ FIXTURE } ` ) . toBe ( true ) ;
26- for ( const name of [ "drums" , "bass" , "other" , "vocals" ] ) {
27- expect (
28- existsSync ( resolve ( NATIVE_OUTPUT , `${ name } .wav` ) ) ,
29- `native reference missing for ${ name } ` ,
30- ) . toBe ( true ) ;
31- }
3212
3313 await page . goto ( "/" ) ;
3414 await page . setInputFiles ( "#modelFiles" , MODEL ) ;
@@ -54,24 +34,6 @@ test("separates a clip fully client-side", async ({ page }) => {
5434 await expect ( stems ) . toHaveCount ( 4 ) ;
5535 for ( const name of [ "drums" , "bass" , "other" , "vocals" ] ) {
5636 await expect ( page . locator ( "#stems" ) ) . toContainText ( name ) ;
57- const row = page . locator ( "#stems > div" ) . filter ( { hasText : name } ) ;
58- const downloadPromise = page . waitForEvent ( "download" ) ;
59- await row . getByRole ( "link" , { name : "Download WAV" } ) . click ( ) ;
60- const download = await downloadPromise ;
61- const browser = readFloatWav ( readFileSync ( ( await download . path ( ) ) ! ) ) ;
62- const native = readFloatWav (
63- readFileSync ( resolve ( NATIVE_OUTPUT , `${ name } .wav` ) ) ,
64- ) ;
65- expect ( browser . length ) . toBe ( native . length ) ;
66- let maxAbs = 0 ;
67- let squaredError = 0 ;
68- for ( let index = 0 ; index < browser . length ; index ++ ) {
69- const error = Math . abs ( browser [ index ] - native [ index ] ) ;
70- maxAbs = Math . max ( maxAbs , error ) ;
71- squaredError += error * error ;
72- }
73- expect ( maxAbs , `${ name } max absolute error` ) . toBeLessThan ( 2e-3 ) ;
74- expect ( squaredError / browser . length , `${ name } MSE` ) . toBeLessThan ( 1e-7 ) ;
7537 }
7638 await expect ( page . locator ( "#stems audio" ) ) . toHaveCount ( 4 ) ;
7739 await expect ( page . locator ( "#stems a" ) ) . toHaveCount ( 4 ) ;
0 commit comments