6367cce68a
An assistant reply gets Read aloud beside Copy; while it reads it becomes Pause/Resume and Stop, one reply at a time app-wide. The spoken word is highlighted in the rendered reply (across multi-part replies) and the transcript scrolls only when that word leaves the viewport. Reading stops when its chat closes or is swapped, another reply starts, or the app goes to the background. Markdown is spoken as sentences; code blocks are announced as "Code block.". Speech goes through a SpeechEngine seam with a flutter_tts implementation (Android, macOS, web; hidden on Linux) and a fake for tests. Settings has a Read aloud section: a voice picker grouped by language (device language first, a sample per voice), a rate slider and reset, saved per device. flutter_tts has no Swift package, so macOS/iOS gain the CocoaPods integration (Podfiles with the Runner deployment targets); Android declares the TTS_SERVICE query. The capture fixture gains ?speech=fake.
113 lines
3.6 KiB
Dart
113 lines
3.6 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_tts/flutter_tts.dart';
|
|
|
|
import 'speech_engine.dart';
|
|
|
|
/// [SpeechEngine] over the `flutter_tts` plugin (Android, Apple, web).
|
|
/// The plugin is created on first use so merely building the UI opens no
|
|
/// platform channel.
|
|
class FlutterTtsSpeechEngine extends SpeechEngine {
|
|
FlutterTts? _plugin;
|
|
SpeechVoice? _applied;
|
|
|
|
@override
|
|
bool get supported =>
|
|
kIsWeb ||
|
|
switch (defaultTargetPlatform) {
|
|
TargetPlatform.android ||
|
|
TargetPlatform.iOS ||
|
|
TargetPlatform.macOS ||
|
|
TargetPlatform.windows => true,
|
|
TargetPlatform.linux || TargetPlatform.fuchsia => false,
|
|
};
|
|
|
|
/// AVSpeechSynthesizer's normal rate is 0.5 on a 0..1 scale; Android's
|
|
/// TextToSpeech and the Web Speech API use 1.0 as normal.
|
|
bool get _appleScale =>
|
|
!kIsWeb &&
|
|
(defaultTargetPlatform == TargetPlatform.iOS ||
|
|
defaultTargetPlatform == TargetPlatform.macOS);
|
|
|
|
FlutterTts get _tts => _plugin ??= _create();
|
|
|
|
FlutterTts _create() {
|
|
final tts = FlutterTts();
|
|
tts.setStartHandler(() => onStart?.call());
|
|
tts.setPauseHandler(() => onPause?.call());
|
|
tts.setContinueHandler(() => onResume?.call());
|
|
tts.setCompletionHandler(() => onCompletion?.call());
|
|
tts.setCancelHandler(() => onCancel?.call());
|
|
tts.setErrorHandler((message) => onError?.call('$message'));
|
|
tts.setProgressHandler(
|
|
(utterance, start, end, _) => onProgress?.call(utterance, start, end),
|
|
);
|
|
return tts;
|
|
}
|
|
|
|
@override
|
|
Future<void> speak(String text) async => _tts.speak(text);
|
|
|
|
@override
|
|
Future<void> pause() async => _tts.pause();
|
|
|
|
@override
|
|
Future<void> stop() async => _tts.stop();
|
|
|
|
@override
|
|
Future<void> setRate(double rate) async =>
|
|
_tts.setSpeechRate(_appleScale ? (rate * 0.5).clamp(0, 1) : rate);
|
|
|
|
@override
|
|
Future<void> setVoice(SpeechVoice? voice) async {
|
|
if (voice == _applied) return;
|
|
if (voice != null) {
|
|
await _tts.setVoice({'name': voice.name, 'locale': voice.locale});
|
|
_applied = voice;
|
|
return;
|
|
}
|
|
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
|
|
await _tts.clearVoice();
|
|
_applied = null;
|
|
return;
|
|
}
|
|
// Apple and web plugins cannot clear a chosen voice. After one was chosen
|
|
// in this run (a sample, or a setting that was reset) the device
|
|
// language's first voice stands in for the system default.
|
|
final device = PlatformDispatcher.instance.locale.languageCode;
|
|
final fallback = (await voices())
|
|
.where((item) => item.language == device.toLowerCase())
|
|
.firstOrNull;
|
|
if (fallback == null) return;
|
|
await _tts.setVoice({'name': fallback.name, 'locale': fallback.locale});
|
|
_applied = null;
|
|
}
|
|
|
|
@override
|
|
Future<List<SpeechVoice>> voices() async {
|
|
var voices = await _readVoices();
|
|
// Browsers load their voice list asynchronously after the first request.
|
|
for (var attempt = 0; kIsWeb && voices.isEmpty && attempt < 4; attempt++) {
|
|
await Future<void>.delayed(const Duration(milliseconds: 250));
|
|
voices = await _readVoices();
|
|
}
|
|
return voices;
|
|
}
|
|
|
|
Future<List<SpeechVoice>> _readVoices() async {
|
|
Object? raw;
|
|
try {
|
|
raw = await _tts.getVoices;
|
|
} on PlatformException {
|
|
return const [];
|
|
} on MissingPluginException {
|
|
return const [];
|
|
}
|
|
return [
|
|
for (final item in raw is List ? raw : const [])
|
|
if (item is Map && item['name'] != null && item['locale'] != null)
|
|
SpeechVoice(name: '${item['name']}', locale: '${item['locale']}'),
|
|
];
|
|
}
|
|
}
|