
    [^j+                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
m
Z
mZ d dlmZ  ee                                          j        d         Zedz  Zedz  Zedz  Zedz  Zed	z  Zed
z  Zedz  Zedz  ZdZdZdZd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%e&dk    rS	  ej'         e%                       dS # e($ r0Z) e*de) ej+                    ej'        d           Y dZ)[)dS dZ)[)ww xY wdS )    N)datetimetimezone)Path   zstory/story-canon.txtzstory/spoken-narration.txtzstory/tts-pronunciation.txtzlog/tts-pronunciation.jsonzaudio/pronunciation-segmentszaudio/story-full.wavzscript/tts-manifest.jsonzlog/tts-verification.jsonzhttp://192.168.40.33:7862zngoc-huyen-vbeei  c                 N    t          j        |                                           S N)hashlibsha256	hexdigest)datas    script/render-tts.py	sha_bytesr      s    >$))+++    c                    t          j                    }|                     d          5 t          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   |                                S )Nrbc                  .                          d          S )Ni   )read)handles   r   <lambda>zsha_file.<locals>.<lambda>#   s    &++o">"> r   r   )r	   r
   openiterupdater   )pathdigestblockr   s      @r   sha_filer       s    ^F	4 !F>>>>DD 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,A##A'*A'c                     g }dt                     k     rt          t          z   t                               t                     k     rH fddD             }t          |          }|t          dz  z   k    r ||dz            dv rdnd}||z   |                                         t                     k     d                    |           k    rt          d          |S )	Nr   c                 >    g | ]}                     |          S  )rfind).0tokenendstarttexts     r   
<listcomp>z chunks_exact.<locals>.<listcomp>.   s)    qqqE$**UE377qqqr   )

. ? ! ; ,     >   r'   r*   r,   r(   r+   r)   r    z!TTS chunk reconstruction mismatch)lenmin	MAX_CHARSmaxappendjoinRuntimeError)r%   chunks
candidatescut	token_lenr#   r$   s   `    @@r   chunks_exactr;   (   s   FE
#d))

%)#SYY//T??qqqqqqEpqqqJj//CUY!^+++!%c#'k!26\!\!\AAbc	Iod59o&&& #d))

 
wwv$>???Mr   c                     t          j        dddddddddt          |           g
d	d	d	
          }t          j        |j                  S )Nffprobez-verrorz-show_entrieszformat=duration,sizez,stream=index,codec_name,sample_rate,channelsz-ofjsonT)capture_outputr%   check)
subprocessrunstrr?   loadsstdout)r   results     r   proberH   :   sY    ^4/3IGvs4yy T	3 3 3F
 :fm$$$r   c           	         t          j        t          |           d          5 }|                                |                                |                                |                                |                                |                                |                                z  dcd d d            S # 1 swxY w Y   d S )Nr   )channelssample_widthsample_rateframescompressionduration_seconds)waver   rD   getnchannelsgetsampwidthgetframerate
