tts-narrator
Version:
Generate narration with Text-To-Speech technology
114 lines (113 loc) • 4.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveScript = exports.loadScript = exports.NarrationScript = exports.NarrationChapter = exports.NarrationSection = exports.NarrationParagraph = void 0;
const tslib_1 = require("tslib");
/* eslint-disable no-use-before-define */
const js_yaml_1 = require("js-yaml");
const lodash_1 = require("lodash");
const fs = tslib_1.__importStar(require("node:fs"));
const node_util_1 = require("node:util");
const readFileAsPromise = (0, node_util_1.promisify)(fs.readFile);
const writeFileAsPromise = (0, node_util_1.promisify)(fs.writeFile);
class NarrationParagraph {
constructor(paragraph, index, section, chapter, script) {
this.paragraph = paragraph;
this.index = index;
this.section = section;
this.chapter = chapter;
this.script = script;
}
get settings() {
return (Object.assign(Object.assign({}, this.section.settings), this.paragraph.settings));
}
get key() {
var _a;
return (_a = this.paragraph.key) !== null && _a !== void 0 ? _a : String(this.index);
}
get text() {
return this.paragraph.text;
}
get ssml() {
return this.paragraph.ssml;
}
}
exports.NarrationParagraph = NarrationParagraph;
class NarrationSection {
constructor(section, index, chapter, script) {
this.section = section;
this.index = index;
this.chapter = chapter;
this.script = script;
this.paragraphs = section.paragraphs.map((paragraph, index) => new NarrationParagraph(paragraph, index + 1, this, chapter, script));
}
get settings() {
return (Object.assign(Object.assign({}, this.chapter.settings), this.section.settings));
}
get key() {
var _a;
return (_a = this.section.key) !== null && _a !== void 0 ? _a : String(this.index);
}
}
exports.NarrationSection = NarrationSection;
class NarrationChapter {
constructor(chapter, index, script) {
this.chapter = chapter;
this.index = index;
this.script = script;
this.sections = chapter.sections.map((section, index) => new NarrationSection(section, index + 1, this, script));
}
get settings() {
var _a;
return (Object.assign(Object.assign({}, (_a = this.script.settings) === null || _a === void 0 ? void 0 : _a.voice), this.chapter.settings));
}
get key() {
var _a;
return (_a = this.chapter.key) !== null && _a !== void 0 ? _a : String(this.index);
}
getSectionByKey(key) {
if (!this.sectionsByKeys) {
this.sectionsByKeys = this.sections.reduce((map, section) => map.set(section.key, section), new Map());
}
return this.sectionsByKeys.get(key);
}
}
exports.NarrationChapter = NarrationChapter;
class NarrationScript {
constructor(script, scriptFilePath) {
this.script = script;
this.scriptFilePath = scriptFilePath;
this.chapters = script.chapters.map((chapter, index) => new NarrationChapter(chapter, index + 1, this));
}
get settings() {
return this.script.settings;
}
getChapterByKey(key) {
if (!this.chaptersByKeys) {
this.chaptersByKeys = this.chapters.reduce((map, chapter) => map.set(chapter.key, chapter), new Map());
}
return this.chaptersByKeys.get(key);
}
export() {
return (0, lodash_1.cloneDeep)(this.script);
}
}
exports.NarrationScript = NarrationScript;
async function loadScript(scriptFilePath) {
const yamlContent = await readFileAsPromise(scriptFilePath, 'utf8');
const script = (0, js_yaml_1.load)(yamlContent);
return new NarrationScript(script, scriptFilePath);
}
exports.loadScript = loadScript;
async function saveScript(script, scriptFilePath) {
if (!scriptFilePath) {
const parsedScript = script;
scriptFilePath = parsedScript.scriptFilePath;
script = parsedScript.export();
}
// console.log(inspect(script, false, 8, true));
const yamlContent = (0, js_yaml_1.dump)(script, {
lineWidth: 200,
});
await writeFileAsPromise(scriptFilePath, yamlContent, 'utf8');
}
exports.saveScript = saveScript;