textToSpeech method

  1. @override
Future<List<int>> textToSpeech(
  1. String text, {
  2. String? voice,
})

Synthesizes spoken audio from text.

text The text to synthesize. voice The identifier for the voice to use. Returns the raw audio bytes (usually MP3).

Implementation

@override
Future<List<int>> textToSpeech(
  String text, {
  String? voice,
}) async {
  final uri = Uri.parse('$_baseUrl/audio/speech');
  final response = await _httpClient
      .post(
        uri,
        headers: _headers(),
        body: jsonEncode({
          'model': 'tts-1',
          'input': text,
          'voice':
              voice ?? 'alloy', // alloy, echo, fable, onyx, nova, shimmer
        }),
      )
      .timeout(config.timeout);

  if (response.statusCode != 200) {
    final json = jsonDecode(response.body) as Map<String, dynamic>;
    throw _parseError(response.statusCode, json);
  }
  return response.bodyBytes;
}