synthesize method

Future<TTSOutput> synthesize(
  1. TTSInput input
)

Implementation

Future<TTSOutput> synthesize(TTSInput input) async {
  if (_configuration == null) {
    throw StateError('SystemTTSService not initialized');
  }

  final completer = Completer<void>();
  // Note: startTime could be used for telemetry/metrics in the future

  // Set up completion handlers for this synthesis
  _flutterTts.setCompletionHandler(() {
    if (!completer.isCompleted) completer.complete();
  });

  _flutterTts.setErrorHandler((msg) {
    if (!completer.isCompleted) completer.complete();
  });

  // Get text to synthesize
  final text = input.text;

  // Configure voice
  final voice = input.voiceId ?? _configuration!.voice;
  final language = _configuration!.language;

  if (voice != 'system') {
    await _flutterTts.setVoice({
      'name': voice,
      'locale': language,
    });
  } else {
    await _flutterTts.setLanguage(language);
  }

  // Configure speech parameters
  await _flutterTts.setSpeechRate(_configuration!.speakingRate);
  await _flutterTts.setPitch(_configuration!.pitch);
  await _flutterTts.setVolume(_configuration!.volume);

  // Speak the text
  await _flutterTts.speak(text);

  // Wait for synthesis to complete
  await completer.future;

  // Note: flutter_tts doesn't provide direct audio data access
  // It plays audio directly through the system
  return TTSOutput(
    audioData: const [],
    format: _configuration!.audioFormat,
    sampleRate: 22050,
  );
}