@storyteller-platform/align 0.0.1
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.
- package/LICENSE.txt +21 -0
- package/README.md +3 -0
- package/dist/align/align.cjs +525 -0
- package/dist/align/align.d.cts +58 -0
- package/dist/align/align.d.ts +58 -0
- package/dist/align/align.js +458 -0
- package/dist/align/fuzzy.cjs +164 -0
- package/dist/align/fuzzy.d.cts +6 -0
- package/dist/align/fuzzy.d.ts +6 -0
- package/dist/align/fuzzy.js +141 -0
- package/dist/align/getSentenceRanges.cjs +304 -0
- package/dist/align/getSentenceRanges.d.cts +31 -0
- package/dist/align/getSentenceRanges.d.ts +31 -0
- package/dist/align/getSentenceRanges.js +277 -0
- package/dist/align/parse.cjs +63 -0
- package/dist/align/parse.d.cts +30 -0
- package/dist/align/parse.d.ts +30 -0
- package/dist/align/parse.js +51 -0
- package/dist/chunk-BIEQXUOY.js +50 -0
- package/dist/cli/bin.cjs +368 -0
- package/dist/cli/bin.d.cts +1 -0
- package/dist/cli/bin.d.ts +1 -0
- package/dist/cli/bin.js +319 -0
- package/dist/common/ffmpeg.cjs +232 -0
- package/dist/common/ffmpeg.d.cts +33 -0
- package/dist/common/ffmpeg.d.ts +33 -0
- package/dist/common/ffmpeg.js +196 -0
- package/dist/common/logging.cjs +45 -0
- package/dist/common/logging.d.cts +5 -0
- package/dist/common/logging.d.ts +5 -0
- package/dist/common/logging.js +12 -0
- package/dist/common/parse.cjs +73 -0
- package/dist/common/parse.d.cts +28 -0
- package/dist/common/parse.d.ts +28 -0
- package/dist/common/parse.js +56 -0
- package/dist/common/shell.cjs +30 -0
- package/dist/common/shell.d.cts +3 -0
- package/dist/common/shell.d.ts +3 -0
- package/dist/common/shell.js +7 -0
- package/dist/index.cjs +37 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +11 -0
- package/dist/markup/__tests__/markup.test.cjs +464 -0
- package/dist/markup/__tests__/markup.test.d.cts +2 -0
- package/dist/markup/__tests__/markup.test.d.ts +2 -0
- package/dist/markup/__tests__/markup.test.js +441 -0
- package/dist/markup/markup.cjs +316 -0
- package/dist/markup/markup.d.cts +24 -0
- package/dist/markup/markup.d.ts +24 -0
- package/dist/markup/markup.js +254 -0
- package/dist/markup/parse.cjs +55 -0
- package/dist/markup/parse.d.cts +17 -0
- package/dist/markup/parse.d.ts +17 -0
- package/dist/markup/parse.js +43 -0
- package/dist/markup/segmentation.cjs +87 -0
- package/dist/markup/segmentation.d.cts +8 -0
- package/dist/markup/segmentation.d.ts +8 -0
- package/dist/markup/segmentation.js +67 -0
- package/dist/markup/semantics.cjs +79 -0
- package/dist/markup/semantics.d.cts +6 -0
- package/dist/markup/semantics.d.ts +6 -0
- package/dist/markup/semantics.js +53 -0
- package/dist/process/AudioEncoding.cjs +16 -0
- package/dist/process/AudioEncoding.d.cts +8 -0
- package/dist/process/AudioEncoding.d.ts +8 -0
- package/dist/process/AudioEncoding.js +0 -0
- package/dist/process/__tests__/processAudiobook.test.cjs +232 -0
- package/dist/process/__tests__/processAudiobook.test.d.cts +2 -0
- package/dist/process/__tests__/processAudiobook.test.d.ts +2 -0
- package/dist/process/__tests__/processAudiobook.test.js +209 -0
- package/dist/process/mime.cjs +43 -0
- package/dist/process/mime.d.cts +3 -0
- package/dist/process/mime.d.ts +3 -0
- package/dist/process/mime.js +24 -0
- package/dist/process/parse.cjs +84 -0
- package/dist/process/parse.d.cts +28 -0
- package/dist/process/parse.d.ts +28 -0
- package/dist/process/parse.js +73 -0
- package/dist/process/processAudiobook.cjs +220 -0
- package/dist/process/processAudiobook.d.cts +24 -0
- package/dist/process/processAudiobook.d.ts +24 -0
- package/dist/process/processAudiobook.js +166 -0
- package/dist/process/ranges.cjs +203 -0
- package/dist/process/ranges.d.cts +15 -0
- package/dist/process/ranges.d.ts +15 -0
- package/dist/process/ranges.js +137 -0
- package/dist/transcribe/parse.cjs +149 -0
- package/dist/transcribe/parse.d.cts +114 -0
- package/dist/transcribe/parse.d.ts +114 -0
- package/dist/transcribe/parse.js +143 -0
- package/dist/transcribe/transcribe.cjs +400 -0
- package/dist/transcribe/transcribe.d.cts +41 -0
- package/dist/transcribe/transcribe.d.ts +41 -0
- package/dist/transcribe/transcribe.js +330 -0
- package/package.json +96 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shane Friedman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
9
|
+
var __typeError = (msg) => {
|
|
10
|
+
throw TypeError(msg);
|
|
11
|
+
};
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
+
mod
|
|
31
|
+
));
|
|
32
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
33
|
+
var __using = (stack, value, async) => {
|
|
34
|
+
if (value != null) {
|
|
35
|
+
if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
|
|
36
|
+
var dispose, inner;
|
|
37
|
+
if (async) dispose = value[__knownSymbol("asyncDispose")];
|
|
38
|
+
if (dispose === void 0) {
|
|
39
|
+
dispose = value[__knownSymbol("dispose")];
|
|
40
|
+
if (async) inner = dispose;
|
|
41
|
+
}
|
|
42
|
+
if (typeof dispose !== "function") __typeError("Object not disposable");
|
|
43
|
+
if (inner) dispose = function() {
|
|
44
|
+
try {
|
|
45
|
+
inner.call(this);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
return Promise.reject(e);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
stack.push([async, dispose, value]);
|
|
51
|
+
} else if (async) {
|
|
52
|
+
stack.push([async]);
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
};
|
|
56
|
+
var __callDispose = (stack, error, hasError) => {
|
|
57
|
+
var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
|
|
58
|
+
return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
|
|
59
|
+
};
|
|
60
|
+
var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
|
|
61
|
+
var next = (it) => {
|
|
62
|
+
while (it = stack.pop()) {
|
|
63
|
+
try {
|
|
64
|
+
var result = it[1] && it[1].call(it[2]);
|
|
65
|
+
if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
|
|
66
|
+
} catch (e) {
|
|
67
|
+
fail(e);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (hasError) throw error;
|
|
71
|
+
};
|
|
72
|
+
return next();
|
|
73
|
+
};
|
|
74
|
+
var align_exports = {};
|
|
75
|
+
__export(align_exports, {
|
|
76
|
+
Aligner: () => Aligner,
|
|
77
|
+
align: () => align,
|
|
78
|
+
concatTranscriptions: () => concatTranscriptions
|
|
79
|
+
});
|
|
80
|
+
module.exports = __toCommonJS(align_exports);
|
|
81
|
+
var import_promises = require("node:fs/promises");
|
|
82
|
+
var import_node_path = require("node:path");
|
|
83
|
+
var import_posix = require("node:path/posix");
|
|
84
|
+
var import_memoize = __toESM(require("memoize"), 1);
|
|
85
|
+
var import_audiobook = require("@storyteller-platform/audiobook");
|
|
86
|
+
var import_epub = require("@storyteller-platform/epub");
|
|
87
|
+
var import_ghost_story = require("@storyteller-platform/ghost-story");
|
|
88
|
+
var import_ffmpeg = require("../common/ffmpeg.cjs");
|
|
89
|
+
var import_segmentation = require("../markup/segmentation.cjs");
|
|
90
|
+
var import_fuzzy = require("./fuzzy.cjs");
|
|
91
|
+
var import_getSentenceRanges = require("./getSentenceRanges.cjs");
|
|
92
|
+
const OFFSET_SEARCH_WINDOW_SIZE = 5e3;
|
|
93
|
+
async function align(input, output, transcriptionsDir, audiobookDir, options) {
|
|
94
|
+
var _stack = [];
|
|
95
|
+
try {
|
|
96
|
+
await (0, import_promises.copyFile)(input, output);
|
|
97
|
+
const audiobookFiles = await (0, import_promises.readdir)(audiobookDir).then(
|
|
98
|
+
(filenames) => filenames.filter((f) => (0, import_audiobook.isAudioFile)(f)).map((f) => (0, import_node_path.join)(audiobookDir, f))
|
|
99
|
+
);
|
|
100
|
+
const epub = __using(_stack, await import_epub.Epub.from(output));
|
|
101
|
+
const transcriptions = await (0, import_promises.readdir)(transcriptionsDir).then(
|
|
102
|
+
(filenames) => filenames.filter((f) => f.endsWith(".json")).map((f) => (0, import_node_path.join)(transcriptionsDir, f))
|
|
103
|
+
).then(
|
|
104
|
+
(filepaths) => Promise.all(
|
|
105
|
+
filepaths.map(async (p) => (0, import_promises.readFile)(p, { encoding: "utf-8" }))
|
|
106
|
+
)
|
|
107
|
+
).then(
|
|
108
|
+
(contents) => contents.map(
|
|
109
|
+
(c) => JSON.parse(c)
|
|
110
|
+
)
|
|
111
|
+
);
|
|
112
|
+
const aligner = new Aligner(
|
|
113
|
+
epub,
|
|
114
|
+
audiobookFiles,
|
|
115
|
+
transcriptions,
|
|
116
|
+
options.granularity,
|
|
117
|
+
options.primaryLocale,
|
|
118
|
+
options.logger
|
|
119
|
+
);
|
|
120
|
+
const timing = await aligner.alignBook(options.onProgress);
|
|
121
|
+
if (options.reportsPath) {
|
|
122
|
+
await (0, import_promises.mkdir)((0, import_node_path.dirname)(options.reportsPath), { recursive: true });
|
|
123
|
+
await (0, import_promises.writeFile)(
|
|
124
|
+
options.reportsPath,
|
|
125
|
+
JSON.stringify(aligner.report, null, 2),
|
|
126
|
+
{
|
|
127
|
+
encoding: "utf-8"
|
|
128
|
+
}
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
return timing;
|
|
132
|
+
} catch (_) {
|
|
133
|
+
var _error = _, _hasError = true;
|
|
134
|
+
} finally {
|
|
135
|
+
__callDispose(_stack, _error, _hasError);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
class Aligner {
|
|
139
|
+
constructor(epub, audiofiles, transcriptions, granularity, languageOverride, logger) {
|
|
140
|
+
this.epub = epub;
|
|
141
|
+
this.languageOverride = languageOverride;
|
|
142
|
+
this.logger = logger;
|
|
143
|
+
this.transcription = concatTranscriptions(transcriptions, audiofiles);
|
|
144
|
+
this.getChapterSentences = (0, import_memoize.default)(this.getChapterSentences.bind(this));
|
|
145
|
+
this.granularity = granularity ?? "sentence";
|
|
146
|
+
}
|
|
147
|
+
transcription;
|
|
148
|
+
totalDuration = 0;
|
|
149
|
+
alignedChapters = [];
|
|
150
|
+
timing = (0, import_ghost_story.createAggregator)();
|
|
151
|
+
granularity;
|
|
152
|
+
report = {
|
|
153
|
+
chapters: []
|
|
154
|
+
};
|
|
155
|
+
findBestOffset(epubSentences, transcriptionText, lastMatchOffset) {
|
|
156
|
+
let i = 0;
|
|
157
|
+
while (i < transcriptionText.length) {
|
|
158
|
+
let startSentence = 0;
|
|
159
|
+
const proposedStartIndex = (lastMatchOffset + i) % transcriptionText.length;
|
|
160
|
+
const proposedEndIndex = (proposedStartIndex + OFFSET_SEARCH_WINDOW_SIZE) % transcriptionText.length;
|
|
161
|
+
const wrapping = proposedEndIndex < proposedStartIndex;
|
|
162
|
+
let endIndex = wrapping ? transcriptionText.length : proposedEndIndex;
|
|
163
|
+
let startIndex = proposedStartIndex;
|
|
164
|
+
let startSeen = null;
|
|
165
|
+
let endSeen = null;
|
|
166
|
+
for (const aligned of this.alignedChapters) {
|
|
167
|
+
if (startSeen !== null && endSeen === aligned.startOffset) {
|
|
168
|
+
endSeen = aligned.endOffset;
|
|
169
|
+
} else {
|
|
170
|
+
startSeen = aligned.startOffset;
|
|
171
|
+
endSeen = aligned.endOffset;
|
|
172
|
+
}
|
|
173
|
+
if (startIndex >= startSeen && startIndex < endSeen) {
|
|
174
|
+
startIndex = endSeen;
|
|
175
|
+
}
|
|
176
|
+
if (endIndex >= startSeen && endIndex <= endSeen) {
|
|
177
|
+
endIndex = startSeen;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (startIndex < endIndex) {
|
|
181
|
+
const transcriptionTextSlice = transcriptionText.slice(
|
|
182
|
+
startIndex,
|
|
183
|
+
endIndex
|
|
184
|
+
);
|
|
185
|
+
while (startSentence < epubSentences.length) {
|
|
186
|
+
const queryString = epubSentences.slice(startSentence, startSentence + 6).join(" ");
|
|
187
|
+
const firstMatch = (0, import_fuzzy.findNearestMatch)(
|
|
188
|
+
queryString.toLowerCase(),
|
|
189
|
+
transcriptionTextSlice.toLowerCase(),
|
|
190
|
+
Math.max(Math.floor(0.1 * queryString.length), 1)
|
|
191
|
+
);
|
|
192
|
+
if (firstMatch) {
|
|
193
|
+
return {
|
|
194
|
+
startSentence,
|
|
195
|
+
transcriptionOffset: (firstMatch.index + startIndex) % transcriptionText.length
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
startSentence += 3;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (wrapping) {
|
|
202
|
+
i += transcriptionText.length - proposedStartIndex;
|
|
203
|
+
} else {
|
|
204
|
+
i += Math.floor(OFFSET_SEARCH_WINDOW_SIZE / 2);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return { startSentence: 0, transcriptionOffset: null };
|
|
208
|
+
}
|
|
209
|
+
async getChapterSentences(chapterId) {
|
|
210
|
+
const chapterXml = await this.epub.readXhtmlItemContents(chapterId);
|
|
211
|
+
const segmentation = await (0, import_segmentation.getXhtmlSegmentation)(
|
|
212
|
+
import_epub.Epub.getXhtmlBody(chapterXml),
|
|
213
|
+
{
|
|
214
|
+
primaryLocale: this.languageOverride ?? await this.epub.getLanguage()
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
return segmentation.sentences.map((s) => s.text);
|
|
218
|
+
}
|
|
219
|
+
async writeAlignedChapter(alignedChapter) {
|
|
220
|
+
const { chapter, sentenceRanges, xml } = alignedChapter;
|
|
221
|
+
const audiofiles = Array.from(
|
|
222
|
+
new Set(sentenceRanges.map(({ audiofile }) => audiofile))
|
|
223
|
+
);
|
|
224
|
+
await Promise.all(
|
|
225
|
+
audiofiles.map(async (audiofile) => {
|
|
226
|
+
const { name, base } = (0, import_posix.parse)(audiofile);
|
|
227
|
+
const id = `audio_${name}`;
|
|
228
|
+
const manifest = await this.epub.getManifest();
|
|
229
|
+
if (id in manifest) return;
|
|
230
|
+
const epubAudioFilename = `Audio/${base}`;
|
|
231
|
+
const duration = await (0, import_ffmpeg.getTrackDuration)(audiofile);
|
|
232
|
+
this.totalDuration += duration;
|
|
233
|
+
const audio = await (0, import_promises.readFile)(audiofile);
|
|
234
|
+
const mediaType = (0, import_audiobook.lookupAudioMime)(base) ?? void 0;
|
|
235
|
+
await this.epub.addManifestItem(
|
|
236
|
+
{
|
|
237
|
+
id,
|
|
238
|
+
href: epubAudioFilename,
|
|
239
|
+
mediaType
|
|
240
|
+
},
|
|
241
|
+
audio
|
|
242
|
+
);
|
|
243
|
+
})
|
|
244
|
+
);
|
|
245
|
+
const { name: chapterStem } = (0, import_posix.parse)(chapter.href);
|
|
246
|
+
const mediaOverlayId = `${chapter.id}_overlay`;
|
|
247
|
+
await this.epub.addManifestItem(
|
|
248
|
+
{
|
|
249
|
+
id: mediaOverlayId,
|
|
250
|
+
href: `MediaOverlays/${chapterStem}.smil`,
|
|
251
|
+
mediaType: "application/smil+xml"
|
|
252
|
+
},
|
|
253
|
+
createMediaOverlay(chapter, sentenceRanges),
|
|
254
|
+
"xml"
|
|
255
|
+
);
|
|
256
|
+
await this.epub.updateManifestItem(chapter.id, {
|
|
257
|
+
...chapter,
|
|
258
|
+
mediaOverlay: mediaOverlayId
|
|
259
|
+
});
|
|
260
|
+
await this.epub.writeXhtmlItemContents(chapter.id, xml);
|
|
261
|
+
const chapterDuration = (0, import_getSentenceRanges.getChapterDuration)(sentenceRanges);
|
|
262
|
+
await this.epub.addMetadata({
|
|
263
|
+
type: "meta",
|
|
264
|
+
properties: {
|
|
265
|
+
property: "media:duration",
|
|
266
|
+
refines: `#${mediaOverlayId}`
|
|
267
|
+
},
|
|
268
|
+
value: import_epub.Epub.formatSmilDuration(chapterDuration)
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
addChapterReport(chapter, chapterSentences, sentenceRanges, startSentence, transcriptionOffset) {
|
|
272
|
+
this.report.chapters.push({
|
|
273
|
+
href: chapter.href,
|
|
274
|
+
transcriptionOffset,
|
|
275
|
+
transcriptionContext: {
|
|
276
|
+
before: this.transcription.transcript.slice(
|
|
277
|
+
Math.max(0, transcriptionOffset - 30),
|
|
278
|
+
transcriptionOffset
|
|
279
|
+
),
|
|
280
|
+
after: this.transcription.transcript.slice(
|
|
281
|
+
transcriptionOffset,
|
|
282
|
+
Math.min(
|
|
283
|
+
transcriptionOffset + 30,
|
|
284
|
+
this.transcription.transcript.length - 1
|
|
285
|
+
)
|
|
286
|
+
)
|
|
287
|
+
},
|
|
288
|
+
firstMatchedSentenceId: startSentence,
|
|
289
|
+
firstMatchedSentenceContext: {
|
|
290
|
+
prevSentence: chapterSentences[startSentence - 1] ?? null,
|
|
291
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
292
|
+
matchedSentence: chapterSentences[startSentence],
|
|
293
|
+
nextSentence: chapterSentences[startSentence + 1] ?? null
|
|
294
|
+
},
|
|
295
|
+
audioFiles: sentenceRanges.reduce((acc, range) => {
|
|
296
|
+
const existing = acc.find(
|
|
297
|
+
(context) => context.filepath === range.audiofile
|
|
298
|
+
);
|
|
299
|
+
if (existing) {
|
|
300
|
+
existing.end = range.end;
|
|
301
|
+
return acc;
|
|
302
|
+
}
|
|
303
|
+
acc.push({
|
|
304
|
+
filepath: range.audiofile,
|
|
305
|
+
start: range.start,
|
|
306
|
+
end: range.end
|
|
307
|
+
});
|
|
308
|
+
return acc;
|
|
309
|
+
}, [])
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
async alignChapter(startSentence, chapterId, transcriptionOffset, lastSentenceRange) {
|
|
313
|
+
const timing = (0, import_ghost_story.createTiming)();
|
|
314
|
+
timing.start("read contents");
|
|
315
|
+
const manifest = await this.epub.getManifest();
|
|
316
|
+
const chapter = manifest[chapterId];
|
|
317
|
+
if (!chapter)
|
|
318
|
+
throw new Error(
|
|
319
|
+
`Failed to align chapter: could not find chapter with id ${chapterId} in manifest`
|
|
320
|
+
);
|
|
321
|
+
const chapterXml = await this.epub.readXhtmlItemContents(chapterId);
|
|
322
|
+
timing.end("read contents");
|
|
323
|
+
timing.start("split to sentences");
|
|
324
|
+
const chapterSentences = await this.getChapterSentences(chapterId);
|
|
325
|
+
timing.end("split to sentences");
|
|
326
|
+
timing.start("align sentences");
|
|
327
|
+
const { sentenceRanges, transcriptionOffset: endTranscriptionOffset } = await (0, import_getSentenceRanges.getSentenceRanges)(
|
|
328
|
+
startSentence,
|
|
329
|
+
this.transcription,
|
|
330
|
+
chapterSentences,
|
|
331
|
+
transcriptionOffset,
|
|
332
|
+
lastSentenceRange
|
|
333
|
+
);
|
|
334
|
+
timing.end("align sentences");
|
|
335
|
+
timing.start("expand ranges");
|
|
336
|
+
const interpolated = await (0, import_getSentenceRanges.interpolateSentenceRanges)(
|
|
337
|
+
sentenceRanges,
|
|
338
|
+
lastSentenceRange
|
|
339
|
+
);
|
|
340
|
+
const expanded = (0, import_getSentenceRanges.expandEmptySentenceRanges)(interpolated);
|
|
341
|
+
timing.end("expand ranges");
|
|
342
|
+
const storytellerStylesheetUrl = (0, import_posix.relative)(
|
|
343
|
+
(0, import_posix.dirname)(chapter.href),
|
|
344
|
+
"Styles/storyteller-readaloud.css"
|
|
345
|
+
);
|
|
346
|
+
import_epub.Epub.addLinkToXhtmlHead(chapterXml, {
|
|
347
|
+
rel: "stylesheet",
|
|
348
|
+
href: storytellerStylesheetUrl,
|
|
349
|
+
type: "text/css"
|
|
350
|
+
});
|
|
351
|
+
this.alignedChapters.push({
|
|
352
|
+
chapter,
|
|
353
|
+
xml: chapterXml,
|
|
354
|
+
sentenceRanges: expanded,
|
|
355
|
+
startOffset: transcriptionOffset,
|
|
356
|
+
endOffset: endTranscriptionOffset
|
|
357
|
+
});
|
|
358
|
+
this.addChapterReport(
|
|
359
|
+
chapter,
|
|
360
|
+
chapterSentences,
|
|
361
|
+
expanded,
|
|
362
|
+
startSentence,
|
|
363
|
+
transcriptionOffset
|
|
364
|
+
);
|
|
365
|
+
return {
|
|
366
|
+
lastSentenceRange: expanded[expanded.length - 1] ?? null,
|
|
367
|
+
endTranscriptionOffset,
|
|
368
|
+
timing
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
async alignBook(onProgress) {
|
|
372
|
+
var _a, _b, _c, _d, _e, _f;
|
|
373
|
+
this.timing.setMetadata(
|
|
374
|
+
"language",
|
|
375
|
+
((_a = this.languageOverride ?? await this.epub.getLanguage()) == null ? void 0 : _a.language) ?? "unknown"
|
|
376
|
+
);
|
|
377
|
+
this.timing.setMetadata("granularity", this.granularity);
|
|
378
|
+
const spine = await this.epub.getSpineItems();
|
|
379
|
+
const transcriptionText = this.transcription.transcript;
|
|
380
|
+
let lastTranscriptionOffset = 0;
|
|
381
|
+
let lastSentenceRange = null;
|
|
382
|
+
for (let index = 0; index < spine.length; index++) {
|
|
383
|
+
onProgress == null ? void 0 : onProgress(index / spine.length);
|
|
384
|
+
const spineItem = spine[index];
|
|
385
|
+
(_b = this.logger) == null ? void 0 : _b.info(
|
|
386
|
+
`Aligning epub item #${index} : ${(0, import_posix.basename)(spineItem.href)}`
|
|
387
|
+
);
|
|
388
|
+
const chapterId = spineItem.id;
|
|
389
|
+
const chapterSentences = await this.getChapterSentences(chapterId);
|
|
390
|
+
if (chapterSentences.length === 0) {
|
|
391
|
+
(_c = this.logger) == null ? void 0 : _c.info(`Chapter #${index} has no text; skipping`);
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
if (chapterSentences.length < 2 && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
395
|
+
chapterSentences[0].split(" ").length < 4) {
|
|
396
|
+
(_d = this.logger) == null ? void 0 : _d.info(
|
|
397
|
+
`Chapter #${index} is fewer than four words; skipping`
|
|
398
|
+
);
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
const { startSentence, transcriptionOffset } = this.findBestOffset(
|
|
402
|
+
chapterSentences,
|
|
403
|
+
transcriptionText,
|
|
404
|
+
lastTranscriptionOffset
|
|
405
|
+
);
|
|
406
|
+
if (transcriptionOffset === null) {
|
|
407
|
+
(_e = this.logger) == null ? void 0 : _e.info(
|
|
408
|
+
`Couldn't find matching transcription for chapter #${index}`
|
|
409
|
+
);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
(_f = this.logger) == null ? void 0 : _f.info(
|
|
413
|
+
`Chapter #${index} best matches transcription at offset ${transcriptionOffset}, starting at sentence ${startSentence}`
|
|
414
|
+
);
|
|
415
|
+
const result = await this.alignChapter(
|
|
416
|
+
startSentence,
|
|
417
|
+
chapterId,
|
|
418
|
+
transcriptionOffset,
|
|
419
|
+
lastSentenceRange
|
|
420
|
+
);
|
|
421
|
+
lastSentenceRange = result.lastSentenceRange;
|
|
422
|
+
lastTranscriptionOffset = result.endTranscriptionOffset;
|
|
423
|
+
this.timing.add(result.timing.summary());
|
|
424
|
+
}
|
|
425
|
+
if (lastSentenceRange) {
|
|
426
|
+
lastSentenceRange.end = await (0, import_ffmpeg.getTrackDuration)(
|
|
427
|
+
lastSentenceRange.audiofile
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
for (const alignedChapter of this.alignedChapters) {
|
|
431
|
+
await this.writeAlignedChapter(alignedChapter);
|
|
432
|
+
}
|
|
433
|
+
await this.epub.addMetadata({
|
|
434
|
+
type: "meta",
|
|
435
|
+
properties: { property: "media:duration" },
|
|
436
|
+
value: import_epub.Epub.formatSmilDuration(this.totalDuration)
|
|
437
|
+
});
|
|
438
|
+
await this.epub.addMetadata({
|
|
439
|
+
type: "meta",
|
|
440
|
+
properties: { property: "media:active-class" },
|
|
441
|
+
value: "-epub-media-overlay-active"
|
|
442
|
+
});
|
|
443
|
+
await this.epub.addManifestItem(
|
|
444
|
+
{
|
|
445
|
+
id: "storyteller_readaloud_styles",
|
|
446
|
+
href: "Styles/storyteller-readaloud.css",
|
|
447
|
+
mediaType: "text/css"
|
|
448
|
+
},
|
|
449
|
+
`
|
|
450
|
+
.-epub-media-overlay-active {
|
|
451
|
+
background-color: #ffb;
|
|
452
|
+
}
|
|
453
|
+
`,
|
|
454
|
+
"utf-8"
|
|
455
|
+
);
|
|
456
|
+
return this.timing;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
function createMediaOverlay(chapter, sentenceRanges) {
|
|
460
|
+
return [
|
|
461
|
+
import_epub.Epub.createXmlElement(
|
|
462
|
+
"smil",
|
|
463
|
+
{
|
|
464
|
+
xmlns: "https://www.w3.org/ns/SMIL",
|
|
465
|
+
"xmlns:epub": "http://www.idpf.org/2007/ops",
|
|
466
|
+
version: "3.0"
|
|
467
|
+
},
|
|
468
|
+
[
|
|
469
|
+
import_epub.Epub.createXmlElement("body", {}, [
|
|
470
|
+
import_epub.Epub.createXmlElement(
|
|
471
|
+
"seq",
|
|
472
|
+
{
|
|
473
|
+
id: `${chapter.id}_overlay`,
|
|
474
|
+
"epub:textref": `../${chapter.href}`,
|
|
475
|
+
"epub:type": "chapter"
|
|
476
|
+
},
|
|
477
|
+
sentenceRanges.map(
|
|
478
|
+
(sentenceRange) => import_epub.Epub.createXmlElement(
|
|
479
|
+
"par",
|
|
480
|
+
{
|
|
481
|
+
id: `${chapter.id}-s${sentenceRange.id}`
|
|
482
|
+
},
|
|
483
|
+
[
|
|
484
|
+
import_epub.Epub.createXmlElement("text", {
|
|
485
|
+
src: `../${chapter.href}#${chapter.id}-s${sentenceRange.id}`
|
|
486
|
+
}),
|
|
487
|
+
import_epub.Epub.createXmlElement("audio", {
|
|
488
|
+
src: `../Audio/${(0, import_posix.basename)(sentenceRange.audiofile)}`,
|
|
489
|
+
clipBegin: `${sentenceRange.start.toFixed(3)}s`,
|
|
490
|
+
clipEnd: `${sentenceRange.end.toFixed(3)}s`
|
|
491
|
+
})
|
|
492
|
+
]
|
|
493
|
+
)
|
|
494
|
+
)
|
|
495
|
+
)
|
|
496
|
+
])
|
|
497
|
+
]
|
|
498
|
+
)
|
|
499
|
+
];
|
|
500
|
+
}
|
|
501
|
+
function concatTranscriptions(transcriptions, audiofiles) {
|
|
502
|
+
return transcriptions.reduce(
|
|
503
|
+
(acc, transcription, index) => ({
|
|
504
|
+
...acc,
|
|
505
|
+
transcript: acc.transcript + " " + transcription.transcript,
|
|
506
|
+
timeline: [
|
|
507
|
+
...acc.timeline,
|
|
508
|
+
...transcription.timeline.map((entry) => ({
|
|
509
|
+
...entry,
|
|
510
|
+
startOffsetUtf16: (entry.startOffsetUtf16 ?? 0) + acc.transcript.length + 1,
|
|
511
|
+
endOffsetUtf16: (entry.endOffsetUtf16 ?? 0) + acc.transcript.length + 1,
|
|
512
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
513
|
+
audiofile: audiofiles[index]
|
|
514
|
+
}))
|
|
515
|
+
]
|
|
516
|
+
}),
|
|
517
|
+
{ transcript: "", timeline: [] }
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
521
|
+
0 && (module.exports = {
|
|
522
|
+
Aligner,
|
|
523
|
+
align,
|
|
524
|
+
concatTranscriptions
|
|
525
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as _storyteller_platform_ghost_story from '@storyteller-platform/ghost-story';
|
|
2
|
+
import { Logger } from 'pino';
|
|
3
|
+
import { Epub } from '@storyteller-platform/epub';
|
|
4
|
+
import { RecognitionResult } from '@storyteller-platform/ghost-story/recognition';
|
|
5
|
+
import { StorytellerTranscription } from './getSentenceRanges.cjs';
|
|
6
|
+
|
|
7
|
+
interface AudioFileContext {
|
|
8
|
+
start: number;
|
|
9
|
+
end: number;
|
|
10
|
+
filepath: string;
|
|
11
|
+
}
|
|
12
|
+
interface ChapterReport {
|
|
13
|
+
href: string;
|
|
14
|
+
transcriptionOffset: number;
|
|
15
|
+
transcriptionContext: {
|
|
16
|
+
before: string;
|
|
17
|
+
after: string;
|
|
18
|
+
};
|
|
19
|
+
firstMatchedSentenceId: number;
|
|
20
|
+
firstMatchedSentenceContext: {
|
|
21
|
+
prevSentence: string | null;
|
|
22
|
+
matchedSentence: string;
|
|
23
|
+
nextSentence: string | null;
|
|
24
|
+
};
|
|
25
|
+
audioFiles: AudioFileContext[];
|
|
26
|
+
}
|
|
27
|
+
interface Report {
|
|
28
|
+
chapters: ChapterReport[];
|
|
29
|
+
}
|
|
30
|
+
interface AlignOptions {
|
|
31
|
+
reportsPath?: string | null | undefined;
|
|
32
|
+
granularity: "sentence" | "word" | null | undefined;
|
|
33
|
+
primaryLocale?: Intl.Locale | null | undefined;
|
|
34
|
+
logger?: Logger | null | undefined;
|
|
35
|
+
onProgress?: ((progress: number) => void) | null | undefined;
|
|
36
|
+
}
|
|
37
|
+
declare function align(input: string, output: string, transcriptionsDir: string, audiobookDir: string, options: AlignOptions): Promise<_storyteller_platform_ghost_story.TimingAggregator>;
|
|
38
|
+
declare class Aligner {
|
|
39
|
+
epub: Epub;
|
|
40
|
+
private languageOverride?;
|
|
41
|
+
private logger?;
|
|
42
|
+
private transcription;
|
|
43
|
+
private totalDuration;
|
|
44
|
+
private alignedChapters;
|
|
45
|
+
private timing;
|
|
46
|
+
private granularity;
|
|
47
|
+
report: Report;
|
|
48
|
+
constructor(epub: Epub, audiofiles: string[], transcriptions: Pick<RecognitionResult, "transcript" | "timeline">[], granularity: "sentence" | "word" | null | undefined, languageOverride?: (Intl.Locale | null) | undefined, logger?: (Logger | null) | undefined);
|
|
49
|
+
private findBestOffset;
|
|
50
|
+
private getChapterSentences;
|
|
51
|
+
private writeAlignedChapter;
|
|
52
|
+
private addChapterReport;
|
|
53
|
+
private alignChapter;
|
|
54
|
+
alignBook(onProgress?: ((progress: number) => void) | null): Promise<_storyteller_platform_ghost_story.TimingAggregator>;
|
|
55
|
+
}
|
|
56
|
+
declare function concatTranscriptions(transcriptions: Pick<RecognitionResult, "transcript" | "timeline">[], audiofiles: string[]): StorytellerTranscription;
|
|
57
|
+
|
|
58
|
+
export { type AlignOptions, Aligner, align, concatTranscriptions };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as _storyteller_platform_ghost_story from '@storyteller-platform/ghost-story';
|
|
2
|
+
import { Logger } from 'pino';
|
|
3
|
+
import { Epub } from '@storyteller-platform/epub';
|
|
4
|
+
import { RecognitionResult } from '@storyteller-platform/ghost-story/recognition';
|
|
5
|
+
import { StorytellerTranscription } from './getSentenceRanges.js';
|
|
6
|
+
|
|
7
|
+
interface AudioFileContext {
|
|
8
|
+
start: number;
|
|
9
|
+
end: number;
|
|
10
|
+
filepath: string;
|
|
11
|
+
}
|
|
12
|
+
interface ChapterReport {
|
|
13
|
+
href: string;
|
|
14
|
+
transcriptionOffset: number;
|
|
15
|
+
transcriptionContext: {
|
|
16
|
+
before: string;
|
|
17
|
+
after: string;
|
|
18
|
+
};
|
|
19
|
+
firstMatchedSentenceId: number;
|
|
20
|
+
firstMatchedSentenceContext: {
|
|
21
|
+
prevSentence: string | null;
|
|
22
|
+
matchedSentence: string;
|
|
23
|
+
nextSentence: string | null;
|
|
24
|
+
};
|
|
25
|
+
audioFiles: AudioFileContext[];
|
|
26
|
+
}
|
|
27
|
+
interface Report {
|
|
28
|
+
chapters: ChapterReport[];
|
|
29
|
+
}
|
|
30
|
+
interface AlignOptions {
|
|
31
|
+
reportsPath?: string | null | undefined;
|
|
32
|
+
granularity: "sentence" | "word" | null | undefined;
|
|
33
|
+
primaryLocale?: Intl.Locale | null | undefined;
|
|
34
|
+
logger?: Logger | null | undefined;
|
|
35
|
+
onProgress?: ((progress: number) => void) | null | undefined;
|
|
36
|
+
}
|
|
37
|
+
declare function align(input: string, output: string, transcriptionsDir: string, audiobookDir: string, options: AlignOptions): Promise<_storyteller_platform_ghost_story.TimingAggregator>;
|
|
38
|
+
declare class Aligner {
|
|
39
|
+
epub: Epub;
|
|
40
|
+
private languageOverride?;
|
|
41
|
+
private logger?;
|
|
42
|
+
private transcription;
|
|
43
|
+
private totalDuration;
|
|
44
|
+
private alignedChapters;
|
|
45
|
+
private timing;
|
|
46
|
+
private granularity;
|
|
47
|
+
report: Report;
|
|
48
|
+
constructor(epub: Epub, audiofiles: string[], transcriptions: Pick<RecognitionResult, "transcript" | "timeline">[], granularity: "sentence" | "word" | null | undefined, languageOverride?: (Intl.Locale | null) | undefined, logger?: (Logger | null) | undefined);
|
|
49
|
+
private findBestOffset;
|
|
50
|
+
private getChapterSentences;
|
|
51
|
+
private writeAlignedChapter;
|
|
52
|
+
private addChapterReport;
|
|
53
|
+
private alignChapter;
|
|
54
|
+
alignBook(onProgress?: ((progress: number) => void) | null): Promise<_storyteller_platform_ghost_story.TimingAggregator>;
|
|
55
|
+
}
|
|
56
|
+
declare function concatTranscriptions(transcriptions: Pick<RecognitionResult, "transcript" | "timeline">[], audiofiles: string[]): StorytellerTranscription;
|
|
57
|
+
|
|
58
|
+
export { type AlignOptions, Aligner, align, concatTranscriptions };
|