import 'dart:async'; import 'package:pipelines_app/features/chat/speech/speech.dart'; /// Opt-in (`?speech=fake`) scripted reader for captures: headless browsers /// have no speech voices, so this engine "speaks" one word every /// `?speechStep=` ms (default 600) and reports each as progress, which drives /// the real controller, buttons, highlight and reveal scrolling. void installSpeechFixture(Map params) { if (params['speech'] != 'fake') return; final step = int.tryParse(params['speechStep'] ?? '') ?? 600; final hint = params['speechHint'] == '1'; final empty = params['speechEmpty'] == '1'; final failure = params['speechFailure'] == '1'; debugSpeechController = SpeechController( engine: _ScriptedSpeechEngine( Duration(milliseconds: step), appleHint: hint, empty: empty, failure: failure, ), ); } class _ScriptedSpeechEngine extends SpeechEngine { _ScriptedSpeechEngine( this.step, { this.appleHint = false, this.empty = false, this.failure = false, }); final Duration step; final bool appleHint; final bool empty; final bool failure; Timer? _timer; String _text = ''; List _words = const []; var _next = 0; var _paused = false; @override bool get supported => true; @override bool get supportsVoiceDownloadHint => appleHint; @override Future speak(String text) async { if (_paused && text == _text) { _paused = false; onResume?.call(); _run(); return; } _text = text; _words = RegExp(r'\S+').allMatches(text).toList(); _next = 0; _paused = false; Timer.run(() => onStart?.call()); _run(); } void _run() { _timer?.cancel(); _emit(); _timer = Timer.periodic(step, (_) => _emit()); } void _emit() { if (_next >= _words.length) { _timer?.cancel(); onCompletion?.call(); return; } final word = _words[_next++]; onProgress?.call(_text, word.start, word.end); } @override Future pause() async { _timer?.cancel(); _paused = true; if (_next > 0) _next -= 1; onPause?.call(); } @override Future stop() async { _timer?.cancel(); _paused = false; onCancel?.call(); } @override Future setRate(double rate) async {} @override Future setVoice(SpeechVoice? voice) async {} @override Future> voices() async { if (failure) throw StateError('fixture voice inventory failed'); if (empty) return const []; return [ const SpeechVoice( name: 'Samantha Compact', locale: 'en-US', qualityRank: 1, ), if (!appleHint) const SpeechVoice( name: 'Samantha Enhanced', locale: 'en-US', qualityRank: 2, ), if (!appleHint) const SpeechVoice( name: 'Microsoft Aria Online (Natural)', locale: 'en-US', qualityRank: 3, ), const SpeechVoice(name: 'Bells', locale: 'en-US', qualityRank: 1), const SpeechVoice(name: 'Daniel', locale: 'en-GB', qualityRank: 1), const SpeechVoice(name: 'Thomas', locale: 'fr-FR', qualityRank: 1), const SpeechVoice(name: 'Anna', locale: 'de-DE', qualityRank: 1), const SpeechVoice(name: 'Kyoko', locale: 'ja-JP', qualityRank: 1), ]; } }