
    qi'                        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mZ dd
lmZ ddlmZ ddlmZ 	 ddlmZ e G d de             Z$ G d de      Z%de&de&de'de&fdZ(y# e $ r7Z! ejD                  de!         ejD                  d        e#de!       dZ![!ww xY w)z%Speechmatics TTS service integration.    N)	dataclassfield)AsyncGeneratorOptional)	urlencode)logger)	BaseModel)
ErrorFrameFrameTTSAudioRawFrame)	NOT_GIVENTTSSettings	_NotGiven_warn_deprecated_param)
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                   6    e Zd ZU dZ ed       Zeez  ed<   y)SpeechmaticsTTSSettingsz|Settings for SpeechmaticsTTSService.

    Parameters:
        max_retries: Maximum number of retries for HTTP requests.
    c                      t         S )N)r        S/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/speechmatics/tts.py<lambda>z SpeechmaticsTTSSettings.<lambda>.   s     r   )default_factorymax_retriesN)	__name__
__module____qualname____doc__r   r   intr   __annotations__r   r   r   r   r   &   s     $)9J#KKyKr   r   c                        e Zd ZU dZeZeed<   dZ G 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ZdefdZede
de
deedf   fd       Z x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                        e Zd ZU dZdZeed<   y)"SpeechmaticsTTSService.InputParamsa  Optional input parameters for Speechmatics TTS configuration.

        .. deprecated:: 0.0.105
            Use ``settings=SpeechmaticsTTSSettings(...)`` 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   InputParamsr(   =   s    	 Sr   r*   z$https://preview.tts.speechmatics.comN)base_urlvoice_idsample_rateparamssettingsapi_keyr+   r,   aiohttp_sessionr-   r.   r/   c                   |r5|| j                   k7  r&t        j                  d| j                    d| d       t        dddd      }	|t	        dt        d	       ||	_        |#t	        d
t               |s|j                  |	_        ||	j                  |       t        
| $  d|dd|	d| || _
        || _        || _        | j                  st        d      y)ae  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=SpeechmaticsTTSSettings(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=SpeechmaticsTTSSettings(...)`` 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,   r5   r.   T)r-   push_start_framepush_stop_framesr/   zMissing Speechmatics API keyr   )SPEECHMATICS_SAMPLE_RATEr   warningr   r   r5   r   apply_updatesuper__init___api_key	_base_url_session
ValueError)selfr0   r+   r,   r1   r-   r.   r/   kwargsdefault_settings	__class__s             r   r=   zSpeechmaticsTTSService.__init__I   s   B ;$*G*GGNN1$2O2O1P Q##.-/CE 3	
 ":/FP%-" "8-DE/5/A/A , ))(3 	
#!!%		

 	
 %&' }};<< r   returnc                      y)zCheck if this service can generate processing metrics.

        Returns:
            True, as Speechmatics service supports metrics generation.
        Tr   )rB   s    r   can_generate_metricsz+SpeechmaticsTTSService.can_generate_metrics   s     r   text
context_idc                6  K   t        j                  |  d| d       d| j                   dd}d|i}t        | j                  | j
                  j                  | j                        }	 d}	 | j                  j                  |||	      4 d
{   }	 |j                  dk(  r	 	 t        |ddd      }|dz  }|| j
                  j                  k\  r
t               t        d| d|dd       t        j                   |       d
{    	 d
d
d
      d
{    |j                  dk7  rGt        d|j                   d       	 d
d
d
      d
{    | j%                          d
{    y
| j'                  |       d
{    d}	d}
|j(                  j+                         2 3 d
{   }|s|	r| j%                          d
{    d}	|
|z  }
t-        |
      dk\  s<t-        |
      dz  }|dz  }|
d
| }|
|d
 }
t/        || j                  d|       v7 7 7 # t        t"        f$ r@ t        d| d       Y d
d
d
      d
{  7   | j%                          d
{  7   y
w xY w7 &7 7 7 7 6 	 d
d
d
      d
{  7   n# 1 d
{  7  sw Y   nxY wKn(# t0        $ r}t        d|        Y d
}~nd
}~ww xY w| j%                          d
{  7   y
# | j%                          d
{  7   w xY w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-TyperI   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 [r   F   )audior-   num_channelsrJ   zError generating TTS: )r   debugr>   _get_endpoint_urlr?   r&   r5   r-   r@   poststatusr   r   rA   r
   asynciosleepArithmeticErrorstop_ttfb_metricsstart_tts_usage_metricscontentiter_anylenr   	Exception)rB   rI   rJ   rO   payloadurlrP   responsebackoff_timefirst_chunkbufferchunkcomplete_samplescomplete_bytes
audio_dataes                   r   run_ttszSpeechmaticsTTSService.run_tts   s     	v/vQ78  't}}o6.
 D

  0D0DdFVFVWW	+G ==--c-Q L LU]=  #-C#+C(/$Y\,L
 $qLG  '$..*D*DD&0l 2 #-(KG9T_`lmp_qqs&t# 
 #*--"=== %;L L LL  #-(1FxFWWX/YZZQL Lb ((***M 66t<<< #'K F (0'7'7'@'@'B  e$$&"&"8"8":::*/K% v;!+/26{a/?,-=-AN)/)@J%+NO%<F #3&0,0,<,<-.+5	# IL4 >5L> !+O< #",(LWIUV&W#  #GL L Lb ((***e#?Lb +M = ;	 (C4 YL L L L L L\  	A%;A3#?@@@	A ((***$((***s  A'L*%J9 H&J9 J!&A&H/H)H/J9 H,J9 #-J!J9 JJ9  L3J4L9J!J"J!0J4J	5J8J!JJ!,:J!&J9 )H/,J9 /"I>J!J9 I J9 #L6I97L=I>>J!J9 LJ!	JJ!J!J9 JJ9 !J3'J*(J3/	J9 8K; 9	KKK; KK; !L4K75L;LLLL)r   r   r    r!   r   Settingsr#   r9   r	   r*   strr   aiohttpClientSessionr"   r=   boolrH   r   r   r   rs   __classcell__)rE   s   @r   r%   r%   1   s     'H&&$
i 
  ?"&%=(,6:M= M= 	M=
 3-M= !..M= c]M= %M= 23M=^d  r+# r+3 r+>%QU+;V r+ r+r   r%   r+   r5   r-   rF   c                 X    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+   r5   r-   query_paramsquerys        r   r\   r\     sL     L&*;-$8L!'}5Ll#EZz%%11r   ))r!   r_   dataclassesr   r   typingr   r   urllib.parser   rv   logurur   pydanticr	   pipecat.frames.framesr
   r   r   pipecat.services.settingsr   r   r   r   pipecat.services.tts_servicer   pipecat.utils.networkr   (pipecat.utils.tracing.service_decoratorsr   speechmatics.rtr   ModuleNotFoundErrorrr   rU   rg   r   r%   ru   r"   r\   r   r   r   <module>r      s    ,  ( + "    
 ` _ 3 : ?,+ Lk L Lb+Z b+J2 2C 2c 2c 2q  ,FLL;qc"#FLL[ &qc*
++,s   B C 	2B;;C 