getnframesgetcomptype)r   r   s     r   wav_inforV   C   s    	3t99d	#	# 
v++--"//11!..00''))!--// & 1 1 3 3f6I6I6K6K K
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   B
B::B>B>c                     t           j                            t          dz   d          5 } t	          j        |           }d d d            n# 1 swxY w Y   |                    d          dk    rt          d          t           j                            t          dz   d          5 } t	          j        |                               dg           }d d d            n# 1 swxY w Y   t          d	 |D             d           }|r.|                    d
          dus|                    d          durt          d          ||fS )Nz/health   timeoutstatusokzPiper health check failedz/voicesvoicesc              3   V   K   | ]$}|                     d           t          k     |V  %dS )idN)getVOICE)r!   items     r   	<genexpr>zpreflight.<locals>.<genexpr>V   s6      CC488D>>U+B+B+B+B+B+BCCr   model_existsTconfig_existsz(ngoc-huyen-vbee model/config unavailable)	urllibrequesturlopenBASEr?   loadr`   r6   next)responsehealthr]   rows       r   	preflightro   O   s   			y 0"		=	= %8$$% % % % % % % % % % % % % % %zz(t##6777			y 0"		=	= 78$$((2667 7 7 7 7 7 7 7 7 7 7 7 7 7 7
CCCCCT
J
JC G#''.))559Q9QY]9]9]EFFF3;s#   AAA')CC #C c                    t           j                            | t          d                              d          }t           j                            t          dz   |ddid          }d }t          dd	          D ]=}	 t           j        	                    |d
          5 }|
                                }d d d            n# 1 swxY w Y   t          |          dk     s|d d	         dk    s|dd         dk    rt          d          |                    |           t          |          }|d         dk    s|d         dk    rt          d          ||fc S # t          $ rR}	|	}|                                r|                                 |dk     rt%          j        d|z             Y d }	~	7d }	~	ww xY wt          d|           )N)r%   voiceutf-8z/ttszContent-Typez!application/x-www-form-urlencodedPOST)r   headersmethodr      i,  rY   ,   s   RIFF      s   WAVEzPiper response is not WAVrO   r   rN   NONEzWAV verification failed   r.   z"TTS request failed after retries: )rf   parse	urlencodera   encoderg   Requestri   rangerh   r   r0   r6   write_bytesrV   	Exceptionexistsunlinktimesleep)
r%   tempbodyrg   lastattemptrl   r   infoexcs
             r   
synthesizer   \   s   <!!4%"@"@AAHHQQDn$$vD!DEf %  G DA;; ) )	)'''== '}}' ' ' ' ' ' ' ' ' ' ' ' ' ' '4yy2~~bqbW!4!4QrT
g8M8M"#>???T"""D>>D&'1,,]0Cv0M0M"#<===D=    	) 	) 	)D{{}} {{
1<(((	) BDBB
C
CCs>   >!EC 4E C	EC	BE
F6$AF11F6c                 ~   t          | d                   }|d         |d         |d         f}|                    d          }t          j        t	          |          d          5 }|                    |d                    |                    |d                    |                    |d                    | D ]}t          j        t	          |          d	          5 }|                                |	                                |
                                f}||k    s|                                d
k    rt          d|           	 |                    d          }	|	sn|                    |	           .	 d d d            n# 1 swxY w Y   	 d d d            n# 1 swxY w Y   t          j        ||           d S )Nr   rJ   rK   rL   	.part.wavwbr   r.   r   rz   zSegment WAV format mismatch: Ti   )rV   with_suffixrP   r   rD   setnchannelssetsampwidthsetframeraterQ   rR   rS   rU   r6   
readframeswriteframesrawosreplace)
pathsoutputfirstparamsr   targetr   sourcecurrentrM   s
             r   concatenater   w   s5   U1XEJ~!6m8LMFk**D	3t99d	#	# 2vF1I&&&F1I&&&F1I&&& 		2 		2D3t99d++ 2v!..00&2E2E2G2GI\I\I^I^_f$$(:(:(<(<(F(F&'Mt'M'MNNN2#..u55F! ))&111	2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2		2	2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 JtVs8   $A8FBF8FFFFFF!$F!c                     t           t          t          t          f} t	          d | D                       rt          d          t          j        t                              d                    }|	                    d          dus|	                    d          durt          d	          t          t                     }t          t                    }t          t                    }|	                    d
          |k    rt          d          |	                    d          |k    rt          d          |	                    d          |k    rt          d          t                              d          }|}t                      \  }}t          |          }	t                              dd           i }
t                                          rt          j        t                              d                    }|	                    d          |k    r>|	                    d          t"          k    r d |	                    dg           D             }
g }g }t%          |	d          D ]\  }}t          d|ddz  }t'          |                    d                    }|
	                    |i           }|                                r||	                    d          du re|	                    d          |k    rL|	                    d          t          |          k    r&t+          |          }|	                    dd          }nc|                                rt          d|           |                    d          }t/          ||          \  }}t1          j        ||           |t5          |          t5          |                                          |t9          |                    t<                              t          |          |                                j         |d         ||dd}|!                    |           |!                    |           t          "                    t          j#        ddt9          t                              t<                              |t"          tH          d%                    |	          |k    t5          |	          t5          |          |d 
dd!"          d#z   d           tM          t          j#        |t5          |	          tO          |d         d$          d%d&          d'           tQ          |tR                     t+          tR                    }tU          tR                    }tW          d( |D                       }d%                    |	          |k    ot5          |          t5          |	          k    ogtY          |d         |z
            d)k     oKt5          |	                    d*g                     dk    o$|d*         d+         	                    d,          d-k    }i d.dd/|rd0nd1d2t9          t                              t<                              d|d
|d3|d4t9          t                              t<                              d5t          t                    dt"          d6tH          d7dd8t5          |          d9t5          |          d|d:t9          tR                              t<                              dt          tR                    d|d         d;t[          j.        t^          j0                  1                                i}t          "                    t          j#        |dd!"          d#z   d           i d.dd|d/|d/         d<|	                    d<          d6tH          dt"          d=|d>t9          t                              t<                              d|d
|d?t9          t                              t<                              d3|d4t9          t                              t<                              d5t          t                    d@ddAt'          d%                    |	                              d                    d8t5          |          t5          |          t9          tR                              t<                              |d         |d         |t[          j.        t^          j0                  1                                dB}td          j3                            dd           td          "                    t          j#        |dd!"          d#z   d           tM          t          j#        |t5          |          |d         |d         dCd&                     |rd+ndS )DNc              3   @   K   | ]}|                                  V  d S r   )is_file)r!   r   s     r   rc   zmain.<locals>.<genexpr>   s-      
3
3$t||~~
3
3
3
3
3
3r   z-Pronunciation-ready TTS prerequisites missingrr   )encodingverifiedTsemantic_content_changedFz%Pronunciation receipt is not verifiedsource_canon_sha256z)Pronunciation receipt canon hash mismatchsource_sha256z*Pronunciation receipt spoken hash mismatchoutput_sha256z*Pronunciation receipt output hash mismatch)parentsexist_okrq   c                      i | ]}|d          |S )indexr   r!   rn   s     r   
<dictcomp>zmain.<locals>.<dictcomp>   s    MMMcGcMMMr   segmentsr   zsegment-03dz.wavtext_sha256attemptsz&Stale unversioned TTS segment exists: r   rO   )r   
characterswordsr   r   r   bytesrO   formatr   r   runningr/   )
versionr[   r   r   rq   base_urlreconstruction_verifiedsegments_totalsegments_completedr   r.   )ensure_asciiindent
r{   )segmenttotalduration)r   )flushc              3   &   K   | ]}|d          V  dS )rO   Nr   r   s     r   rc   zmain.<locals>.<genexpr>   s(      DDC 23DDDDDDr   g?streamsr   
codec_name	pcm_s16ler   r[   	completedfailedr   spoken_narration_sha256pronunciation_receiptpronunciation_receipt_sha256r   r   r   r   r   completed_atservicevoice_verificationsource_pathspoken_narration_pathpronunciation_verifiedchunk_reconstruction_sha256)r   r   r   rO   rH   
checked_at)r   r   rO   r   )4CANONSPOKENSOURCEPRONUNCIATION_RECEIPTanyr6   r?   rE   	read_textr`   r   ro   r;   SEGMENT_DIRmkdirMANIFESTr   ra   	enumerater   r~   rV   r   r   r   r   r0   splitrD   relative_toPROJECTstatst_sizer4   
write_textdumpsri   r5   printroundr   OUTPUTrH   sumabsr   nowr   utc	isoformatRECEIPTparent)requiredpronunciation	canon_sha
spoken_shapronunciation_shar%   
source_sharm   	voice_rowr7   existingoldrowsr   r   chunkr   text_shapriorr   r   r   rn   output_infooutput_probeexpected_durationr   manifestreceipts                                r   mainr     s
   vv'<=H

