o
    :i                     @   s>   d dl Z d dlmZmZmZmZ d dlmZ G dd dZdS )    N)process_audiogenerateload_models_gpuload_models_cpu)generate_cpuc                   @   s.   e Zd ZdZdddZdd	d
ZdddZdS )LuxTTSzQ
    LuxTTS class for encoding prompt and generating speech on cpu/cuda/mps.
    YatharthS/LuxTTScuda   c           	      C   s   |dkrd }|dkr"t j s"t jj rtd d}ntd d}|dkr5t||\}}}}}td nt||d\}}}}}td	 || _|| _	|| _
|| _|| _|| _d
| j
_d S )Nr   r	   z$CUDA not available, switching to MPSmpsz$CUDA not available, switching to CPUcpuzLoading model on CPU)devicezLoading model on GPUi.  )torchr	   is_availablebackendsr   printr   r   modelfeature_extractorvocos	tokenizertranscriberr   
freq_range)	self
model_pathr   threadsr   r   r   r   r    r   (/home/ubuntu/LuxTTS/zipvoice/luxvoice.py__init__
   s(   
zLuxTTS.__init__   MbP?c           	   	   C   s8   t || j| j| j| j||d\}}}}||||d}|S )zBencodes audio prompt according to duration and rms(volume control))
target_rmsduration)prompt_tokensprompt_features_lensprompt_features
prompt_rms)r   r   r   r   r   )	r   prompt_audior!   rmsr"   r#   r$   r%   encode_dictr   r   r   encode_prompt(   s   &zLuxTTS.encode_prompt      @      ?      ?Fc                 C   s   |  \}}	}
}|dkrd| j_nd| j_| jdkr1t||	|
||| j| j| j||||d}| S t||	|
||| j| j| j||||d}| S )zvencodes text and generates speech using flow matching model according to steps, guidance scale, and t_shift(like temp)TFr   )num_stepguidance_scalet_shiftspeed)	valuesr   
return_48kr   r   r   r   r   r   )r   textr(   	num_stepsr.   r/   r0   return_smoothr"   r#   r$   r%   	final_wavr   r   r   generate_speech/   s   

&&zLuxTTS.generate_speechN)r   r	   r
   )r   r   )r
   r*   r+   r,   F)__name__
__module____qualname____doc__r   r)   r7   r   r   r   r   r      s
    

r   )	r   zipvoice.modeling_utilsr   r   r   r   zipvoice.onnx_modelingr   r   r   r   r   r   <module>   s    