
    Qp_jR)                        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
Z
d dlmZ  ee                                          j        d         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    r e             dS dS )    N)Path   zhttp://192.168.40.33:7862zngoc-huyen-vbeeu   Các bạn đang nghe truyện được phát từ Gác Mái Audio, chúc các bạn có những giây phút nghe truyện vui vẻ. Hãy ủng hộ chúng tôi bằng cách like video và đăng ký kênh.c                 N    t          j        |                                           S )N)hashlibsha256	hexdigest)datas    script/build-intro-audio.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_path.<locals>.<lambda>   s    &++o">"> r   r   )r   r   openiterupdater   )pathdigestblockr   s      @r
   sha_pathr      s    ^F	4 !F>>>>DD 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,A##A'*A'c                     | j                             dd           |                     | j        dz             }|                    t          j        |dd          dz   d	           t          j        ||            d S )
NTparentsexist_ok.partF   )ensure_asciiindent
utf-8encoding)	parentmkdirwith_suffixsuffix
write_textjsondumpsosreplace)r   valueparts      r
   atomic_jsonr2       s|    KdT222DK'122DOODJu5CCCdJU\O]]]JtTr   c                     t           j                            t          | z   d          5 }|j        dz  dk    rt          d|  d|j                   t          j        |          cd d d            S # 1 swxY w Y   d S )N   timeoutd   r    zGET z HTTP )urllibrequesturlopenBASEstatusRuntimeErrorr,   load)r   responses     r
   get_jsonr@   '   s    			tR		8	8 #H?c!Q&&CdCC(/CCDDDy""# # # # # # # # # # # # # # # # # #s   <A33A7:A7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,sizezBstream=index,codec_type,codec_name,sample_rate,channels,sample_fmtz-ofr,   T)checkcapture_outputtext)
subprocessrunstrr,   loadsstdout)r   results     r
   proberM   .   s[    ^tWo7Ma63t99	

 	 	 	F :fm$$$r   c                 B   |                      d                              d          }t          |          dk    s|d d         dk    s|dd         dk    rt          d|            t	          |           }d |                    d	g           D             }d
 |                    d	g           D             }t          |                    di                               dd                    }t          |          dk    s|s|dk    rt          d|            ||fS )Nr         s   RIFF   s   WAVEzinvalid RIFF/WAVE: c                 D    g | ]}|                     d           dk    |S )
codec_typeaudioget.0rows     r
   
