o
    i_(                  
   @   s2  d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
 ddlZddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ zddlmZ ddlmZ W n  eyr Z zede  ed ede dZ[ww eG dd deZ G dd deZ!eG dd deZ"G dd deZ#dS )z!Piper TTS service implementation.    N)	dataclass)Path)AnyAsyncGeneratorAsyncIteratorOptional)logger)
ErrorFrameFrameTTSStoppedFrame)TTSSettings)
TTSService)
traced_tts)
PiperVoice)download_voicezException: zCIn order to use Piper, you need to `pip install pipecat-ai[piper]`.zMissing module: c                   @      e Zd ZdZdS )PiperTTSSettingszSettings for PiperTTSService.N__name__
__module____qualname____doc__ r   r   N/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/services/piper/tts.pyr   #       r   c                       s   e Zd ZU dZeZeed< dddddddee dee	 de
d	e
d
ee f
 fddZde
fddZdedeeef f fddZedededeedf fddZ  ZS )PiperTTSServicezPiper TTS service implementation.

    Provides local text-to-speech synthesis using Piper voice models. Automatically
    downloads voice models if not already present and resamples audio output to
    match the configured sample rate.
    	_settingsNF)voice_iddownload_dirforce_redownloaduse_cudasettingsr   r   r   r    r!   c                   s   | j dddd}|dur| dd ||_|dur|| t jddd|d| |p0t }| jj}| d}	t||	 }
|
	 sTt
d| d	 t|||d
 t
d| d|
  tj|
|d| _t
d| d	 dS )a  Initialize the Piper TTS service.

        Args:
            voice_id: Piper voice model identifier (e.g. `en_US-ryan-high`).

                .. deprecated:: 0.0.105
                    Use ``settings=PiperTTSService.Settings(voice=...)`` instead.

            download_dir: Directory for storing voice model files. Defaults to
                the current working directory.
            force_redownload: Re-download the voice model even if it already exists.
            use_cuda: Use CUDA for GPU-accelerated inference.
            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
            **kwargs: Additional arguments passed to the parent `TTSService`.
        Nmodelvoicelanguager   r$   Tpush_start_framepush_stop_framesr!   z.onnxzDownloading Piper 'z' model)r   zLoading Piper 'z' model from )r    zLoaded Piper 'r   )Settings"_warn_init_param_moved_to_settingsr$   apply_updatesuper__init__r   cwdr   existsr   debugr   r   load_voice)selfr   r   r   r    r!   kwargsdefault_settingsr2   
model_filemodel_path_resolved	__class__r   r   r-   5   s.   

zPiperTTSService.__init__returnc                 C      dS zCheck if this service can generate processing metrics.

        Returns:
            True, as Piper service supports metrics generation.
        Tr   r3   r   r   r   can_generate_metricst      z$PiperTTSService.can_generate_metricsdeltac                    s*   t  |I dH }|s|S | | |S )zgApply a settings delta.

        Settings are stored but not applied to the active connection.
        N)r,   _update_settings _warn_unhandled_updated_settings)r3   r@   changedr8   r   r   rA   |   s   
z PiperTTSService._update_settingstext
context_idc                   sX  dd  dt t f fdd}t|  d| d z}z-| |I dH  | j|| j|| jjj	|d	2 z3 dH W }| 
 I dH  |V  q66 W n% tyn } zt|  d
|  td| dV  W Y d}~nd}~ww W t|  d| d | 
 I dH  dS W t|  d| d | 
 I dH  dS t|  d| d | 
 I dH  w )a  Generate speech from text using Piper.

        Args:
            text: The text to convert to speech.
            context_id: Unique identifier for this TTS context.

        Yields:
            Frame: Audio frames containing the synthesized speech and status frames.
        c                 S   s    zt | W S  ty   Y d S w N)nextStopIteration)itr   r   r   
async_next   s
   
