"""v0.7 regressions: additional BASS/NOISE lanes must never double-count energy.""" import hashlib import io import time from pathlib import Path import numpy as np import pytest import soundfile as sf from fastapi.testclient import TestClient from app.unified import build_unified_graph from app.mixer import render_mix from app.server import create_app def write(path, samples, sr=8000): path.parent.mkdir(parents=True, exist_ok=True) sf.write(path, samples, sr, subtype="FLOAT", format="WAV") return path def fixtures(tmp): sr=8000; t=np.arange(sr*2)/sr harm=.11*np.sin(2*np.pi*90*t) perc=np.zeros_like(harm);perc[::401]=.22 residue=.008*np.cos(2*np.pi*1733*t) noise=np.zeros_like(harm);noise[778]=.065 stereo=lambda x:np.column_stack((x,x*.92)).astype("float32") a,b,r,n=map(stereo,(harm,perc,residue,noise)) original=a+b+r root=tmp/"project";root.mkdir() write(root/"original.wav",original) for name,x in [("harmonic",a),("percussive",b),("residual",r)]: write(root/"stems"/f"{name}.wav",x) # Emulate already computed BASS raw estimates. Enhanced bass mixes are excluded. kick=b*.35 line=a*.42 write(root/"basscore"/"testbass"/"kick_estimate.wav",kick) write(root/"basscore"/"testbass"/"bassline_estimate.wav",line) write(root/"noisecore"/"testnoise"/"difference.wav",n) asset_hash=hashlib.sha256((root/"original.wav").read_bytes()).hexdigest() m={"asset":{"stored_name":"original.wav","sha256":asset_hash}, "stems":{n:{"master":f"{n}.wav"} for n in ("harmonic","percussive","residual")}, "bass":{"id":"testbass","mode":"hpss","stems":{ "kick_estimate":{"master":"kick_estimate.wav"}, "bassline_estimate":{"master":"bassline_estimate.wav"}}}, "noise":{"repair":{"id":"testnoise","tracks":{"difference":{"master":"difference.wav"}}}}} return root,m,original,kick,line,n,asset_hash def summed(root, graph, ignore=()): return np.sum([sf.read(root/v["master"],always_2d=True)[0] for name,v in graph["stems"].items() if name not in ignore], axis=0) def test_combined_bass_noise_graph_preserves_original_and_tracks(tmp_path): root,m,x,k,b,n,sha=fixtures(tmp_path) graph=build_unified_graph(root,m,blocksize=1027) assert graph["mix_consistent"] assert graph["bass_included"] and graph["noise_included"] assert set(graph["stems"])=={"harmonic","percussive","residual", "kick_core","bassline_core","noise_difference"} assert graph["mix_consistency_max_error"]<5e-7 assert np.max(np.abs(summed(root,graph)-x))<5e-7 assert np.max(np.abs(summed(root,graph,{"noise_difference"})-(x-n)))<5e-7 assert np.max(np.abs(summed(root,graph,{"kick_core"})-(x-k)))<5e-7 assert hashlib.sha256((root/"original.wav").read_bytes()).hexdigest()==sha assert graph["noise_default_mute"] is False assert all("preview" in v and (root/v["preview"]).is_file() for v in graph["stems"].values()) def test_mix_render_no_double_bass_or_noise(tmp_path): root,m,x,k,b,n,_=fixtures(tmp_path) graph=build_unified_graph(root,m) sources={name:root/entry["master"] for name,entry in graph["stems"].items()} out=root/"neutral.wav" render_mix(sources,{"tracks":{}},out) mix,_=sf.read(out,always_2d=True) assert np.max(np.abs(mix-x))<5e-7 render_mix(sources,{"tracks":{"noise_difference":{"mute":True}}},out) after,_=sf.read(out,always_2d=True) assert np.max(np.abs(after-(x-n)))<5e-7 render_mix(sources,{"tracks":{"bassline_core":{"mute":True},"kick_core":{"mute":True}}},out) after,_=sf.read(out,always_2d=True) assert np.max(np.abs(after-(x-k-b)))<5e-7 def test_graph_for_only_noise_and_none(tmp_path): root,m,x,k,b,n,_=fixtures(tmp_path) m["bass"]=None graph=build_unified_graph(root,m) assert set(graph["stems"])=={"harmonic","percussive","residual","noise_difference"} assert np.max(np.abs(summed(root,graph)-x))<5e-7 m["noise"]=None assert build_unified_graph(root,m) is None def test_graph_for_bass_without_separation(tmp_path): root,m,x,k,b,n,_=fixtures(tmp_path) m["stems"]={} m["noise"]=None m["bass"]["mode"]="dsp" graph=build_unified_graph(root,m) assert set(graph["stems"])=={"source_body","kick_core","bassline_core"} assert np.max(np.abs(summed(root,graph)-x))<5e-7 def poll(client,jid): for _ in range(250): result=client.get("/api/v1/jobs/"+jid).json() if result["status"] in ("done","failed"):return result time.sleep(.04) pytest.fail("Job timed out") def test_unified_api_full_flow_bass_and_mixer(tmp_path): root,m,x,k,b,n,_=fixtures(tmp_path) # Real API path, real BASS-CORE algorithm. No model checkpoint required. app=create_app(tmp_path/"runtime") payload=io.BytesIO() sf.write(payload,x,8000,format="WAV",subtype="FLOAT") with TestClient(app) as client: pid=client.post("/api/v1/projects",json={"name":"Unified"}).json()["id"] response=client.post("/api/v1/assets",data={"project_id":pid}, files={"file":("demo.wav",payload.getvalue(),"audio/wav")}) assert response.status_code==201,response.text bassjob=client.post("/api/v1/jobs/bass",json={ "project_id":pid, "source_mode":"dsp", "crossover_hz":150, "harmonics":0, "wet":0}).json() result=poll(client,bassjob["id"]) assert result["status"]=="done",result manifest=client.get(f"/api/v1/projects/{pid}").json() assert manifest["unified"]["mix_consistent"] assert set(manifest["unified"]["stems"])=={"source_body","kick_core","bassline_core"} for track in manifest["unified"]["stems"]: assert client.get(f"/api/v1/projects/{pid}/wave/{track}").status_code==200 assert client.get(f"/api/v1/projects/{pid}/audio/{track}?preview=true").status_code==200 assert client.get(f"/api/v1/projects/{pid}/export").status_code==200 job=client.post("/api/v1/jobs/mix",json={"project_id":pid}).json() assert poll(client,job["id"])["status"]=="done" rendered=client.get(f"/api/v1/projects/{pid}/mixer/audio") assert rendered.status_code==200 mixed,_=sf.read(io.BytesIO(rendered.content),always_2d=True) assert np.max(np.abs(mixed-x))<2e-5 def test_full_api_hpss_bass_noise_repaired_additive(tmp_path): """Actual HPSS + BASS-CORE + manually approved NOISE-CORE on same source.""" t=np.arange(24000)/8000 wave=.09*np.sin(2*np.pi*73*t)+.033*np.sin(2*np.pi*512*t) stereo=np.stack((wave,wave*.85),axis=1).astype("float32") stereo[5017,:]+=.82 bio=io.BytesIO();sf.write(bio,stereo,8000,format="WAV",subtype="FLOAT") app=create_app(tmp_path/"complete") with TestClient(app) as client: pid=client.post("/api/v1/projects",json={"name":"Full"}).json()["id"] assert client.post("/api/v1/assets",data={"project_id":pid}, files={"file":("full.wav",bio.getvalue(),"audio/wav")}).status_code==201 run=client.post("/api/v1/jobs/separate",json={"project_id":pid,"model":"hpss"}) assert run.status_code==202,run.text assert poll(client,run.json()["id"])["status"]=="done" run=client.post("/api/v1/jobs/bass",json={ "project_id":pid,"source_mode":"auto", "crossover_hz":140, "harmonics":0,"wet":0}) assert poll(client,run.json()["id"])["status"]=="done" run=client.post("/api/v1/jobs/noise/scan",json={"project_id":pid}) assert poll(client,run.json()["id"])["status"]=="done" scan=client.get(f"/api/v1/projects/{pid}/noise").json()["noise"]["scan"] events=[z["sample"] for z in scan["events"]] assert 5017 in events, events run=client.post("/api/v1/jobs/noise/repair",json={ "project_id":pid,"approved_samples":[5017], "strength":.8}) assert poll(client,run.json()["id"])["status"]=="done" manifest=client.get(f"/api/v1/projects/{pid}").json() graph=manifest["unified"] assert graph and graph["mix_consistent"],graph assert len(graph["stems"])==6 run=client.post("/api/v1/jobs/mix",json={"project_id":pid}) assert poll(client,run.json()["id"])["status"]=="done" wav=client.get(f"/api/v1/projects/{pid}/mixer/audio") assert wav.status_code==200 reconstructed,_=sf.read(io.BytesIO(wav.content),always_2d=True) assert np.max(np.abs(stereo-reconstructed))<5e-5 run=client.post("/api/v1/jobs/mix",json={ "project_id":pid,"tracks":{"noise_difference":{"mute":True}}}) assert poll(client,run.json()["id"])["status"]=="done" clean,_=sf.read(io.BytesIO(client.get( f"/api/v1/projects/{pid}/mixer/audio").content),always_2d=True) difference=reconstructed-clean assert np.max(np.abs(difference))>.1 mask=np.ones(len(difference),dtype=bool);mask[5017]=False assert np.max(np.abs(difference[mask]))<2e-5