<listcomp>zverify_wav.<locals>.<listcomp>A   /    XXXSsww|7L7LPW7W7WS7W7W7Wr   streamsc                 D    g | ]}|                     d           dk    |S )rS   videorU   rW   s     r
   rZ   zverify_wav.<locals>.<listcomp>B   r[   r   formatdurationr   r   zinvalid WAV stream/duration: )r   r   lenr=   rM   rV   float)r   rawinforT   r^   r`   s         r
   
verify_wavre   <   s   
))D//

r
"
"C
3xx2~~RaRG++s1R4yG/C/C777888;;DXXDHHY33XXXEXXDHHY33XXXETXXh++//
A>>??H
5zzQ%8q==A4AABBB>r   c                 v    t           j                            | t          d                              d          S )N)rF   voicer$   )r8   parse	urlencodeVOICEencode)rF   s    r
   request_bodyrl   I   s.    <!!4%"@"@AAHHQQQr   c                 J   t           j                            t          dz   t	          |           ddid          }t           j                            |d          5 |                    d          5 }j        d	z  d
k    rt          dj                   j	        
                                }|dvrt          d|           t          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )N/ttszContent-Type!application/x-www-form-urlencodedPOST)r	   headersmethodih  r5   wbr7   r    zPOST /tts HTTP >   	audio/wav
audio/waveaudio/x-wavapplication/octet-streamzunexpected TTS content type: c                  .                          d          S )Ni   r   )r?   s   r
   r   zsynthesize.<locals>.<lambda>Z   s    (--"<"< r   r   )r8   r9   Requestr;   rl   r:   r   r<   r=   rq   get_content_typer   write)rF   r1   r9   r   content_typer   r?   s         @r
   
synthesizer}   M   s   n$$v$!DE	 %  G 
				5	5  499T??  V\?c!Q&&BBBCCC'88::eeeM|MMNNN<<<<cBB 	  	 ELL	                                                                  s7   D3B D 4D D	DD	DDDc                     t           dz  } t          j        |                     d                    }|d         }t           |d         z  }t           |d         z  }t           |d         z  }t           |d         z  }t           |d	         z  }t           |d
         z  }|                    di                               d          dk    rt          d          ||||fD ]W}	|	                                s/|	                    |	j        dz                                             rt          d|	           Xt          j        |                    d                    }
t          j        |                    d                    }|
                    d          dk    s|
                    d          durt          d          |                    d          dk    s|                    d          durt          d          |                    d          t          |          k    rt          d          |
                    di           }|                    d          }|                    d          }|t          k    rt          d          |                    d          t          |                    d                    k    rt          d          |r9|                    d          t          |                    d                    k    rt          d          |                    d d!          d"k     s|                    d#          durt          d$          t          d%          }t          d&          }|                    d't          |t                    r|ng           }t!          d( |D             d           }|                    d          d)k    s|st          d*          |                    d+          d,u s|                    d-          d,u rt          d.          |j                            dd/           |                    |j        dz             }g }d }t'          d0d1          D ]f}	 t)          ||           t+          |           t-          j        ||           |                    |dd2            n# t2          j        j        $ r}|                    |d3t9          |          j        |j        d4           |}|                    d5           d6|j        cxk    rd7k     r	n nY d }~ n|d"k     rtA          j!        d8|z             Y d }~d }~wtD          $ rk}|                    |d3t9          |          j        d9           |}|                    d5           |d"k     rtA          j!        d8|z             Y d }~`d }~ww xY w|                                s|t+          |          \  }}tG          j$        tK          |          d:          5 }|&                                }|j'        d;k    rt          d<          |(                    |)                                          }d d d            n# 1 swxY w Y   |                    |j        dz             }|j*        d8z  }tG          j$        tK          |          d=          5 }|+                    |j,        |j-        |j*        d!|j'        |j.        f           |/                    d>|z  |j,        z  |j-        z             d d d            n# 1 swxY w Y   t+          |           t-          j        ||           |                    |j        dz             } tG          j$        tK          |           d=          5 }!|!+                    |j,        |j-        |j*        d!|j'        |j.        f           |!/                    |           |!/                    d>|z  |j,        z  |j-        z             d d d            n# 1 swxY w Y   t+          |            t-          j        | |           t+          |          \  }"}#t+          |          \  }$}%ta          |#d?z
            d@k    sta          |%|z
  d?z
            dAk    rt          dB          tc          |          }&td          j2        3                    td          j4        j5                  6                                }'i dddddCdDdEtn          dFdGdHtp          dIdJdHgdKdLdMt          |&          dN|dO|d         dPt          |          dQ|
dQ         dR|dS|d         dT|dU|d         |d         t          |          ||d         t          |          |#|d	         t          |          |%||"|$|'dV}(ts          ||(           dd|d
         |(dW         |%|'dX|d	<   |'|dY<   ts          | |           tu          t          j;        d||%|(dW         dZd,[                     d S )\Nzscript/project-manifest.jsonr$   r%   active_pathstts_pronunciation_receipttts_receiptintro_voiceintro_silenceintro_audiointro_audio_receiptstepstts	completedz1narration TTS must be terminal before intro voicer   z#intro audio output must be virgin: r<   verifiedTz"pronunciation receipt not terminalz"narration TTS receipt not terminal tts_pronunciation_receipt_sha256z!intro pronunciation lineage driftintrosource_texttts_textzcanonical intro source driftsource_text_sha256z!canonical intro source hash drifttts_text_sha256zintro TTS text/hash invalidreplacement_countr      reverse_reconstruction_verifiedz*intro pronunciation transform not verifiedz/healthz/voicesvoicesc              3   V   K   | ]$}|                     d           t          k     |V  %dS )idN)rV   rj   rW   s     r
   	<genexpr>zmain.<locals>.<genexpr>   s6      FFcswwt}}/E/Ec/E/E/E/EFFr   okzTTS preflight failedmodel_existsFconfig_existsz"TTS voice model/config unavailabler   r   rP   )attemptr<   failed)r   r<   error_classhttp_status)
