WIP read aloud keeps playing across chats
This commit is contained in:
@@ -7,11 +7,18 @@ import 'flutter_tts_speech_engine.dart'
|
||||
import 'speech_document.dart';
|
||||
import 'speech_engine.dart';
|
||||
import 'speech_preferences.dart';
|
||||
import 'speech_reading.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].
|
||||
///
|
||||
/// It belongs to the app (see [speechController]), not to the conversation
|
||||
/// that started it: leaving that chat, switching panes or opening the list
|
||||
/// keeps the reply reading. It ends when stopped, when it finishes, when
|
||||
/// another reply starts, when its chat is closed or deleted ([stopScope]),
|
||||
/// and when the app goes to the background or closes.
|
||||
class SpeechController extends ChangeNotifier with WidgetsBindingObserver {
|
||||
SpeechController({SpeechEngine? engine, SpeechPreferencesStore? store})
|
||||
: engine = engine ?? createSpeechEngine(),
|
||||
@@ -44,6 +51,10 @@ class SpeechController extends ChangeNotifier with WidgetsBindingObserver {
|
||||
/// The range of the word being spoken in [document]'s text, or null.
|
||||
final word = ValueNotifier<SpeechRange?>(null);
|
||||
|
||||
/// The conversation whose reply is reading (paused included), or null.
|
||||
/// It changes only when a reading starts, ends or its chat is renamed.
|
||||
final reading = ValueNotifier<SpeechReading?>(null);
|
||||
|
||||
bool get supported => engine.supported;
|
||||
SpeechState get state => _state;
|
||||
String? get messageId => _messageId;
|
||||
@@ -64,12 +75,25 @@ class SpeechController extends ChangeNotifier with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
/// Reads reply [id] made of markdown [parts], stopping any other reading.
|
||||
Future<void> read(String id, List<String> parts) async {
|
||||
/// [scope] is the reply's conversation (by default the part of [id] before
|
||||
/// its first `/`, see [speechMessageId]) and [title] its name, which the
|
||||
/// speaking indicator and the global Stop show.
|
||||
Future<void> read(
|
||||
String id,
|
||||
List<String> parts, {
|
||||
String? scope,
|
||||
String title = '',
|
||||
}) async {
|
||||
final document = speechDocumentFor(parts);
|
||||
await stop();
|
||||
await restore();
|
||||
if (document.text.isEmpty) return;
|
||||
await _speak(id, document, _voice);
|
||||
await _speak(
|
||||
id,
|
||||
document,
|
||||
_voice,
|
||||
SpeechReading(scope: scope ?? speechScopeOf(id), title: title),
|
||||
);
|
||||
}
|
||||
|
||||
/// Speaks a short sample in [voice] (or the chosen one) at the chosen rate.
|
||||
@@ -80,12 +104,19 @@ class SpeechController extends ChangeNotifier with WidgetsBindingObserver {
|
||||
speechSampleId,
|
||||
speechDocumentFor(['This is how replies will sound when read aloud.']),
|
||||
voice ?? _voice,
|
||||
null,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _speak(String id, SpeechDocument document, SpeechVoice? voice) {
|
||||
Future<void> _speak(
|
||||
String id,
|
||||
SpeechDocument document,
|
||||
SpeechVoice? voice,
|
||||
SpeechReading? origin,
|
||||
) {
|
||||
_messageId = id;
|
||||
_document = document;
|
||||
reading.value = origin;
|
||||
_awaitingStart = true;
|
||||
word.value = null;
|
||||
_set(SpeechState.speaking);
|
||||
@@ -131,13 +162,24 @@ class SpeechController extends ChangeNotifier with WidgetsBindingObserver {
|
||||
_clear();
|
||||
}
|
||||
|
||||
/// Stops reading when the reply being read has an id under [scope].
|
||||
/// Stops reading when the reply being read belongs to conversation
|
||||
/// [scope]: the chat was closed or deleted.
|
||||
Future<void> stopScope(String scope) async {
|
||||
if (_messageId?.startsWith(speechScopePrefix(scope)) ?? false) {
|
||||
if (reading.value?.scope == scope ||
|
||||
(_messageId?.startsWith(speechScopePrefix(scope)) ?? false)) {
|
||||
await stop();
|
||||
}
|
||||
}
|
||||
|
||||
/// Follows a rename of the conversation being read.
|
||||
void retitle(String scope, String title) {
|
||||
final current = reading.value;
|
||||
if (current == null || current.scope != scope || current.title == title) {
|
||||
return;
|
||||
}
|
||||
reading.value = SpeechReading(scope: scope, title: title);
|
||||
}
|
||||
|
||||
/// 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);
|
||||
@@ -219,6 +261,7 @@ class SpeechController extends ChangeNotifier with WidgetsBindingObserver {
|
||||
_messageId = null;
|
||||
_document = null;
|
||||
word.value = null;
|
||||
reading.value = null;
|
||||
_state = SpeechState.idle;
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -247,6 +290,7 @@ class SpeechController extends ChangeNotifier with WidgetsBindingObserver {
|
||||
..onProgress = null;
|
||||
if (_state != SpeechState.idle) unawaited(engine.stop().catchError((_) {}));
|
||||
word.dispose();
|
||||
reading.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -260,13 +304,8 @@ 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;
|
||||
/// The conversation part of a reader id made by [speechMessageId].
|
||||
String speechScopeOf(String id) {
|
||||
final slash = id.indexOf('/');
|
||||
return slash < 0 ? id : id.substring(0, slash);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user