o
    i'                  
   @   s8  d Z ddlZddlmZmZ ddlmZmZ ddlm	Z	 ddl
Z
ddlmZ ddlmZ ddlmZmZ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 W n  eyz 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$de%de%de&de%fddZ'dS )z%Speechmatics TTS service integration.    N)	dataclassfield)AsyncGeneratorOptional)	urlencode)logger)	BaseModel)
ErrorFrameFrameTTSAudioRawFrame)	NOT_GIVENTTSSettings	_NotGiven)
TTSService)exponential_backoff_time)
traced_tts)__version__zException: zQIn order to use Speechmatics, you need to `pip install pipecat-ai[speechmatics]`.zMissing module: c                   @   s,   e Zd ZU dZedd dZeeB ed< dS )SpeechmaticsTTSSettingsz|Settings for SpeechmaticsTTSService.

    Parameters:
        max_retries: Maximum number of retries for HTTP requests.
    c                   C   s   t S )N)r    r   r   U/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/services/speechmatics/tts.py<lambda>.   s    z SpeechmaticsTTSSettings.<lambda>)default_factorymax_retriesN)	__name__
__module____qualname____doc__r   r   intr   __annotations__r   r   r   r   r   &   s   
 r   c                       s   e Zd ZU dZeZeed< dZG dd deZ	ddedddd	e
d
e
dee
 dejdee 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 )SpeechmaticsTTSServicezSpeechmatics TTS service implementation.

    This service provides text-to-speech synthesis using the Speechmatics HTTP API.
    It converts text to speech and returns raw PCM audio data for real-time playback.
    	_settingsi>  c                   @   s   e Zd ZU dZdZeed< dS )z"SpeechmaticsTTSService.InputParamsa  Optional input parameters for Speechmatics TTS configuration.

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

        Parameters:
            max_retries: Maximum number of retries for TTS requests. Defaults to 5.
           r   N)r   r   r   r   r   r   r   r   r   r   r   InputParams=   s   
 	r"   z$https://preview.tts.speechmatics.comN)base_urlvoice_idsample_rateparamssettingsapi_keyr#   r$   aiohttp_sessionr%   r&   r'   c          
         s   |r|| j krtd| j  d| d | jddddd}	|dur*| dd	 ||	_|dur9| d
 |s9|j|	_|durB|	| t j	d|dd|	d| || _
|| _|| _| j
s`tddS )au  Initialize the Speechmatics TTS service.

        Args:
            api_key: Speechmatics API key for authentication.
            base_url: Base URL for Speechmatics TTS API.
            voice_id: Voice model to use for synthesis.

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

            aiohttp_session: Shared aiohttp session for HTTP requests.
            sample_rate: Audio sample rate in Hz.
            params: Input parameters for the service.

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

            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
            **kwargs: Additional arguments passed to TTSService.
        zSpeechmatics TTS only supports z Hz sample rate. Current rate of zHz may cause issues.Nsarahr!   )modelvoicelanguager   r$   r,   r&   T)r%   push_start_framepush_stop_framesr'   zMissing Speechmatics API keyr   )SPEECHMATICS_SAMPLE_RATEr   warningSettings"_warn_init_param_moved_to_settingsr,   r   apply_updatesuper__init___api_key	_base_url_session
ValueError)
selfr(   r#   r$   r)   r%   r&   r'   kwargsdefault_settings	__class__r   r   r6   I   sF   !


	zSpeechmaticsTTSService.__init__returnc                 C   s   dS )zCheck if this service can generate processing metrics.

        Returns:
            True, as Speechmatics service supports metrics generation.
        Tr   )r;   r   r   r   can_generate_metrics   s   z+SpeechmaticsTTSService.can_generate_metricstext
context_idc              
   C  s  t |  d| d d| j dd}d|i}t| j| jj| j}z:zd}	 | jj	|||d	4 I d
H }	 |j
dkr	 z6t|dddd}|d7 }|| jjkrUt td| d|dddV  t|I d
H  W W d
  I d
H  q) ttfy   td| ddV  Y W d
  I d
H  W W |  I d
H  d
S w |j
dkrtd|j
 ddV  	 W d
  I d
H  W W |  I d
H  d
S | |I d
H  d}	d}
|j 2 z?3 d
H W }|sq|	r|  I d
H  d}	|
|7 }
t|
dkrt|
d }|d }|
d
| }|
|d
 }
t|| jd|dV  q6 	 W d
  I d
H  n1 I d
H s'w   Y  q*W n tyJ } 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 Speechmatics' HTTP API.

        Args:
            text: The text to synthesize into speech.
            context_id: The context ID for tracking audio frames.

        Yields:
            Frame: Audio frames containing the synthesized speech.
        z: Generating TTS []zBearer zapplication/json)AuthorizationzContent-TyperB   r   T)jsonheadersNi  g      ?g       @g      ?)attemptmin_waitmax_wait
multiplier   z#Service unavailable [503] (attempt z, retry in z.2fzs))errorz$Service unavailable [503] (attempts )   zService unavailable [    F   )audior%   num_channelsrC   zError generating TTS: )r   debugr7   _get_endpoint_urlr8   r    r,   r%   r9   poststatusr   r   r:   r	   asynciosleepArithmeticErrorstop_ttfb_metricsstart_tts_usage_metricscontentiter_anylenr   	Exception)r;   rB   rC   rG   payloadurlrH   responsebackoff_timefirst_chunkbufferchunkcomplete_samplescomplete_bytes
audio_dataer   r   r   run_tts   s   


Q
Q,NQ"zSpeechmaticsTTSService.run_tts)r   r   r   r   r   r2   r   r0   r   r"   strr   aiohttpClientSessionr   r6   boolrA   r   r   r
   rl   __classcell__r   r   r>   r   r   1   s:   
 	O(r   r#   r,   r%   r@   c                 C   s<   i }d| |d< dt  |d< t|}|  d| d| S )a  Format the TTS endpoint URL with voice, output format, and version params.

    Args:
        base_url: The base URL for the TTS endpoint.
        voice: The voice model to use.
        sample_rate: The audio sample rate.

    Returns:
        str: The formatted TTS endpoint URL.
    pcm_output_formatzpipecat/zsm-appz
/generate/?)r   r   )r#   r,   r%   query_paramsqueryr   r   r   rU     s
   rU   )(r   rX   dataclassesr   r   typingr   r   urllib.parser   rn   logurur   pydanticr   pipecat.frames.framesr	   r
   r   pipecat.services.settingsr   r   r   pipecat.services.tts_servicer   pipecat.utils.networkr   (pipecat.utils.tracing.service_decoratorsr   speechmatics.rtr   ModuleNotFoundErrorrk   rM   r`   r   r   rm   r   rU   r   r   r   r   <module>   s8   
 f