GoogleTTSResponse.fromApiResponse constructor

GoogleTTSResponse.fromApiResponse(
  1. Map<String, dynamic> response
)

Create response from Google API response

Implementation

factory GoogleTTSResponse.fromApiResponse(Map<String, dynamic> response) {
  final candidate = response['candidates']?[0];
  final content = candidate?['content'];
  final parts = content?['parts'];
  final inlineData = parts?[0]?['inlineData'];
  final data = inlineData?['data'] as String?;

  if (data == null) {
    throw ArgumentError('No audio data found in response');
  }

  // Decode base64 audio data
  final audioBytes = base64Decode(data);

  return GoogleTTSResponse(
    audioData: audioBytes,
    contentType: inlineData?['mimeType'] as String?,
    usage: response['usageMetadata'] != null
        ? _parseUsageInfo(response['usageMetadata'] as Map<String, dynamic>)
        : null,
    model: response['modelVersion'] as String?,
    metadata: response,
  );
}