o
    ²ƒðjN  ã                   @   sÚ   d Z ddlZddlmZ ddlmZmZmZ ddlZ	ddl
Z
ddlmZ e e¡Zde
jde
jfdd	„Z	
dde	jdede	jfdd„Zdeeef deeef fdd„Zdd„ Z	ddedededee def
dd„ZdS )zUtility methods.é    N)ÚPath)ÚDictÚOptionalÚUnion)Úsanitize_filenameÚxÚreturnc                 C   s   |   ¡ jdd�S )NT)Únon_blocking)Ú
contiguousÚcuda)r   © r   ú˜/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/piper/train/vits/utils.pyÚto_gpu   s   r   ç    Àÿß@ÚaudioÚmax_wav_valuec              	   C   s<   | |t dt  t | ¡¡ƒ  }t || |¡}| d¡}|S )z*Normalize audio and convert to int16 rangeg{®Gáz„?Úint16)ÚmaxÚnpÚabsÚclipÚastype)r   r   Ú
audio_normr   r   r   Úaudio_float_to_int16   s   
r   Úphonemes_pathc                 C   sf   i }t | ddd�� }d}|D ]}| d¡}|sq|||< |d7 }qW d   ƒ |S 1 s,w   Y  |S )NÚrzutf-8)Úencodingr   z
é   )ÚopenÚstrip)r   ÚphonemesÚphonemes_fileÚphoneme_idxÚliner   r   r   Úload_phonemes   s   

ú
þ
ö
r$   c                 C   sV   |   ¡ }i }| ¡ D ]\}}||v r|| ||< q
t d|¡ |||< q
|  |¡ d S )Nz%s is not in the checkpoint)Ú
state_dictÚitemsÚ_LOGGERÚdebugÚload_state_dict)ÚmodelÚsaved_state_dictr%   Únew_state_dictÚkÚvr   r   r   r)   +   s   
r)   é2   Ú
row_numberÚtextÚ
max_lengthÚ
speaker_idc                 C   s:   d}|d urd|› �}t | ƒ| d t|ƒ }|d |… S )NÚ Ú_)Ústrr   )r0   r1   r2   r3   Úspeaker_id_strÚcache_idr   r   r   Úget_cache_id;   s
   
r9   )r   )r/   N)Ú__doc__ÚloggingÚpathlibr   Útypingr   r   r   Únumpyr   ÚtorchÚpathvalidater   Ú	getLoggerÚ__name__r'   ÚTensorr   ÚndarrayÚfloatr   r6   Úintr$   r)   r9   r   r   r   r   Ú<module>   s>    
ÿÿÿ
þ"
ÿÿÿÿÿþ