
    qil                     b   d Z ddl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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#y# e$ r7Z e
j@                  de         e
j@                  d        e!de       dZ[ww xY w)z+Groq text-to-speech service implementation.    N)	dataclassfield)AsyncGeneratorOptional)logger)	BaseModel)
ErrorFrameFrameTTSAudioRawFrame)	NOT_GIVENTTSSettings	_NotGiven_warn_deprecated_param)
TTSService)Language)
traced_tts)	AsyncGroqzException: zAIn order to use Groq, you need to `pip install pipecat-ai[groq]`.zMissing module: c                   <    e Zd ZU dZ ed       Zedz  ez  ed<   y)GroqTTSSettingszkSettings for GroqTTSService.

    Parameters:
        speed: Speech speed multiplier. Defaults to 1.0.
    c                      t         S )N)r        K/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/groq/tts.py<lambda>zGroqTTSSettings.<lambda>+   s    I r   )default_factoryNspeed)	__name__
__module____qualname____doc__r   r   floatr   __annotations__r   r   r   r   r   #   s"     ',<M&NE54<)#Nr   r   c                        e Zd ZU dZeZeed<    G d de      ZdZ	dddde	ddd	e
d
e
dee   dee
   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 )GroqTTSServicezGroq text-to-speech service implementation.

    Provides text-to-speech synthesis using Groq's TTS API. The service
    operates at a fixed 48kHz sample rate and supports various voices
    and output formats.
    	_settingsc                   N    e Zd ZU dZej
                  Zee   ed<   dZ	ee
   ed<   y)GroqTTSService.InputParamsa-  Input parameters for Groq TTS configuration.

        .. deprecated:: 0.0.105
            Use ``settings=GroqTTSSettings(...)`` instead.

        Parameters:
            language: Language for speech synthesis. Defaults to English.
            speed: Speech speed multiplier. Defaults to 1.0.
        language      ?r   N)r   r   r   r    r   ENr(   r   r"   r   r!   r   r   r   InputParamsr'   9   s*    	 (0{{(8$2!$x$r   r+   i  wavN)output_formatparams
model_namevoice_idsample_ratesettingsapi_keyr-   r.   r/   r0   r1   r2   c          	      ,   || j                   k7  r#t        j                  d| j                    d       t        dddd      }	|t	        d	t        d
       ||	_        |t	        dt        d       ||	_        |Kt	        dt               |s9|j                  rt        |j                        nd|	_        |j                  |	_	        ||	j                  |       t        
| 0  dddd||	d| || _        || _        t        | j                        | _        y)a  Initialize Groq TTS service.

        Args:
            api_key: Groq API key for authentication.
            output_format: Audio output format. Defaults to "wav".
            params: Additional input parameters for voice customization.

                .. deprecated:: 0.0.105
                    Use ``settings=GroqTTSSettings(...)`` instead.

            model_name: TTS model to use.

                .. deprecated:: 0.0.105
                    Use ``settings=GroqTTSSettings(model=...)`` instead.

            voice_id: Voice identifier to use.

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

            sample_rate: Audio sample rate. Must be 48000 Hz for Groq TTS.
            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
            **kwargs: Additional arguments passed to parent TTSService class.
        zGroq TTS only supports zHz sample rate. zcanopylabs/orpheus-v1-englishautumnenr)   )modelvoicer(   r   Nr/   r7   r0   r8   r.   T)pause_frame_processingpush_start_framepush_stop_framesr1   r2   )r3   r   )GROQ_SAMPLE_RATEr   warningr   r   r7   r8   r(   strr   apply_updatesuper__init___api_key_output_formatr   _client)selfr3   r-   r.   r/   r0   r1   r2   kwargsdefault_settings	__class__s             r   rA   zGroqTTSService.__init__I   s"   J $///NN4T5J5J4KK[\] +1	
 !"<'J%/"":H%-" "8_=DJOOC,@Y] ))/ & ))(3 	
#'!!#%	
 	
  + 7r   returnc                      y)zCheck if this service can generate processing metrics.

        Returns:
            True, as Groq TTS service supports metrics generation.
        Tr   )rE   s    r   can_generate_metricsz#GroqTTSService.can_generate_metrics   s     r   text
context_idc                6  K   t        j                  |  d| d       d}	 | j                  j                  j                  j                  | j                  j                  | j                  j                  | j                  | j                  j                  |       d{   }|j                         2 3 d{   }|r| j                          d{    d}t        j                  t        j                   |            5 }|j#                         }|j%                         }|j'                         }	|j)                  |	      }
t+        |
|||       ddd       7 7 7 # 1 sw Y   xY w6 y# t,        $ r}t/        d| 	       Y d}~yd}~ww xY ww)
a  Generate speech from text using Groq's TTS 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 data.
        z: Generating TTS []T)r7   r8   response_formatr   inputNF)rM   zUnknown error occurred: )error)r   debugrD   audiospeechcreater%   r7   r8   rC   r   
iter_bytesstop_ttfb_metricswaveopenioBytesIOgetnchannelsgetframerate
getnframes
readframesr   	Exceptionr	   )rE   rL   rM   measuring_ttfbresponsedatawchannels
frame_rate
num_framesbyteses               r   run_ttszGroqTTSService.run_tts   sz     	v/vQ78	C!\\//66==nn**nn** $ 3 3 nn** >  H '113 
_ 
_d!00222%*NYYrzz$/0 _A ~~/H!"!1J!"JLL4E*5*hS]^^_ _
_2_ _ 4  	C%=aS#ABBB	Cs   FA=E1 EE1 3E/7E8E/;E1 E!.E1  AE#
E1 E/!E1 #E,	(E1 0F1	F:FFFF)r   r   r   r    r   Settingsr"   r   r+   r<   r>   r   intrA   boolrK   r   r   r
   rk   __classcell__)rH   s   @r   r$   r$   .   s     H%i %  #(,$("&%5.2O8 O8 	O8
 %O8 SMO8 3-O8 c]O8 ?+O8bd  #C# #C3 #C>%QU+;V #C #Cr   r$   )$r    r[   rY   dataclassesr   r   typingr   r   logurur   pydanticr   pipecat.frames.framesr	   r
   r   pipecat.services.settingsr   r   r   r   pipecat.services.tts_servicer   pipecat.transcriptions.languager   (pipecat.utils.tracing.service_decoratorsr   groqr   ModuleNotFoundErrorrj   rR   ra   r   r$   r   r   r   <module>r{      s    2 	  ( +   
 ` _ 3 4 ?, Ok O OXCZ XC#  ,FLL;qc"#FLLTU
&qc*
++,s   A2 2B.72B))B.