o
    ìcj	                     @  sZ   d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 e
eZG dd deZdS )u%   TTS vía Google Cloud Text-to-Speech.    )annotationsN)AudioFormatTextToSpeech)sanitize_for_ttsc                   @  s@   e Zd ZdZdddddddZdd ZdddddddZdS )GoogleTextToSpeechgooglezes-USzes-US-Neural2-Ag      ?)language_code
voice_namespeaking_rater   strr	   r
   floatreturnNonec                C  s   || _ || _|| _d | _d S )N)r   r	   r
   _client)selfr   r	   r
    r   >/var/www/html/bot-asterisk-ari/backend/voice/tts/google_tts.py__init__   s   
zGoogleTextToSpeech.__init__c                 C  s,   | j d u rddlm} | | _ || _| j S )Nr   texttospeech_v1)r   google.cloudr   TextToSpeechClient_tts)r   ttsr   r   r   _get_client   s
   

zGoogleTextToSpeech._get_clientN)audio_formatvoicer   textr   AudioFormat | Noner   
str | Nonebytesc                  sx   |pt dd|pj|pjt|  sdS d	 fdd}z	t|I d H W S  ty;   td  w )
Nmulaw)encoding    r   r    c                    sj   ddl m}   }jdkr| jjn| jj}|j| j d| j	d| j
|jjdd}|jp4dS )	Nr   r   r!   )r   )r   name)audio_encodingsample_rate_hertzr
   )inputr   audio_configr#   )r   r   r   r"   AudioEncodingMULAWLINEAR16synthesize_speechSynthesisInputVoiceSelectionParamsAudioConfigsample_rate_hzr
   audio_content)r   clientr"   responsecleanfmtlangr   r	   r   r   _sync4   s&   


z,GoogleTextToSpeech.synthesize.<locals>._syncu   Google TTS falló)r   r    )	r   r   r	   r   asyncio	to_thread	Exceptionlogger	exception)r   r   r   r   r   r8   r   r4   r   
synthesize%   s   


zGoogleTextToSpeech.synthesize)r   r   r	   r   r
   r   r   r   )
r   r   r   r   r   r   r   r   r   r    )__name__
__module____qualname__provider_namer   r   r>   r   r   r   r   r      s    r   )__doc__
__future__r   r9   loggingvoice.protocolsr   r   voice.tts.sanitizer   	getLoggerr?   r<   r   r   r   r   r   <module>   s    
