textToSpeech method

Future<File> textToSpeech({
  1. required String text,
  2. String voiceName = 'de-DE-Wavenet-D',
  3. String audioEncoding = 'MP3',
  4. String languageCode = 'de-DE',
  5. double pitch = 0.00,
  6. double speakingRate = 1.00,
})

Implementation

Future<File> textToSpeech({
  required String text,
  String voiceName = 'de-DE-Wavenet-D',
  String audioEncoding = 'MP3',
  String languageCode = 'de-DE',
  double pitch = 0.00,
  double speakingRate = 1.00,
}) async {
  const endpoint = 'text:synthesize';
  final bodyMap = <String, dynamic>{
    "input": {
      "text": text,
    },
    "voice": {
      "languageCode": languageCode,
      "name": voiceName,
    },
    "audioConfig": {
      "audioEncoding": audioEncoding,
      "pitch": pitch,
      "speakingRate": speakingRate,
    },
  };

  String body = jsonEncode(bodyMap);
  Future request = http.post(_getApiUrl(endpoint), body: body);

  try {
    var response =
        await _getResponse(request.then((value) => value as http.Response));
    AudioResponse audioResponse = AudioResponse.fromJson(response);
    return _createMp3File(audioResponse);
  } catch (e) {
    throw (e);
  }
}