missing_oki  i  r    )r   r<   r   r   NONEz#intro voice is not uncompressed PCMrs       g       @g-C6?gMbP?zintro duration relation failedproviderzpiper-wrapperbase_urlendpointrn   rg   request_fieldsrF   request_content_typero   payload_sha256attemptspronunciation_receipt_pathpronunciation_receipt_sha256source_canon_sha256intro_source_textintro_source_text_sha256intro_tts_textintro_tts_text_sha256)
voice_pathvoice_sha256voice_duration_secondssilence_pathsilence_sha256silence_duration_secondsintro_full_pathintro_full_sha256intro_full_duration_secondsvoice_probesilence_probeintro_full_probecompleted_atr   )r<   r   receiptartifact_sha256duration_secondsr   
updated_at)r<   voice_durationfull_durationr   )r!   )<ROOTr,   rJ   	read_textrV   r=   existsr)   r*   r   CANONICAL_INTROr   rk   r@   
isinstancelistnextr'   r(   ranger}   re   r.   r/   appendr8   rC   	HTTPErrortype__name__codeunlinktimesleep	Exceptionwaver   rI   	getparamscomptype
readframes
getnframes	framerate	setparams	nchannels	sampwidthcompnamewriteframesabsrl   datetimenowtimezoneutc	isoformatr;   rj   r2   printr-   ))manifest_pathmanifestpathspronunciation_pathnarration_receipt_pathr   r   	full_pathreceipt_pathoutputpronunciationnarration_receiptr   r   r   healthvoices_payloadr   	voice_rowr1   r   rC   r   excr   r   sourceparamsvoice_framessilence_partsilence_framessilence	full_partfullr   silence_duration
full_prober   bodyr   r   s)                                            r
   mainr   ^   s   99Mz-1171CCDDH^$E&A BB!E-$88m,,J%00Lu]++I% 566L||GR  $$U++{::NOOO|YE O O==?? 	Of001HIIPPRR 	OMVMMNNN	O J1;;W;MMNNM
#9#C#CW#C#U#UVV""k11]5F5Fz5R5RZ^5^5^?@@@X&&+559J9N9Nz9Z9Zbf9f9f?@@@?@@HM_D`D```>???gr**E))M**Kyy$$Ho%%9:::yy%&&)K4F4Fw4O4O*P*PPP>??? :uyy!233yQXAYAY7Z7ZZZ8999yy$a((1,,		:[0\0\dh0h0hGHHHi  Fi((NJ~W[<\<\*d..bdeeFFFVFFFMMIzz(t##9#1222}}^$$--1O1OSX1X1X?@@@D4888!!*"3g"=>>DHEA;; ) )	)x&&&tJtZ(((OO;GGHHHE|% 	) 	) 	)OO8TXY\T]T]Tfwzw  A  A  B  B  BEKK4K(((ch$$$$$$$$${{
1<((( 	) 	) 	)OO8TXY\T]T]TfgghhhEKK4K((({{
1<(((	)  ",Z"8"8K	3z??D	)	) >V!!##?f$$DEEE(():):)<)<==	> > > > > > > > > > > > > > >  ++L,?',IJJL%)N	3|$$d	+	+ \w6+V-=v?OQRTZTceketuvvvGn4v7GG&JZZ[[[\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ |J|\***%%i&6&@AAI	3y>>4	(	( YD(&*:F<LaQWQ`bhbqrsss&&&>1F4DDvGWWXXXY Y Y Y Y Y Y Y Y Y Y Y Y Y Y yJy)$$$&0&>&>#M# *9 5 5J
c!""V++s=>3QTW3W/X/X[`/`/`;<<<!!D



 1 5
6
6
@
@
B
BC+D 	O 	D	
 	F 	 	67+ 	 C 	)D// 	H 	%e,G&H 	'1C(D(D 	}-BC 	[ 	#E*>$?  	(!" 	 '8!9#$ M* ,,"0o."<00$4 /%i00'4"&&=  G@ g&&&./"#67) H] !H\x(((	$*~`m  zA  BU  zV  W  W  fk  l  l  l  m  m  m  m  msf   ASW"$AU*U**W"7A WW"/AZZZ%A]]]<A2`::`>`>__main__)r   r   r,   r.   rG   r   urllib.errorr8   urllib.parseurllib.requestr   pathlibr   __file__resolver   r   r;   rj   r   r   r   r2   r@   rM   re   rl   r}   r   r    r   r
   <module>r     se      				                        tH~~'*" c, , ,    # # #% % %
 
 
R R R     "Tm Tm Tmn zDFFFFF r   