Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Find the audio for a passage at the start of a chapter when the chapter is its own file

Barnabas1b12decb763e
diff --git a/lib/services/transcription_service.dart b/lib/services/transcription_service.dart
index d4199e77..c0f76791 100644
--- a/lib/services/transcription_service.dart
+++ b/lib/services/transcription_service.dart
@@ -535,6 +535,21 @@ class TranscriptionService {
/// Per-track durations (seconds) from the cached offline session metadata,
/// index-aligned with the downloaded track files. Null when unavailable.
+ /// The first track (file) boundary strictly after [from] and no later than
+ /// [upTo], or null. Windows never cross a file boundary, so a probe that
+ /// starts just before one is cut short and never hears what follows it.
+ double? trackBoundaryBetween(String itemId, double from, double upTo) {
+ final durations = _trackDurations(itemId);
+ if (durations == null || durations.length < 2) return null;
+ var acc = 0.0;
+ for (var i = 0; i < durations.length - 1; i++) {
+ acc += durations[i];
+ if (acc > from + 0.05 && acc <= upTo) return acc;
+ if (acc > upTo) break;
+ }
+ return null;
+ }
+
List<double>? _trackDurations(String itemId) {
final raw = DownloadService().getCachedSessionData(itemId);
if (raw == null || raw.isEmpty) return null;
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index 17a81207..d21030c4 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -2985,7 +2985,19 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
for (var attempt = 0; attempt < maxProbes; attempt++) {
final maxStart =
(hi - _probeWindowSeconds) > lo ? hi - _probeWindowSeconds : lo;
- final probeStart = (est - _probeWindowSeconds / 2).clamp(lo, maxStart);
+ var probeStart = (est - _probeWindowSeconds / 2).clamp(lo, maxStart);
+ // A chapter start is usually a file start too. A window centred on it
+ // begins in the previous file and gets cut at the boundary, so it only
+ // ever hears the end of the last chapter. Start at the boundary instead
+ // so the estimate is inside what gets transcribed.
+ final boundary = TranscriptionService.instance
+ .trackBoundaryBetween(widget.itemId, probeStart, est);
+ if (boundary != null) {
+ debugPrint('[FindAudio] probe#$attempt window start '
+ '${probeStart.toStringAsFixed(1)} -> file boundary '
+ '${boundary.toStringAsFixed(1)}');
+ probeStart = boundary.clamp(lo, maxStart > boundary ? maxStart : boundary);
+ }
List<({double start, double end, String text})> segs;
try {