Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

my first commit with experimental live transcribe and read along features

Barnabasf256ef371433
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index da791b69..dae1767e 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -105,7 +105,6 @@
android:name="com.dexterous.flutterlocalnotifications.ForegroundService"
android:exported="false"
android:foregroundServiceType="dataSync" />
-
<!-- background_downloader runs downloads in WorkManager's foreground
service. Declare its dataSync type so startForeground doesn't crash
on API 34+ (the library's own declaration has no type). -->
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index b7134bc9..839b5362 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -4823,7 +4823,7 @@
"transcriptionModelBaseDesc": "A good middle ground. About 57 MB.",
"transcriptionModelSmall": "Small",
"transcriptionModelSmallDesc": "Slower but most accurate - best on a high-end phone. About 182 MB.",
- "transcriptionAutoHint": "With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.",
+ "transcriptionAutoHint": "With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.",
"transcriptionDownload": "Download",
"transcriptionDownloadFailed": "Download failed. Check your connection and try again.",
"transcribe": "Transcribe",
@@ -4842,6 +4842,29 @@
"transcriptionPauseSnippet": "Pause",
"transcriptionIntroBody": "This listens to the chosen amount of audio, starting just before the bookmark, and turns it into text on your device. Longer clips take longer, and transcription isn't 100% accurate. The text is saved into the bookmark's note when it's done, ready to fix up or share.",
"transcriptionUseEbookText": "Use the ebook's exact text when it can be matched",
+ "lyricsMode": "Live transcript",
+ "lyricsListeningAhead": "Listening ahead...",
+ "lyricsDisplaySection": "Live transcript",
+ "lyricsFontSize": "Line size",
+ "lyricsMaxLines": "Lines shown",
+ "lyricsFullCover": "Use the whole cover",
+ "lyricsFullCoverHint": "Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.",
+ "lyricsBatteryInfo": "Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.",
+ "lyricsIntroBody": "This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.",
+ "lyricsBuildingLead": "Getting ahead of the narration... {percent}",
+ "@lyricsBuildingLead": { "placeholders": { "percent": { "type": "String" } } },
+ "lyricsCantKeepUp": "Can't transcribe as fast as you're listening at {speed}",
+ "@lyricsCantKeepUp": { "placeholders": { "speed": { "type": "String" } } },
+ "lyricsTurnOn": "Turn on",
+ "readAlong": "Read along",
+ "readAlongNeedsPlaying": "Play this book's audiobook first",
+ "transcriptionNeedsDownload": "This needs the audio on your phone. Download it now?",
+ "transcriptionDownloadStarted": "Downloading. Turn this on again when it finishes",
+ "readAlongFollow": "Read along follows",
+ "readAlongFollowHint": "Word timing is worked out between the transcript's timestamps, so it can drift slightly inside a sentence.",
+ "readAlongFollowWord": "Word",
+ "readAlongFollowSentence": "Sentence",
+ "readAlongColor": "Read along color",
"findInEbook": "Find in ebook",
"findInEbookSearching": "Finding this spot in the ebook...",
"findInEbookNotFound": "Couldn't find this spot in the ebook.",
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 294f007a..67b35e3f 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -14616,7 +14616,7 @@ abstract class AppLocalizations {
/// No description provided for @transcriptionAutoHint.
///
/// In en, this message translates to:
- /// **'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.'**
+ /// **'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.'**
String get transcriptionAutoHint;
/// No description provided for @transcriptionDownload.
@@ -14727,6 +14727,132 @@ abstract class AppLocalizations {
/// **'Use the ebook\'s exact text when it can be matched'**
String get transcriptionUseEbookText;
+ /// No description provided for @lyricsMode.
+ ///
+ /// In en, this message translates to:
+ /// **'Live transcript'**
+ String get lyricsMode;
+
+ /// No description provided for @lyricsListeningAhead.
+ ///
+ /// In en, this message translates to:
+ /// **'Listening ahead...'**
+ String get lyricsListeningAhead;
+
+ /// No description provided for @lyricsDisplaySection.
+ ///
+ /// In en, this message translates to:
+ /// **'Live transcript'**
+ String get lyricsDisplaySection;
+
+ /// No description provided for @lyricsFontSize.
+ ///
+ /// In en, this message translates to:
+ /// **'Line size'**
+ String get lyricsFontSize;
+
+ /// No description provided for @lyricsMaxLines.
+ ///
+ /// In en, this message translates to:
+ /// **'Lines shown'**
+ String get lyricsMaxLines;
+
+ /// No description provided for @lyricsFullCover.
+ ///
+ /// In en, this message translates to:
+ /// **'Use the whole cover'**
+ String get lyricsFullCover;
+
+ /// No description provided for @lyricsFullCoverHint.
+ ///
+ /// In en, this message translates to:
+ /// **'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.'**
+ String get lyricsFullCoverHint;
+
+ /// No description provided for @lyricsBatteryInfo.
+ ///
+ /// In en, this message translates to:
+ /// **'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.'**
+ String get lyricsBatteryInfo;
+
+ /// No description provided for @lyricsIntroBody.
+ ///
+ /// In en, this message translates to:
+ /// **'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.'**
+ String get lyricsIntroBody;
+
+ /// No description provided for @lyricsBuildingLead.
+ ///
+ /// In en, this message translates to:
+ /// **'Getting ahead of the narration... {percent}'**
+ String lyricsBuildingLead(String percent);
+
+ /// No description provided for @lyricsCantKeepUp.
+ ///
+ /// In en, this message translates to:
+ /// **'Can\'t transcribe as fast as you\'re listening at {speed}'**
+ String lyricsCantKeepUp(String speed);
+
+ /// No description provided for @lyricsTurnOn.
+ ///
+ /// In en, this message translates to:
+ /// **'Turn on'**
+ String get lyricsTurnOn;
+
+ /// No description provided for @readAlong.
+ ///
+ /// In en, this message translates to:
+ /// **'Read along'**
+ String get readAlong;
+
+ /// No description provided for @readAlongNeedsPlaying.
+ ///
+ /// In en, this message translates to:
+ /// **'Play this book\'s audiobook first'**
+ String get readAlongNeedsPlaying;
+
+ /// No description provided for @transcriptionNeedsDownload.
+ ///
+ /// In en, this message translates to:
+ /// **'This needs the audio on your phone. Download it now?'**
+ String get transcriptionNeedsDownload;
+
+ /// No description provided for @transcriptionDownloadStarted.
+ ///
+ /// In en, this message translates to:
+ /// **'Downloading. Turn this on again when it finishes'**
+ String get transcriptionDownloadStarted;
+
+ /// No description provided for @readAlongFollow.
+ ///
+ /// In en, this message translates to:
+ /// **'Read along follows'**
+ String get readAlongFollow;
+
+ /// No description provided for @readAlongFollowHint.
+ ///
+ /// In en, this message translates to:
+ /// **'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.'**
+ String get readAlongFollowHint;
+
+ /// No description provided for @readAlongFollowWord.
+ ///
+ /// In en, this message translates to:
+ /// **'Word'**
+ String get readAlongFollowWord;
+
+ /// No description provided for @readAlongFollowSentence.
+ ///
+ /// In en, this message translates to:
+ /// **'Sentence'**
+ String get readAlongFollowSentence;
+
+ /// No description provided for @readAlongColor.
+ ///
+ /// In en, this message translates to:
+ /// **'Read along color'**
+ String get readAlongColor;
+
/// No description provided for @findInEbook.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index 1802df64..f7f6e9c0 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -8571,7 +8571,7 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8636,6 +8636,79 @@ class AppLocalizationsDe extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index da48fd17..336d9370 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -8459,7 +8459,7 @@ class AppLocalizationsEl extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8524,6 +8524,79 @@ class AppLocalizationsEl extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index 78f8654d..cc580ffe 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -8459,7 +8459,7 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8524,6 +8524,79 @@ class AppLocalizationsEn extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 69a831cb..a57ce08a 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -8461,7 +8461,7 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8526,6 +8526,79 @@ class AppLocalizationsEs extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index 5378b634..10571978 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -8508,7 +8508,7 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8573,6 +8573,79 @@ class AppLocalizationsFr extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index 2913909c..29724395 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -8459,7 +8459,7 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8524,6 +8524,79 @@ class AppLocalizationsIt extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index d50c0924..c718e078 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -8459,7 +8459,7 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8524,6 +8524,79 @@ class AppLocalizationsJa extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index 6ece2f2e..8e723067 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -8459,7 +8459,7 @@ class AppLocalizationsNo extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8524,6 +8524,79 @@ class AppLocalizationsNo extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index d7aff842..239ef21f 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -8459,7 +8459,7 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8524,6 +8524,79 @@ class AppLocalizationsPt extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index bc4636f2..3b281431 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -8459,7 +8459,7 @@ class AppLocalizationsRo extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8524,6 +8524,79 @@ class AppLocalizationsRo extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index c2ef7335..beceb882 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -8464,7 +8464,7 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8529,6 +8529,79 @@ class AppLocalizationsRu extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index 85257b06..e1fc7ffe 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -8367,7 +8367,7 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get transcriptionAutoHint =>
- 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Bookmarks on books without one get the most accurate.';
+ 'With more than one downloaded, each job picks between them. Books with an ebook get the quickest one, since the exact words come from the ebook anyway. Books without one get the most accurate - except read along, which stays below Small so it can keep up with the narration.';
@override
String get transcriptionDownload => 'Download';
@@ -8432,6 +8432,79 @@ class AppLocalizationsZh extends AppLocalizations {
String get transcriptionUseEbookText =>
'Use the ebook\'s exact text when it can be matched';
+ @override
+ String get lyricsMode => 'Live transcript';
+
+ @override
+ String get lyricsListeningAhead => 'Listening ahead...';
+
+ @override
+ String get lyricsDisplaySection => 'Live transcript';
+
+ @override
+ String get lyricsFontSize => 'Line size';
+
+ @override
+ String get lyricsMaxLines => 'Lines shown';
+
+ @override
+ String get lyricsFullCover => 'Use the whole cover';
+
+ @override
+ String get lyricsFullCoverHint =>
+ 'Shows the transcript in place of the artwork while it runs, fitting as many lines as there is room for.';
+
+ @override
+ String get lyricsBatteryInfo =>
+ 'Live transcript and read along use extra battery and processing while they run. On phones with slower processors, lines can take longer to appear.';
+
+ @override
+ String get lyricsIntroBody =>
+ 'This listens a little ahead of the narration and shows each line as it is spoken, transcribed on your phone - nothing leaves the device. After turning it on, hold off on pressing play for a few seconds while it gets ahead. The transcript appears once its head start is ready, and starting paused gives it the smoothest run. It uses extra battery and processing while it runs.';
+
+ @override
+ String lyricsBuildingLead(String percent) {
+ return 'Getting ahead of the narration... $percent';
+ }
+
+ @override
+ String lyricsCantKeepUp(String speed) {
+ return 'Can\'t transcribe as fast as you\'re listening at $speed';
+ }
+
+ @override
+ String get lyricsTurnOn => 'Turn on';
+
+ @override
+ String get readAlong => 'Read along';
+
+ @override
+ String get readAlongNeedsPlaying => 'Play this book\'s audiobook first';
+
+ @override
+ String get transcriptionNeedsDownload =>
+ 'This needs the audio on your phone. Download it now?';
+
+ @override
+ String get transcriptionDownloadStarted =>
+ 'Downloading. Turn this on again when it finishes';
+
+ @override
+ String get readAlongFollow => 'Read along follows';
+
+ @override
+ String get readAlongFollowHint =>
+ 'Word timing is worked out between the transcript\'s timestamps, so it can drift slightly inside a sentence.';
+
+ @override
+ String get readAlongFollowWord => 'Word';
+
+ @override
+ String get readAlongFollowSentence => 'Sentence';
+
+ @override
+ String get readAlongColor => 'Read along color';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/screens/transcription_settings_screen.dart b/lib/screens/transcription_settings_screen.dart
index 3bb5be32..abc20526 100644
--- a/lib/screens/transcription_settings_screen.dart
+++ b/lib/screens/transcription_settings_screen.dart
@@ -3,8 +3,22 @@ import 'package:url_launcher/url_launcher.dart';
import '../l10n/app_localizations.dart';
import '../services/player_settings.dart';
import '../services/transcription_service.dart';
+import '../services/lyrics_service.dart';
import '../widgets/overlay_toast.dart';
+/// Colors the spoken words can be painted in. Vivid enough to read on the
+/// light, sepia and dark reader pages alike, and over cover art.
+const _readAlongPalette = [
+ 0xFFFFC400,
+ 0xFFFF6D00,
+ 0xFFFF1744,
+ 0xFFFF4081,
+ 0xFFD500F9,
+ 0xFF2979FF,
+ 0xFF00B8D4,
+ 0xFF00C853,
+];
+
/// Advanced > Bookmark transcription settings: opt-in toggle and on-device
/// Whisper model management (download / pick / delete). Everything here runs
/// locally - no audio or text ever leaves the device.
@@ -20,6 +34,11 @@ class _TranscriptionSettingsScreenState
extends State<TranscriptionSettingsScreen> {
bool _loaded = false;
bool _enabled = false;
+ double _lyricsFontSize = 16;
+ int _lyricsMaxLines = 3;
+ bool _lyricsFullCover = true;
+ String _readAlongMode = 'word';
+ int _readAlongColor = PlayerSettings.defaultReadAlongColor;
final Map<TranscriptionModelSize, bool> _downloaded = {};
TranscriptionModelSize? _downloading;
double _progress = 0;
@@ -32,6 +51,11 @@ class _TranscriptionSettingsScreenState
Future<void> _load() async {
_enabled = await PlayerSettings.getTranscriptionEnabled();
+ _lyricsFontSize = await PlayerSettings.getLyricsFontSize();
+ _lyricsMaxLines = await PlayerSettings.getLyricsMaxLines();
+ _lyricsFullCover = await PlayerSettings.getLyricsFullCover();
+ _readAlongMode = await PlayerSettings.getReadAlongMode();
+ _readAlongColor = await PlayerSettings.getReadAlongColor();
for (final s in TranscriptionModelSize.values) {
_downloaded[s] = await TranscriptionService.instance.isModelDownloaded(s);
}
@@ -202,11 +226,157 @@ class _TranscriptionSettingsScreenState
?.copyWith(color: cs.onSurfaceVariant, height: 1.35),
),
),
+ const Divider(height: 24),
+ Padding(
+ padding: const EdgeInsets.fromLTRB(16, 0, 16, 4),
+ child: Text(l.lyricsDisplaySection,
+ style: tt.titleSmall?.copyWith(color: cs.primary)),
+ ),
+ Padding(
+ padding: const EdgeInsets.fromLTRB(16, 4, 16, 8),
+ child: Container(
+ padding: const EdgeInsets.all(14),
+ decoration: BoxDecoration(
+ color: cs.surfaceContainerHigh,
+ borderRadius: BorderRadius.circular(12),
+ ),
+ child: Row(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Icon(Icons.battery_alert_rounded, size: 18, color: cs.primary),
+ const SizedBox(width: 10),
+ Expanded(
+ child: Text(
+ l.lyricsBatteryInfo,
+ style: tt.bodySmall
+ ?.copyWith(color: cs.onSurfaceVariant, height: 1.4),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ ListTile(
+ title: Text(l.lyricsFontSize),
+ subtitle: Slider(
+ value: _lyricsFontSize,
+ min: 12,
+ max: 24,
+ divisions: 12,
+ label: _lyricsFontSize.toStringAsFixed(0),
+ onChanged: (v) => setState(() => _lyricsFontSize = v),
+ onChangeEnd: (v) async {
+ await PlayerSettings.setLyricsFontSize(v);
+ await LyricsService.instance.reloadDisplayPrefs();
+ },
+ ),
+ trailing: Text(_lyricsFontSize.toStringAsFixed(0),
+ style: tt.titleMedium),
+ ),
+ SwitchListTile(
+ title: Text(l.lyricsFullCover),
+ subtitle: Text(l.lyricsFullCoverHint,
+ style: tt.bodySmall
+ ?.copyWith(color: cs.onSurfaceVariant, height: 1.35)),
+ value: _lyricsFullCover,
+ onChanged: (v) async {
+ setState(() => _lyricsFullCover = v);
+ await PlayerSettings.setLyricsFullCover(v);
+ await LyricsService.instance.reloadDisplayPrefs();
+ },
+ ),
+ // Taking the whole cover fits as many lines as the space
+ // allows, so the count only applies to the strip.
+ if (!_lyricsFullCover)
+ ListTile(
+ title: Text(l.lyricsMaxLines),
+ trailing: SegmentedButton<int>(
+ segments: [
+ for (final n in const [2, 3, 4, 5])
+ ButtonSegment(value: n, label: Text('$n')),
+ ],
+ selected: {_lyricsMaxLines},
+ showSelectedIcon: false,
+ onSelectionChanged: (sel) async {
+ setState(() => _lyricsMaxLines = sel.first);
+ await PlayerSettings.setLyricsMaxLines(sel.first);
+ await LyricsService.instance.reloadDisplayPrefs();
+ },
+ ),
+ ),
+ ListTile(
+ title: Text(l.readAlongFollow),
+ subtitle: Text(l.readAlongFollowHint,
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ trailing: SegmentedButton<String>(
+ segments: [
+ ButtonSegment(
+ value: 'word', label: Text(l.readAlongFollowWord)),
+ ButtonSegment(
+ value: 'sentence',
+ label: Text(l.readAlongFollowSentence)),
+ ],
+ selected: {_readAlongMode},
+ showSelectedIcon: false,
+ onSelectionChanged: (sel) async {
+ setState(() => _readAlongMode = sel.first);
+ await PlayerSettings.setReadAlongMode(sel.first);
+ await LyricsService.instance.reloadDisplayPrefs();
+ },
+ ),
+ ),
+ Padding(
+ padding: const EdgeInsets.fromLTRB(16, 8, 16, 4),
+ child: Text(l.readAlongColor, style: tt.bodyLarge),
+ ),
+ Padding(
+ padding: const EdgeInsets.fromLTRB(16, 4, 16, 20),
+ child: Wrap(
+ spacing: 12,
+ runSpacing: 12,
+ children: [
+ for (final c in _readAlongPalette)
+ _colorSwatch(c, cs),
+ ],
+ ),
+ ),
],
),
);
}
+
+ Widget _colorSwatch(int argb, ColorScheme cs) {
+ final selected = _readAlongColor == argb;
+ return GestureDetector(
+ onTap: () async {
+ setState(() => _readAlongColor = argb);
+ await PlayerSettings.setReadAlongColor(argb);
+ await LyricsService.instance.reloadDisplayPrefs();
+ },
+ child: Container(
+ width: 40,
+ height: 40,
+ decoration: BoxDecoration(
+ color: Color(argb),
+ shape: BoxShape.circle,
+ border: Border.all(
+ color: selected ? cs.onSurface : cs.outlineVariant,
+ width: selected ? 3 : 1,
+ ),
+ ),
+ child: selected
+ ? Icon(Icons.check_rounded,
+ size: 20,
+ color: ThemeData.estimateBrightnessForColor(Color(argb)) ==
+ Brightness.dark
+ ? Colors.white
+ : Colors.black)
+ : null,
+ ),
+ );
+ }
+
Widget _modelTile({
required TranscriptionModelSize size,
required String label,
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 44052bb6..b2bdd9cb 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -1333,6 +1333,17 @@ class AudioPlayerService extends ChangeNotifier {
/// instead of a live `/play` session. Position still syncs via /me/progress.
bool _localSessionMode = false;
+ // Set when a loadOnly playItem skipped starting the local session; the
+ // first real play() begins it, so listening time still gets recorded.
+ ({
+ String progressKey,
+ String itemId,
+ String? episodeId,
+ double duration,
+ String title,
+ String author,
+ })? _pendingLoadOnlySession;
+
/// Externally-pushed signal that the server is currently unreachable.
/// AuthProvider mirrors `serverReachable` here on each ping result so we
/// can short-circuit pre-play server calls (e.g. session creation) for
@@ -2661,6 +2672,11 @@ class AudioPlayerService extends ChangeNotifier {
}
}
+ /// Public view of the BT check, for callers that need to compensate for
+ /// the delay Bluetooth adds between the playhead and your ears.
+ static Future<bool> isBluetoothAudioConnected() =>
+ _isBluetoothAudioConnected();
+
/// True when BT/headphones just disconnected — callers can check before
/// starting new playback to avoid blasting audio on the phone speaker.
static bool get wasNoisyPause => _noisyPause;
@@ -3039,6 +3055,11 @@ class AudioPlayerService extends ChangeNotifier {
// Auto, the widget, headphones or a cold-start restore begin at the
// phone's own position immediately. See play().
bool fromUi = false,
+ // Load the item into the player at its resume position but leave it
+ // paused, with no playback session: the live transcript needs the book
+ // up so its runway can build, without deciding for the user that audio
+ // starts now. Downloaded items only - a streamed item plays normally.
+ bool loadOnly = false,
}) async {
_pauseRequested = false;
_playFromUi = fromUi;
@@ -3102,6 +3123,7 @@ class AudioPlayerService extends ChangeNotifier {
// for downloaded items. Without this it leaks from a prior downloaded play
// into a following streaming play and misroutes the listening time.
_localSessionMode = false;
+ _pendingLoadOnlySession = null;
_currentEpisodeTitle = episodeTitle;
_currentTitle = title;
_currentAuthor = author;
@@ -3197,6 +3219,7 @@ class AudioPlayerService extends ChangeNotifier {
chapters,
startTime,
forceStartTime,
+ loadOnly,
);
} else {
// Check manual offline — don't stream from server
@@ -3533,6 +3556,7 @@ class AudioPlayerService extends ChangeNotifier {
List<dynamic> chapters,
double startTime, [
bool forceStartTime = false,
+ bool loadOnly = false,
]) async {
debugPrint('[Player] Playing from local files: $title');
// Alpha [PodDur]: trace podcast-episode duration loading. Symptom:
@@ -3773,6 +3797,25 @@ class AudioPlayerService extends ChangeNotifier {
chapter: initChapter,
);
await EqualizerService().switchItem(speedKey);
+ if (loadOnly) {
+ // Loaded and idle at the resume position: the transcript can build
+ // its runway and the lock screen shows the book paused, but nothing
+ // plays and no session exists until the user presses play - the
+ // normal play() path creates the session then.
+ debugPrint('[Player] Loaded paused (no session) at '
+ '${startTime.toStringAsFixed(0)}s');
+ _pendingLoadOnlySession = (
+ progressKey: pKey,
+ itemId: _currentItemId!,
+ episodeId: _currentEpisodeId,
+ duration: totalDuration,
+ title: title,
+ author: author,
+ );
+ _handler?.refreshPlaybackState();
+ notifyListeners();
+ return null;
+ }
debugPrint('[Player] Starting local playback at ${speed}x');
_handler?.refreshPlaybackState();
await Future.delayed(const Duration(milliseconds: 200));
@@ -6028,6 +6071,24 @@ class AudioPlayerService extends ChangeNotifier {
_noisyPause =
false; // User explicitly resumed — allow interrupt-resume again
_handler?._noisyPauseAt = null; // Clear noisy suppression window
+ // A loadOnly playItem left the item paused with no session; this first
+ // real play is where listening actually starts, so the session does too.
+ final pendingSession = _pendingLoadOnlySession;
+ if (pendingSession != null) {
+ _pendingLoadOnlySession = null;
+ if (_currentItemId == pendingSession.itemId) {
+ await LocalSessionService().beginSession(
+ progressKey: pendingSession.progressKey,
+ libraryItemId: pendingSession.itemId,
+ episodeId: pendingSession.episodeId,
+ mediaType: pendingSession.episodeId != null ? 'podcast' : 'book',
+ duration: pendingSession.duration,
+ startTime: position.inMilliseconds / 1000.0,
+ displayTitle: pendingSession.title,
+ displayAuthor: pendingSession.author,
+ );
+ }
+ }
// A seek while paused (user, or the socket adopting another device's
// position) is the position the user expects to hear next - don't let
// the server check below override it.
diff --git a/lib/services/lyrics_service.dart b/lib/services/lyrics_service.dart
new file mode 100644
index 00000000..a5bb1961
--- /dev/null
+++ b/lib/services/lyrics_service.dart
@@ -0,0 +1,480 @@
+import 'dart:async';
+
+import 'package:flutter/widgets.dart';
+
+import 'audio_player_service.dart';
+import 'transcript_chunker.dart';
+import 'transcript_line_store.dart';
+import 'transcription_service.dart';
+
+/// Live transcript ("lyrics") mode: while enabled, transcribes ahead of the
+/// playhead in 30s chunks and exposes the line under the current position for
+/// the player to display. Lines go through [TranscriptLineStore], so a
+/// listened stretch is never transcribed twice - and the store persists, so
+/// a book you ran this on before starts with its runway already built.
+///
+/// The loop builds a deep runway: it transcribes flat-out until it is
+/// [_targetListeningSeconds] of listening time ahead, then idles and tops
+/// back up as you listen. Lines are held back until an initial
+/// [_gateListeningSeconds] of runway exists, so the transcript never starts
+/// in a stutter - the overlay shows build progress until then. It always
+/// yields to user-triggered transcriptions (bookmark transcribe, the find
+/// features), which the deep runway absorbs without visible gaps, and stops
+/// with the mode, the screen, or playback.
+/// Watches for the app leaving the foreground. Transcribing is heavy enough
+/// to be felt on the whole phone, and it is only worth anything while you are
+/// looking at it - so backgrounding the app stops it, and it has to be turned
+/// on again deliberately.
+class _LyricsLifecycle extends WidgetsBindingObserver {
+ @override
+ void didChangeAppLifecycleState(AppLifecycleState state) {
+ // `inactive` also fires for a pulled-down notification shade or a
+ // permission dialog, which shouldn't count as leaving.
+ if (state == AppLifecycleState.paused ||
+ state == AppLifecycleState.hidden) {
+ final svc = LyricsService.instance;
+ if (!svc.isOn) return;
+ // The transcript is only worth anything on screen, so it stops. The
+ // runway built so far is on disk and is still there next time.
+ debugPrint('[Lyrics] app backgrounded - stopping');
+ svc.disable();
+ }
+ }
+}
+
+class LyricsService extends ChangeNotifier {
+ LyricsService._();
+ static final LyricsService instance = LyricsService._();
+
+ final _LyricsLifecycle _lifecycle = _LyricsLifecycle();
+ TranscriptChunker? _chunker;
+ bool _watching = false;
+
+ /// How much runway to build, in seconds of listening time - multiplied by
+ /// the playback speed to get book-seconds. Deep on purpose: momentary
+ /// stalls (model load, a bookmark transcription borrowing the engine, one
+ /// slow chunk) drain a shallow buffer and put gaps on screen; a deep one
+ /// absorbs them.
+ static const double _targetListeningSeconds = 600;
+
+ /// Runway required before lines are shown, in seconds of listening time.
+ /// Starting to display with no slack means the first hiccup stutters;
+ /// holding lines back until this much is banked means once the transcript
+ /// appears, it stays. Books with saved coverage pass instantly.
+ static const double _gateListeningSeconds = 60;
+
+ static const double _chunkSeconds = 30;
+
+ bool _on = false;
+ String? _key;
+ String? _epubItemId; // itemId to look for a cached EPUB under (books only)
+ Timer? _tick;
+ bool _working = false;
+ TranscriptLine? _current;
+ bool _transcribingVisible = false;
+ bool _pastCoverage = false;
+ bool _gatePassed = false;
+ double _gateProgress = 0;
+ double fontSize = 16;
+ int maxLines = 3;
+ bool fullCover = true;
+ /// Read-along coloring: the ARGB value the spoken words are painted in,
+ /// and whether tracking follows single words or whole sentences.
+ int readAlongColor = PlayerSettings.defaultReadAlongColor;
+ String readAlongMode = 'word';
+ int _wordIndex = -1;
+ int _probeTick = 0;
+ double? _rate;
+ /// Milliseconds the transcript is held back to match what you are hearing,
+ /// and which output that figure belongs to.
+ int offsetMs = 0;
+ bool onBluetooth = false;
+ bool _routeLoaded = false;
+ int _routeTick = 0;
+ int _slowChunks = 0;
+ bool _forceFastModel = false;
+ bool _reachedEnd = false;
+
+ bool get isOn => _on;
+ /// The store key lyrics are currently running for, so card overlays can
+ /// render only on the playing item's card.
+ String? get activeKey => _key;
+ TranscriptLine? get current => _current;
+ /// The transcript sync offset in seconds - subtract it from the playhead to
+ /// get the moment you are actually hearing.
+ double get offsetSeconds => offsetMs / 1000.0;
+
+
+ /// 0-1 progress toward the initial runway while lines are still held back,
+ /// or null once the gate has passed and lines are showing.
+ double? get gateProgress => _gatePassed ? null : _gateProgress;
+
+ /// True when this device measurably cannot transcribe as fast as you are
+ /// listening, so the transcript is going to keep running out.
+ bool get cannotKeepUp {
+ final rate = _rate;
+ if (rate == null) return false;
+ return rate < AudioPlayerService().speed * 1.2;
+ }
+
+ /// Which word of [current] is being spoken, or -1 when the line has no word
+ /// timing or word tracking is off.
+ int get currentWordIndex => readAlongMode == 'word' ? _wordIndex : -1;
+ /// True when the transcript has taken this item's cover over, so the card
+ /// should hide its artwork and let the transcript sit on the card's own
+ /// background instead.
+ bool coversArtFor(String key) => _on && fullCover && _key == key;
+
+ /// True when the playhead is past coverage and a chunk is being worked on -
+ /// the overlay shows a subtle "listening ahead" state instead of nothing.
+ bool get buffering => _on && _current == null && _pastCoverage;
+
+ /// The store key for what the player has loaded right now, matching the
+ /// download map's keying: composite for podcast episodes, itemId for books.
+ static String? playerKey() {
+ final p = AudioPlayerService();
+ final itemId = p.currentItemId;
+ if (itemId == null) return null;
+ final ep = p.currentEpisodeId;
+ return ep != null ? '$itemId-$ep' : itemId;
+ }
+
+ Future<void> enableForCurrent() async {
+ final key = playerKey();
+ if (key == null) return;
+ _key = key;
+ final ep = AudioPlayerService().currentEpisodeId;
+ _epubItemId = ep == null ? AudioPlayerService().currentItemId : null;
+ _chunker = TranscriptChunker(key: key, epubItemId: _epubItemId);
+ _on = true;
+ _rate = null;
+ _slowChunks = 0;
+ _forceFastModel = false;
+ _gatePassed = false;
+ _gateProgress = 0;
+ _reachedEnd = false;
+ _chunker?.reset();
+ _routeLoaded = false;
+ if (!_watching) {
+ _watching = true;
+ WidgetsBinding.instance.addObserver(_lifecycle);
+ }
+ await _refreshRoute();
+ await reloadDisplayPrefs();
+ await TranscriptLineStore.instance.hydrate(key);
+ _tick?.cancel();
+ _tick = Timer.periodic(const Duration(milliseconds: 200), (_) => _onTick());
+ notifyListeners();
+ final pos = AudioPlayerService().position.inMilliseconds / 1000.0;
+ final covered = TranscriptLineStore.instance.coveredUntil(key, pos);
+ debugPrint('[Lyrics] enabled for $key (epub lookup: ${_epubItemId != null}, '
+ 'saved runway ${(covered - pos).clamp(0, double.infinity).toStringAsFixed(0)}s '
+ 'from pos ${pos.toStringAsFixed(0)}s)');
+ }
+
+ /// Re-read the overlay display settings; the settings screen calls this so
+ /// changes apply live while lyrics are running.
+ Future<void> reloadDisplayPrefs() async {
+ fontSize = await PlayerSettings.getLyricsFontSize();
+ maxLines = await PlayerSettings.getLyricsMaxLines();
+ fullCover = await PlayerSettings.getLyricsFullCover();
+ readAlongColor = await PlayerSettings.getReadAlongColor();
+ readAlongMode = await PlayerSettings.getReadAlongMode();
+ notifyListeners();
+ }
+
+ void disable() {
+ _on = false;
+ if (_watching) {
+ _watching = false;
+ WidgetsBinding.instance.removeObserver(_lifecycle);
+ }
+ _tick?.cancel();
+ _tick = null;
+ _current = null;
+ _wordIndex = -1;
+ _key = null;
+ notifyListeners();
+ debugPrint('[Lyrics] disabled');
+ }
+
+ void _onTick() {
+ if (!_on) return;
+ final key = _key;
+ if (key == null) return;
+ // The player moved to a different book/episode: follow it.
+ final nowKey = playerKey();
+ if (nowKey == null) {
+ disable();
+ return;
+ }
+ if (nowKey != key) {
+ // A different book or episode started. Don't carry the transcript over
+ // to it - it costs battery and processing, so it stays something you
+ // switch on deliberately, every time.
+ debugPrint('[Lyrics] the player moved to $nowKey - stopping');
+ disable();
+ return;
+ }
+ final pos = AudioPlayerService().position.inMilliseconds / 1000.0;
+ if (++_routeTick % 25 == 0) _refreshRoute();
+ final speed = AudioPlayerService().speed;
+ final covered = TranscriptLineStore.instance.coveredUntil(key, pos);
+ final lead = covered - pos;
+ _pastCoverage = lead <= 0.5;
+
+ // Hold lines back until the initial runway exists (or the book's end is
+ // inside it) - showing lines with no slack means the first stall
+ // stutters. Once passed, the gate stays passed for the session, even if
+ // the runway later drains: switching the display off mid-listen would
+ // hide lines that are still correct.
+ if (!_gatePassed) {
+ // Once this device's real rate is known, the gate only needs enough
+ // runway to ride out a couple of chunks' worth of transcription time -
+ // on a fast device that's a fraction of the full gate, so lines appear
+ // in seconds instead of half a minute. Until a rate exists, or on a
+ // slow device, the full gate stands.
+ var gateNeed = _gateListeningSeconds * speed;
+ final r = _rate;
+ if (r != null && r > 0) {
+ final adaptive =
+ ((_chunkSeconds / r) * speed * 2.5).clamp(10.0, gateNeed);
+ gateNeed = adaptive;
+ }
+ final progress = (lead / gateNeed).clamp(0.0, 1.0).toDouble();
+ final total = AudioPlayerService().totalDuration;
+ final endInReach =
+ _reachedEnd || (total > 0 && covered >= total - _chunkSeconds);
+ if (lead >= gateNeed || endInReach) {
+ _gatePassed = true;
+ debugPrint('[Lyrics] gate passed: ${lead.toStringAsFixed(0)}s of '
+ 'runway banked (needed ${gateNeed.toStringAsFixed(0)}s at '
+ '${speed}x${endInReach ? ', end of item in reach' : ''})');
+ notifyListeners();
+ } else if ((progress - _gateProgress).abs() > 0.01) {
+ _gateProgress = progress;
+ notifyListeners();
+ }
+ }
+
+ // What you are hearing right now, which on Bluetooth is a fraction of a
+ // second behind the playhead. Only the display is shifted - transcribing
+ // still works from the real position.
+ final heard = pos - offsetSeconds;
+ // Small display lead: a line arriving a beat early reads like lyrics, a
+ // beat late reads like subtitles falling behind. The gap-tolerant lookup
+ // holds a line through the short silences Whisper's VAD trims out.
+ // The lead is in book-seconds, so scale it by playback speed to keep the
+ // same wall-clock feel at 1.5x or 2x.
+ final line = _gatePassed
+ ? TranscriptLineStore.instance.lineNear(key, heard + 0.35 * speed)
+ : null;
+ var changed = line?.start != _current?.start || line?.text != _current?.text;
+ _current = line;
+ // Previews have guessed timing - a moving word highlight on them would
+ // look confidently wrong, so they show as a whole dimmed line instead.
+ final wordIndex = line == null || line.approx
+ ? -1
+ : line.wordIndexAt(heard + 0.15 * speed);
+ if (wordIndex != _wordIndex) {
+ _wordIndex = wordIndex;
+ if (readAlongMode == 'word') changed = true;
+ }
+ if (changed) notifyListeners();
+ // The one line that tells the whole story in a shared log: where the
+ // playhead is, how much runway is banked against the target, and whether
+ // this device is outrunning the narration.
+ if (_routeTick % 75 == 0) {
+ debugPrint('[Lyrics] status pos=${pos.toStringAsFixed(0)}s '
+ 'lead=${lead.toStringAsFixed(0)}s/'
+ '${(_targetListeningSeconds * speed).toStringAsFixed(0)}s '
+ 'gate=${_gatePassed ? 'open' : '${(_gateProgress * 100).round()}%'} '
+ 'rate=${_rate?.toStringAsFixed(2) ?? '?'}x speed=${speed}x '
+ 'fastModel=$_forceFastModel end=$_reachedEnd');
+ }
+ _probe(pos, line);
+ _ensureAhead(key, pos, covered);
+ }
+
+ /// Re-read which output is in use and load that output's offset. Cheap
+ /// enough to run every few seconds, which is how a headphone connection
+ /// mid-listen gets picked up.
+ Future<void> _refreshRoute() async {
+ final bt = await AudioPlayerService.isBluetoothAudioConnected();
+ if (_routeLoaded && bt == onBluetooth) return;
+ onBluetooth = bt;
+ _routeLoaded = true;
+ offsetMs = await PlayerSettings.getTranscriptOffsetMs(bluetooth: bt);
+ debugPrint('[Lyrics] output is ${bt ? 'bluetooth' : 'the device'}, '
+ 'sync offset ${offsetMs}ms');
+ notifyListeners();
+ }
+
+ /// Shift the transcript against the audio by [deltaMs], remembered against
+ /// whichever output you are on.
+ Future<void> nudgeOffset(int deltaMs) async {
+ offsetMs = (offsetMs + deltaMs).clamp(-1000, 2000);
+ notifyListeners();
+ await PlayerSettings.setTranscriptOffsetMs(offsetMs, bluetooth: onBluetooth);
+ }
+
+ Future<void> clearOffset() async {
+ offsetMs = 0;
+ notifyListeners();
+ await PlayerSettings.setTranscriptOffsetMs(0, bluetooth: onBluetooth);
+ }
+
+ /// How fast this device transcribes, in book-seconds per wall-second,
+ /// smoothed over recent chunks. Below the playback speed the transcript can
+ /// never catch the narration.
+ void _noteRate(double span, double wall, double speed) {
+ if (wall < 0.5 || span < 1) return;
+ final rate = span / wall;
+ _rate = _rate == null ? rate : _rate! * 0.6 + rate * 0.4;
+ final need = speed * 1.2;
+ debugPrint('[Lyrics] transcribed ${span.toStringAsFixed(0)}s in '
+ '${wall.toStringAsFixed(1)}s = ${rate.toStringAsFixed(2)}x '
+ '(need ${need.toStringAsFixed(2)}x at ${speed}x playback)');
+ if (_rate! >= need) {
+ _slowChunks = 0;
+ return;
+ }
+ _slowChunks++;
+ // One badly slow chunk, or two merely slow ones - the first chunk carries
+ // the model load, so a single mild miss isn't proof on its own.
+ if (_forceFastModel || !(_slowChunks >= 2 || rate < speed * 0.6)) return;
+ _forceFastModel = true;
+ _slowChunks = 0;
+ final fastest = TranscriptionService.instance.lastModelUsed ==
+ TranscriptionModelSize.tiny;
+ if (fastest) {
+ // Already on the fastest model: nothing to switch to, and the gaps are
+ // this device's limit rather than a setting to fix. Keep the measured
+ // rate - aiming chunks ahead is all that is left.
+ debugPrint('[Lyrics] cannot keep up at ${speed}x even on the fast '
+ 'model - the transcript will have gaps');
+ return;
+ }
+ // Re-measure from scratch: the slow model's rate would otherwise aim the
+ // next chunk far ahead and skip audio the fast model could have covered.
+ _rate = null;
+ debugPrint('[Lyrics] too slow to keep up at ${speed}x - switching to the '
+ 'fast model for the rest of this session');
+ }
+
+ /// Once a second, write down where the audio is and what we believe is
+ /// being said there. Played back against a screen recording it shows
+ /// whether a line is early, late, or simply the wrong line.
+ void _probe(double pos, TranscriptLine? line) {
+ if (++_probeTick % 5 != 0) return;
+ if (line == null) {
+ debugPrint('[LyricsSync] pos=${pos.toStringAsFixed(1)} no line '
+ '(${_pastCoverage ? 'past coverage' : 'gap'})');
+ return;
+ }
+ final words = line.words;
+ final word = _wordIndex >= 0 && _wordIndex < words.length
+ ? words[_wordIndex]
+ : '-';
+ final text = line.text.length > 70
+ ? '${line.text.substring(0, 70)}...'
+ : line.text;
+ debugPrint('[LyricsSync] pos=${pos.toStringAsFixed(1)} '
+ 'line=[${line.start.toStringAsFixed(1)}-${line.end.toStringAsFixed(1)}] '
+ 'word=$_wordIndex/${line.wordStarts.length} "$word" '
+ 'exact=${line.exact} offset=${offsetMs}ms | $text');
+ }
+
+ Future<void> _ensureAhead(String key, double pos, double covered) async {
+ if (_working) return;
+ final speed = AudioPlayerService().speed;
+ // The end of the item is inside coverage: the runway is as deep as it
+ // will ever need to be.
+ final dur = AudioPlayerService().totalDuration;
+ if (_reachedEnd || (dur > 0 && covered >= dur - 1)) {
+ if (!_reachedEnd) {
+ _reachedEnd = true;
+ debugPrint('[Lyrics] coverage reached the end of $key');
+ }
+ if (_transcribingVisible) {
+ _transcribingVisible = false;
+ notifyListeners();
+ }
+ return;
+ }
+ // The target is listening time, so it scales with the playback speed -
+ // ten minutes of runway at 1.5x is ten minutes of runway at 1x.
+ if (covered - pos >= _targetListeningSeconds * speed) {
+ if (_transcribingVisible) {
+ _transcribingVisible = false;
+ notifyListeners();
+ }
+ return;
+ }
+ // User-triggered transcriptions (bookmark, find) always win the engine.
+ if (TranscriptionService.instance.isBusy) return;
+ _working = true;
+ _transcribingVisible = true;
+ notifyListeners();
+ // Starting fresh at the playhead, begin a few seconds early: pressing
+ // play auto-rewinds, and a runway that starts exactly at the enable
+ // position leaves that rewind in a hole with zero lead. Overlap with
+ // anything already stored is dropped by the store.
+ var start = covered > pos
+ ? covered
+ : (_rate == null && covered <= pos ? (pos - 5).clamp(0.0, pos) : pos);
+ if (covered <= pos + 1 && _rate != null) {
+ // Behind the playhead. Transcribing from where you are now hands back
+ // audio you have already heard by the time it finishes, and it never
+ // catches up - so aim at where the playhead will BE when this chunk is
+ // done. The stretch in between is skipped rather than shown late.
+ final wall = _chunkSeconds / _rate!;
+ start = pos + wall * speed + 1.5;
+ debugPrint('[Lyrics] behind the playhead, aiming '
+ '${(start - pos).toStringAsFixed(1)}s ahead');
+ }
+ try {
+ // With the book's EPUB cached, every word gets replaced by the book's
+ // own, so tiny is enough and its speed is what keeps the transcript
+ // ahead of the narration. Without one, these words are what you read -
+ // unless this device can't transcribe them fast enough to be read at
+ // all, in which case the fast model beats a blank screen.
+ // Sprint the session's first window: a short one puts the first line
+ // (and the rate measurement the adaptive gate needs) on screen in a
+ // few seconds; full windows take over from there.
+ final out = await _chunker!.transcribeInto(
+ start: start,
+ windowSeconds: _rate == null ? 12.0 : _chunkSeconds,
+ preferAccuracy: !_forceFastModel,
+ );
+ _noteRate(out.span, out.wall, speed);
+ if (out.lines > 0) {
+ debugPrint('[Lyrics] chunk at ${start.toStringAsFixed(1)}s: '
+ '${out.lines} lines, exact=${out.exact}');
+ }
+ } on TranscriptionException catch (e) {
+ if (e.kind == TranscriptionError.empty) {
+ // Silence or music: mark the stretch covered so the loop moves past
+ // it instead of retrying the same window forever.
+ await TranscriptLineStore.instance.addLines(key, [
+ TranscriptLine(start, start + _chunkSeconds, ' '),
+ ]);
+ } else if (e.kind == TranscriptionError.extractFailed &&
+ dur > 0 &&
+ start + _chunkSeconds >= dur - 1) {
+ // The window ran off the end of the audio.
+ _reachedEnd = true;
+ debugPrint('[Lyrics] the last window ran past the end of $key');
+ } else {
+ debugPrint('[Lyrics] chunk failed: $e');
+ // Setup problems (model gone, download removed) end the session.
+ if (e.kind != TranscriptionError.busy) disable();
+ }
+ } catch (e) {
+ debugPrint('[Lyrics] chunk failed: $e');
+ } finally {
+ _working = false;
+ }
+ }
+
+}
diff --git a/lib/services/player_settings.dart b/lib/services/player_settings.dart
index bb95a894..669fd795 100644
--- a/lib/services/player_settings.dart
+++ b/lib/services/player_settings.dart
@@ -229,15 +229,99 @@ class PlayerSettings {
await prefs.setBool('transcriptionUseEbookText', value);
}
+ /// Live transcript overlay: line font size and how many lines may wrap.
+ static Future<double> getLyricsFontSize() async {
+ final prefs = await SharedPreferences.getInstance();
+ return prefs.getDouble('lyricsFontSize') ?? 16;
+ }
+ static Future<void> setLyricsFontSize(double value) async {
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.setDouble('lyricsFontSize', value);
+ }
+ static Future<bool> getLyricsIntroShown() async {
+ final prefs = await SharedPreferences.getInstance();
+ return prefs.getBool('lyricsIntroShown') ?? false;
+ }
+ static Future<void> setLyricsIntroShown() async {
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.setBool('lyricsIntroShown', true);
+ }
+
+ /// Color the spoken words are painted in for read along and the live
+ /// transcript overlay, as an ARGB value. Amber by default: it stays legible
+ /// on the light, sepia and dark reader themes alike.
+ static const int defaultReadAlongColor = 0xFFFFC400;
+ static Future<int> getReadAlongColor() async {
+ final prefs = await SharedPreferences.getInstance();
+ return prefs.getInt('readAlongColor') ?? defaultReadAlongColor;
+ }
+ static Future<void> setReadAlongColor(int value) async {
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.setInt('readAlongColor', value);
+ }
+
+ /// How closely read along follows the narration: 'word' colors one word at
+ /// a time with the rest of the sentence dimmed, 'sentence' colors the whole
+ /// sentence at once.
+ static Future<String> getReadAlongMode() async {
+ final prefs = await SharedPreferences.getInstance();
+ return prefs.getString('readAlongMode') ?? 'word';
+ }
+ static Future<void> setReadAlongMode(String value) async {
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.setString('readAlongMode', value);
+ }
+
+ /// How far ahead of your ears the playhead runs, in milliseconds, so the
+ /// transcript can be held back to match. Bluetooth adds a couple of hundred
+ /// milliseconds that the phone's own speaker doesn't, so the two are
+ /// remembered separately and picked by whatever you are listening on.
+ static Future<int> getTranscriptOffsetMs({required bool bluetooth}) async {
+ final prefs = await SharedPreferences.getInstance();
+ return prefs.getInt(bluetooth ? 'transcriptOffsetBt' : 'transcriptOffset') ?? 0;
+ }
+ static Future<void> setTranscriptOffsetMs(int value,
+ {required bool bluetooth}) async {
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.setInt(
+ bluetooth ? 'transcriptOffsetBt' : 'transcriptOffset', value);
+ }
+
+ /// Whether the live transcript takes over the whole cover instead of
+ /// sitting in a strip across the bottom of it.
+ static Future<bool> getLyricsFullCover() async {
+ final prefs = await SharedPreferences.getInstance();
+ return prefs.getBool('lyricsFullCover') ?? true;
+ }
+ static Future<void> setLyricsFullCover(bool value) async {
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.setBool('lyricsFullCover', value);
+ }
+
+ static Future<int> getLyricsMaxLines() async {
+ final prefs = await SharedPreferences.getInstance();
+ return prefs.getInt('lyricsMaxLines') ?? 3;
+ }
+ static Future<void> setLyricsMaxLines(int value) async {
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.setInt('lyricsMaxLines', value);
+ }
+
/// Where Find in audiobook lands on success: true = switch to the player,
/// false = stay in the reader while the audio plays.
- static Future<bool> getFindInAudiobookGoToPlayer() async {
+ /// Where a successful Find in audiobook lands: 'stay' in the reader,
+ /// 'player', or 'readalong' - stay and switch read along on from there.
+ static Future<String> getFindInAudiobookAfter() async {
final prefs = await SharedPreferences.getInstance();
- return prefs.getBool('findInAudiobookGoToPlayer') ?? true;
+ final v = prefs.getString('findInAudiobookAfter');
+ if (v != null) return v;
+ // The setting used to be a two-way switch.
+ final old = prefs.getBool('findInAudiobookGoToPlayer');
+ return old == false ? 'stay' : 'player';
}
- static Future<void> setFindInAudiobookGoToPlayer(bool value) async {
+ static Future<void> setFindInAudiobookAfter(String value) async {
final prefs = await SharedPreferences.getInstance();
- await prefs.setBool('findInAudiobookGoToPlayer', value);
+ await prefs.setString('findInAudiobookAfter', value);
}
// The dedicated Podcasts tab implies merged behavior: playback must survive
@@ -1055,7 +1139,7 @@ class PlayerSettings {
// ── Card button order ──
- static const defaultButtonOrder = ['chapters', 'speed', 'sleep', 'bookmarks', 'details', 'ebook', 'findinebook', 'equalizer', 'cast', 'airplay', 'history', 'remove', 'car', 'notes', 'download'];
+ static const defaultButtonOrder = ['chapters', 'speed', 'sleep', 'bookmarks', 'details', 'ebook', 'findinebook', 'lyrics', 'equalizer', 'cast', 'airplay', 'history', 'remove', 'car', 'notes', 'download'];
static Future<List<String>> getCardButtonOrder() async {
final stored = await ScopedPrefs.getStringList('card_button_order');
diff --git a/lib/services/read_along_script.dart b/lib/services/read_along_script.dart
new file mode 100644
index 00000000..31808683
--- /dev/null
+++ b/lib/services/read_along_script.dart
@@ -0,0 +1,381 @@
+/// The in-page script behind read along's coloring.
+///
+/// epub.js annotations are an SVG overlay drawn on top of the page, so they
+/// can only ever box or underline text - they cannot repaint the words
+/// themselves. This paints the real glyphs instead: the CSS Custom Highlight
+/// API where the WebView has it (no DOM changes at all), and wrapping the
+/// range in spans on older WebViews (iOS below 17.2), unwrapped again on every
+/// move so the page ends up exactly as it started.
+library;
+
+/// Six-digit hex for an ARGB value, for CSS.
+String cssHex(int argb) =>
+ '#${(argb & 0xFFFFFF).toRadixString(16).padLeft(6, '0').toUpperCase()}';
+
+/// Installs `window.__absorbRA` in the reader page. Safe to run more than
+/// once; a second run just refreshes the color.
+String readAlongBootstrap(int argb) {
+ final hex = cssHex(argb);
+ final r = (argb >> 16) & 0xFF, g = (argb >> 8) & 0xFF, b = argb & 0xFF;
+ return '''
+(function() {
+ var RA = window.__absorbRA = window.__absorbRA || {};
+ RA.color = '$hex';
+ RA.dimRgba = 'rgba($r, $g, $b, 0.8)';
+ RA.cur = null;
+
+ RA.css = function() {
+ var mix = 'color-mix(in srgb, ' + RA.color + ' 55%, currentColor)';
+ return '::highlight(absorbRaWord) { color: ' + RA.color + '; }' +
+ '::highlight(absorbRaDim) { color: ' + RA.dimRgba + '; }' +
+ '.absorb-ra-word { color: ' + RA.color + ' !important; }' +
+ '.absorb-ra-dim { color: ' + RA.dimRgba + ' !important; }' +
+ '@supports (color: color-mix(in srgb, red 50%, blue)) {' +
+ ' ::highlight(absorbRaDim) { color: ' + mix + '; }' +
+ ' .absorb-ra-dim { color: ' + mix + ' !important; }' +
+ '}';
+ };
+
+ RA.style = function(doc) {
+ try {
+ if (!doc) return;
+ var st = doc.getElementById('absorbReadAlongStyle');
+ if (!st) {
+ st = doc.createElement('style');
+ st.id = 'absorbReadAlongStyle';
+ (doc.head || doc.documentElement).appendChild(st);
+ }
+ st.textContent = RA.css();
+ } catch (e) {}
+ };
+
+ RA.docs = function() {
+ var out = [];
+ try {
+ var contents = (typeof rendition.getContents === 'function') ? rendition.getContents() : [];
+ for (var i = 0; i < contents.length; i++) {
+ if (contents[i] && contents[i].document) out.push(contents[i]);
+ }
+ } catch (e) {}
+ return out;
+ };
+
+ RA.setColor = function(hex) {
+ RA.color = hex;
+ RA.dimRgba = hex;
+ var m = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\$/i.exec(hex);
+ if (m) {
+ RA.dimRgba = 'rgba(' + parseInt(m[1], 16) + ', ' + parseInt(m[2], 16) +
+ ', ' + parseInt(m[3], 16) + ', 0.8)';
+ }
+ var cs = RA.docs();
+ for (var i = 0; i < cs.length; i++) RA.style(cs[i].document);
+ };
+
+ // Char offset -> text node map for a page document. Rebuilt after any span
+ // wrapping, since that splits text nodes; the raw text never changes, so
+ // offsets stay valid across rebuilds.
+ RA.nodeMap = function(doc) {
+ var tw = doc.createTreeWalker(doc.body || doc, NodeFilter.SHOW_TEXT, null, false);
+ var nodes = [], raw = '', node;
+ while (node = tw.nextNode()) {
+ nodes.push({ node: node, start: raw.length, len: node.textContent.length });
+ raw += node.textContent;
+ }
+ return { nodes: nodes, raw: raw };
+ };
+
+ RA.locateOffset = function(map, off) {
+ for (var i = 0; i < map.nodes.length; i++) {
+ var e = map.nodes[i];
+ if (off < e.start + e.len) return { node: e.node, offset: off - e.start };
+ }
+ var last = map.nodes[map.nodes.length - 1];
+ return last ? { node: last.node, offset: last.len } : null;
+ };
+
+ RA.range = function(doc, map, a, b) {
+ var sp = RA.locateOffset(map, a), ep = RA.locateOffset(map, b);
+ if (!sp || !ep) return null;
+ var r = doc.createRange();
+ r.setStart(sp.node, sp.offset);
+ r.setEnd(ep.node, ep.offset);
+ return r;
+ };
+
+ RA.useHighlights = function(doc) {
+ var win = doc && doc.defaultView;
+ return !!(win && win.CSS && win.CSS.highlights && win.Highlight);
+ };
+
+ RA.unwrap = function(doc) {
+ try {
+ var els = doc.querySelectorAll('span.absorb-ra');
+ for (var i = 0; i < els.length; i++) {
+ var el = els[i], p = el.parentNode;
+ if (!p) continue;
+ while (el.firstChild) p.insertBefore(el.firstChild, el);
+ p.removeChild(el);
+ }
+ if (els.length) (doc.body || doc).normalize();
+ } catch (e) {}
+ };
+
+ RA.wrap = function(doc, map, a, b, cls) {
+ if (b <= a) return;
+ for (var i = 0; i < map.nodes.length; i++) {
+ var e = map.nodes[i];
+ var s0 = Math.max(a, e.start), e0 = Math.min(b, e.start + e.len);
+ if (e0 <= s0) continue;
+ var node = e.node;
+ if (!node.parentNode) continue;
+ var local = s0 - e.start, localEnd = e0 - e.start;
+ var target = node;
+ if (local > 0) target = target.splitText(local);
+ if (localEnd - local < target.textContent.length) target.splitText(localEnd - local);
+ var span = doc.createElement('span');
+ span.className = cls + ' absorb-ra';
+ target.parentNode.insertBefore(span, target);
+ span.appendChild(target);
+ }
+ };
+
+ /// Paint the current sentence, with [wordIndex] (page word, -1 for none)
+ /// carrying the full color and the rest of the sentence dimmed.
+ RA.paint = function(wordIndex) {
+ var st = RA.cur;
+ if (!st) return;
+ var doc = st.doc;
+ var word = (wordIndex >= 0 && st.words.length)
+ ? st.words[Math.min(wordIndex, st.words.length - 1)] : null;
+ if (RA.useHighlights(doc)) {
+ var win = doc.defaultView, reg = win.CSS.highlights;
+ var map = st.map || (st.map = RA.nodeMap(doc));
+ reg.delete('absorbRaWord');
+ reg.delete('absorbRaDim');
+ var sentence = RA.range(doc, map, st.sStart, st.sEnd);
+ if (!sentence) return;
+ if (word) {
+ var wr = RA.range(doc, map, word[0], word[1]);
+ var dim = new win.Highlight(sentence);
+ dim.priority = 1;
+ reg.set('absorbRaDim', dim);
+ if (wr) {
+ var hi = new win.Highlight(wr);
+ hi.priority = 2;
+ reg.set('absorbRaWord', hi);
+ }
+ } else {
+ var whole = new win.Highlight(sentence);
+ whole.priority = 2;
+ reg.set('absorbRaWord', whole);
+ }
+ return;
+ }
+ RA.unwrap(doc);
+ var m2 = st.map = RA.nodeMap(doc);
+ if (word) {
+ RA.wrap(doc, m2, st.sStart, word[0], 'absorb-ra-dim');
+ m2 = st.map = RA.nodeMap(doc);
+ RA.wrap(doc, m2, word[0], word[1], 'absorb-ra-word');
+ m2 = st.map = RA.nodeMap(doc);
+ RA.wrap(doc, m2, word[1], st.sEnd, 'absorb-ra-dim');
+ } else {
+ RA.wrap(doc, m2, st.sStart, st.sEnd, 'absorb-ra-word');
+ }
+ st.map = null;
+ };
+
+ RA.location = function() {
+ if (RA.loc) return RA.loc;
+ try { RA.loc = rendition.currentLocation(); } catch (e) { RA.loc = null; }
+ return RA.loc;
+ };
+
+ /// Whether [range] sits on the page currently on screen, and its CFI so the
+ /// caller can turn to it when it doesn't.
+ RA.visibleAt = function(range) {
+ var out = { v: true };
+ try {
+ var cfi = RA.cur.contents.cfiFromRange(range);
+ if (!cfi) return out;
+ out.cfi = cfi;
+ var loc = RA.location();
+ if (!loc || !loc.start || !loc.end) return out;
+ var cmp = new ePub.CFI();
+ out.v = cmp.compare(cfi, loc.start.cfi) >= 0 &&
+ cmp.compare(cfi, loc.end.cfi) <= 0;
+ } catch (e) {}
+ return out;
+ };
+
+ // held:false means the anchor is gone (the page turned into another
+ // section) and the caller should locate the sentence again. visible:false
+ // means this word is on the next page - following word by word, that is
+ // where the page should turn, not at the end of the sentence.
+ RA.word = function(i) {
+ if (!RA.cur) return JSON.stringify({ held: false });
+ if (RA.cur.painted !== i) {
+ RA.cur.painted = i;
+ RA.paint(i);
+ }
+ var out = { held: true, visible: true };
+ try {
+ var st = RA.cur;
+ var w = (i >= 0 && st.words.length)
+ ? st.words[Math.min(i, st.words.length - 1)] : null;
+ if (w) {
+ var map = st.map || (st.map = RA.nodeMap(st.doc));
+ var r = RA.range(st.doc, map, w[0], w[1]);
+ if (r) {
+ var vis = RA.visibleAt(r);
+ out.visible = vis.v;
+ if (vis.cfi) out.cfi = vis.cfi;
+ }
+ }
+ } catch (e) {}
+ return JSON.stringify(out);
+ };
+
+ RA.clearDoc = function(doc) {
+ if (!doc) return;
+ try {
+ if (RA.useHighlights(doc)) {
+ doc.defaultView.CSS.highlights.delete('absorbRaWord');
+ doc.defaultView.CSS.highlights.delete('absorbRaDim');
+ }
+ } catch (e) {}
+ RA.unwrap(doc);
+ };
+
+ RA.clear = function() {
+ var cs = RA.docs();
+ for (var i = 0; i < cs.length; i++) RA.clearDoc(cs[i].document);
+ RA.cur = null;
+ };
+
+ RA.start = function() {
+ var cs = RA.docs();
+ for (var i = 0; i < cs.length; i++) RA.style(cs[i].document);
+ if (!RA.hooked) {
+ RA.hooked = true;
+ try {
+ rendition.hooks.content.register(function(c) { RA.style(c.document); });
+ } catch (e) {}
+ try {
+ // A page turn drops the old document; the follow mark goes with it.
+ // The location is cached here because working it out costs real time
+ // and the word check runs several times a second.
+ rendition.on('relocated', function(loc) {
+ RA.loc = loc || null;
+ if (RA.cur && RA.docs().indexOf(RA.cur.contents) === -1) RA.cur = null;
+ });
+ } catch (e) {}
+ }
+ };
+
+ RA.stop = function() {
+ RA.clear();
+ var cs = RA.docs();
+ for (var i = 0; i < cs.length; i++) {
+ try {
+ var st = cs[i].document.getElementById('absorbReadAlongStyle');
+ if (st && st.parentNode) st.parentNode.removeChild(st);
+ } catch (e) {}
+ }
+ };
+
+ /// Find [needle] in the rendered pages, snap it out to sentence bounds, and
+ /// paint it. Returns whether it was found, whether it sits on the visible
+ /// page, and how many words the matched span holds.
+ RA.locate = function(needle, wordMode, minOffset) {
+ var out = { found: false };
+ try {
+ var cs = RA.docs();
+ for (var i = 0; i < cs.length; i++) {
+ var c = cs[i], doc = c.document;
+ var map = RA.nodeMap(doc);
+ var raw = map.raw;
+ var lower = raw.toLowerCase();
+ var normChars = [], idx = [], prevSpace = true;
+ for (var k = 0; k < lower.length; k++) {
+ var ch = lower[k];
+ if (/\\s/.test(ch)) {
+ if (!prevSpace) { normChars.push(' '); idx.push(k); }
+ prevSpace = true;
+ } else { normChars.push(ch); idx.push(k); prevSpace = false; }
+ }
+ var hay = normChars.join('');
+ var q = needle.toLowerCase().replace(/\\s+/g, ' ').trim();
+ // A phrase can occur more than once on a page ("he said", a repeated
+ // refrain). Narration only moves forward, so prefer the first
+ // occurrence at or after where the last sentence was, and fall back to
+ // the first one anywhere when there is nothing ahead.
+ var pos = -1;
+ if (typeof minOffset === 'number' && minOffset >= 0) {
+ for (var sp = 0; sp < idx.length; sp++) {
+ if (idx[sp] >= minOffset) { pos = hay.indexOf(q, sp); break; }
+ }
+ }
+ if (pos === -1) pos = hay.indexOf(q);
+ if (pos === -1) continue;
+ var rs = idx[pos], reEnd = idx[pos + q.length - 1] + 1;
+ // Snap the span to full sentence boundaries: Whisper segments cut
+ // wherever they like, and mid-sentence spans read as glitches. A whole
+ // sentence is also stable - consecutive segments inside one sentence
+ // resolve to the same range, so the coloring holds still.
+ var sStart = rs;
+ for (var bk = rs - 1; bk >= 0 && rs - bk < 400; bk--) {
+ var cb = raw[bk];
+ if (cb === '.' || cb === '!' || cb === '?' || cb === '\\n') { sStart = bk + 1; break; }
+ if (bk === 0) sStart = 0;
+ }
+ while (sStart < rs && /\\s/.test(raw[sStart])) sStart++;
+ // End at the FIRST sentence terminator after the needle's start, so
+ // the span is always exactly one sentence - even when an older cached
+ // line covers several.
+ var sEnd = reEnd;
+ for (var f = rs; f < raw.length && f - rs < 400; f++) {
+ var cf = raw[f];
+ if (cf === '.' || cf === '!' || cf === '?') {
+ var j2 = f + 1;
+ while (j2 < raw.length && (raw[j2] === '"' || raw[j2] === "'" || raw[j2] === '”' || raw[j2] === '’')) j2++;
+ sEnd = j2; break;
+ }
+ if (cf === '\\n') { sEnd = f; break; }
+ }
+ if (sEnd <= sStart) sEnd = reEnd;
+ var words = [], wre = /\\S+/g, sub = raw.slice(rs, reEnd), wm;
+ while ((wm = wre.exec(sub))) words.push([rs + wm.index, rs + wm.index + wm[0].length]);
+ out.found = true;
+ out.words = words.length;
+ out.visible = true;
+ out.at = sStart;
+ out.sentence = raw.slice(sStart, Math.min(sEnd, sStart + 90)).replace(/\\s+/g, ' ');
+ // Work the CFI out before painting: on the span fallback, painting
+ // splits text nodes, and a CFI taken from that DOM would not survive
+ // the unwrap.
+ try {
+ var r = RA.range(doc, map, sStart, sEnd);
+ var cfi = r ? c.cfiFromRange(r) : null;
+ out.cfi = cfi || '';
+ var loc = RA.location();
+ if (cfi && loc && loc.start && loc.end) {
+ var cmp = new ePub.CFI();
+ out.visible = cmp.compare(cfi, loc.start.cfi) >= 0 && cmp.compare(cfi, loc.end.cfi) <= 0;
+ }
+ } catch (e1) {}
+ if (RA.cur && RA.cur.doc !== doc) RA.clearDoc(RA.cur.doc);
+ RA.style(doc);
+ RA.cur = { doc: doc, contents: c, sStart: sStart, sEnd: sEnd, words: words, map: map, painted: -2 };
+ var first = (wordMode && words.length) ? 0 : -1;
+ RA.paint(first);
+ RA.cur.painted = first;
+ break;
+ }
+ } catch (e) { out.err = String(e); }
+ return JSON.stringify(out);
+ };
+})();
+''';
+}
diff --git a/lib/services/transcript_chunker.dart b/lib/services/transcript_chunker.dart
new file mode 100644
index 00000000..fdb865d7
--- /dev/null
+++ b/lib/services/transcript_chunker.dart
@@ -0,0 +1,170 @@
+import 'dart:io';
+
+import 'package:flutter/foundation.dart';
+
+import '../utils/passage_match.dart';
+import '../utils/transcript_align.dart';
+import 'ebook_cache.dart';
+import 'transcript_line_store.dart';
+import 'transcription_service.dart';
+
+/// What one transcribed window produced.
+typedef ChunkOutcome = ({
+ /// Seconds of audio the window actually yielded, for measuring how fast
+ /// this device is getting through it.
+ double span,
+ double wall,
+ int lines,
+ bool exact,
+});
+
+/// Turns one window of an item's audio into stored transcript lines: Whisper,
+/// then the book's own words where the EPUB has them, timed against what was
+/// actually heard, split into sentences.
+///
+/// Shared by the live transcript and the whole-book preparation job so both
+/// produce identical lines - the store is the same cache either way, and a
+/// stretch prepared ahead of time must be indistinguishable from one
+/// transcribed as you listen.
+class TranscriptChunker {
+ TranscriptChunker({required this.key, this.epubItemId});
+
+ /// Store key: itemId for books, "itemId-episodeId" for podcast episodes.
+ final String key;
+
+ /// Item to look for a cached EPUB under, or null for podcasts.
+ final String? epubItemId;
+
+ /// Chunks in a row the ebook couldn't be matched against. Bonus content and
+ /// audio-only extras aren't in the EPUB at all, and scanning the whole book
+ /// for every chunk of them is work that can never pay off.
+ int _misses = 0;
+ int _chunks = 0;
+
+ void reset() {
+ _misses = 0;
+ _chunks = 0;
+ }
+
+ /// Path to this book's cached EPUB, or null when there isn't one. Checked
+ /// per chunk rather than once, so opening the ebook mid-listen starts
+ /// correcting the next chunk.
+ Future<String?> cachedEpubPath() async {
+ final itemId = epubItemId;
+ if (itemId == null) return null;
+ try {
+ final ef = await cachedEbookFileFor(itemId);
+ if (ef == null || ebookExtFromFile(ef) != '.epub') return null;
+ final f = await ebookCacheFileFor(itemId, ef);
+ return f.existsSync() ? f.path : null;
+ } catch (e) {
+ debugPrint('[Transcript] ebook lookup failed: $e');
+ return null;
+ }
+ }
+
+ /// The book's own words for a chunk (plus the sentences that follow, for
+ /// the estimated preview), when the EPUB is cached and the passage matches
+ /// confidently. Timing them against what was actually heard is
+ /// [alignCorrected]'s job - this only finds the text. Any failure returns
+ /// null, which leaves the honest Whisper transcript in place.
+ Future<PassageWithNext?> _correctedText(
+ List<TimedWord> heard, String? epubPath) async {
+ if (epubPath == null || heard.isEmpty) return null;
+ try {
+ if (!File(epubPath).existsSync()) return null;
+ final joined = heard.map((w) => w.text).join(' ');
+ final corrected = await compute(
+ correctFromEpubWithNext, (epubPath: epubPath, transcript: joined));
+ if (corrected == null || corrected.passage.trim().isEmpty) return null;
+ return corrected;
+ } catch (e) {
+ debugPrint('[Transcript] ebook correction failed: $e');
+ return null;
+ }
+ }
+
+ /// Transcribe [windowSeconds] from [start] and file the lines. Throws
+ /// [TranscriptionException] the same way the engine does, so callers can
+ /// tell silence from a missing model.
+ Future<ChunkOutcome> transcribeInto({
+ required double start,
+ required double windowSeconds,
+ required bool preferAccuracy,
+ }) async {
+ final epub = await cachedEpubPath();
+ final watch = Stopwatch()..start();
+ final segs = await TranscriptionService.instance.transcribeWindowSegments(
+ itemId: key,
+ startSeconds: start,
+ windowSeconds: windowSeconds,
+ preferAccuracy: preferAccuracy && epub == null,
+ );
+ final wall = watch.elapsedMilliseconds / 1000.0;
+ final heard = wordsFromSegments([
+ for (final s in segs)
+ (start: start + s.start, end: start + s.end, text: s.text.trim()),
+ ]);
+ var words = heard;
+ var exact = false;
+ // After a few misses in a row, only try occasionally - an audio-only
+ // extra will never match, but front matter that missed early shouldn't
+ // rule the book's own chapters out for good.
+ final skipCorrection = _misses >= 3 && (++_chunks % 5 != 0);
+ final corrected =
+ skipCorrection ? null : await _correctedText(heard, epub);
+ if (corrected == null && !skipCorrection && epub != null) {
+ _misses++;
+ if (_misses == 3) {
+ debugPrint('[Transcript] this stretch is not in the ebook (bonus or '
+ 'audio-only content) - checking it only occasionally now');
+ }
+ }
+ var continuation = '';
+ if (corrected != null) {
+ final aligned = alignCorrected(heard, corrected.passage);
+ if (aligned != null && aligned.words.isNotEmpty) {
+ words = aligned.words;
+ exact = true;
+ continuation = corrected.continuation;
+ _misses = 0;
+ debugPrint('[Transcript] aligned book text: matched '
+ '${aligned.matched}/${aligned.total} words, '
+ 'heard=${heard.length} book=${words.length}');
+ } else {
+ // The book's words are the right words at the wrong moments unless
+ // enough of them anchor to what was actually heard.
+ _misses++;
+ debugPrint('[Transcript] correction rejected (matched '
+ '${aligned?.matched ?? 0}/${aligned?.total ?? 0}, miss '
+ '$_misses) - keeping the transcript');
+ }
+ }
+ final lines = linesFromWords(words, exact: exact);
+ await TranscriptLineStore.instance.addLines(key, lines);
+ // Preview: the book's next sentences at estimated pace, dimmed on screen
+ // until the real transcription of that stretch replaces them. Keeps the
+ // display readable when playback outruns the transcriber.
+ if (exact && continuation.isNotEmpty && words.isNotEmpty) {
+ final avgWord = ((words.last.end - words.first.start) / words.length)
+ .clamp(0.15, 0.6);
+ final preview = estimatedWords(continuation, words.last.end, avgWord);
+ if (preview.isNotEmpty) {
+ await TranscriptLineStore.instance.addLines(
+ key, linesFromWords(preview, exact: true, approx: true));
+ }
+ }
+ return (
+ span: segs.isEmpty ? windowSeconds : segs.last.end,
+ wall: wall,
+ lines: lines.length,
+ exact: exact,
+ );
+ }
+
+ /// Mark a stretch as covered without any text - silence or music, so the
+ /// loop moves past it instead of retrying the same window forever.
+ Future<void> markEmpty(double start, double windowSeconds) =>
+ TranscriptLineStore.instance
+ .addLines(key, [TranscriptLine(start, start + windowSeconds, ' ')]);
+}
diff --git a/lib/services/transcript_line_store.dart b/lib/services/transcript_line_store.dart
new file mode 100644
index 00000000..486dbb9c
--- /dev/null
+++ b/lib/services/transcript_line_store.dart
@@ -0,0 +1,275 @@
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:flutter/foundation.dart';
+import 'package:path_provider/path_provider.dart';
+
+/// One displayed transcript line: a Whisper segment (or its ebook-corrected
+/// replacement), timed in the item's own seconds (episode-relative for
+/// podcast episodes, global book time for audiobooks).
+class TranscriptLine {
+ final double start;
+ final double end;
+ final String text;
+ /// True when the text came from the ebook's real words, not Whisper.
+ final bool exact;
+ /// Start time of each whitespace-separated word in [text], for read-along
+ /// word tracking. Empty on lines cached before word timing existed, and on
+ /// anything the timing couldn't be worked out for - callers fall back to
+ /// whole-line highlighting.
+ final List<double> wordStarts;
+ /// True for an estimated preview: the book's next sentences after the last
+ /// transcribed stretch, timed by pace guess rather than heard audio. Shown
+ /// dimmed, never counted as coverage, and replaced the moment the real
+ /// transcription of that stretch lands.
+ final bool approx;
+ const TranscriptLine(this.start, this.end, this.text,
+ {this.exact = false, this.wordStarts = const [], this.approx = false});
+
+ /// The words [wordStarts] is indexed against.
+ List<String> get words =>
+ text.trim().split(RegExp(r'\s+')).where((w) => w.isNotEmpty).toList();
+
+ /// Index into [words] of the word being spoken at [t], or -1 when the line
+ /// has no word timing or [t] sits before the first word.
+ int wordIndexAt(double t) {
+ if (wordStarts.isEmpty) return -1;
+ if (t < wordStarts.first) return -1;
+ var lo = 0, hi = wordStarts.length - 1;
+ while (lo < hi) {
+ final mid = (lo + hi + 1) >> 1;
+ if (wordStarts[mid] <= t) {
+ lo = mid;
+ } else {
+ hi = mid - 1;
+ }
+ }
+ return lo;
+ }
+
+ Map<String, dynamic> toJson() => {
+ 's': start,
+ 'e': end,
+ 't': text,
+ if (exact) 'x': true,
+ if (approx) 'a': true,
+ if (wordStarts.isNotEmpty)
+ 'w': [for (final w in wordStarts) double.parse(w.toStringAsFixed(2))],
+ };
+
+ factory TranscriptLine.fromJson(Map<String, dynamic> j) => TranscriptLine(
+ (j['s'] as num).toDouble(),
+ (j['e'] as num).toDouble(),
+ j['t'] as String? ?? '',
+ exact: j['x'] == true,
+ approx: j['a'] == true,
+ wordStarts: [
+ for (final w in (j['w'] as List<dynamic>? ?? const []))
+ (w as num).toDouble(),
+ ],
+ );
+}
+
+/// Per-book / per-episode cache of transcript lines, so audio is transcribed
+/// once ever: replays, seeks and app restarts are lookups. Keys are the same
+/// composite the rest of the app uses - itemId for books,
+/// "itemId-episodeId" for podcast episodes. One JSON file per key under
+/// app support; writes are debounced so the transcribe-ahead loop doesn't
+/// grind the disk.
+class TranscriptLineStore {
+ TranscriptLineStore._();
+ static final TranscriptLineStore instance = TranscriptLineStore._();
+
+ final Map<String, List<TranscriptLine>> _cache = {};
+ final Set<String> _dirty = {};
+ bool _flushScheduled = false;
+
+ Future<Directory> _dir() async {
+ final base = await getApplicationSupportDirectory();
+ final dir = Directory('${base.path}/transcript_lines');
+ if (!dir.existsSync()) dir.createSync(recursive: true);
+ return dir;
+ }
+
+ Future<File> _fileFor(String key) async =>
+ File('${(await _dir()).path}/$key.json');
+
+ Future<List<TranscriptLine>> _load(String key) async {
+ final cached = _cache[key];
+ if (cached != null) return cached;
+ var lines = <TranscriptLine>[];
+ try {
+ final f = await _fileFor(key);
+ if (f.existsSync()) {
+ final j = jsonDecode(await f.readAsString()) as Map<String, dynamic>;
+ lines = (j['lines'] as List<dynamic>? ?? const [])
+ .map((e) => TranscriptLine.fromJson(e as Map<String, dynamic>))
+ .toList();
+ }
+ } catch (e) {
+ debugPrint('[Lyrics] store load failed for $key: $e');
+ }
+ lines.sort((a, b) => a.start.compareTo(b.start));
+ _cache[key] = lines;
+ return lines;
+ }
+
+ /// Warm the in-memory cache; call before the sync lookups below.
+ Future<void> hydrate(String key) => _load(key);
+
+ /// The line containing [t], or null when that moment isn't transcribed yet
+ /// (or was silence/music). Binary search over the sorted cache.
+ TranscriptLine? lineAt(String key, double t) {
+ final lines = _cache[key];
+ if (lines == null || lines.isEmpty) return null;
+ var lo = 0, hi = lines.length - 1;
+ while (lo <= hi) {
+ final mid = (lo + hi) >> 1;
+ final l = lines[mid];
+ if (t < l.start) {
+ hi = mid - 1;
+ } else if (t > l.end) {
+ lo = mid + 1;
+ } else {
+ return l;
+ }
+ }
+ return null;
+ }
+
+ /// Like [lineAt], but forgiving around the gaps Whisper's VAD leaves
+ /// between segments: holds the previous line up to [holdAfter] seconds past
+ /// its end, and shows the next line up to [preShow] seconds early - so
+ /// inter-sentence silences don't blank the overlay.
+ TranscriptLine? lineNear(String key, double t,
+ {double holdAfter = 2.5, double preShow = 0.8}) {
+ final exact = lineAt(key, t);
+ if (exact != null) return exact;
+ final lines = _cache[key];
+ if (lines == null || lines.isEmpty) return null;
+ TranscriptLine? prev;
+ for (final l in lines) {
+ if (l.start > t) {
+ if (l.start - t <= preShow && l.text.trim().isNotEmpty) return l;
+ break;
+ }
+ prev = l;
+ }
+ if (prev != null && t - prev.end <= holdAfter && prev.text.trim().isNotEmpty) {
+ return prev;
+ }
+ return null;
+ }
+
+ /// Up to [count] lines running on from the one that begins at [start],
+ /// for filling a page of transcript downwards. Silence placeholders are
+ /// left out. Empty when that line isn't in the cache.
+ List<TranscriptLine> fromLine(String key, double start, int count) {
+ final lines = _cache[key];
+ if (lines == null || lines.isEmpty) return const [];
+ final idx = lines.indexWhere((l) => l.start == start);
+ if (idx < 0) return const [];
+ final out = <TranscriptLine>[];
+ for (var i = idx; i < lines.length && out.length < count; i++) {
+ if (lines[i].text.trim().isNotEmpty) out.add(lines[i]);
+ }
+ return out;
+ }
+
+ /// Total seconds of this item that have been transcribed, gaps excluded.
+ /// Silence placeholders count - that stretch is done with either way.
+ double preparedSeconds(String key) {
+ final lines = _cache[key];
+ if (lines == null || lines.isEmpty) return 0;
+ var total = 0.0;
+ var last = double.negativeInfinity;
+ for (final l in lines) {
+ if (l.approx) continue;
+ final from = l.start > last ? l.start : last;
+ if (l.end > from) total += l.end - from;
+ if (l.end > last) last = l.end;
+ }
+ return total;
+ }
+
+ /// How far continuous coverage extends from [from]: the end of the chained
+ /// lines starting at or before [from], tolerating small inter-line gaps
+ /// (Whisper trims silence between segments). Returns [from] when nothing
+ /// covers it yet.
+ double coveredUntil(String key, double from) {
+ final lines = _cache[key];
+ if (lines == null || lines.isEmpty) return from;
+ const gapTolerance = 4.0;
+ var covered = from;
+ for (final l in lines) {
+ // Estimated previews are display-only - counting them as coverage
+ // would stop the loop from ever really transcribing that stretch.
+ if (l.approx) continue;
+ if (l.end <= covered) continue;
+ if (l.start > covered + gapTolerance) break;
+ covered = l.end;
+ }
+ return covered;
+ }
+
+ /// Merge [fresh] into the cache. Lines overlapping an existing one are
+ /// dropped unless they're exact and the existing one isn't - an ebook
+ /// correction may upgrade a line, but a re-transcription never downgrades.
+ /// Estimated previews are the weakest of all: real lines always replace
+ /// them, and they never displace anything real.
+ Future<void> addLines(String key, List<TranscriptLine> fresh) async {
+ final lines = await _load(key);
+ var changed = false;
+ for (final nl in fresh) {
+ if (nl.text.trim().isEmpty) continue;
+ final overlapIdx = lines.indexWhere((e) =>
+ nl.start < e.end - 0.3 && nl.end > e.start + 0.3);
+ if (overlapIdx >= 0) {
+ final old = lines[overlapIdx];
+ final upgrade = old.approx
+ ? true // anything beats a preview, and a newer preview is fresher
+ : (!nl.approx && nl.exact && !old.exact);
+ if (upgrade) {
+ lines[overlapIdx] = nl;
+ // One real line can span several stale previews - sweep the rest.
+ lines.removeWhere((e) =>
+ !identical(e, nl) &&
+ e.approx &&
+ nl.start < e.end - 0.3 &&
+ nl.end > e.start + 0.3);
+ changed = true;
+ }
+ continue;
+ }
+ lines.add(nl);
+ changed = true;
+ }
+ if (!changed) return;
+ lines.sort((a, b) => a.start.compareTo(b.start));
+ _dirty.add(key);
+ _scheduleFlush();
+ }
+
+ void _scheduleFlush() {
+ if (_flushScheduled) return;
+ _flushScheduled = true;
+ Future.delayed(const Duration(seconds: 3), () async {
+ _flushScheduled = false;
+ final keys = _dirty.toList();
+ _dirty.clear();
+ for (final key in keys) {
+ final lines = _cache[key];
+ if (lines == null) continue;
+ try {
+ final f = await _fileFor(key);
+ await f.writeAsString(jsonEncode({
+ 'v': 1,
+ 'lines': [for (final l in lines) l.toJson()],
+ }));
+ } catch (e) {
+ debugPrint('[Lyrics] store write failed for $key: $e');
+ }
+ }
+ });
+ }
+}
diff --git a/lib/services/transcription_service.dart b/lib/services/transcription_service.dart
index 9c179a19..cec68fde 100644
--- a/lib/services/transcription_service.dart
+++ b/lib/services/transcription_service.dart
@@ -138,6 +138,9 @@ class TranscriptionService {
bool _busy = false;
bool _cancelDownload = false;
+ /// Which model the last run actually used, after [_modelFor] had its say.
+ TranscriptionModelSize? lastModelUsed;
+
// Detected language per book. 'auto' makes whisper run the encoder twice
// (once to detect, once to transcribe), so pay that only on a book's first
// window and pass the answer explicitly from then on.
@@ -224,6 +227,44 @@ class TranscriptionService {
bool canTranscribeBook(String itemId) =>
DownloadService().getLocalPaths(itemId)?.isNotEmpty ?? false;
+ /// Which model to run for one job.
+ ///
+ /// [preferAccuracy] says whether the words have to stand on their own. When
+ /// the book's EPUB is there, every word gets replaced by the book's own
+ /// anyway, so tiny is enough - and being several times faster is what keeps
+ /// the live transcript ahead of the narration. With no ebook the transcript
+ /// IS the text, so a bigger model earns its time: small for a one-shot
+ /// bookmark, base for read along, which small cannot keep realtime on.
+ Future<TranscriptionModelInfo> _modelFor(
+ bool? preferAccuracy, TranscriptionFeature feature) async {
+ final want = preferAccuracy == false
+ ? TranscriptionModelSize.tiny
+ : feature == TranscriptionFeature.bookmarks
+ ? TranscriptionModelSize.small
+ : TranscriptionModelSize.base;
+ // Only run what is actually on the device. When the intended model is
+ // missing, the nearest smaller one stands in (base for small) before
+ // anything bigger gets a turn.
+ const ladder = [
+ TranscriptionModelSize.small,
+ TranscriptionModelSize.base,
+ TranscriptionModelSize.tiny,
+ ];
+ final i = ladder.indexOf(want);
+ final order = [...ladder.sublist(i), ...ladder.sublist(0, i).reversed];
+ for (final s in order) {
+ if (!await isModelDownloaded(s)) continue;
+ final picked = TranscriptionModelInfo.forSize(s);
+ if (s != want) {
+ debugPrint('[Transcribe] using ${picked.fileName} - '
+ '${TranscriptionModelInfo.forSize(want).fileName} is not downloaded');
+ }
+ return picked;
+ }
+ // Nothing downloaded at all - the caller turns this into modelMissing.
+ return TranscriptionModelInfo.forSize(want);
+ }
+
/// Transcribe the audio around [positionSeconds] of [itemId]. Returns the
/// transcript text plus the path to the extracted 16kHz WAV clip, which the
/// caller can play back for review and MUST delete when finished. Throws
@@ -245,6 +286,7 @@ class TranscriptionService {
if (!await isModelDownloaded(info.size)) {
throw TranscriptionException(TranscriptionError.modelMissing);
}
+ lastModelUsed = info.size;
final localPaths = DownloadService().getLocalPaths(itemId);
if (localPaths == null || localPaths.isEmpty) {
@@ -368,6 +410,7 @@ class TranscriptionService {
if (!await isModelDownloaded(info.size)) {
throw TranscriptionException(TranscriptionError.modelMissing);
}
+ lastModelUsed = info.size;
final localPaths = DownloadService().getLocalPaths(itemId);
if (localPaths == null || localPaths.isEmpty) {
@@ -430,8 +473,8 @@ class TranscriptionService {
withTimestamps: true,
// See transcribeAt - Silero cost more per window than whisper.
vadMode: WhisperVadMode.disabled,
- // This path feeds alignment, where a late answer is as bad as a
- // wrong one. A failed window just gets skipped over.
+ // This path feeds the live transcript, where a late answer is as
+ // bad as a wrong one. A failed window just gets skipped over.
noFallback: true,
threads: _threads(),
);
@@ -451,7 +494,7 @@ class TranscriptionService {
debugPrint('[Transcribe] segments window=${window.toStringAsFixed(1)}s '
'model=${info.fileName} lang=$lang extract=${extractMs}ms '
'whisper=${watch.elapsedMilliseconds - extractMs}ms '
- 'segments=${segments.length}');
+ 'threads=${_threads()} segments=${segments.length}');
if (segments.isEmpty) throw TranscriptionException(TranscriptionError.empty);
return segments;
} finally {
@@ -467,44 +510,12 @@ class TranscriptionService {
// Internals
- int _threads() => (Platform.numberOfProcessors - 1).clamp(2, 6);
-
- /// Which model to run for one job.
- ///
- /// [preferAccuracy] says whether the words have to stand on their own. When
- /// the book's EPUB is there, every word gets matched against the book's own
- /// anyway, so tiny is enough - and it is several times faster. With no ebook
- /// the transcript IS the text, so a bigger model earns its time: small for a
- /// one-shot bookmark, base for continuous alignment work.
- Future<TranscriptionModelInfo> _modelFor(
- bool? preferAccuracy, TranscriptionFeature feature) async {
- final want = preferAccuracy == false
- ? TranscriptionModelSize.tiny
- : feature == TranscriptionFeature.bookmarks
- ? TranscriptionModelSize.small
- : TranscriptionModelSize.base;
- // Only run what is actually on the device. When the intended model is
- // missing, the nearest smaller one stands in (base for small) before
- // anything bigger gets a turn.
- const ladder = [
- TranscriptionModelSize.small,
- TranscriptionModelSize.base,
- TranscriptionModelSize.tiny,
- ];
- final i = ladder.indexOf(want);
- final order = [...ladder.sublist(i), ...ladder.sublist(0, i).reversed];
- for (final s in order) {
- if (!await isModelDownloaded(s)) continue;
- final picked = TranscriptionModelInfo.forSize(s);
- if (s != want) {
- debugPrint('[Transcribe] using ${picked.fileName} - '
- '${TranscriptionModelInfo.forSize(want).fileName} is not downloaded');
- }
- return picked;
- }
- // Nothing downloaded at all - the caller turns this into modelMissing.
- return TranscriptionModelInfo.forSize(want);
- }
+ // Capped at 4, not core count: whisper barriers its threads at every layer,
+ // so the slowest thread gates each step. On big.LITTLE phones a fifth and
+ // sixth thread land on little cores, and once Android's touch boost decays
+ // those drop to idle clocks and drag a 7s window out past 30s. Four threads
+ // stay on the big and mid cores and hold their speed unboosted.
+ int _threads() => (Platform.numberOfProcessors - 1).clamp(2, 4);
/// Remember what language detection settled on, but only from a window that
/// actually produced output - a detection made on silence or music would
diff --git a/lib/utils/passage_match.dart b/lib/utils/passage_match.dart
index 1a1ad487..a955ae04 100644
--- a/lib/utils/passage_match.dart
+++ b/lib/utils/passage_match.dart
@@ -116,6 +116,57 @@ String? correctFromEpub(({String epubPath, String transcript}) args) {
return _snapToSentences(sections[b.section], b.start, b.end);
}
+/// What the book says next, after a matched passage.
+typedef PassageWithNext = ({String passage, String continuation});
+
+/// [correctFromEpub], plus the book's next couple of sentences after the
+/// matched passage. The continuation is text the audio window never reached -
+/// the live transcript shows it as an estimated preview until the real
+/// transcription of that stretch lands.
+PassageWithNext? correctFromEpubWithNext(
+ ({String epubPath, String transcript}) args) {
+ final passage = correctFromEpub(args);
+ if (passage == null) return null;
+ // Find the passage again to know where it ends. Cheap next to the match
+ // itself, and it keeps correctFromEpub's shape untouched for its other
+ // callers.
+ try {
+ for (final text in _epubSectionTexts(args.epubPath)) {
+ final normalized = text.replaceAll(RegExp(r'\s+'), ' ');
+ final idx = normalized.indexOf(passage);
+ if (idx < 0) continue;
+ return (
+ passage: passage,
+ continuation: _followingSentences(normalized, idx + passage.length),
+ );
+ }
+ } catch (e) {
+ debugPrint('[PassageMatch] continuation lookup failed: $e');
+ }
+ return (passage: passage, continuation: '');
+}
+
+/// Up to two sentences (bounded) after [from], for the estimated preview.
+String _followingSentences(String text, int from) {
+ final maxE = (from + 320).clamp(0, text.length);
+ var sentences = 0;
+ var e = maxE;
+ for (var i = from; i < maxE; i++) {
+ final c = text[i];
+ if (c == '.' || c == '!' || c == '?') {
+ var j = i + 1;
+ while (j < text.length && const ['"', "'", '”', '’'].contains(text[j])) {
+ j++;
+ }
+ if (++sentences >= 2) {
+ e = j;
+ break;
+ }
+ }
+ }
+ return text.substring(from, e).trim();
+}
+
class _Candidate {
final int section;
final int start;
diff --git a/lib/utils/transcript_align.dart b/lib/utils/transcript_align.dart
new file mode 100644
index 00000000..23af2337
--- /dev/null
+++ b/lib/utils/transcript_align.dart
@@ -0,0 +1,243 @@
+import '../services/transcript_line_store.dart';
+
+/// Word-level alignment between what Whisper heard and what the book says.
+///
+/// Whisper gives timestamps per segment, not per word, and the ebook
+/// correction hands back a passage in the book's own words that only roughly
+/// corresponds to the segments. Spreading that passage over the segment times
+/// by character share (the old approach) drifts further the longer a chunk
+/// runs, because the book's phrasing and the narrator's pace don't line up
+/// character for character.
+///
+/// So instead: give every heard word a time inside its own segment, then
+/// align the book's words onto the heard words and let each one inherit the
+/// time of the word it matched. Anchors land every few words rather than
+/// every 30 seconds, and text the alignment can't account for is dropped
+/// rather than given an invented timestamp.
+
+/// A word and when it is spoken, in the item's own seconds.
+class TimedWord {
+ final String text;
+ final double start;
+ final double end;
+ const TimedWord(this.text, this.start, this.end);
+}
+
+final _wordRe = RegExp(r'\S+');
+
+/// Strip a word down to what two spellings of it have in common, for matching.
+String normalizeWord(String w) {
+ final buf = StringBuffer();
+ for (final c in w.toLowerCase().codeUnits) {
+ final isDigit = c >= 0x30 && c <= 0x39;
+ final isLower = c >= 0x61 && c <= 0x7A;
+ final isApos = c == 0x27;
+ if (isDigit || isLower || isApos) buf.writeCharCode(c);
+ }
+ return buf.toString().replaceAll("'", '');
+}
+
+/// Every word of [segments], timed inside its own segment by character share.
+/// The segment bounds are real Whisper timestamps, so a word is never more
+/// than one segment away from solid ground.
+List<TimedWord> wordsFromSegments(
+ List<({double start, double end, String text})> segments) {
+ final out = <TimedWord>[];
+ for (final seg in segments) {
+ final text = seg.text;
+ final matches = _wordRe.allMatches(text).toList();
+ if (matches.isEmpty) continue;
+ final span = seg.end - seg.start;
+ final chars = text.length;
+ double at(int offset) =>
+ chars <= 0 ? seg.start : seg.start + span * (offset / chars);
+ for (var i = 0; i < matches.length; i++) {
+ final m = matches[i];
+ final start = at(m.start);
+ final end = i + 1 < matches.length ? at(matches[i + 1].start) : seg.end;
+ out.add(TimedWord(m.group(0)!, start, end < start ? start : end));
+ }
+ }
+ return out;
+}
+
+/// True when two words are the same word, allowing for one character of
+/// Whisper mishearing on words long enough for that to be a typo rather than
+/// a different word.
+bool _sameWord(String a, String b) {
+ if (a == b) return true;
+ if (a.length < 4 || b.length < 4) return false;
+ if ((a.length - b.length).abs() > 1) return false;
+ var i = 0, j = 0, edits = 0;
+ while (i < a.length && j < b.length) {
+ if (a[i] == b[j]) {
+ i++;
+ j++;
+ continue;
+ }
+ if (++edits > 1) return false;
+ if (a.length == b.length) {
+ i++;
+ j++;
+ } else if (a.length > b.length) {
+ i++;
+ } else {
+ j++;
+ }
+ }
+ return edits + (a.length - i) + (b.length - j) <= 1;
+}
+
+/// Result of laying the book's words over the heard ones.
+typedef AlignedText = ({List<TimedWord> words, int matched, int total});
+
+/// Time [corrected] (the book's own wording) against [spoken] (what Whisper
+/// heard, already timed). Book words that matched a heard word take its time;
+/// runs in between are spread across the gap by character share. Text before
+/// the first match is dropped, and text after the last match is kept only far
+/// enough to finish the sentence in progress - the passage matcher snaps out
+/// to sentence boundaries, so anything beyond that was never in this stretch
+/// of audio.
+///
+/// Returns null when too little of the passage lines up to trust it, which
+/// leaves the honest Whisper text in place rather than showing the right
+/// words at the wrong moments.
+AlignedText? alignCorrected(List<TimedWord> spoken, String corrected,
+ {double minMatchRatio = 0.5}) {
+ if (spoken.isEmpty) return null;
+ final bookWords =
+ _wordRe.allMatches(corrected).map((m) => m.group(0)!).toList();
+ if (bookWords.isEmpty) return null;
+
+ final a = [for (final w in spoken) normalizeWord(w.text)];
+ final b = [for (final w in bookWords) normalizeWord(w)];
+
+ // Longest common subsequence over the two word lists; the backtrack gives
+ // the pairs that anchor the book's words to real timestamps.
+ final n = a.length, m = b.length;
+ final dp = List.generate(n + 1, (_) => List<int>.filled(m + 1, 0));
+ for (var i = n - 1; i >= 0; i--) {
+ for (var j = m - 1; j >= 0; j--) {
+ dp[i][j] = a[i].isNotEmpty && _sameWord(a[i], b[j])
+ ? dp[i + 1][j + 1] + 1
+ : (dp[i + 1][j] >= dp[i][j + 1] ? dp[i + 1][j] : dp[i][j + 1]);
+ }
+ }
+ final anchors = <({int book, int heard})>[];
+ var i = 0, j = 0;
+ while (i < n && j < m) {
+ if (a[i].isNotEmpty && _sameWord(a[i], b[j])) {
+ anchors.add((book: j, heard: i));
+ i++;
+ j++;
+ } else if (dp[i + 1][j] >= dp[i][j + 1]) {
+ i++;
+ } else {
+ j++;
+ }
+ }
+ if (anchors.isEmpty) return null;
+ final shorter = n < m ? n : m;
+ if (anchors.length < (shorter * minMatchRatio)) {
+ return (words: const [], matched: anchors.length, total: shorter);
+ }
+
+ final out = <TimedWord>[];
+ void addAnchor(int idx) {
+ final at = anchors[idx];
+ final s = spoken[at.heard];
+ out.add(TimedWord(bookWords[at.book], s.start, s.end));
+ }
+
+ addAnchor(0);
+ for (var t = 1; t < anchors.length; t++) {
+ final prev = anchors[t - 1], next = anchors[t];
+ final from = spoken[prev.heard].end;
+ final to = spoken[next.heard].start;
+ final span = to > from ? to - from : 0.0;
+ var total = 0;
+ for (var q = prev.book + 1; q < next.book; q++) {
+ total += bookWords[q].length + 1;
+ }
+ // Words the narrator's version didn't have: share the gap between the two
+ // anchors out by how much text each one is.
+ var before = 0;
+ for (var q = prev.book + 1; q < next.book; q++) {
+ final start = total <= 0 ? from : from + span * (before / total);
+ out.add(TimedWord(bookWords[q], start, start));
+ before += bookWords[q].length + 1;
+ }
+ addAnchor(t);
+ }
+ // Finish the sentence the last anchor sits in. The window cut it off
+ // mid-flow and Whisper garbles the truncated edge, so its closing words
+ // anchor in neither this chunk nor the next - without this they vanish
+ // from the transcript entirely (the store then rejects the next chunk's
+ // overlapping partial repeat of the same sentence). Timing is estimated
+ // from this chunk's own word pace and capped, so the reach past the last
+ // real timestamp stays small.
+ final lastBook = anchors.last.book;
+ if (lastBook + 1 < bookWords.length &&
+ !_endsSentence.hasMatch(bookWords[lastBook])) {
+ final avgWord = ((spoken.last.end - spoken.first.start) / spoken.length)
+ .clamp(0.15, 0.6);
+ var at = out.last.end;
+ final capEnd = out.last.end + 3.0;
+ for (var q = lastBook + 1;
+ q < bookWords.length && q <= lastBook + 12 && at < capEnd;
+ q++) {
+ final end = (at + avgWord) > capEnd ? capEnd : at + avgWord;
+ out.add(TimedWord(bookWords[q], at, end));
+ at = end;
+ if (_endsSentence.hasMatch(bookWords[q])) break;
+ }
+ }
+ // Every word ends where the next one starts, so a line's span is continuous.
+ for (var k = 0; k < out.length; k++) {
+ final end = k + 1 < out.length ? out[k + 1].start : out[k].end;
+ if (end > out[k].start) {
+ out[k] = TimedWord(out[k].text, out[k].start, end);
+ }
+ }
+ return (words: out, matched: anchors.length, total: shorter);
+}
+
+final _endsSentence = RegExp(r'''[.!?]["'”’)\]]*$''');
+
+/// [text]'s words timed by a flat per-word estimate from [from] - for the
+/// preview of book sentences no audio has reached yet.
+List<TimedWord> estimatedWords(String text, double from, double perWord) {
+ final out = <TimedWord>[];
+ var at = from;
+ for (final m in _wordRe.allMatches(text)) {
+ out.add(TimedWord(m.group(0)!, at, at + perWord));
+ at += perWord;
+ }
+ return out;
+}
+
+/// Group timed words into one line per sentence, each carrying its words'
+/// start times for word-by-word read along.
+List<TranscriptLine> linesFromWords(List<TimedWord> words,
+ {required bool exact, bool approx = false}) {
+ final out = <TranscriptLine>[];
+ var from = 0;
+ void flush(int to) {
+ if (to <= from) return;
+ final run = words.sublist(from, to);
+ final text = run.map((w) => w.text).join(' ');
+ if (text.trim().isNotEmpty) {
+ out.add(TranscriptLine(run.first.start, run.last.end, text,
+ exact: exact,
+ approx: approx,
+ wordStarts: [for (final w in run) w.start]));
+ }
+ from = to;
+ }
+
+ for (var i = 0; i < words.length; i++) {
+ if (_endsSentence.hasMatch(words[i].text)) flush(i + 1);
+ }
+ flush(words.length);
+ return out;
+}
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index 397dbe14..0482bc92 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -16,6 +16,8 @@ import 'ebook_router.dart';
import 'overlay_toast.dart';
import '../services/ebook_cache.dart';
import '../services/find_in_ebook.dart';
+import '../services/lyrics_service.dart';
+import 'lyrics_overlay.dart';
import 'card_edge_progress_bar.dart';
import 'card_progress_bar.dart';
import 'card_playback_controls.dart';
@@ -592,6 +594,10 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
}
+ /// The store key the live transcript runs under for this card.
+ String get _lyricsKey =>
+ _episodeId != null ? '$_itemId-$_episodeId' : _itemId;
+
@override
Widget build(BuildContext context) {
super.build(context); // required for AutomaticKeepAliveClientMixin
@@ -924,8 +930,16 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
child: Stack(
fit: StackFit.expand,
children: [
- // Cover image
- EinkCoverTone(child: coverUrl != null && coverIdentity != null
+ // Cover image - hidden while the transcript
+ // has the cover.
+ AnimatedBuilder(
+ animation: LyricsService.instance,
+ builder: (context, child) =>
+ LyricsService.instance
+ .coversArtFor(_lyricsKey)
+ ? Offstage(child: child)
+ : child!,
+ child: EinkCoverTone(child: coverUrl != null && coverIdentity != null
? isLocalCover
? BlurPaddedCover(blurChild: Image.file(File(coverUrl), fit: BoxFit.cover,
gaplessPlayback: true,
@@ -945,7 +959,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
},
placeholder: (_, __) => CoverPlaceholder(title: _title, author: _author),
errorWidget: (_, __, ___) => CoverPlaceholder(title: _title, author: _author)))
- : CoverPlaceholder(title: _title, author: _author)),
+ : CoverPlaceholder(title: _title, author: _author))),
// Casting overlay
if (isCastingThis) ...[
Positioned.fill(
@@ -978,9 +992,18 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
),
),
],
- // Play/pause overlay
+ // Play/pause overlay - the tap still works
+ // with the transcript up, but the button would
+ // sit on the words.
if (_coverPlayButton) Positioned.fill(
- child: AnimatedContainer(
+ child: AnimatedBuilder(
+ animation: LyricsService.instance,
+ builder: (context, child) =>
+ LyricsService.instance
+ .coversArtFor(_lyricsKey)
+ ? Offstage(child: child)
+ : child!,
+ child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
decoration: BoxDecoration(
color: coverPlaying ? Colors.transparent : Colors.black.withValues(alpha: 0.25),
@@ -1015,7 +1038,13 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
),
),
),
- ),
+ )),
+ // Live transcript (lyrics mode)
+ LyricsOverlay(
+ compact: true,
+ forKey: _lyricsKey,
+ surface: cs.surface,
+ onSurface: cs.onSurface),
],
),
),
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index 84c233f1..20b74819 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -991,7 +991,8 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
])),
]));
})]],
- if (!lib.isOffline) ...[const SizedBox(height: 20),
+ const SizedBox(height: 20),
+ if (!lib.isOffline) ...[const SizedBox(height: 8),
OutlinedButton.icon(
onPressed: () => showBookStatsSheet(context, itemId: widget.itemId, title: title),
icon: const Icon(Icons.insights_rounded, size: 18),
diff --git a/lib/widgets/card_button_config.dart b/lib/widgets/card_button_config.dart
index 13baae4f..870131b3 100644
--- a/lib/widgets/card_button_config.dart
+++ b/lib/widgets/card_button_config.dart
@@ -45,6 +45,8 @@ String localizedCardButtonLabel(AppLocalizations l, CardButtonDef def) {
return 'Read';
case 'findinebook':
return l.findInEbook;
+ case 'lyrics':
+ return l.lyricsMode;
}
return def.label;
}
@@ -76,6 +78,7 @@ const _allCardButtons = [
CardButtonDef('download', 'Download', Icons.download_outlined),
CardButtonDef('ebook', 'Read', Icons.menu_book_rounded),
CardButtonDef('findinebook', 'Find position in ebook', Icons.manage_search_rounded),
+ CardButtonDef('lyrics', 'Live transcript', Icons.subtitles_rounded),
];
/// Card buttons filtered for the current platform.
diff --git a/lib/widgets/card_buttons.dart b/lib/widgets/card_buttons.dart
index 3131ccb1..c9e98108 100644
--- a/lib/widgets/card_buttons.dart
+++ b/lib/widgets/card_buttons.dart
@@ -13,6 +13,8 @@ import '../services/audio_player_service.dart';
import '../services/bookmark_service.dart';
import '../services/chromecast_service.dart';
import '../services/download_service.dart';
+import '../services/lyrics_service.dart';
+import '../services/transcription_service.dart';
import '../services/equalizer_service.dart';
import '../services/playback_history_service.dart';
import '../services/scoped_prefs.dart';
@@ -33,6 +35,7 @@ import 'listening_session_card.dart';
import 'notes_sheet.dart';
import 'overlay_toast.dart';
import 'sleep_timer_sheet.dart';
+import 'transcription_download_prompt.dart';
/// E-ink screens dither faint greys and cover-colored accents into noise, so
/// the card controls bump to near-black when e-ink mode is on. Disabled stays
@@ -1542,6 +1545,87 @@ class CardActionDelegate {
int get visibleButtonCount => visibleCount;
+ /// Toggle the live transcript (lyrics) overlay for whatever is loaded in
+ /// the player. Guards mirror the other transcription entry points.
+ Future<void> _toggleLyrics() async {
+ final l = AppLocalizations.of(context)!;
+ if (LyricsService.instance.isOn) {
+ LyricsService.instance.disable();
+ return;
+ }
+ if (!await PlayerSettings.getTranscriptionEnabled()) {
+ if (context.mounted) {
+ showOverlayToast(context, l.transcriptionDisabledHint,
+ icon: Icons.record_voice_over_rounded);
+ }
+ return;
+ }
+ final cardKey = episodeId != null ? '$itemId-$episodeId' : itemId;
+ if (!TranscriptionService.instance.canTranscribeBook(cardKey)) {
+ if (context.mounted) {
+ await promptDownloadForTranscription(context,
+ itemId: itemId,
+ episodeId: episodeId,
+ title: title,
+ author: author,
+ coverUrl: coverUrl);
+ }
+ return;
+ }
+ // One-time expectation setting: what this does, why to let the head
+ // start build before pressing play, and the battery cost.
+ if (!await PlayerSettings.getLyricsIntroShown()) {
+ if (!context.mounted) return;
+ final go = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ icon: const Icon(Icons.subtitles_rounded),
+ title: Text(l.lyricsMode),
+ content: Text(l.lyricsIntroBody),
+ actions: [
+ TextButton(
+ onPressed: () => Navigator.pop(ctx, false),
+ child: Text(l.cancel),
+ ),
+ FilledButton(
+ onPressed: () => Navigator.pop(ctx, true),
+ child: Text(l.lyricsTurnOn),
+ ),
+ ],
+ ),
+ );
+ if (go != true) return;
+ await PlayerSettings.setLyricsIntroShown();
+ }
+ // The transcript is for THIS card's book. When something else (or
+ // nothing) is loaded, load it paused with no session - the runway builds
+ // while it sits ready, and pressing play is still the user's move.
+ if (LyricsService.playerKey() != cardKey) {
+ if (!context.mounted) return;
+ final api = context.read<AuthProvider>().apiService;
+ if (api == null) return;
+ final error = await player.playItem(
+ api: api,
+ itemId: itemId,
+ episodeId: episodeId,
+ title: title,
+ author: author ?? '',
+ coverUrl: coverUrl,
+ totalDuration: duration,
+ chapters: chapters,
+ libraryId: item['libraryId'] as String?,
+ loadOnly: true,
+ );
+ if (error != null) {
+ if (context.mounted) {
+ showOverlayToast(context, error, icon: Icons.error_outline_rounded);
+ }
+ return;
+ }
+ }
+ await LyricsService.instance.enableForCurrent();
+ }
+
// Predefined row groupings per button count.
// Labels: max 3 per row. Icons: max 4 per row (5 allowed at count=5).
static const _labelRows = <int, List<int>>{
@@ -1786,6 +1870,16 @@ class CardActionDelegate {
accent: accent, isActive: true, alwaysEnabled: true, large: large, compact: compact, iconsOnly: iconsOnly,
onTap: () => onFindInEbookTap?.call(),
);
+ case 'lyrics':
+ // Usable with nothing playing: the handler hot-loads this card's
+ // book paused so the runway can build before the user presses play.
+ return CardWideButton(
+ icon: Icons.subtitles_rounded,
+ label: l.lyricsMode,
+ accent: accent, isActive: true, alwaysEnabled: true, large: large, compact: compact, iconsOnly: iconsOnly,
+ highlighted: LyricsService.instance.isOn,
+ onTap: _toggleLyrics,
+ );
default:
return const SizedBox.shrink();
}
@@ -1969,6 +2063,15 @@ class CardActionDelegate {
onFindInEbookTap?.call();
},
);
+ case 'lyrics':
+ return MoreMenuItem(
+ icon: Icons.subtitles_rounded,
+ label: l.lyricsMode, accent: accent,
+ onTap: () {
+ Navigator.pop(ctx);
+ _toggleLyrics();
+ },
+ );
default:
return const SizedBox.shrink();
}
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index 1efb79ec..6d3257ce 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -18,9 +18,13 @@ import '../services/find_in_ebook.dart';
import '../services/progress_sync_service.dart';
import '../services/reader_font_service.dart';
import '../services/scoped_prefs.dart';
+import '../services/lyrics_service.dart';
+import '../services/read_along_script.dart';
+import '../services/transcript_line_store.dart';
import '../services/transcription_service.dart';
import '../services/volume_key_service.dart';
import 'overlay_toast.dart';
+import 'transcription_download_prompt.dart';
import 'progress_dialog.dart';
import 'quote_share_sheet.dart';
import 'card_buttons.dart' show CardSpeedSheet;
@@ -149,6 +153,27 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
bool _finding = false;
// Shows the Find-in-audiobook action on the selection toolbar.
bool _transcriptionOn = false;
+ // Read along: follow the audiobook by coloring the words being spoken.
+ bool _readAlongOn = false;
+ Timer? _readAlongTimer;
+ double? _readAlongLineStart;
+ int _readAlongColor = PlayerSettings.defaultReadAlongColor;
+ String _readAlongMode = 'word';
+ // Words in the sentence as the page spells it, and as the transcript line
+ // does - the two can differ, so word positions are scaled between them.
+ int _readAlongPageWords = 0;
+ int _readAlongLineWords = 0;
+ int _readAlongWordIndex = -1;
+ // Where the current sentence sits in the page text, so the next lookup can
+ // prefer a match ahead of it rather than an earlier repeat of the phrase.
+ int _readAlongAt = -1;
+ double _readAlongLastPos = 0;
+ DateTime _readAlongLastScan = DateTime.fromMillisecondsSinceEpoch(0);
+ // Guards against turning the page twice for the same spot.
+ String? _readAlongTurnedTo;
+ DateTime _readAlongLastTurn = DateTime.fromMillisecondsSinceEpoch(0);
+ bool _readAlongStartedPipeline = false;
+ bool _readAlongLocating = false;
// Reader settings
int _fontSize = 16;
@@ -244,11 +269,21 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
if (mounted && _readerActive) _epubController?.display(cfi: cfi);
});
}
- } else {
+ } else if (state == AppLifecycleState.paused ||
+ state == AppLifecycleState.hidden) {
// Leaving the foreground: snapshot the real page and stop trusting
// relocations until we're back (they'll be resize artifacts).
if (_readerActive) _bgCfi = _currentCfi;
_readerActive = false;
+ // Following along costs transcription and a WebView call several times
+ // a second, and nobody is reading a page they can't see.
+ if (_readAlongOn) {
+ debugPrint('[ReadAlong] app backgrounded - stopping');
+ _stopReadAlong();
+ }
+ } else {
+ if (_readerActive) _bgCfi = _currentCfi;
+ _readerActive = false;
}
}
@@ -498,6 +533,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
@override
void dispose() {
+ _stopReadAlong();
_quietLib.setReaderQuiet(false);
WidgetsBinding.instance.removeObserver(this);
_volumeNav.detach();
@@ -2206,51 +2242,54 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
// Set expectations (it listens for a while, a bad match moves nothing) and
// let the user pick where a successful find lands. Choice is remembered.
- var goToPlayer = await PlayerSettings.getFindInAudiobookGoToPlayer();
+ var after = await PlayerSettings.getFindInAudiobookAfter();
if (!mounted) return;
final go = await showDialog<bool>(
context: context,
builder: (ctx) => StatefulBuilder(
- builder: (ctx, setDialogState) => AlertDialog(
- icon: const Icon(Icons.headphones_rounded),
- title: Text(l.findInAudiobook),
- content: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(l.findInAudiobookIntroBody),
- const SizedBox(height: 16),
- Text(l.findInAudiobookAfterLabel,
- style: Theme.of(ctx).textTheme.labelMedium?.copyWith(
- color: Theme.of(ctx).colorScheme.onSurfaceVariant)),
- const SizedBox(height: 8),
- SegmentedButton<bool>(
- segments: [
- ButtonSegment(value: false, label: Text(l.findInAudiobookStay)),
- ButtonSegment(value: true, label: Text(l.findInAudiobookGoPlayer)),
- ],
- selected: {goToPlayer},
- showSelectedIcon: false,
- onSelectionChanged: (sel) =>
- setDialogState(() => goToPlayer = sel.first),
+ builder: (ctx, setDialogState) {
+ Widget option(String value, String label) => RadioListTile<String>(
+ dense: true,
+ contentPadding: EdgeInsets.zero,
+ title: Text(label),
+ value: value,
+ groupValue: after,
+ onChanged: (v) => setDialogState(() => after = v ?? after),
+ );
+ return AlertDialog(
+ icon: const Icon(Icons.headphones_rounded),
+ title: Text(l.findInAudiobook),
+ content: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(l.findInAudiobookIntroBody),
+ const SizedBox(height: 16),
+ Text(l.findInAudiobookAfterLabel,
+ style: Theme.of(ctx).textTheme.labelMedium?.copyWith(
+ color: Theme.of(ctx).colorScheme.onSurfaceVariant)),
+ const SizedBox(height: 4),
+ option('stay', l.findInAudiobookStay),
+ option('player', l.findInAudiobookGoPlayer),
+ option('readalong', l.readAlong),
+ ],
+ ),
+ actions: [
+ TextButton(
+ onPressed: () => Navigator.pop(ctx, false),
+ child: Text(l.cancel),
+ ),
+ FilledButton(
+ onPressed: () => Navigator.pop(ctx, true),
+ child: Text(l.findInAudiobook),
),
],
- ),
- actions: [
- TextButton(
- onPressed: () => Navigator.pop(ctx, false),
- child: Text(l.cancel),
- ),
- FilledButton(
- onPressed: () => Navigator.pop(ctx, true),
- child: Text(l.findInAudiobook),
- ),
- ],
- ),
+ );
+ },
),
);
if (go != true || !mounted) return;
- await PlayerSettings.setFindInAudiobookGoToPlayer(goToPlayer);
+ await PlayerSettings.setFindInAudiobookAfter(after);
if (!mounted) return;
showProgressDialog(context, l.findInAudiobookSearching);
@@ -2267,7 +2306,20 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
icon: Icons.search_off_rounded);
return;
}
- await _startAudioAt(targetTime, goToPlayer: goToPlayer, passageCfi: cfi);
+ await _startAudioAt(targetTime,
+ goToPlayer: after == 'player', passageCfi: cfi);
+ // Third landing: stay in the reader and follow along from the spot the
+ // user highlighted, now that the audio is playing this book. playItem can
+ // resolve a beat before the player state settles - wait for it, or the
+ // toggle would reload the book and lose the found position.
+ if (after == 'readalong' && mounted && !_readAlongOn) {
+ for (var i = 0;
+ i < 20 && AudioPlayerService().currentItemId != widget.itemId;
+ i++) {
+ await Future.delayed(const Duration(milliseconds: 150));
+ }
+ if (mounted) await _toggleReadAlong();
+ }
}
Future<double?> _locateAudioForSelection(String cfi, String selText) async {
@@ -2500,6 +2552,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
itemId: widget.itemId,
startSeconds: probeStart,
windowSeconds: _probeWindowSeconds,
+ preferAccuracy: false,
);
} on TranscriptionException catch (e) {
// A silent or music-only window transcribes to nothing, and a probe
@@ -2848,6 +2901,386 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
_flashHighlight(cfi);
}
+ /// Load this book into the player and start it, for read along pressed
+ /// while something else (or nothing) is playing - pressing the feature IS
+ /// the intent to hear this book. False when it could not start (offline
+ /// with no API, or the item failed to load).
+ Future<bool> _ensureThisBookPlaying() async {
+ final player = AudioPlayerService();
+ if (player.currentItemId == widget.itemId &&
+ player.currentEpisodeId == null) {
+ if (!player.isPlaying) await player.play();
+ return true;
+ }
+ final api = context.read<AuthProvider>().apiService;
+ final lib = context.read<LibraryProvider>();
+ if (api == null) return false;
+ final fullItem = await api.getLibraryItem(widget.itemId);
+ if (fullItem == null || !mounted) return false;
+ final media = fullItem['media'] as Map<String, dynamic>? ?? {};
+ final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
+ final error = await player.playItem(
+ api: api,
+ itemId: widget.itemId,
+ title: metadata['title'] as String? ?? widget.title,
+ author: metadata['authorName'] as String? ?? '',
+ coverUrl: lib.getCoverUrl(widget.itemId),
+ totalDuration:
+ (media['duration'] is num) ? (media['duration'] as num).toDouble() : 0.0,
+ chapters: (media['chapters'] as List<dynamic>?) ?? [],
+ libraryId: fullItem['libraryId'] as String?,
+ );
+ return error == null;
+ }
+
+ /// Toggle read along (episodes have no ebook). Reuses the live-transcript
+ /// pipeline for lines, so the cache is shared with the player overlay. A
+ /// book that isn't playing yet gets loaded and started - nobody presses
+ /// read along on a book they don't want to hear.
+ Future<void> _toggleReadAlong() async {
+ final l = AppLocalizations.of(context)!;
+ if (_readAlongOn) {
+ _stopReadAlong();
+ return;
+ }
+ final player = AudioPlayerService();
+ if (player.currentItemId != widget.itemId ||
+ player.currentEpisodeId != null) {
+ final ok = await _ensureThisBookPlaying();
+ if (!ok || !mounted) {
+ if (mounted) {
+ showOverlayToast(context, l.readAlongNeedsPlaying,
+ icon: Icons.headphones_rounded);
+ }
+ return;
+ }
+ }
+ if (!await PlayerSettings.getTranscriptionEnabled()) {
+ if (mounted) {
+ showOverlayToast(context, l.transcriptionDisabledHint,
+ icon: Icons.record_voice_over_rounded);
+ }
+ return;
+ }
+ if (!TranscriptionService.instance.canTranscribeBook(widget.itemId)) {
+ if (mounted) {
+ await promptDownloadForTranscription(context,
+ itemId: widget.itemId, title: widget.title);
+ }
+ return;
+ }
+ if (!LyricsService.instance.isOn) {
+ await LyricsService.instance.enableForCurrent();
+ _readAlongStartedPipeline = true;
+ }
+ _readAlongColor = LyricsService.instance.readAlongColor;
+ _readAlongMode = LyricsService.instance.readAlongMode;
+ _readAlongLastPos = player.position.inMilliseconds / 1000.0;
+ final wc = _epubController?.webViewController;
+ await wc?.evaluateJavascript(source: readAlongBootstrap(_readAlongColor));
+ await wc?.evaluateJavascript(
+ source: 'window.__absorbRA && __absorbRA.start()');
+ if (mounted) setState(() => _readAlongOn = true);
+ _readAlongTimer?.cancel();
+ _readAlongTimer = Timer.periodic(
+ const Duration(milliseconds: 250), (_) => _readAlongTick());
+ debugPrint('[ReadAlong] enabled for ${widget.itemId}');
+ }
+
+ /// The same transcript sync adjuster the player overlay has, for tuning
+ /// read along against Bluetooth latency without leaving the reader. Lives
+ /// in the bottom chrome, so it is there whenever the controls are.
+ Widget _readAlongSyncPill(Color fg) {
+ final svc = LyricsService.instance;
+ return AnimatedBuilder(
+ animation: svc,
+ builder: (context, _) {
+ final ms = svc.offsetMs;
+ final label = ms == 0
+ ? '0'
+ : '${ms > 0 ? '+' : '-'}${(ms.abs() / 1000).toStringAsFixed(2)}s';
+ Widget button(IconData icon, int delta) => GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: () => svc.nudgeOffset(delta),
+ child: Padding(
+ padding:
+ const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
+ child: Icon(icon, size: 16, color: fg.withValues(alpha: 0.75)),
+ ),
+ );
+ return Container(
+ decoration: BoxDecoration(
+ color: fg.withValues(alpha: 0.08),
+ borderRadius: BorderRadius.circular(20),
+ ),
+ child: Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Padding(
+ padding: const EdgeInsets.only(left: 10, right: 2),
+ child: Icon(
+ svc.onBluetooth
+ ? Icons.bluetooth_rounded
+ : Icons.volume_up_rounded,
+ size: 13,
+ color: fg.withValues(alpha: 0.5)),
+ ),
+ button(Icons.remove_rounded, -50),
+ // Long press to drop back to no offset.
+ GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onLongPress: svc.clearOffset,
+ child: SizedBox(
+ width: 52,
+ child: Text(label,
+ textAlign: TextAlign.center,
+ style: TextStyle(
+ fontSize: 12,
+ fontWeight: FontWeight.w600,
+ color: fg.withValues(alpha: 0.75))),
+ ),
+ ),
+ button(Icons.add_rounded, 50),
+ const SizedBox(width: 4),
+ ],
+ ),
+ );
+ },
+ );
+ }
+
+ void _stopReadAlong() {
+ _readAlongTimer?.cancel();
+ _readAlongTimer = null;
+ // Runs from dispose() too, when the WebView may already be on its way out.
+ try {
+ _epubController?.webViewController
+ ?.evaluateJavascript(source: 'window.__absorbRA && __absorbRA.stop()')
+ .catchError((_) => null);
+ } catch (_) {}
+ _readAlongLineStart = null;
+ _readAlongPageWords = 0;
+ _readAlongLineWords = 0;
+ _readAlongWordIndex = -1;
+ _readAlongAt = -1;
+ _readAlongTurnedTo = null;
+ if (_readAlongStartedPipeline) {
+ LyricsService.instance.disable();
+ _readAlongStartedPipeline = false;
+ }
+ if (mounted && _readAlongOn) setState(() => _readAlongOn = false);
+ }
+
+ Future<void> _readAlongTick() async {
+ if (!mounted || !_readAlongOn) return;
+ final player = AudioPlayerService();
+ if (player.currentItemId != widget.itemId) {
+ _stopReadAlong();
+ return;
+ }
+ final svc = LyricsService.instance;
+ // Changing the color or word/sentence tracking in settings applies without
+ // leaving the reader.
+ if (svc.readAlongColor != _readAlongColor) {
+ _readAlongColor = svc.readAlongColor;
+ _epubController?.webViewController?.evaluateJavascript(
+ source: 'window.__absorbRA && '
+ '__absorbRA.setColor("${cssHex(_readAlongColor)}")');
+ }
+ if (svc.readAlongMode != _readAlongMode) {
+ _readAlongMode = svc.readAlongMode;
+ _readAlongLineStart = null;
+ }
+ final pos = player.position.inMilliseconds / 1000.0;
+ // Same sync offset the player uses, so headphones don't run the reader's
+ // coloring ahead of the voice either.
+ final heard = pos - svc.offsetSeconds;
+ final line = TranscriptLineStore.instance
+ .lineNear(widget.itemId, heard + 0.35 * player.speed);
+ // A seek re-anchors everything; without one, narration only moves
+ // forward, so an older line surfacing again (overlapping cached lines,
+ // a repeated phrase) must not drag the coloring back up the page.
+ final seeked = (pos - _readAlongLastPos).abs() > 3;
+ _readAlongLastPos = pos;
+ if (seeked) {
+ _readAlongAt = -1;
+ _readAlongLineStart = null;
+ debugPrint('[ReadAlong] seek to ${pos.toStringAsFixed(1)}s, re-anchoring');
+ }
+ // Previews carry guessed timing for text the audio hasn't reached. The
+ // player overlay can dim them; a highlight in the book can't look
+ // tentative, and anchoring on one freezes the never-go-backwards guard
+ // when the real line lands - so read along waits for real coverage.
+ if (line == null || line.approx || line.text.trim().isEmpty) return;
+ if (!seeked &&
+ _readAlongLineStart != null &&
+ line.start < _readAlongLineStart!) {
+ debugPrint('[ReadAlong] skipped a line that goes backwards: '
+ '${line.start.toStringAsFixed(1)}s < '
+ '${_readAlongLineStart!.toStringAsFixed(1)}s');
+ return;
+ }
+ if (line.start != _readAlongLineStart) {
+ if (_readAlongLocating) return;
+ _readAlongLineStart = line.start;
+ _readAlongLineWords = line.words.length;
+ _readAlongLocating = true;
+ try {
+ // A line cached before word timing existed can only be followed whole.
+ await _paintReadAlongLine(line.text.trim(),
+ exact: line.exact,
+ wordMode: svc.readAlongMode == 'word' && line.wordStarts.isNotEmpty);
+ } finally {
+ _readAlongLocating = false;
+ }
+ return;
+ }
+ // Same sentence, later word: a cheap repaint from the cached offsets.
+ if (_readAlongLocating ||
+ _readAlongPageWords == 0 ||
+ svc.readAlongMode != 'word') {
+ return;
+ }
+ final idx = line.wordIndexAt(heard + 0.15 * player.speed);
+ if (idx < 0) return;
+ // Whisper's word count and the page's can differ on a corrected line, so
+ // scale the index across instead of running off the end.
+ final scaled = _readAlongLineWords > 0
+ ? (idx * _readAlongPageWords / _readAlongLineWords).floor()
+ : idx;
+ final wordIndex = scaled.clamp(0, _readAlongPageWords - 1);
+ if (wordIndex == _readAlongWordIndex) return;
+ _readAlongWordIndex = wordIndex;
+ final raw = await _epubController?.webViewController?.evaluateJavascript(
+ source: 'window.__absorbRA ? __absorbRA.word($wordIndex) : ""');
+ if (!mounted || !_readAlongOn) return;
+ Map<String, dynamic>? d;
+ if (raw is String && raw.isNotEmpty) {
+ try {
+ d = jsonDecode(raw) as Map<String, dynamic>;
+ } catch (_) {}
+ }
+ // The page turned into a different section and took the anchor with it:
+ // locate the sentence again on the next tick.
+ if (d == null || d['held'] != true) {
+ _readAlongLineStart = null;
+ _readAlongPageWords = 0;
+ _readAlongAt = -1;
+ return;
+ }
+ // The narration has run onto the next page mid-sentence. Turn there now
+ // rather than leaving you staring at a page the voice has left behind -
+ // and, coming the other way, without turning early and stranding the
+ // colouring off-screen until the sentence catches up.
+ final cfi = d['cfi'];
+ if (d['visible'] == false && cfi is String && cfi.isNotEmpty) {
+ final since = DateTime.now().difference(_readAlongLastTurn);
+ if (cfi != _readAlongTurnedTo && since > const Duration(milliseconds: 500)) {
+ _readAlongTurnedTo = cfi;
+ _readAlongLastTurn = DateTime.now();
+ debugPrint('[ReadAlong] word $wordIndex ran onto the next page, turning');
+ _epubController?.display(cfi: cfi);
+ }
+ }
+ }
+
+ /// Find the line's text in the rendered pages and move the follow-coloring
+ /// there, page-turning when the line has moved past the visible page. A
+ /// line that isn't in the rendered sections (chapter boundary) is found via
+ /// the search book when its text is ebook-exact; Whisper-flavored lines
+ /// that don't match are skipped - the next exact line re-anchors.
+ Future<void> _paintReadAlongLine(String needle,
+ {required bool exact, required bool wordMode}) async {
+ final wc = _epubController?.webViewController;
+ if (wc == null) return;
+ final watch = Stopwatch()..start();
+ final res = await wc.callAsyncJavaScript(
+ functionBody: 'return window.__absorbRA ? '
+ '__absorbRA.locate(needle, wordMode, minOffset) : "";',
+ arguments: {
+ 'needle': needle,
+ 'wordMode': wordMode,
+ 'minOffset': _readAlongAt,
+ },
+ );
+ final raw = res?.value;
+ if (!mounted || !_readAlongOn) return;
+ Map<String, dynamic>? d;
+ if (raw is String && raw.isNotEmpty) {
+ try {
+ d = jsonDecode(raw) as Map<String, dynamic>;
+ } catch (_) {}
+ }
+ final head = needle.length > 50 ? '${needle.substring(0, 50)}...' : needle;
+ if (d != null && d['found'] == true) {
+ _readAlongPageWords = (d['words'] as num?)?.toInt() ?? 0;
+ _readAlongWordIndex = wordMode && _readAlongPageWords > 0 ? 0 : -1;
+ _readAlongAt = (d['at'] as num?)?.toInt() ?? -1;
+ // Logs the heard line against the page sentence it landed on, so a
+ // mis-mapping shows up as two different sentences on one line.
+ debugPrint('[ReadAlong] "$head" -> page@$_readAlongAt '
+ 'words=$_readAlongPageWords visible=${d['visible']} '
+ '${watch.elapsedMilliseconds}ms | page: "${d['sentence']}"');
+ final cfi = d['cfi'];
+ if (d['visible'] == false && cfi is String && cfi.isNotEmpty) {
+ _epubController?.display(cfi: cfi);
+ }
+ return;
+ }
+ debugPrint('[ReadAlong] "$head" not on the rendered pages '
+ '(${watch.elapsedMilliseconds}ms, exact=$exact)');
+ // Not in the rendered pages: an exact line can be found globally (chapter
+ // crossings land here); a whisper line just waits for the next one.
+ if (!exact) return;
+ // That scan loads every section of the book, which takes seconds and
+ // freezes tracking while it runs. One miss is usually just a sentence
+ // sitting off the current page, so don't pay for it on every miss.
+ final since = DateTime.now().difference(_readAlongLastScan);
+ if (since < const Duration(seconds: 10)) {
+ debugPrint('[ReadAlong] skipping the whole-book scan '
+ '(last one ${since.inSeconds}s ago)');
+ return;
+ }
+ _readAlongLastScan = DateTime.now();
+ final hres = await wc.callAsyncJavaScript(functionBody: r'''
+ var sb = window.__absorbSearchBook;
+ if (!sb) {
+ sb = ePub();
+ sb.spine.unpack(book.packaging, book.resolve.bind(book), book.canonical.bind(book));
+ window.__absorbSearchBook = sb;
+ }
+ var q = needle.toLowerCase().replace(/\s+/g,' ').trim();
+ var items = (sb.spine && sb.spine.spineItems) ? sb.spine.spineItems : [];
+ var found = '';
+ for (var i = 0; i < items.length; i++) {
+ var item = items[i], text = '';
+ try {
+ await item.load(book.load.bind(book));
+ var d = item.document;
+ text = d ? ((d.body && d.body.textContent) || (d.documentElement && d.documentElement.textContent) || '') : '';
+ } catch(e){}
+ finally { try { item.unload(); } catch(e2){} }
+ if (!text) continue;
+ if (text.toLowerCase().replace(/\s+/g,' ').indexOf(q) !== -1) { found = item.href || ''; break; }
+ }
+ return JSON.stringify({href: found});
+ ''', arguments: {'needle': needle});
+ final hraw = hres?.value;
+ if (!mounted || !_readAlongOn || hraw is! String) return;
+ String href = '';
+ try {
+ href = (jsonDecode(hraw) as Map<String, dynamic>)['href'] as String? ?? '';
+ } catch (_) {}
+ if (href.isEmpty) return;
+ // Turn to the section and let the next tick locate and paint through the
+ // normal path.
+ wc.evaluateJavascript(source: 'window.__absorbRA && __absorbRA.clear()');
+ _readAlongPageWords = 0;
+ _readAlongAt = -1;
+ _epubController?.display(cfi: href);
+ _readAlongLineStart = null;
+ }
+
/// Brief highlight so the user can spot the passage on the page. Strong
/// opacity and a vivid amber so it stays visible across light, sepia, and
/// dark themes (the highlight blends with the page, so a faint tint washes
@@ -3519,6 +3952,13 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
tooltip: AppLocalizations.of(context)!.readerTooltipSearch,
onPressed: _openSearchScreen,
),
+ if (_transcriptionOn)
+ IconButton(
+ icon: Icon(Icons.graphic_eq_rounded,
+ color: _readAlongOn ? accent : fg),
+ tooltip: AppLocalizations.of(context)!.readAlong,
+ onPressed: _toggleReadAlong,
+ ),
IconButton(
icon: Icon(Icons.sticky_note_2_outlined, color: fg),
onPressed: _showAnnotationsSheet,
@@ -3563,6 +4003,11 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
+ if (_readAlongOn)
+ Padding(
+ padding: const EdgeInsets.only(bottom: 6),
+ child: Center(child: _readAlongSyncPill(fg)),
+ ),
_buildMediaBar(fg, accent),
ClipRRect(
borderRadius: BorderRadius.circular(2),
diff --git a/lib/widgets/expanded_card.dart b/lib/widgets/expanded_card.dart
index 5fb68b88..18edbdd4 100644
--- a/lib/widgets/expanded_card.dart
+++ b/lib/widgets/expanded_card.dart
@@ -16,6 +16,8 @@ import 'card_progress_bar.dart';
import 'card_playback_controls.dart';
import 'card_buttons.dart';
import 'ebook_router.dart';
+import '../services/lyrics_service.dart';
+import 'lyrics_overlay.dart';
import 'overlay_toast.dart';
import '../services/ebook_cache.dart';
import '../services/find_in_ebook.dart';
@@ -122,6 +124,10 @@ class _ExpandedCardState extends State<ExpandedCard> {
late Map<String, dynamic> _item;
String get _itemId => _item['id'] as String? ?? '';
+ /// The store key the live transcript runs under for this card.
+ String get _lyricsKey =>
+ _episodeId != null ? '$_itemId-$_episodeId' : _itemId;
+
Map<String, dynamic> get _media => _item['media'] as Map<String, dynamic>? ?? {};
Map<String, dynamic> get _metadata => _media['metadata'] as Map<String, dynamic>? ?? {};
String get _title {
@@ -813,8 +819,16 @@ class _ExpandedCardState extends State<ExpandedCard> {
child: Stack(
fit: StackFit.expand,
children: [
- // Cover image
- EinkCoverTone(child: _coverUrl != null
+ // Cover image - hidden while the
+ // transcript has the cover.
+ AnimatedBuilder(
+ animation: LyricsService.instance,
+ builder: (context, child) =>
+ LyricsService.instance
+ .coversArtFor(_lyricsKey)
+ ? Offstage(child: child)
+ : child!,
+ child: EinkCoverTone(child: _coverUrl != null
? _isLocalCover
? BlurPaddedCover(blurChild: Image.file(File(_coverUrl!), fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const SizedBox.shrink()),
@@ -827,11 +841,20 @@ class _ExpandedCardState extends State<ExpandedCard> {
httpHeaders: mediaHeaders,
placeholder: (_, __) => CoverPlaceholder(title: _title, author: _author),
errorWidget: (_, __, ___) => CoverPlaceholder(title: _title, author: _author)))
- : CoverPlaceholder(title: _title, author: _author)),
- // Play/pause overlay
+ : CoverPlaceholder(title: _title, author: _author))),
+ // Play/pause overlay - the tap still
+ // works with the transcript up, but
+ // the button would sit on the words.
if (_coverPlayButton && !isCastingThis && !isFinished)
Positioned.fill(
- child: AnimatedContainer(
+ child: AnimatedBuilder(
+ animation: LyricsService.instance,
+ builder: (context, child) =>
+ LyricsService.instance
+ .coversArtFor(_lyricsKey)
+ ? Offstage(child: child)
+ : child!,
+ child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
decoration: BoxDecoration(
color: coverPlaying ? Colors.transparent : Colors.black.withValues(alpha: 0.25),
@@ -866,7 +889,7 @@ class _ExpandedCardState extends State<ExpandedCard> {
),
),
),
- ),
+ )),
// Casting overlay
if (isCastingThis) ...[
Positioned.fill(
@@ -899,6 +922,11 @@ class _ExpandedCardState extends State<ExpandedCard> {
),
),
],
+ // Live transcript (lyrics mode)
+ LyricsOverlay(
+ forKey: _lyricsKey,
+ surface: cs.surface,
+ onSurface: cs.onSurface),
],
),
),
diff --git a/lib/widgets/lyrics_overlay.dart b/lib/widgets/lyrics_overlay.dart
new file mode 100644
index 00000000..3b62d219
--- /dev/null
+++ b/lib/widgets/lyrics_overlay.dart
@@ -0,0 +1,501 @@
+import 'dart:async';
+import 'dart:math' as math;
+
+import 'package:flutter/material.dart';
+
+import '../l10n/app_localizations.dart';
+import '../services/audio_player_service.dart';
+import '../services/lyrics_service.dart';
+import '../services/transcript_line_store.dart';
+
+/// Live transcript on the player cover.
+///
+/// It reads like a page of a book: sentences fill downwards, the spoken one
+/// lights up as the narration walks down them, and only when it reaches the
+/// bottom does the next page come up. Sentences are measured before they are
+/// drawn, so a page holds what fits whole - a long sentence takes the room it
+/// needs instead of being cut off.
+///
+/// Taking over the cover (the default) the card hides its artwork, and the
+/// transcript sits straight on the card's own background - blur, gradient or
+/// flat, whatever the user has it set to. Otherwise it's a strip across the
+/// bottom of the artwork, which works the same way in less room.
+class LyricsOverlay extends StatefulWidget {
+ /// When set, renders only while lyrics run for this store key - card lists
+ /// show several books, and the line belongs to the playing one alone.
+ final String? forKey;
+ final bool compact;
+ /// The card's ink color, so the transcript reads like the rest of the card
+ /// on whatever background it has. Defaults to white-on-artwork.
+ final Color? onSurface;
+ /// The card's base color, used to keep the read-along highlight readable.
+ final Color? surface;
+ const LyricsOverlay({
+ super.key,
+ this.forKey,
+ this.compact = false,
+ this.onSurface,
+ this.surface,
+ });
+
+ @override
+ State<LyricsOverlay> createState() => _LyricsOverlayState();
+}
+
+/// [color], nudged toward the background's opposite until it can actually be
+/// read on it - a pale amber on a pale cover is a picked color nobody can see.
+/// Stops at a 3:1 ratio, the readable threshold for text this size.
+Color readableOn(Color color, Color background) {
+ final bg = background.computeLuminance();
+ var c = color;
+ for (var i = 0; i < 8; i++) {
+ final lum = c.computeLuminance();
+ final contrast = (math.max(lum, bg) + 0.05) / (math.min(lum, bg) + 0.05);
+ if (contrast >= 3) break;
+ c = Color.lerp(c, bg > 0.5 ? Colors.black : Colors.white, 0.2)!;
+ }
+ return c;
+}
+
+class _LyricsOverlayState extends State<LyricsOverlay> {
+ /// Start time of the first sentence on the page being shown. Kept here so
+ /// the page holds still while the narration moves down it, instead of
+ /// re-centring on every sentence.
+ double? _pageStart;
+
+ /// The sync nudge is a tuning control, not something to stare at all
+ /// session, so it shows itself when it is likely to be wanted - the
+ /// transcript starting, or the output changing to or from headphones - and
+ /// otherwise waits for a long press on the transcript.
+ bool _pillVisible = false;
+ Timer? _pillTimer;
+ bool _lastOn = false;
+ bool? _lastBluetooth;
+
+ static const _linePad = 6.0;
+
+ void _revealPill() {
+ _pillTimer?.cancel();
+ if (mounted && !_pillVisible) setState(() => _pillVisible = true);
+ _pillTimer = Timer(const Duration(seconds: 6), () {
+ if (mounted) setState(() => _pillVisible = false);
+ });
+ }
+
+ @override
+ void dispose() {
+ _pillTimer?.cancel();
+ super.dispose();
+ }
+
+ Color get _ink => widget.onSurface ?? Colors.white;
+ Color get _inkQuiet => _ink.withValues(alpha: 0.4);
+
+ /// The transcript can sit on blurred artwork, so it always carries a halo -
+ /// dark behind light text, light behind dark text.
+ Color get _halo => _ink.computeLuminance() > 0.5
+ ? Colors.black.withValues(alpha: 0.7)
+ : Colors.white.withValues(alpha: 0.7);
+
+ TextStyle _base(double size) => TextStyle(
+ fontSize: size,
+ height: 1.35,
+ fontWeight: FontWeight.w600,
+ shadows: [Shadow(blurRadius: 6, color: _halo)],
+ );
+
+ /// The line, with the word being spoken in the read-along color and the
+ /// rest of it a shade back. Falls back to plain text when the line has no
+ /// word timing (older cached lines) or word tracking is off.
+ InlineSpan _lineSpan(LyricsService svc, String line) {
+ final index = svc.currentWordIndex;
+ final words = svc.current?.words ?? const <String>[];
+ if (index < 0 || index >= words.length) return TextSpan(text: line);
+ final accent =
+ readableOn(Color(svc.readAlongColor), widget.surface ?? Colors.black);
+ final rest = _ink.withValues(alpha: 0.8);
+ return TextSpan(
+ children: [
+ for (var i = 0; i < words.length; i++)
+ TextSpan(
+ text: i == words.length - 1 ? words[i] : '${words[i]} ',
+ style: TextStyle(color: i == index ? accent : rest),
+ ),
+ ],
+ );
+ }
+
+ /// Must match how the page's Text widgets actually render, or _fit packs
+ /// more lines than the page can hold and the bottom one gets clipped -
+ /// which is why this applies the ambient text scale the widgets will get.
+ TextScaler _scaler = TextScaler.noScaling;
+
+ double _measure(String text, TextStyle style, double maxWidth) {
+ final tp = TextPainter(
+ text: TextSpan(text: text, style: style),
+ textDirection: TextDirection.ltr,
+ textAlign: TextAlign.center,
+ textScaler: _scaler,
+ )..layout(maxWidth: maxWidth);
+ return tp.height;
+ }
+
+ Widget _spoken(LyricsService svc, String text, double size, {int? maxLines}) =>
+ Text.rich(
+ _lineSpan(svc, text),
+ textAlign: TextAlign.center,
+ maxLines: maxLines,
+ overflow: maxLines == null ? TextOverflow.clip : TextOverflow.ellipsis,
+ style: _base(size).copyWith(color: _ink),
+ );
+
+ Widget _quiet(String text, double size) => Text(
+ text,
+ textAlign: TextAlign.center,
+ style: _base(size).copyWith(
+ color: _inkQuiet,
+ fontWeight: FontWeight.w500,
+ ),
+ );
+
+ /// A page of sentences starting at [anchor]: as many as fit whole in
+ /// [maxHeight]. The first is always included even when it is taller than the
+ /// page on its own - it gets scaled down rather than clipped.
+ List<TranscriptLine> _fit(String key, double anchor, double maxWidth,
+ double maxHeight, TextStyle style) {
+ final candidates = TranscriptLineStore.instance.fromLine(key, anchor, 16);
+ final out = <TranscriptLine>[];
+ var used = 0.0;
+ for (final l in candidates) {
+ final h = _measure(l.text.trim(), style, maxWidth) + _linePad;
+ if (out.isNotEmpty && used + h > maxHeight) break;
+ out.add(l);
+ used += h;
+ }
+ return out;
+ }
+
+ /// Fills downwards, turning a page only when the spoken sentence runs off
+ /// the bottom of the current one.
+ Widget _page(
+ LyricsService svc, double maxWidth, double maxHeight, double size) {
+ final key = svc.activeKey;
+ final current = svc.current;
+ final style = _base(size);
+ if (key == null || current == null) {
+ return _spoken(svc, current?.text.trim() ?? '', size, maxLines: 4);
+ }
+ var anchor = _pageStart;
+ // A seek backwards, or a page we no longer have lines for, starts over at
+ // whatever is being spoken now.
+ if (anchor == null || current.start < anchor) anchor = current.start;
+ var page = _fit(key, anchor, maxWidth, maxHeight, style);
+ // The narration walked off the bottom of this page: turn to a new one
+ // starting at the sentence being spoken.
+ if (page.isEmpty || !page.any((l) => l.start == current.start)) {
+ anchor = current.start;
+ page = _fit(key, anchor, maxWidth, maxHeight, style);
+ }
+ _pageStart = anchor;
+ if (page.isEmpty) {
+ return current.approx
+ ? _quiet(current.text.trim(), size)
+ : _spoken(svc, current.text.trim(), size, maxLines: 4);
+ }
+ final tall = page.length == 1 &&
+ _measure(page.first.text.trim(), style, maxWidth) > maxHeight;
+ final column = Column(
+ key: ValueKey('p$anchor'),
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ for (final l in page)
+ Padding(
+ padding: const EdgeInsets.symmetric(vertical: _linePad / 2),
+ // Previews stay in the quiet style even while being "spoken" -
+ // the dimming is what says their timing is estimated.
+ child: l.start == current.start && !l.approx
+ ? _spoken(svc, l.text.trim(), size)
+ : _quiet(l.text.trim(), size),
+ ),
+ ],
+ );
+ // One sentence longer than the whole page: shrink it rather than hide
+ // half of it.
+ return tall ? FittedBox(fit: BoxFit.scaleDown, child: column) : column;
+ }
+
+ /// Waiting on the transcript. Says what it is waiting for rather than
+ /// spinning forever: how far the initial runway has got, or - when this
+ /// device simply cannot transcribe as fast as you are listening - that it
+ /// cannot.
+ Widget _waiting(BuildContext context, LyricsService svc) {
+ final l = AppLocalizations.of(context)!;
+ final quiet = TextStyle(color: _inkQuiet, fontSize: 12);
+ final progress = svc.gateProgress;
+ final rows = <Widget>[];
+ if (progress != null) {
+ final percent = '${(progress * 100).round()}%';
+ rows.add(Text(l.lyricsBuildingLead(percent),
+ textAlign: TextAlign.center, style: quiet));
+ rows.add(const SizedBox(height: 8));
+ rows.add(SizedBox(
+ width: 180,
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(3),
+ child: LinearProgressIndicator(
+ value: progress,
+ minHeight: 4,
+ backgroundColor: _ink.withValues(alpha: 0.15),
+ valueColor: AlwaysStoppedAnimation(_ink.withValues(alpha: 0.6)),
+ ),
+ ),
+ ));
+ } else if (svc.cannotKeepUp) {
+ final speed = AudioPlayerService().speed;
+ rows.add(Text(
+ l.lyricsCantKeepUp('${speed.toStringAsFixed(speed % 1 == 0 ? 0 : 2)}x'),
+ textAlign: TextAlign.center,
+ style: quiet));
+ } else {
+ rows.add(Row(
+ mainAxisSize: MainAxisSize.min,
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ SizedBox(
+ width: 12,
+ height: 12,
+ child: CircularProgressIndicator(strokeWidth: 1.5, color: _inkQuiet),
+ ),
+ const SizedBox(width: 8),
+ Text(l.lyricsListeningAhead, style: quiet),
+ ],
+ ));
+ }
+ return Column(
+ key: const ValueKey('_buffering'),
+ mainAxisSize: MainAxisSize.min,
+ children: rows,
+ );
+ }
+
+ /// Fades between pages, not between sentences - inside a page the spoken
+ /// line just moves down, which shouldn't animate the whole block.
+ Widget _switcher(Widget child) => AnimatedSwitcher(
+ duration: const Duration(milliseconds: 350),
+ switchInCurve: Curves.easeOut,
+ switchOutCurve: Curves.easeIn,
+ transitionBuilder: (child, anim) => FadeTransition(
+ opacity: anim,
+ child: SlideTransition(
+ position: Tween<Offset>(
+ begin: const Offset(0, 0.25),
+ end: Offset.zero,
+ ).animate(anim),
+ child: child,
+ ),
+ ),
+ child: child,
+ );
+
+ /// Sync nudge, on the player itself: shifting the transcript against the
+ /// audio is only judgeable while listening to it, so it can't live in a
+ /// settings screen you have to walk back and forth from. Bluetooth runs a
+ /// couple of hundred milliseconds behind the playhead, and every pair of
+ /// headphones is different.
+ Widget _syncPill(LyricsService svc) {
+ return AnimatedOpacity(
+ opacity: _pillVisible ? 1 : 0,
+ duration: const Duration(milliseconds: 250),
+ child: IgnorePointer(
+ ignoring: !_pillVisible,
+ child: _syncPillBody(svc),
+ ),
+ );
+ }
+
+ Widget _syncPillBody(LyricsService svc) {
+ final ms = svc.offsetMs;
+ final label = ms == 0
+ ? '0'
+ : '${ms > 0 ? '+' : '-'}${(ms.abs() / 1000).toStringAsFixed(2)}s';
+ Widget button(IconData icon, int delta) => GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: () {
+ svc.nudgeOffset(delta);
+ _revealPill();
+ },
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
+ child: Icon(icon, size: 16, color: _ink.withValues(alpha: 0.75)),
+ ),
+ );
+ return Container(
+ decoration: BoxDecoration(
+ color: _ink.withValues(alpha: 0.10),
+ borderRadius: BorderRadius.circular(20),
+ ),
+ child: Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Padding(
+ padding: const EdgeInsets.only(left: 10, right: 2),
+ child: Icon(
+ svc.onBluetooth
+ ? Icons.bluetooth_rounded
+ : Icons.volume_up_rounded,
+ size: 13,
+ color: _ink.withValues(alpha: 0.5)),
+ ),
+ button(Icons.remove_rounded, -50),
+ // Long press to drop back to no offset.
+ GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onLongPress: () {
+ svc.clearOffset();
+ _revealPill();
+ },
+ child: SizedBox(
+ width: 52,
+ child: Text(label,
+ textAlign: TextAlign.center,
+ style: TextStyle(
+ fontSize: 12,
+ fontWeight: FontWeight.w600,
+ color: _ink.withValues(alpha: 0.75))),
+ ),
+ ),
+ button(Icons.add_rounded, 50),
+ const SizedBox(width: 4),
+ ],
+ ),
+ );
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return AnimatedBuilder(
+ animation: LyricsService.instance,
+ builder: (context, _) {
+ final svc = LyricsService.instance;
+ if (!svc.isOn) return const SizedBox.shrink();
+ if (widget.forKey != null && svc.activeKey != widget.forKey) {
+ return const SizedBox.shrink();
+ }
+ _scaler = MediaQuery.textScalerOf(context);
+ final line = svc.current?.text.trim() ?? '';
+ // The initial runway build shows its progress even when the playhead
+ // is inside saved coverage - lines are held back until the gate opens.
+ final showBuffer =
+ line.isEmpty && (svc.buffering || svc.gateProgress != null);
+ if (line.isEmpty && !showBuffer) return const SizedBox.shrink();
+ final size = widget.compact ? svc.fontSize * 0.8 : svc.fontSize;
+ final padH = widget.compact ? 14.0 : 18.0;
+ // Starting the transcript, or plugging in headphones, is exactly when
+ // the offset might need a nudge - so offer it, briefly, unasked.
+ if (svc.isOn != _lastOn || svc.onBluetooth != _lastBluetooth) {
+ _lastOn = svc.isOn;
+ _lastBluetooth = svc.onBluetooth;
+ if (svc.isOn && !widget.compact) {
+ WidgetsBinding.instance
+ .addPostFrameCallback((_) => _revealPill());
+ }
+ }
+
+ // Taking the cover: the card hides its artwork, so the transcript
+ // paints nothing of its own and sits on the card's background.
+ if (svc.fullCover) {
+ return Positioned.fill(
+ child: Stack(
+ children: [
+ IgnorePointer(
+ child: Padding(
+ padding: EdgeInsets.symmetric(
+ horizontal: padH, vertical: widget.compact ? 10 : 14),
+ child: LayoutBuilder(
+ builder: (context, box) => Align(
+ alignment: Alignment.topCenter,
+ child: _switcher(showBuffer
+ ? _waiting(context, svc)
+ : _page(
+ svc,
+ box.maxWidth,
+ box.maxHeight.isFinite ? box.maxHeight : 260,
+ size)),
+ ),
+ ),
+ ),
+ ),
+ if (!widget.compact) ...[
+ // Long press claims only the long press, so a plain tap
+ // still falls through to play/pause on the cover.
+ Positioned.fill(
+ child: GestureDetector(
+ behavior: HitTestBehavior.translucent,
+ onLongPress: _revealPill,
+ ),
+ ),
+ Positioned(
+ left: 0,
+ right: 0,
+ bottom: 8,
+ child: Center(child: _syncPill(svc)),
+ ),
+ ],
+ ],
+ ),
+ );
+ }
+
+ // Strip across the bottom of the artwork: the same page in less room,
+ // capped so it never swallows the whole cover.
+ return Positioned.fill(
+ child: Stack(children: [
+ IgnorePointer(
+ child: Align(
+ alignment: Alignment.bottomCenter,
+ child: LayoutBuilder(
+ builder: (context, box) {
+ final height = box.maxHeight.isFinite ? box.maxHeight : 260.0;
+ return Container(
+ padding: EdgeInsets.fromLTRB(padH, widget.compact ? 26 : 36,
+ padH, widget.compact ? 10 : 16),
+ decoration: const BoxDecoration(
+ gradient: LinearGradient(
+ begin: Alignment.topCenter,
+ end: Alignment.bottomCenter,
+ colors: [
+ Colors.transparent,
+ Colors.black54,
+ Colors.black87
+ ],
+ ),
+ ),
+ child: _switcher(showBuffer
+ ? _waiting(context, svc)
+ : _page(svc, box.maxWidth - padH * 2, height * 0.6,
+ size)),
+ );
+ },
+ ),
+ ),
+ ),
+ if (!widget.compact) ...[
+ Positioned.fill(
+ child: GestureDetector(
+ behavior: HitTestBehavior.translucent,
+ onLongPress: _revealPill,
+ ),
+ ),
+ Positioned(
+ left: 0,
+ right: 0,
+ bottom: 8,
+ child: Center(child: _syncPill(svc)),
+ ),
+ ],
+ ]),
+ );
+ },
+ );
+ }
+}
diff --git a/lib/widgets/transcription_download_prompt.dart b/lib/widgets/transcription_download_prompt.dart
new file mode 100644
index 00000000..58d35e3b
--- /dev/null
+++ b/lib/widgets/transcription_download_prompt.dart
@@ -0,0 +1,61 @@
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+
+import '../l10n/app_localizations.dart';
+import '../providers/auth_provider.dart';
+import '../providers/library_provider.dart';
+import '../services/download_service.dart';
+import 'overlay_toast.dart';
+
+/// Offer to download the audio the transcription features need, instead of
+/// only saying they can't run. Transcription reads the downloaded file, so
+/// "not downloaded" is a step away from working rather than a dead end.
+///
+/// Returns true when a download was started.
+Future<bool> promptDownloadForTranscription(
+ BuildContext context, {
+ required String itemId,
+ String? episodeId,
+ required String title,
+ String? author,
+ String? coverUrl,
+}) async {
+ final l = AppLocalizations.of(context)!;
+ final go = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ content: Text(l.transcriptionNeedsDownload),
+ actions: [
+ TextButton(
+ onPressed: () => Navigator.pop(ctx, false),
+ child: Text(l.cancel),
+ ),
+ FilledButton(
+ onPressed: () => Navigator.pop(ctx, true),
+ child: Text(l.download),
+ ),
+ ],
+ ),
+ );
+ if (go != true || !context.mounted) return false;
+ final api = context.read<AuthProvider>().apiService;
+ if (api == null) return false;
+ final libraryId = context.read<LibraryProvider>().selectedLibraryId;
+ final error = await DownloadService().downloadItem(
+ api: api,
+ itemId: itemId,
+ episodeId: episodeId,
+ title: title,
+ author: author,
+ coverUrl: coverUrl,
+ libraryId: libraryId,
+ );
+ if (!context.mounted) return error == null;
+ if (error != null) {
+ showOverlayToast(context, error, icon: Icons.error_outline_rounded);
+ return false;
+ }
+ showOverlayToast(context, l.transcriptionDownloadStarted,
+ icon: Icons.download_rounded);
+ return true;
+}