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:

To use a custom voice, specify its ID in VoiceId. The voice must be in active status. If the voice is in pending or failed status, the request fails with ConflictException. If no custom voice with the specified ID exists in your account and Region, the request fails with ResourceNotFoundException. If the custom voice uses a customer managed key, you need kms:Decrypt permission on the key. If you disabled the key or scheduled it for deletion, the request fails with ValidationException. If you don't have permission to use the key, the request fails with AccessDeniedException. If Amazon Web Services KMS throttles Amazon Polly's request to use the key, the request fails with ThrottlingException.

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 ID to use for the synthesis. To get a list of available voice IDs, use the DescribeVoices operation. To use a custom voice, specify the voice ID that CreateVoice returned, for example, vc-9a8fa85dd3. You can't specify the ARN of a voice.

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

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

Raises:

  • ConflictException –

    The request conflicts with the current state of the resource. For a custom voice, the voice isn't in the status that the operation requires. Check the status with GetVoice, and then try again.

  • ResourceNotFoundException –

    Amazon Polly can't find the specified resource. Verify that the resource exists in the Amazon Web Services account and Region of the request and that its ARN or ID is spelled correctly, and then try again.

  • ServiceFailureException –

    An unknown condition has caused a service failure.

  • ServiceQuotaExceededException –

    The request would cause a service quota to be exceeded.

  • ThrottlingException –

    The request was denied because of request throttling.

  • ValidationException –

    The input fails to satisfy the constraints specified by the service.

  • UnknownServiceError –

    The service returned an error code this client does not model.

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/dg/API_SynthesizeSpeech.html">SynthesizeSpeech</a> </p> </li> </ul> <p>To use a custom voice, specify its ID in <code>VoiceId</code>. The voice must be in <code>active</code> status. If the voice is in <code>pending</code> or <code>failed</code> status, the request fails with <code>ConflictException</code>. If no custom voice with the specified ID exists in your account and Region, the request fails with <code>ResourceNotFoundException</code>. If the custom voice uses a customer managed key, you need <code>kms:Decrypt</code> permission on the key. If you disabled the key or scheduled it for deletion, the request fails with <code>ValidationException</code>. If you don't have permission to use the key, the request fails with <code>AccessDeniedException</code>. If Amazon Web Services KMS throttles Amazon Polly's request to use the key, the request fails with <code>ThrottlingException</code>.</p>

    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 ID to use for the synthesis. To get a list of available voice IDs, use the <a href="https://docs.aws.amazon.com/polly/latest/dg/API_DescribeVoices.html">DescribeVoices</a> operation. To use a custom voice, specify the voice ID that <code>CreateVoice</code> returned, for example, <code>vc-9a8fa85dd3</code>. You can't specify the ARN of a voice.</p>
        action_stream: <p>The input event stream that contains text events and stream control events.</p>

    Raises:
        capo_polly.errors.conflict_exception.ConflictException: <p>The request conflicts with the current state of the resource. For a custom voice, the voice isn't in the status that the operation requires. Check the status with <code>GetVoice</code>, and then try again.</p>
        capo_polly.errors.resource_not_found_exception.ResourceNotFoundException: <p>Amazon Polly can't find the specified resource. Verify that the resource exists in the Amazon Web Services account and Region of the request and that its ARN or ID is spelled correctly, and then try again.</p>
        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
        )
        awatch_event_stream(output, "event_stream", req.options)
        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()