@storyteller-platform/align 0.1.20 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -348,16 +348,24 @@ class Aligner {
348
348
  };
349
349
  }
350
350
  narrowToAvailableBoundary(boundary) {
351
- const narrowed = { ...boundary };
352
- for (const chapter of this.alignedChapters) {
353
- if (chapter.startOffset > narrowed.start && chapter.startOffset <= narrowed.end) {
354
- narrowed.end = chapter.startOffset - 1;
355
- }
356
- if (chapter.endOffset < narrowed.end && chapter.endOffset >= narrowed.start) {
357
- narrowed.start = chapter.endOffset + 1;
351
+ const available = [
352
+ -1,
353
+ ...this.alignedChapters.toSorted((a, b) => a.startOffset - b.startOffset).flatMap(({ startOffset, endOffset }) => [startOffset, endOffset]),
354
+ Infinity
355
+ ];
356
+ const withinBoundary = [];
357
+ for (let i = 0; i < available.length - 1; i += 2) {
358
+ const [start, end] = [available[i], available[i + 1]];
359
+ if (boundary.start <= start && boundary.end >= start || boundary.start <= end && boundary.end >= end) {
360
+ withinBoundary.push([
361
+ Math.max(boundary.start, start + 1),
362
+ Math.min(boundary.end, end - 1)
363
+ ]);
358
364
  }
359
365
  }
360
- return narrowed;
366
+ const largestBoundary = (0, import_itertools.max)(withinBoundary, ([start, end]) => end - start);
367
+ if (!largestBoundary) return { start: boundary.start, end: boundary.end };
368
+ return { start: largestBoundary[0], end: largestBoundary[1] };
361
369
  }
362
370
  async alignBook(onProgress) {
363
371
  const locale = this.languageOverride ?? await this.epub.getLanguage() ?? new Intl.Locale("en-US");
@@ -5,7 +5,7 @@ import {
5
5
  import { copyFile, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
6
6
  import { dirname as autoDirname, join as autoJoin } from "node:path";
7
7
  import { basename, dirname, parse, relative } from "node:path/posix";
8
- import { enumerate } from "itertools";
8
+ import { enumerate, max } from "itertools";
9
9
  import memoize from "memoize";
10
10
  import { isAudioFile, lookupAudioMime } from "@storyteller-platform/audiobook";
11
11
  import {
@@ -284,16 +284,24 @@ class Aligner {
284
284
  };
285
285
  }
286
286
  narrowToAvailableBoundary(boundary) {
287
- const narrowed = { ...boundary };
288
- for (const chapter of this.alignedChapters) {
289
- if (chapter.startOffset > narrowed.start && chapter.startOffset <= narrowed.end) {
290
- narrowed.end = chapter.startOffset - 1;
291
- }
292
- if (chapter.endOffset < narrowed.end && chapter.endOffset >= narrowed.start) {
293
- narrowed.start = chapter.endOffset + 1;
287
+ const available = [
288
+ -1,
289
+ ...this.alignedChapters.toSorted((a, b) => a.startOffset - b.startOffset).flatMap(({ startOffset, endOffset }) => [startOffset, endOffset]),
290
+ Infinity
291
+ ];
292
+ const withinBoundary = [];
293
+ for (let i = 0; i < available.length - 1; i += 2) {
294
+ const [start, end] = [available[i], available[i + 1]];
295
+ if (boundary.start <= start && boundary.end >= start || boundary.start <= end && boundary.end >= end) {
296
+ withinBoundary.push([
297
+ Math.max(boundary.start, start + 1),
298
+ Math.min(boundary.end, end - 1)
299
+ ]);
294
300
  }
295
301
  }
296
- return narrowed;
302
+ const largestBoundary = max(withinBoundary, ([start, end]) => end - start);
303
+ if (!largestBoundary) return { start: boundary.start, end: boundary.end };
304
+ return { start: largestBoundary[0], end: largestBoundary[1] };
297
305
  }
298
306
  async alignBook(onProgress) {
299
307
  const locale = this.languageOverride ?? await this.epub.getLanguage() ?? new Intl.Locale("en-US");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storyteller-platform/align",
3
- "version": "0.1.20",
3
+ "version": "0.1.21",
4
4
  "description": "A library and CLI for automatically aligning audiobooks and EPUBs to produce Media Overlays",
5
5
  "author": "Shane Friedman",
6
6
  "license": "MIT",