"""Pinned ClearVoice enhancement; file mode segments long recordings correctly.""" import json,os,subprocess,sys,tempfile from pathlib import Path import numpy as np import soundfile as sf import torch from separator_runtime import verify_package,CLEARVOICE def enhance(model_dir,source,output): if not verify_package(model_dir,CLEARVOICE):raise ValueError('ClearVoice-Modellpaket-Prüfsumme stimmt nicht.') if not torch.cuda.is_available():raise ValueError('CUDA nicht verfügbar; kein CPU-Fallback.') output=Path(output) with tempfile.TemporaryDirectory(prefix='clearvoice-',dir=output) as temp: work=Path(temp);(work/'checkpoints').symlink_to((Path(model_dir)/'clearvoice').resolve(),target_is_directory=True) subprocess.run(['ffmpeg','-nostdin','-hide_banner','-loglevel','error','-y','-i',str(source),'-ar','48000','-c:a','pcm_f32le',str(work/'input.wav')],check=True,timeout=180,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) audio,sr=sf.read(work/'input.wav',dtype='float32',always_2d=True) previous=Path.cwd() try: os.chdir(work) from clearvoice import ClearVoice from clearvoice.networks import SpeechModel from unittest.mock import patch if torch.cuda.device_count()!=1:raise ValueError('Genau eine von Deck ausgewählte GPU erforderlich.') # Upstream uses physical nvidia-smi indices; Deck exposes only local CUDA:0. with patch.object(SpeechModel,'get_free_gpu',return_value=0):model=ClearVoice(task='speech_enhancement',model_names=[CLEARVOICE]) channels=[] for i in range(audio.shape[1]): path=work/f'channel-{i}.wav';sf.write(path,audio[:,i],sr,subtype='FLOAT') clean=np.asarray(model(str(path),False),dtype='float32').squeeze() if clean.ndim!=1 or not np.isfinite(clean).all():raise ValueError('Ungültige ClearVoice-Ausgabe.') channels.append(clean) length=min(len(audio),*(len(c) for c in channels));speech=np.column_stack([c[:length] for c in channels]);background=audio[:length]-speech finally:os.chdir(previous) for name,data in [('speech.wav',speech),('rest.wav',background)]:sf.write(output/name,np.clip(data,-1,1),sr,subtype='PCM_16') (output/'results.json').write_text(json.dumps(['speech.wav','rest.wav'])) if __name__=='__main__':enhance(sys.argv[1],sys.argv[3],sys.argv[4])