startSpeechSynthesisStream method

Future<StartSpeechSynthesisStreamOutput> startSpeechSynthesisStream({
  1. required Engine engine,
  2. required OutputFormat outputFormat,
  3. required VoiceId voiceId,
  4. StartSpeechSynthesisStreamActionStream? actionStream,
  5. LanguageCode? languageCode,
  6. List<String>? lexiconNames,
  7. String? sampleRate,
})

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:

May throw ServiceFailureException. May throw ServiceQuotaExceededException. May throw ThrottlingException. May throw ValidationException.

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

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

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

Parameter actionStream : The input event stream that contains text events and stream control events.

Parameter languageCode : 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.

Parameter lexiconNames : 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.

Parameter sampleRate : The audio frequency, specified in Hz.

Implementation

Future<StartSpeechSynthesisStreamOutput> startSpeechSynthesisStream({
  required Engine engine,
  required OutputFormat outputFormat,
  required VoiceId voiceId,
  StartSpeechSynthesisStreamActionStream? actionStream,
  LanguageCode? languageCode,
  List<String>? lexiconNames,
  String? sampleRate,
}) async {
  final headers = <String, String>{
    'x-amzn-Engine': engine.value,
    'x-amzn-OutputFormat': outputFormat.value,
    'x-amzn-VoiceId': voiceId.value,
    if (languageCode != null) 'x-amzn-LanguageCode': languageCode.value,
    if (lexiconNames != null)
      'x-amzn-LexiconNames': _s.encodeHttpHeaderList(lexiconNames),
    if (sampleRate != null) 'x-amzn-SampleRate': sampleRate.toString(),
  };
  final response = await _protocol.sendRaw(
    payload: actionStream,
    method: 'POST',
    requestUri: '/v1/synthesisStream',
    headers: headers,
    exceptionFnMap: _exceptionFns,
  );
  final $json = await _s.jsonFromResponse(response);
  return StartSpeechSynthesisStreamOutput(
    eventStream: StartSpeechSynthesisStreamEventStream.fromJson($json),
  );
}