A cross platform Audiobookshelf client for Android and iOS
Dart
Stop the live transcript from transcribing the same stretch over and over after a pause in the narration
Barnabasfdeb7b075f6b
diff --git a/lib/services/lyrics_service.dart b/lib/services/lyrics_service.dartindex 735204b2..6597c7a5 100644--- a/lib/services/lyrics_service.dart+++ b/lib/services/lyrics_service.dart@@ -72,6 +72,47 @@ class LyricsService extends ChangeNotifier {String? _epubItemId; // itemId to look for a cached EPUB under (books only)Timer? _tick;bool _working = false;++ // Stretches already transcribed that added nothing to the runway: silence,+ // music, or a first line the store dropped as overlapping the line before,+ // leaving the next one past its gap tolerance. The store keeps no empty+ // lines, so without these the loop would ask for the same window forever.+ final Map<String, List<({double start, double end})>> _holes = {};++ /// How far the runway reaches from [pos], stepping over [_holes].+ double _coveredUntil(String key, double pos) => coveredPastHoles(+ pos,+ _holes[key] ?? const [],+ (from) => TranscriptLineStore.instance.coveredUntil(key, from),+ );++ /// [coveredFrom] is the store's coverage from a point; a hole that starts+ /// where coverage ends carries it on to the hole's end and beyond.+ @visibleForTesting+ static double coveredPastHoles(+ double pos,+ List<({double start, double end})> holes,+ double Function(double from) coveredFrom,+ ) {+ var covered = coveredFrom(pos);+ var moved = true;+ while (moved) {+ moved = false;+ for (final h in holes) {+ if (h.start <= covered + 0.5 && h.end > covered) {+ covered = coveredFrom(h.end);+ moved = true;+ }+ }+ }+ return covered;+ }++ void _markHole(String key, double start, double window) {+ (_holes[key] ??= []).add((start: start, end: start + window));+ debugPrint('[Lyrics] the window at ${start.toStringAsFixed(1)}s added '+ 'nothing to the runway - moving past it');+ }TranscriptLine? _current;bool _transcribingVisible = false;bool _pastCoverage = false;@@ -180,7 +221,7 @@ class LyricsService extends ChangeNotifier {_tick = Timer.periodic(const Duration(milliseconds: 200), (_) => _onTick());notifyListeners();final pos = AudioPlayerService().position.inMilliseconds / 1000.0;- final covered = TranscriptLineStore.instance.coveredUntil(key, pos);+ final covered = _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)');@@ -285,7 +326,7 @@ class LyricsService extends ChangeNotifier {final pos = AudioPlayerService().position.inMilliseconds / 1000.0;if (++_routeTick % 25 == 0) _refreshRoute();final speed = AudioPlayerService().speed;- final covered = TranscriptLineStore.instance.coveredUntil(key, pos);+ final covered = _coveredUntil(key, pos);final lead = covered - pos;_pastCoverage = lead <= 0.5;@@ -512,6 +553,7 @@ class LyricsService extends ChangeNotifier {debugPrint('[Lyrics] behind the playhead, aiming ''${(start - pos).toStringAsFixed(1)}s ahead');}+ final window = _rate == null ? 12.0 : _chunkSeconds;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@@ -523,7 +565,7 @@ class LyricsService extends ChangeNotifier {// few seconds; full windows take over from there.final out = await _chunker!.transcribeInto(start: start,- windowSeconds: _rate == null ? 12.0 : _chunkSeconds,+ windowSeconds: window,preferAccuracy: !_forceFastModel,);// A chunk can outlive its session - whisper can't be interrupted, so a@@ -537,14 +579,13 @@ class LyricsService extends ChangeNotifier {debugPrint('[Lyrics] chunk at ${start.toStringAsFixed(1)}s: ''${out.lines} lines, exact=${out.exact}');}+ if (_coveredUntil(key, start) < start + 1) _markHole(key, start, window);} on TranscriptionException catch (e) {if (!_on || key != _key) return;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, ' '),- ]);+ _markHole(key, start, window);} else if (e.kind == TranscriptionError.extractFailed &&dur > 0 &&start + _chunkSeconds >= dur - 1) {diff --git a/lib/services/transcript_chunker.dart b/lib/services/transcript_chunker.dartindex 22a9533e..f321d668 100644--- a/lib/services/transcript_chunker.dart+++ b/lib/services/transcript_chunker.dart@@ -180,9 +180,4 @@ class TranscriptChunker {);}- /// 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, ' ')]);}