o
    qmi"                     @   s   d dl Z d dlmZmZ d dlZd dlZd dlm  m	Z
 ddlmZ ddlmZmZ ddlm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 zd dlmZ W n eyd   d dlmZ Y nw G dd dZ G dd dZ!dS )    N)asynccontextmanagercontextmanager   )ApiError)AsyncClientWrapperSyncClientWrapper)RequestOptions   )#AsyncRawTextToSpeechStreamingClientRawTextToSpeechStreamingClient)&AsyncTextToSpeechStreamingSocketClient!TextToSpeechStreamingSocketClient)TextToSpeechStreamingModel)(TextToSpeechStreamingSendCompletionEvent)connectc                   @   x   e Zd ZdefddZedefddZedddddd	e	j
e d
e	j
e de	j
e de	j
e de	je f
ddZdS )TextToSpeechStreamingClientclient_wrapperc                C      t |d| _d S N)r   )r   _raw_clientselfr    r   \/home/ubuntu/.local/lib/python3.10/site-packages/sarvamai/text_to_speech_streaming/client.py__init__      z$TextToSpeechStreamingClient.__init__returnc                 C      | j S )z
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawTextToSpeechStreamingClient
        r   r   r   r   r   with_raw_response      	z-TextToSpeechStreamingClient.with_raw_responseNmodelsend_completion_eventapi_subscription_keyrequest_optionsr$   r%   r&   r'   c             
   c   s    | j j jd }t }|dur|d|}|dur"|d|}|d|  }| j j }|dur9t||d< |rFd|v rF|	|d  z"t
j||d}t|d	V  W d   W dS 1 saw   Y  W dS  tjjy }	 z|	j}
|
d
krt|
t|ddt|
t|ddd}	~	ww )a  
        WebSocket channel for real-time TTS synthesis.

        **Note:** This API Reference page is provided for informational purposes only.
        The Try It playground may not provide the best experience for streaming audio.
        For optimal streaming performance, please use the SDK or implement your own WebSocket client.

        **Model-Specific Notes:**
        - **bulbul:v2:** Supports pitch, loudness, pace (0.3-3.0). Default sample rate: 22050 Hz.
        - **bulbul:v3-beta:** Does NOT support pitch/loudness. Pace range: 0.5-2.0. Supports temperature parameter. Default sample rate: 24000 Hz. Preprocessing is always enabled.

        Parameters
        ----------
        model : typing.Optional[TextToSpeechStreamingModel]
            Text to speech model to use.
            - **bulbul:v2** (default): Standard TTS model with pitch/loudness support
            - **bulbul:v3-beta**: Advanced model with temperature control (no pitch/loudness)

        send_completion_event : typing.Optional[TextToSpeechStreamingSendCompletionEvent]
            Enable completion event notifications when TTS generation finishes. When set to true, an event message will be sent when the final audio chunk has been generated.

        api_subscription_key : typing.Optional[str]
            API subscription key for authentication

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        TextToSpeechStreamingSocketClient
        /text-to-speech/wsNr$   r%   ?Api-Subscription-Keyadditional_headers)r+   	websocket  /Websocket initialized with invalid credentials.status_codeheadersbody8Unexpected error when initializing websocket connection.)r   _client_wrapperget_environment
productionhttpxQueryParamsaddget_headersstrupdatewebsockets_sync_clientr   r   
websockets
exceptionsInvalidStatusCoder1   r   dictr   r$   r%   r&   r'   ws_urlquery_paramsr2   protocolexcr1   r   r   r   r   &   s@   (&z#TextToSpeechStreamingClient.connect)__name__
__module____qualname__r   r   propertyr   r!   r   typingOptionalr   r   r<   r   Iteratorr   r   r   r   r   r   r      (    
r   c                   @   r   ) AsyncTextToSpeechStreamingClientr   c                C   r   r   )r
   r   r   r   r   r   r   m   r   z)AsyncTextToSpeechStreamingClient.__init__r   c                 C   r   )z
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawTextToSpeechStreamingClient
        r   r    r   r   r   r!   p   r"   z2AsyncTextToSpeechStreamingClient.with_raw_responseNr#   r$   r%   r&   r'   c             
   C  s2  | j j jd }t }|dur|d|}|dur"|d|}|d|  }| j j }|dur9t||d< |rFd|v rF|	|d  z+t
||d4 I dH }t|d	V  W d  I dH  W dS 1 I dH sjw   Y  W dS  tjjy }	 z|	j}
|
d
krt|
t|ddt|
t|ddd}	~	ww )a  
        WebSocket channel for real-time TTS synthesis.

        **Note:** This API Reference page is provided for informational purposes only.
        The Try It playground may not provide the best experience for streaming audio.
        For optimal streaming performance, please use the SDK or implement your own WebSocket client.

        **Model-Specific Notes:**
        - **bulbul:v2:** Supports pitch, loudness, pace (0.3-3.0). Default sample rate: 22050 Hz.
        - **bulbul:v3-beta:** Does NOT support pitch/loudness. Pace range: 0.5-2.0. Supports temperature parameter. Default sample rate: 24000 Hz. Preprocessing is always enabled.

        Parameters
        ----------
        model : typing.Optional[TextToSpeechStreamingModel]
            Text to speech model to use.
            - **bulbul:v2** (default): Standard TTS model with pitch/loudness support
            - **bulbul:v3-beta**: Advanced model with temperature control (no pitch/loudness)

        send_completion_event : typing.Optional[TextToSpeechStreamingSendCompletionEvent]
            Enable completion event notifications when TTS generation finishes. When set to true, an event message will be sent when the final audio chunk has been generated.

        api_subscription_key : typing.Optional[str]
            API subscription key for authentication

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncTextToSpeechStreamingSocketClient
        r(   Nr$   r%   r)   r*   r+   )extra_headersr,   r.   r/   r0   r4   )r   r5   r6   r7   r8   r9   r:   r;   r<   r=   websockets_client_connectr   r?   r@   rA   r1   r   rB   rC   r   r   r   r   {   s@   (2z(AsyncTextToSpeechStreamingClient.connect)rH   rI   rJ   r   r   rK   r
   r!   r   rL   rM   r   r   r<   r   AsyncIteratorr   r   r   r   r   r   rP   l   rO   rP   )"rL   
contextlibr   r   r8   websockets.exceptionsr?   websockets.sync.clientsyncclientr>   core.api_errorr   core.client_wrapperr   r   core.request_optionsr   
raw_clientr
   r   socket_clientr   r   $types.text_to_speech_streaming_modelr   4types.text_to_speech_streaming_send_completion_eventr   websockets.legacy.clientr   rR   ImportErrorr   rP   r   r   r   r   <module>   s&   U