z+PiperTTSService.run_tts.<locals>.async_nextr:   c                   s,   	 t  | I d H }|d u rd S |jV  qrF   )asyncio	to_threadaudio_int16_bytes)iteratoritemrJ   r   r   async_iterator   s   z/PiperTTSService.run_tts.<locals>.async_iterator: Generating TTS []N)in_sample_raterE   z exception: Unknown error occurred: errorz: Finished TTS [)r   bytesr   r0   start_tts_usage_metrics"_stream_audio_frames_from_iteratorr2   
synthesizeconfigsample_ratestop_ttfb_metrics	ExceptionrW   r	   )r3   rD   rE   rQ   frameer   rP   r   run_tts   s6   zPiperTTSService.run_tts)r   r   r   r   r   r)   __annotations__r   strr   boolr-   r>   dictr   rA   r   r   r
   rb   __classcell__r   r   r8   r   r   *   s0   
 ?(r   c                   @   r   )PiperHttpTTSSettingsz!Settings for PiperHttpTTSService.Nr   r   r   r   r   rh      r   rh   c                
       s   e Zd ZU dZeZeed< ddddedej	de
e de
e f fd	d
ZdefddZedededeedf fddZ  ZS )PiperHttpTTSServicezPiper HTTP TTS service implementation.

    Provides integration with Piper's HTTP TTS server for text-to-speech
    synthesis. Supports streaming audio generation with configurable sample
    rates and automatic WAV header removal.
    r   N)r   r!   base_urlaiohttp_sessionr   r!   c                   s   | j dddd}|dur| dd ||_|dur|| t jd
dd|d| |dr;td |dd	 }|| _	|| _
dS )as  Initialize the Piper TTS service.

        Args:
            base_url: Base URL for the Piper TTS HTTP server.
            aiohttp_session: aiohttp ClientSession for making HTTP requests.
            voice_id: Piper voice model identifier (e.g. `en_US-ryan-high`).

                .. deprecated:: 0.0.105
                    Use ``settings=PiperHttpTTSService.Settings(voice=...)`` instead.

            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
            **kwargs: Additional arguments passed to the parent TTSService.
        Nr"   r   r$   Tr&   /z0Base URL ends with a slash, this is not allowed.r   )r)   r*   r$   r+   r,   r-   endswithr   warning	_base_url_session)r3   rj   rk   r   r!   r4   r5   r8   r   r   r-      s$   



zPiperHttpTTSService.__init__r:   c                 C   r;   r<   r   r=   r   r   r   r>     r?   z(PiperHttpTTSService.can_generate_metricsrD   rE   c           
   
   C  s  t |  d| d ddi}zz|| jjd}| jj| j||d4 I dH i}|jdkr]| I dH }t	d	|j d
| ddV  t
|dV  	 W d  I dH  W W |  I dH  dS | |I dH  | j}| j|j|d|d2 z3 dH W }|  I dH  |V  qt6 W d  I dH  n1 I dH sw   Y  W n ty }	 zt	d|	 dV  W Y d}	~	nd}	~	ww W |  I dH  dS W |  I dH  dS |  I dH  w )a   Generate speech from text using Piper's HTTP API.

        Args:
            text: The text to convert to speech.
            context_id: Unique identifier for this TTS context.

        Yields:
            Frame: Audio frames containing the synthesized speech and status frames.
        rR   rS   zContent-Typezapplication/json)rD   r$   )jsonheadersN   zError getting audio (status: z	, error: )rV   )rE   T)strip_wav_headerrE   rU   )r   r0   r   r$   rq   postrp   statusrD   r	   r   r^   rY   
chunk_sizerZ   contentiter_chunkedr_   )
r3   rD   rE   rs   dataresponserW   
CHUNK_SIZEr`   ra   r   r   r   rb   
  sL   

("zPiperHttpTTSService.run_tts)r   r   r   r   rh   r)   rc   rd   aiohttpClientSessionr   r-   re   r>   r   r   r
   rb   rg   r   r   r8   r   ri      s$   
 3(ri   )$r   rK   dataclassesr   pathlibr   typingr   r   r   r   r   logurur   pipecat.frames.framesr	   r
   r   pipecat.services.settingsr   pipecat.services.tts_servicer   (pipecat.utils.tracing.service_decoratorsr   piperr   piper.download_voicesr   ModuleNotFoundErrorra   rW   r_   r   r   rh   ri   r   r   r   r   <module>   s6   
 