3164b7c8df
Rank platform voice quality and select the most natural automatic voice while keeping explicit voice choices intact. Surface natural labels, hide novelty voices in the picker, and guide Apple users to download enhanced voices when needed.
271 lines
7.9 KiB
Dart
271 lines
7.9 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'flutter_tts_speech_engine.dart';
|
|
import 'speech_document.dart';
|
|
import 'speech_engine.dart';
|
|
import 'speech_preferences.dart';
|
|
|
|
enum SpeechState { idle, speaking, paused }
|
|
|
|
/// The app-wide reader: one reply speaks at a time. It notifies on state
|
|
/// changes; the spoken word moves on the separate, frequent [word].
|
|
class SpeechController extends ChangeNotifier with WidgetsBindingObserver {
|
|
SpeechController({SpeechEngine? engine, SpeechPreferencesStore? store})
|
|
: engine = engine ?? FlutterTtsSpeechEngine(),
|
|
_store = store ?? const SharedSpeechPreferencesStore() {
|
|
WidgetsBinding.instance.addObserver(this);
|
|
this.engine
|
|
..onStart = _started
|
|
..onPause = (() => _set(SpeechState.paused))
|
|
..onResume = (() => _set(SpeechState.speaking))
|
|
..onCompletion = _ended
|
|
..onCancel = _ended
|
|
..onError = _failed
|
|
..onProgress = _progress;
|
|
}
|
|
|
|
final SpeechEngine engine;
|
|
final SpeechPreferencesStore _store;
|
|
|
|
SpeechState _state = SpeechState.idle;
|
|
String? _messageId;
|
|
SpeechDocument? _document;
|
|
Completer<void>? _stopping;
|
|
Future<void>? _restoring;
|
|
var _awaitingStart = false;
|
|
|
|
double _rate = defaultSpeechRate;
|
|
SpeechVoice? _voice;
|
|
bool saveFailed = false;
|
|
|
|
/// The range of the word being spoken in [document]'s text, or null.
|
|
final word = ValueNotifier<SpeechRange?>(null);
|
|
|
|
bool get supported => engine.supported;
|
|
SpeechState get state => _state;
|
|
String? get messageId => _messageId;
|
|
SpeechDocument? get document => _document;
|
|
double get rate => _rate;
|
|
SpeechVoice? get voice => _voice;
|
|
bool get isDefault => _rate == defaultSpeechRate && _voice == null;
|
|
|
|
/// Loads the saved voice and rate once.
|
|
Future<void> restore() => _restoring ??= _load();
|
|
|
|
Future<void> _load() async {
|
|
final saved = await _store.load();
|
|
if (saved == null) return;
|
|
_rate = saved.rate.clamp(minSpeechRate, maxSpeechRate);
|
|
_voice = saved.voice;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Reads reply [id] made of markdown [parts], stopping any other reading.
|
|
Future<void> read(String id, List<String> parts) async {
|
|
final document = speechDocumentFor(parts);
|
|
await stop();
|
|
await restore();
|
|
if (document.text.isEmpty) return;
|
|
await _speak(id, document, _voice);
|
|
}
|
|
|
|
/// Speaks a short sample in [voice] (or the chosen one) at the chosen rate.
|
|
Future<void> sample([SpeechVoice? voice]) async {
|
|
await stop();
|
|
await restore();
|
|
await _speak(
|
|
speechSampleId,
|
|
speechDocumentFor(['This is how replies will sound when read aloud.']),
|
|
voice ?? _voice,
|
|
);
|
|
}
|
|
|
|
Future<void> _speak(String id, SpeechDocument document, SpeechVoice? voice) {
|
|
_messageId = id;
|
|
_document = document;
|
|
_awaitingStart = true;
|
|
word.value = null;
|
|
_set(SpeechState.speaking);
|
|
return _guard(() async {
|
|
await engine.setRate(_rate);
|
|
final selected = voice ??
|
|
bestSpeechVoice(
|
|
await engine.voices(),
|
|
WidgetsBinding.instance.platformDispatcher.locale.languageCode,
|
|
);
|
|
await engine.setVoice(selected);
|
|
await engine.speak(document.text);
|
|
});
|
|
}
|
|
|
|
Future<void> pause() async {
|
|
if (_state != SpeechState.speaking) return;
|
|
_set(SpeechState.paused);
|
|
await _guard(engine.pause);
|
|
}
|
|
|
|
/// Continues a paused reply where it stopped; every platform resumes when
|
|
/// the paused utterance's text is spoken again.
|
|
Future<void> resume() async {
|
|
final document = _document;
|
|
if (_state != SpeechState.paused || document == null) return;
|
|
_set(SpeechState.speaking);
|
|
await _guard(() => engine.speak(document.text));
|
|
}
|
|
|
|
Future<void> stop() async {
|
|
if (_state == SpeechState.idle) return;
|
|
final stopping = _stopping = Completer<void>();
|
|
await _guard(engine.stop);
|
|
// Wait briefly for the platform's cancel so it cannot end the next reply
|
|
// and so web speech is idle before the next utterance is queued.
|
|
await stopping.future.timeout(
|
|
const Duration(milliseconds: 400),
|
|
onTimeout: () {},
|
|
);
|
|
if (identical(_stopping, stopping)) _stopping = null;
|
|
_clear();
|
|
}
|
|
|
|
/// Stops reading when the reply being read has an id under [scope].
|
|
Future<void> stopScope(String scope) async {
|
|
if (_messageId?.startsWith(speechScopePrefix(scope)) ?? false) {
|
|
await stop();
|
|
}
|
|
}
|
|
|
|
/// Sets the rate for the next reading; [save] false previews a slider drag.
|
|
Future<void> setRate(double value, {bool save = true}) async {
|
|
_rate = value.clamp(minSpeechRate, maxSpeechRate);
|
|
notifyListeners();
|
|
if (save) await _save();
|
|
}
|
|
|
|
Future<void> setVoice(SpeechVoice? value) async {
|
|
_voice = value;
|
|
notifyListeners();
|
|
await _save();
|
|
}
|
|
|
|
Future<void> reset() async {
|
|
_rate = defaultSpeechRate;
|
|
_voice = null;
|
|
notifyListeners();
|
|
await _save();
|
|
}
|
|
|
|
Future<void> _save() async {
|
|
saveFailed = !await _store.save((rate: _rate, voice: _voice));
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<List<SpeechVoice>> voices() => engine.voices();
|
|
|
|
void _started() {
|
|
_awaitingStart = false;
|
|
if (_messageId != null) _set(SpeechState.speaking);
|
|
}
|
|
|
|
void _ended() {
|
|
final stopping = _stopping;
|
|
if (stopping != null) {
|
|
if (!stopping.isCompleted) stopping.complete();
|
|
return;
|
|
}
|
|
// A late cancel from the previous utterance must not end the new one.
|
|
if (_awaitingStart) return;
|
|
_clear();
|
|
}
|
|
|
|
void _failed(String message) {
|
|
if (message == 'interrupted' || message == 'canceled') return _ended();
|
|
final stopping = _stopping;
|
|
if (stopping != null && !stopping.isCompleted) stopping.complete();
|
|
_clear();
|
|
}
|
|
|
|
void _progress(String utterance, int start, int end) {
|
|
final document = _document;
|
|
if (document == null || _state == SpeechState.idle) return;
|
|
// After a resume a platform may speak only the remaining suffix.
|
|
final base = document.text.endsWith(utterance)
|
|
? document.text.length - utterance.length
|
|
: 0;
|
|
_awaitingStart = false;
|
|
word.value = (start: start + base, end: end + base);
|
|
}
|
|
|
|
Future<void> _guard(Future<void> Function() action) async {
|
|
try {
|
|
await action();
|
|
} catch (error) {
|
|
debugPrint('Read aloud failed: $error');
|
|
_clear();
|
|
}
|
|
}
|
|
|
|
void _set(SpeechState value) {
|
|
if (_state == value) return;
|
|
_state = value;
|
|
notifyListeners();
|
|
}
|
|
|
|
void _clear() {
|
|
_awaitingStart = false;
|
|
_messageId = null;
|
|
_document = null;
|
|
word.value = null;
|
|
_state = SpeechState.idle;
|
|
notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
// Inactive is only a lost focus (a desktop window behind another, the
|
|
// Android shade); hidden and paused mean the app is in the background.
|
|
if (state == AppLifecycleState.hidden ||
|
|
state == AppLifecycleState.paused ||
|
|
state == AppLifecycleState.detached) {
|
|
unawaited(stop());
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
engine
|
|
..onStart = null
|
|
..onPause = null
|
|
..onResume = null
|
|
..onCompletion = null
|
|
..onCancel = null
|
|
..onError = null
|
|
..onProgress = null;
|
|
if (_state != SpeechState.idle) unawaited(engine.stop().catchError((_) {}));
|
|
word.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|
|
|
|
/// The reader's id for the Settings voice sample.
|
|
const speechSampleId = 'settings-sample';
|
|
|
|
/// The id prefix for replies of one conversation (see [speechMessageId]).
|
|
String speechScopePrefix(String scope) => '$scope/';
|
|
|
|
/// A reply's id for the reader, unique across conversations.
|
|
String speechMessageId(String scope, String rowId) => '$scope/$rowId';
|
|
|
|
SpeechController? _speechController;
|
|
|
|
/// The app's single reader.
|
|
SpeechController get speechController =>
|
|
_speechController ??= SpeechController();
|
|
|
|
/// Replaces the app's reader (a fake engine in tests and capture fixtures);
|
|
/// null restores the platform one.
|
|
set debugSpeechController(SpeechController? controller) =>
|
|
_speechController = controller;
|