# This file was auto-generated by Fern from our API Definition.

import typing
import urllib.parse
from contextlib import asynccontextmanager, contextmanager

import websockets.sync.client as websockets_sync_client
from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.jsonable_encoder import jsonable_encoder
from ..core.query_encoder import encode_query
from ..core.remove_none_from_dict import remove_none_from_dict
from ..core.request_options import RequestOptions
from ..core.websocket_compat import InvalidWebSocketStatus, get_status_code
from .raw_client import AsyncRawSpeechToTextTranslateStreamingClient, RawSpeechToTextTranslateStreamingClient
from .socket_client import AsyncSpeechToTextTranslateStreamingSocketClient, SpeechToTextTranslateStreamingSocketClient
from .types.speech_to_text_translate_streaming_flush_signal import SpeechToTextTranslateStreamingFlushSignal
from .types.speech_to_text_translate_streaming_high_vad_sensitivity import (
    SpeechToTextTranslateStreamingHighVadSensitivity,
)
from .types.speech_to_text_translate_streaming_input_audio_codec import SpeechToTextTranslateStreamingInputAudioCodec
from .types.speech_to_text_translate_streaming_mode import SpeechToTextTranslateStreamingMode
from .types.speech_to_text_translate_streaming_model import SpeechToTextTranslateStreamingModel
from .types.speech_to_text_translate_streaming_vad_signals import SpeechToTextTranslateStreamingVadSignals

try:
    from websockets.legacy.client import connect as websockets_client_connect  # type: ignore
except ImportError:
    from websockets import connect as websockets_client_connect  # type: ignore


class SpeechToTextTranslateStreamingClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawSpeechToTextTranslateStreamingClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawSpeechToTextTranslateStreamingClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawSpeechToTextTranslateStreamingClient
        """
        return self._raw_client

    @contextmanager
    def connect(
        self,
        *,
        model: typing.Optional[SpeechToTextTranslateStreamingModel] = None,
        mode: typing.Optional[SpeechToTextTranslateStreamingMode] = None,
        sample_rate: typing.Optional[str] = None,
        high_vad_sensitivity: typing.Optional[SpeechToTextTranslateStreamingHighVadSensitivity] = None,
        positive_speech_threshold: typing.Optional[str] = None,
        negative_speech_threshold: typing.Optional[str] = None,
        min_speech_frames: typing.Optional[str] = None,
        first_turn_min_speech_frames: typing.Optional[str] = None,
        negative_frames_count: typing.Optional[str] = None,
        negative_frames_window: typing.Optional[str] = None,
        start_speech_volume_threshold: typing.Optional[str] = None,
        interrupt_min_speech_frames: typing.Optional[str] = None,
        pre_speech_pad_frames: typing.Optional[str] = None,
        num_initial_ignored_frames: typing.Optional[str] = None,
        vad_signals: typing.Optional[SpeechToTextTranslateStreamingVadSignals] = None,
        flush_signal: typing.Optional[SpeechToTextTranslateStreamingFlushSignal] = None,
        input_audio_codec: typing.Optional[SpeechToTextTranslateStreamingInputAudioCodec] = None,
        api_subscription_key: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Iterator[SpeechToTextTranslateStreamingSocketClient]:
        """
        WebSocket channel for real-time speech to text streaming with English translation.

        **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.

        Parameters
        ----------
        model : typing.Optional[SpeechToTextTranslateStreamingModel]
            Model to be used for speech to text translation.

            - **saaras:v3** (default, recommended): State-of-the-art translation model that translates audio from any spoken Indic language to English with flexible output formats via the `mode` parameter.

            - **saaras:v2.5** (legacy): Translation model that translates audio from any spoken Indic language to English. Kept for backward compatibility.
              - Example: Hindi audio → English text output

        mode : typing.Optional[SpeechToTextTranslateStreamingMode]
            Mode of operation. **Only applicable when using saaras:v3 model.**

            - **translate** (default): Translates speech from any supported Indic language to English.
              - Example: Hindi audio → English text output

            - **transcribe**: Standard transcription in the original language.

            - **verbatim**: Exact word-for-word transcription without normalization.

            - **translit**: Romanization - Transliterates speech to Latin/Roman script only.

            - **codemix**: Code-mixed text with English words in English and Indic words in native script.

        sample_rate : typing.Optional[str]
            Audio sample rate for the WebSocket connection. When specified as a connection parameter, only 16kHz and 8kHz are supported. 8kHz is only available via this connection parameter. If not specified, defaults to 16kHz.

        high_vad_sensitivity : typing.Optional[SpeechToTextTranslateStreamingHighVadSensitivity]
            Enable high VAD (Voice Activity Detection) sensitivity

        positive_speech_threshold : typing.Optional[str]
            VAD probability threshold (0.0–1.0) above which a frame is considered speech.
            Overrides the server default when provided.

        negative_speech_threshold : typing.Optional[str]
            VAD probability threshold (0.0–1.0) below which a frame is considered silence.
            Overrides the server default (or the high_vad_sensitivity preset) when provided.

        min_speech_frames : typing.Optional[str]
            Minimum number of consecutive speech frames required to start a speech segment.
            Overrides the server default when provided.

        first_turn_min_speech_frames : typing.Optional[str]
            Minimum speech frames required specifically for the first user turn.
            Overrides the server default when provided.

        negative_frames_count : typing.Optional[str]
            Number of negative (silence) frames needed within the window to end a speech segment.
            Overrides the server default (or the high_vad_sensitivity preset) when provided.

        negative_frames_window : typing.Optional[str]
            Sliding window size (in frames) over which negative frames are counted.
            Overrides the server default (or the high_vad_sensitivity preset) when provided.

        start_speech_volume_threshold : typing.Optional[str]
            Volume level (dB) below which audio is considered too quiet to be speech.
            When not provided, no volume-based filtering is applied.

        interrupt_min_speech_frames : typing.Optional[str]
            Minimum speech frames required to register a barge-in / interruption.
            Overrides the server default when provided.

        pre_speech_pad_frames : typing.Optional[str]
            Number of audio frames to prepend before the detected speech onset,
            ensuring the beginning of speech is not clipped.
            Overrides the server default when provided.

        num_initial_ignored_frames : typing.Optional[str]
            Number of leading audio frames to skip entirely at connection start.
            Useful for discarding connection setup noise.
            Overrides the server default when provided.

        vad_signals : typing.Optional[SpeechToTextTranslateStreamingVadSignals]
            Enable VAD signals in response

        flush_signal : typing.Optional[SpeechToTextTranslateStreamingFlushSignal]
            Signal to flush the audio buffer and finalize transcription and translation

        input_audio_codec : typing.Optional[SpeechToTextTranslateStreamingInputAudioCodec]
            Audio codec/format of the input stream. Use this when sending raw PCM audio.
            Supported values: wav, pcm_s16le, pcm_l16, pcm_raw.

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

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

        Returns
        -------
        SpeechToTextTranslateStreamingSocketClient
        """
        ws_url = self._raw_client._client_wrapper.get_environment().production + "/speech-to-text-translate/ws"
        _encoded_query_params = encode_query(
            jsonable_encoder(
                remove_none_from_dict(
                    {
                        "model": model,
                        "mode": mode,
                        "sample_rate": sample_rate,
                        "high_vad_sensitivity": high_vad_sensitivity,
                        "positive_speech_threshold": positive_speech_threshold,
                        "negative_speech_threshold": negative_speech_threshold,
                        "min_speech_frames": min_speech_frames,
                        "first_turn_min_speech_frames": first_turn_min_speech_frames,
                        "negative_frames_count": negative_frames_count,
                        "negative_frames_window": negative_frames_window,
                        "start_speech_volume_threshold": start_speech_volume_threshold,
                        "interrupt_min_speech_frames": interrupt_min_speech_frames,
                        "pre_speech_pad_frames": pre_speech_pad_frames,
                        "num_initial_ignored_frames": num_initial_ignored_frames,
                        "vad_signals": vad_signals,
                        "flush_signal": flush_signal,
                        "input_audio_codec": input_audio_codec,
                        **(
                            request_options.get("additional_query_parameters", {}) or {}
                            if request_options is not None
                            else {}
                        ),
                    }
                )
            )
        )
        if _encoded_query_params:
            ws_url = ws_url + "?" + urllib.parse.urlencode(_encoded_query_params)
        headers = self._raw_client._client_wrapper.get_headers()
        if api_subscription_key is not None:
            headers["Api-Subscription-Key"] = str(api_subscription_key)
        if request_options and "additional_headers" in request_options:
            headers.update(request_options["additional_headers"])
        try:
            with websockets_sync_client.connect(ws_url, additional_headers=headers) as protocol:
                yield SpeechToTextTranslateStreamingSocketClient(websocket=protocol)
        except InvalidWebSocketStatus as exc:
            status_code: int = get_status_code(exc)
            if status_code == 401:
                raise ApiError(
                    status_code=status_code,
                    headers=dict(headers),
                    body="Websocket initialized with invalid credentials.",
                )
            raise ApiError(
                status_code=status_code,
                headers=dict(headers),
                body="Unexpected error when initializing websocket connection.",
            )


class AsyncSpeechToTextTranslateStreamingClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawSpeechToTextTranslateStreamingClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawSpeechToTextTranslateStreamingClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawSpeechToTextTranslateStreamingClient
        """
        return self._raw_client

    @asynccontextmanager
    async def connect(
        self,
        *,
        model: typing.Optional[SpeechToTextTranslateStreamingModel] = None,
        mode: typing.Optional[SpeechToTextTranslateStreamingMode] = None,
        sample_rate: typing.Optional[str] = None,
        high_vad_sensitivity: typing.Optional[SpeechToTextTranslateStreamingHighVadSensitivity] = None,
        positive_speech_threshold: typing.Optional[str] = None,
        negative_speech_threshold: typing.Optional[str] = None,
        min_speech_frames: typing.Optional[str] = None,
        first_turn_min_speech_frames: typing.Optional[str] = None,
        negative_frames_count: typing.Optional[str] = None,
        negative_frames_window: typing.Optional[str] = None,
        start_speech_volume_threshold: typing.Optional[str] = None,
        interrupt_min_speech_frames: typing.Optional[str] = None,
        pre_speech_pad_frames: typing.Optional[str] = None,
        num_initial_ignored_frames: typing.Optional[str] = None,
        vad_signals: typing.Optional[SpeechToTextTranslateStreamingVadSignals] = None,
        flush_signal: typing.Optional[SpeechToTextTranslateStreamingFlushSignal] = None,
        input_audio_codec: typing.Optional[SpeechToTextTranslateStreamingInputAudioCodec] = None,
        api_subscription_key: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.AsyncIterator[AsyncSpeechToTextTranslateStreamingSocketClient]:
        """
        WebSocket channel for real-time speech to text streaming with English translation.

        **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.

        Parameters
        ----------
        model : typing.Optional[SpeechToTextTranslateStreamingModel]
            Model to be used for speech to text translation.

            - **saaras:v3** (default, recommended): State-of-the-art translation model that translates audio from any spoken Indic language to English with flexible output formats via the `mode` parameter.

            - **saaras:v2.5** (legacy): Translation model that translates audio from any spoken Indic language to English. Kept for backward compatibility.
              - Example: Hindi audio → English text output

        mode : typing.Optional[SpeechToTextTranslateStreamingMode]
            Mode of operation. **Only applicable when using saaras:v3 model.**

            - **translate** (default): Translates speech from any supported Indic language to English.
              - Example: Hindi audio → English text output

            - **transcribe**: Standard transcription in the original language.

            - **verbatim**: Exact word-for-word transcription without normalization.

            - **translit**: Romanization - Transliterates speech to Latin/Roman script only.

            - **codemix**: Code-mixed text with English words in English and Indic words in native script.

        sample_rate : typing.Optional[str]
            Audio sample rate for the WebSocket connection. When specified as a connection parameter, only 16kHz and 8kHz are supported. 8kHz is only available via this connection parameter. If not specified, defaults to 16kHz.

        high_vad_sensitivity : typing.Optional[SpeechToTextTranslateStreamingHighVadSensitivity]
            Enable high VAD (Voice Activity Detection) sensitivity

        positive_speech_threshold : typing.Optional[str]
            VAD probability threshold (0.0–1.0) above which a frame is considered speech.
            Overrides the server default when provided.

        negative_speech_threshold : typing.Optional[str]
            VAD probability threshold (0.0–1.0) below which a frame is considered silence.
            Overrides the server default (or the high_vad_sensitivity preset) when provided.

        min_speech_frames : typing.Optional[str]
            Minimum number of consecutive speech frames required to start a speech segment.
            Overrides the server default when provided.

        first_turn_min_speech_frames : typing.Optional[str]
            Minimum speech frames required specifically for the first user turn.
            Overrides the server default when provided.

        negative_frames_count : typing.Optional[str]
            Number of negative (silence) frames needed within the window to end a speech segment.
            Overrides the server default (or the high_vad_sensitivity preset) when provided.

        negative_frames_window : typing.Optional[str]
            Sliding window size (in frames) over which negative frames are counted.
            Overrides the server default (or the high_vad_sensitivity preset) when provided.

        start_speech_volume_threshold : typing.Optional[str]
            Volume level (dB) below which audio is considered too quiet to be speech.
            When not provided, no volume-based filtering is applied.

        interrupt_min_speech_frames : typing.Optional[str]
            Minimum speech frames required to register a barge-in / interruption.
            Overrides the server default when provided.

        pre_speech_pad_frames : typing.Optional[str]
            Number of audio frames to prepend before the detected speech onset,
            ensuring the beginning of speech is not clipped.
            Overrides the server default when provided.

        num_initial_ignored_frames : typing.Optional[str]
            Number of leading audio frames to skip entirely at connection start.
            Useful for discarding connection setup noise.
            Overrides the server default when provided.

        vad_signals : typing.Optional[SpeechToTextTranslateStreamingVadSignals]
            Enable VAD signals in response

        flush_signal : typing.Optional[SpeechToTextTranslateStreamingFlushSignal]
            Signal to flush the audio buffer and finalize transcription and translation

        input_audio_codec : typing.Optional[SpeechToTextTranslateStreamingInputAudioCodec]
            Audio codec/format of the input stream. Use this when sending raw PCM audio.
            Supported values: wav, pcm_s16le, pcm_l16, pcm_raw.

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

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

        Returns
        -------
        AsyncSpeechToTextTranslateStreamingSocketClient
        """
        ws_url = self._raw_client._client_wrapper.get_environment().production + "/speech-to-text-translate/ws"
        _encoded_query_params = encode_query(
            jsonable_encoder(
                remove_none_from_dict(
                    {
                        "model": model,
                        "mode": mode,
                        "sample_rate": sample_rate,
                        "high_vad_sensitivity": high_vad_sensitivity,
                        "positive_speech_threshold": positive_speech_threshold,
                        "negative_speech_threshold": negative_speech_threshold,
                        "min_speech_frames": min_speech_frames,
                        "first_turn_min_speech_frames": first_turn_min_speech_frames,
                        "negative_frames_count": negative_frames_count,
                        "negative_frames_window": negative_frames_window,
                        "start_speech_volume_threshold": start_speech_volume_threshold,
                        "interrupt_min_speech_frames": interrupt_min_speech_frames,
                        "pre_speech_pad_frames": pre_speech_pad_frames,
                        "num_initial_ignored_frames": num_initial_ignored_frames,
                        "vad_signals": vad_signals,
                        "flush_signal": flush_signal,
                        "input_audio_codec": input_audio_codec,
                        **(
                            request_options.get("additional_query_parameters", {}) or {}
                            if request_options is not None
                            else {}
                        ),
                    }
                )
            )
        )
        if _encoded_query_params:
            ws_url = ws_url + "?" + urllib.parse.urlencode(_encoded_query_params)
        headers = self._raw_client._client_wrapper.get_headers()
        if api_subscription_key is not None:
            headers["Api-Subscription-Key"] = str(api_subscription_key)
        if request_options and "additional_headers" in request_options:
            headers.update(request_options["additional_headers"])
        try:
            async with websockets_client_connect(ws_url, extra_headers=headers) as protocol:
                yield AsyncSpeechToTextTranslateStreamingSocketClient(websocket=protocol)
        except InvalidWebSocketStatus as exc:
            status_code: int = get_status_code(exc)
            if status_code == 401:
                raise ApiError(
                    status_code=status_code,
                    headers=dict(headers),
                    body="Websocket initialized with invalid credentials.",
                )
            raise ApiError(
                status_code=status_code,
                headers=dict(headers),
                body="Unexpected error when initializing websocket connection.",
            )
