
    `gj                     b    d Z ddlZddlZddlZddlmZ d
dededdfdZd Z	e
d	k(  r e	        yy)u  Standalone NeuTTS synthesis helper.

Called by tts_tool.py via subprocess to keep the TTS model (~500MB)
in a separate process that exits after synthesis — no lingering memory.

Usage:
    python -m tools.neutts_synth --text "Hello" --out output.wav         --ref-audio samples/jo.wav --ref-text samples/jo.txt

Requires: python -m pip install -U neutts[all]
System:   apt install espeak-ng  (or brew install espeak-ng)
    N)Pathpathsample_ratereturnc                 <   ddl }t        ||j                        s|j                  ||j                        }|j                         }|j                  |dd      }|dz  j                  |j                        }d}d}||z  |d	z  z  }||d	z  z  }t        |      |d	z  z  }	t        | d
      5 }
|
j                  d       |
j                  t        j                  dd|	z                |
j                  d       |
j                  d       |
j                  t        j                  ddd|||||             |
j                  d       |
j                  t        j                  d|	             |
j                  |j                                ddd       y# 1 sw Y   yxY w)z@Write a WAV file from float32 samples (no soundfile dependency).r   N)dtypeg      g      ?i           wbs   RIFFz<I$   s   WAVEs   fmt z<IHHIIHHs   data)numpy
isinstancendarrayarrayfloat32flattenclipastypeint16lenopenwritestructpacktobytes)r   samplesr   nppcmnum_channelsbits_per_sample	byte_rateblock_align	data_sizefs              E/root/.hermes/venv/lib/python3.12/site-packages/tools/neutts_synth.py
_write_wavr'      s`   grzz*((7"**(5ooG gggtS)GU?
"
"288
,CLOl*o.BCI/Q"67KCOq01I	dD	 	Q		D"y.12			JA|[%{OE 	F		D),-		 	 	s   -CFFc                     t        j                  d      } | j                  ddd       | j                  ddd       | j                  d	dd
       | j                  ddd       | j                  ddd       | j                  ddd       | j                         }t	        |j
                        j                         }t	        |j                        j                         }|j                         s3t        d| t        j                         t        j                  d       |j                         s3t        d| t        j                         t        j                  d       |j                  d      j                         }	 ddlm}  |j$                  |j&                  d|j&                        }|j)                  t+        |            }|j-                  |j.                  ||      }t	        |j0                        }	|	j2                  j5                  dd       	 dd l}
|
j9                  t+        |	      |d        t        d!|	 t        j                         y # t"        $ r4 t        dt        j                         t        j                  d       Y w xY w# t"        $ r t;        t+        |	      |d        Y w xY w)"NzNeuTTS synthesis helper)descriptionz--textTzText to synthesize)requiredhelpz--outzOutput WAV pathz--ref-audiozReference voice audio pathz
--ref-textzReference voice transcript pathz--modelzneuphonic/neutts-air-q4-ggufzHuggingFace backbone model repo)defaultr+   z--devicecpuzDevice (cpu/cuda/mps)z"Error: reference audio not found: )filer	   z!Error: reference text not found: zutf-8)encodingr   )NeuTTSzFError: neutts not installed. Run: python -m pip install -U neutts[all]zneuphonic/neucodec)backbone_repobackbone_device
codec_repocodec_device)parentsexist_ok]  zOK: )argparseArgumentParseradd_argument
parse_argsr   	ref_audio
expanduserref_textexistsprintsysstderrexit	read_textstripneuttsr0   ImportErrormodeldeviceencode_referencestrinfertextoutparentmkdir	soundfiler   r'   )parserargsr<   ref_text_pathr>   r0   tts	ref_codeswavout_pathsfs              r&   mainrZ   3   sP   $$1JKF
46JK
$5FG
;WX
t:[\
	+I>  @

E8OPD T^^$//1I'224M29+>SZZP!1-A

S&&&8>>@H!
 jj'[[	C $$S^4I
))DIIy(
3C DHH~HOO$6.
XU+ 
D

#**-/  V]`]g]gh(  .3x=#u-.s$   I% & J% %9J"!J"%KK__main__)r7   )__doc__r8   r   rA   pathlibr   rK   intr'   rZ   __name__     r&   <module>rb      sL      
 S   <1.h zF ra   