3
3(
3
3
333 LJKKKJ4>>>PPQQM$$D00M4E4EF`4a4ain4n4nBCCCI&!!J ((.//9<<FGGG))Z77GHHH))->>>GHHHW--D"J!FI$FdT222H Nj++W+==>>77?##z11cggg6F6F%6O6OMMSWWZ5L5LMMMHDE!&!,, T Tu9%99999U\\'2233UB''==?? 	%uyy44<<=AYAY]eAeAejojsjs  uD  kE  kE  IQ  RX  IY  IY  kY  kYF##DyyQ//HH}} V"#TF#T#TUUU%%k22D't44NHdJtV$$$#e**s5;;==?Q?Q#s63E3Eg3N3N/O/O%f--8M $%7 8D d
 
 	CVDJIV=O=OPW=X=X9Y9Y'%T')wwv$'>!&kkTX\	(
 (

 a) ) )
 ,00
 ;B 	 	C 	C 	C 	djUS[[eTXYkTlnoNpNpqq  AF  G  G  G  OS  	T  	T  	T  	T  	Tv6""K==LDDtDDDDD
4 	HCIIV$< 	H./2CCDDtK	H  B//00A5	H #A&**<88KG	 1x EX#f((11224CZ 	y +DZ 	 %:%F%Fw%O%O!P!P	
 	'1F(G(G 	 #D +DT 	#d)) &:3t99 GQRV 	#f((1122 5DXfEUEU 	K(:; 	X\22<<>> H 
8%JJJTQ\cddd1 (,4hx6H6::i((*4d<CU 	i *7F<N<Nw<W<W8X8X 		 &;I	
 	 V%7%7%@%@!A!A 	": 	 %:%F%Fw%O%O!P!P 	'1F(G(G 	!$ 	&y1G1G1P1P'Q'Q 	#d)) "$ii3v7I7I'7R7R3S3S!/2TfHgX\(,-G-G-Q-Q-S-S  G  N555tz'aHHH4OZabbb	$*(D		WbcuWv  JQ  Ra  Jb  c  c  rw  x  x  x  y  y  y11ar   __main__zTTS blocked: )file),r	   r?   r   rB   sysr   urllib.parserf   urllib.requestrP   r   r   pathlibr   __file__resolver   r   r   r   r   r   r   r   r   r   ri   ra   r2   r   r   r;   rH   rV   ro   r   r   r  __name__exitr   r   r   stderrr   r   r   <module>r     s2     				     



           ' ' ' ' ' ' ' '      
$x..
 
 
"
"
*1
-))	/	/	0	0">> 66	)	)//
/
/"	, , ,    $% % %	
 	
 	

 
 
D D D6  *c  c  c L z   #c###*5555 s   +C C:
%C55C: