Skip to content

start_speech_synthesis_stream async

start_speech_synthesis_stream(
    engine: Engine,
    output_format: OutputFormat,
    voice_id: VoiceId,
    *,
    config_overrides: Optional[AsyncPollyClientConfig] = None,
    language_code: Optional[LanguageCode] = None,
    lexicon_names: Optional[LexiconNameList] = None,
    sample_rate: Optional[SampleRate] = None,
    action_stream: Optional[
        AsyncIterator[_StartSpeechSynthesisStreamActionStream]
        | _StartSpeechSynthesisStreamActionStream
    ] = None,
) -> AsyncGenerator[StartSpeechSynthesisStreamOutput]

Synthesizes UTF-8 input, plain text, or SSML over a bidirectional streaming connection. Specify synthesis parameters in HTTP/2 headers, send text incrementally as events on the input stream, and receive synthesized audio as it becomes available.

This operation serves as a bidirectional counterpart to SynthesizeSpeech:

Parameters:

  • engine (Engine) –

    Specifies the engine for Amazon Polly to use when processing input text for speech synthesis. Currently, only the generative engine is supported. If you specify a voice that the selected engine doesn't support, Amazon Polly returns an error.

  • language_code (Optional[LanguageCode], default: None ) –

    An optional parameter that sets the language code for the speech synthesis request. Specify this parameter only when using a bilingual voice. If a bilingual voice is used and no language code is specified, Amazon Polly uses the default language of the bilingual voice.

  • lexicon_names (Optional[LexiconNameList], default: None ) –

    The names of one or more pronunciation lexicons for the service to apply during synthesis. Amazon Polly applies lexicons only when the lexicon language matches the voice language.

  • output_format (OutputFormat) –

    The audio format for the synthesized speech. Currently, Amazon Polly does not support JSON speech marks.

  • sample_rate (Optional[SampleRate], default: None ) –

    The audio frequency, specified in Hz.

  • voice_id (VoiceId) –

    The voice to use in synthesis. To get a list of available voice IDs, use the DescribeVoices operation.

  • action_stream (Optional[AsyncIterator[_StartSpeechSynthesisStreamActionStream] | _StartSpeechSynthesisStreamActionStream], default: None ) –

    The input event stream that contains text events and stream control events.

Raises:

Source code in docs_src/capo_polly/_services/async_polly.py
@asynccontextmanager
async def start_speech_synthesis_stream(
    self,
    engine: "capo_polly.types.engine.Engine",
    output_format: "capo_polly.types.output_format.OutputFormat",
    voice_id: "capo_polly.types.voice_id.VoiceId",
    *,
    config_overrides: Optional[AsyncPollyClientConfig] = None,
    language_code: Optional["capo_polly.types.language_code.LanguageCode"] = None,
    lexicon_names: Optional[
        "capo_polly.types.lexicon_name_list.LexiconNameList"
    ] = None,
    sample_rate: Optional["capo_polly.types.sample_rate.SampleRate"] = None,
    action_stream: Optional[
        "AsyncIterator[capo_polly.types.start_speech_synthesis_stream_action_stream._StartSpeechSynthesisStreamActionStream] | capo_polly.types.start_speech_synthesis_stream_action_stream._StartSpeechSynthesisStreamActionStream"
    ] = None,
) -> "AsyncGenerator[capo_polly.types.start_speech_synthesis_stream_output.StartSpeechSynthesisStreamOutput]":
    """<p>Synthesizes UTF-8 input, plain text, or SSML over a bidirectional streaming connection. Specify synthesis parameters in HTTP/2 headers, send text incrementally as events on the input stream, and receive synthesized audio as it becomes available.</p> <p>This operation serves as a bidirectional counterpart to <code>SynthesizeSpeech</code>:</p> <ul> <li> <p> <a href="https://docs.aws.amazon.com/polly/latest/API/API_SynthesizeSpeech.html">SynthesizeSpeech</a> </p> </li> </ul>

    Args:
        engine: <p>Specifies the engine for Amazon Polly to use when processing input text for speech synthesis. Currently, only the <code>generative</code> engine is supported. If you specify a voice that the selected engine doesn't support, Amazon Polly returns an error.</p>
        language_code: <p>An optional parameter that sets the language code for the speech synthesis request. Specify this parameter only when using a bilingual voice. If a bilingual voice is used and no language code is specified, Amazon Polly uses the default language of the bilingual voice.</p>
        lexicon_names: <p>The names of one or more pronunciation lexicons for the service to apply during synthesis. Amazon Polly applies lexicons only when the lexicon language matches the voice language.</p>
        output_format: <p>The audio format for the synthesized speech. Currently, Amazon Polly does not support JSON speech marks.</p>
        sample_rate: <p>The audio frequency, specified in Hz.</p>
        voice_id: <p>The voice to use in synthesis. To get a list of available voice IDs, use the <a href="https://docs.aws.amazon.com/polly/latest/API/API_DescribeVoices.html">DescribeVoices</a> operation.</p>
        action_stream: <p>The input event stream that contains text events and stream control events.</p>

    Raises:
        capo_polly.errors.service_failure_exception.ServiceFailureException: <p>An unknown condition has caused a service failure.</p>
        capo_polly.errors.service_quota_exceeded_exception.ServiceQuotaExceededException: <p>The request would cause a service quota to be exceeded.</p>
        capo_polly.errors.throttling_exception.ThrottlingException: <p>The request was denied because of request throttling.</p>
        capo_polly.errors.validation_exception.ValidationException: <p>The input fails to satisfy the constraints specified by the service.</p>
        capo_polly.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_polly.types.start_speech_synthesis_stream_input.StartSpeechSynthesisStreamInput]",
    ) -> AsyncOperationResponse[
        "capo_polly.types.start_speech_synthesis_stream_output.StartSpeechSynthesisStreamOutput"
    ]:
        import capo_polly._operations.parrot_v1.start_speech_synthesis_stream

        (
            output,
            http_response,
        ) = await capo_polly._operations.parrot_v1.start_speech_synthesis_stream.async_start_speech_synthesis_stream(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_polly.types.start_speech_synthesis_stream_input.StartSpeechSynthesisStreamInput",
    ) -> dict[str, Any]:
        import capo_polly.types.start_speech_synthesis_stream_input

        return (
            capo_polly.types.start_speech_synthesis_stream_input.redact_sensitive(
                input_
            )
        )

    def _redact_output(
        output: "capo_polly.types.start_speech_synthesis_stream_output.StartSpeechSynthesisStreamOutput",
    ) -> dict[str, Any]:
        import capo_polly.types.start_speech_synthesis_stream_output

        return (
            capo_polly.types.start_speech_synthesis_stream_output.redact_sensitive(
                output
            )
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "StartSpeechSynthesisStream",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_polly.types.start_speech_synthesis_stream_input.StartSpeechSynthesisStreamInput = {
        "engine": engine,
        "output_format": output_format,
        "voice_id": voice_id,
    }
    if language_code is not None:
        input_["language_code"] = language_code
    if lexicon_names is not None:
        input_["lexicon_names"] = lexicon_names
    if sample_rate is not None:
        input_["sample_rate"] = sample_rate
    if action_stream is not None:
        input_["action_stream"] = ensure_async_iterator(action_stream)

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    try:
        yield response.output
    finally:
        await response.response.aclose()