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

import typing

from ....core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ....core.request_options import RequestOptions
from .raw_client import AsyncRawAudioClient, RawAudioClient
from .types.audio_generate_request_callback_method import AudioGenerateRequestCallbackMethod
from .types.audio_generate_request_container import AudioGenerateRequestContainer
from .types.audio_generate_request_encoding import AudioGenerateRequestEncoding

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class AudioClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawAudioClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawAudioClient
        """
        return self._raw_client

    def generate(
        self,
        *,
        model: str,
        text: str,
        callback: typing.Optional[str] = None,
        callback_method: typing.Optional[AudioGenerateRequestCallbackMethod] = None,
        mip_opt_out: typing.Optional[bool] = None,
        tag: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        bit_rate: typing.Optional[int] = None,
        container: typing.Optional[AudioGenerateRequestContainer] = None,
        encoding: typing.Optional[AudioGenerateRequestEncoding] = None,
        expressivity: typing.Optional[int] = None,
        sample_rate: typing.Optional[int] = None,
        speed: typing.Optional[float] = None,
        priority: typing.Optional[typing.Literal["low"]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Iterator[bytes]:
        """
        Synthesize a complete block of text into a single audio response using Deepgram's Flux TTS batch (REST) API. Use this for pre-rendering fixed audio (IVR prompts, notifications, narration) where the whole text is known up front and you don't need incremental playback or interruption.

        Parameters
        ----------
        model : str
            Flux TTS model used to synthesize the submitted text, in the form `flux-{voice}-{language}` (for example, `flux-alexis-en`). Required; unlike the v1 (Aura) endpoint there is no default and only flux models are accepted. English-only at launch.

        text : str
            The text content to be converted to speech. The server normalizes and preprocesses the text before synthesis. Inline pause and pronunciation controls are not yet applied; they are stripped from the text before synthesis.

        callback : typing.Optional[str]
            URL to which we'll make the callback request

        callback_method : typing.Optional[AudioGenerateRequestCallbackMethod]
            HTTP method by which the callback request will be made

        mip_opt_out : typing.Optional[bool]
            Opts out requests from the Deepgram Model Improvement Program. Refer to our Docs for pricing impacts before setting this to true. https://dpgr.am/deepgram-mip

        tag : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Label your requests for the purpose of identification during usage reporting

        bit_rate : typing.Optional[int]
            The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type.

        container : typing.Optional[AudioGenerateRequestContainer]
            Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.

        encoding : typing.Optional[AudioGenerateRequestEncoding]
            Encoding allows you to specify the expected encoding of your audio output

        expressivity : typing.Optional[int]
            Expressive range of the generated speech, on a calm-to-animated axis. Accepted values: `-2`, `-1`, `0`, `1`, `2`. `0` (the default) is the voice's tuned delivery and the production-validated setting, with `-2` the calm end of the range and `2` the animated end. Supported on all Flux voices; applies to the whole request. Beta: behavior may change in future model versions, and non-default values increase the risk of hallucinations and pronunciation errors; audition before shipping. An invalid value is rejected with a `400` — `EXPRESSIVITY_OUT_OF_RANGE` for a value outside the range, `EXPRESSIVITY_INCREMENT_INVALID` for a fractional value. See [Expressivity](/docs/tts-expressivity).

        sample_rate : typing.Optional[int]
            Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable

        speed : typing.Optional[float]
            Speaking rate multiplier that adjusts the pace of generated speech while preserving natural prosody and voice quality. Accepted values run `0.85` to `1.15` in `0.05` increments. Not yet supported in all languages.

        priority : typing.Optional[typing.Literal["low"]]
            Processing priority for asynchronous (callback) requests. The only supported value is low.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

        Returns
        -------
        typing.Iterator[bytes]
            Returns the synthesized audio in the requested encoding as a binary stream. When a `callback` URL is supplied, the request is processed asynchronously and the response body is instead a JSON acknowledgement (Content-Type `application/json`) of the form {"request_id": "..."}, with the audio delivered to the callback URL. Because this endpoint is typed as a binary audio stream, SDK callers that set `callback` receive this JSON acknowledgement through the audio byte iterator as raw bytes and must join the chunks and parse `request_id` themselves.

        Examples
        --------
        from deepgram import DeepgramClient

        client = DeepgramClient(
            api_key="YOUR_API_KEY",
        )
        client.speak.v2.audio.generate(
            model="model",
            text="text",
        )
        """
        with self._raw_client.generate(
            model=model,
            text=text,
            callback=callback,
            callback_method=callback_method,
            mip_opt_out=mip_opt_out,
            tag=tag,
            bit_rate=bit_rate,
            container=container,
            encoding=encoding,
            expressivity=expressivity,
            sample_rate=sample_rate,
            speed=speed,
            priority=priority,
            request_options=request_options,
        ) as r:
            yield from r.data


class AsyncAudioClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawAudioClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawAudioClient
        """
        return self._raw_client

    async def generate(
        self,
        *,
        model: str,
        text: str,
        callback: typing.Optional[str] = None,
        callback_method: typing.Optional[AudioGenerateRequestCallbackMethod] = None,
        mip_opt_out: typing.Optional[bool] = None,
        tag: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        bit_rate: typing.Optional[int] = None,
        container: typing.Optional[AudioGenerateRequestContainer] = None,
        encoding: typing.Optional[AudioGenerateRequestEncoding] = None,
        expressivity: typing.Optional[int] = None,
        sample_rate: typing.Optional[int] = None,
        speed: typing.Optional[float] = None,
        priority: typing.Optional[typing.Literal["low"]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.AsyncIterator[bytes]:
        """
        Synthesize a complete block of text into a single audio response using Deepgram's Flux TTS batch (REST) API. Use this for pre-rendering fixed audio (IVR prompts, notifications, narration) where the whole text is known up front and you don't need incremental playback or interruption.

        Parameters
        ----------
        model : str
            Flux TTS model used to synthesize the submitted text, in the form `flux-{voice}-{language}` (for example, `flux-alexis-en`). Required; unlike the v1 (Aura) endpoint there is no default and only flux models are accepted. English-only at launch.

        text : str
            The text content to be converted to speech. The server normalizes and preprocesses the text before synthesis. Inline pause and pronunciation controls are not yet applied; they are stripped from the text before synthesis.

        callback : typing.Optional[str]
            URL to which we'll make the callback request

        callback_method : typing.Optional[AudioGenerateRequestCallbackMethod]
            HTTP method by which the callback request will be made

        mip_opt_out : typing.Optional[bool]
            Opts out requests from the Deepgram Model Improvement Program. Refer to our Docs for pricing impacts before setting this to true. https://dpgr.am/deepgram-mip

        tag : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Label your requests for the purpose of identification during usage reporting

        bit_rate : typing.Optional[int]
            The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type.

        container : typing.Optional[AudioGenerateRequestContainer]
            Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.

        encoding : typing.Optional[AudioGenerateRequestEncoding]
            Encoding allows you to specify the expected encoding of your audio output

        expressivity : typing.Optional[int]
            Expressive range of the generated speech, on a calm-to-animated axis. Accepted values: `-2`, `-1`, `0`, `1`, `2`. `0` (the default) is the voice's tuned delivery and the production-validated setting, with `-2` the calm end of the range and `2` the animated end. Supported on all Flux voices; applies to the whole request. Beta: behavior may change in future model versions, and non-default values increase the risk of hallucinations and pronunciation errors; audition before shipping. An invalid value is rejected with a `400` — `EXPRESSIVITY_OUT_OF_RANGE` for a value outside the range, `EXPRESSIVITY_INCREMENT_INVALID` for a fractional value. See [Expressivity](/docs/tts-expressivity).

        sample_rate : typing.Optional[int]
            Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable

        speed : typing.Optional[float]
            Speaking rate multiplier that adjusts the pace of generated speech while preserving natural prosody and voice quality. Accepted values run `0.85` to `1.15` in `0.05` increments. Not yet supported in all languages.

        priority : typing.Optional[typing.Literal["low"]]
            Processing priority for asynchronous (callback) requests. The only supported value is low.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

        Returns
        -------
        typing.AsyncIterator[bytes]
            Returns the synthesized audio in the requested encoding as a binary stream. When a `callback` URL is supplied, the request is processed asynchronously and the response body is instead a JSON acknowledgement (Content-Type `application/json`) of the form {"request_id": "..."}, with the audio delivered to the callback URL. Because this endpoint is typed as a binary audio stream, SDK callers that set `callback` receive this JSON acknowledgement through the audio byte iterator as raw bytes and must join the chunks and parse `request_id` themselves.

        Examples
        --------
        import asyncio

        from deepgram import AsyncDeepgramClient

        client = AsyncDeepgramClient(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.speak.v2.audio.generate(
                model="model",
                text="text",
            )


        asyncio.run(main())
        """
        async with self._raw_client.generate(
            model=model,
            text=text,
            callback=callback,
            callback_method=callback_method,
            mip_opt_out=mip_opt_out,
            tag=tag,
            bit_rate=bit_rate,
            container=container,
            encoding=encoding,
            expressivity=expressivity,
            sample_rate=sample_rate,
            speed=speed,
            priority=priority,
            request_options=request_options,
        ) as r:
            async for _chunk in r.data:
                yield _chunk
