"""Persistent Q8 CPU worker. Native stdout is excluded from the JSON protocol.""" import json,os,resource,sys,time,wave from pathlib import Path def main(): protocol=os.fdopen(os.dup(1),'w',buffering=1);os.dup2(2,1) def emit(value):protocol.write(json.dumps(value)+'\n');protocol.flush() try: os.nice(10) resource.setrlimit(resource.RLIMIT_AS,(8*1024**3,8*1024**3)) import numpy as np import qwentts_cpp_cpu as q from tts_worker import spoken_chunks assert q.CPU_ONLY is True and q.QT_ABI_VERSION==5 model=q.QwenTTS(sys.argv[1],sys.argv[2],cpu_threads=4,cpu_codec_threads=4,cpu_stream_frames=2,clamp_fp16=False,use_fa=True) voices={} def voice_for(path): path=Path(path);key=(str(path),path.stat().st_mtime_ns) if key not in voices: with wave.open(str(path),'rb') as w: if w.getframerate()!=24000 or w.getnchannels()!=1 or w.getsampwidth()!=2:raise ValueError('Qwen-Referenz benötigt 24 kHz Mono PCM16.') samples=np.frombuffer(w.readframes(min(w.getnframes(),24000*120)),dtype='32*1024**2:raise ValueError('Qwen-Audio überschreitet Größenlimit.') if request.get('streaming'): tmp=root/'chunk.tmp';tmp.write_bytes(pcm);tmp.replace(root/f'chunk-{index:06d}.pcm');index+=1 if not parts:raise ValueError('Keine Audiodaten erzeugt.') with wave.open(str(root/'result.wav'),'wb') as w: w.setnchannels(1);w.setsampwidth(2);w.setframerate(24000);w.writeframes(b''.join(parts)) result=dict(ok=True,duration=total/48000,sample_rate=24000,seconds=time.monotonic()-start,first_chunk_seconds=first) except Exception as exc:result=dict(ok=False,error='Native Qwen-Spracherzeugung fehlgeschlagen ('+type(exc).__name__+').') tmp=root/'result.tmp';tmp.write_text(json.dumps(result));tmp.replace(root/'result.json') model.close() return 0 if __name__=='__main__':sys.exit(main())