tts-narrator
Version:
Generate narration with Text-To-Speech technology
59 lines (58 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TtsNarrator = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("node:fs"));
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const script_processor_1 = require("./script-processor");
function dummyFunc() { }
const silentLogger = {
debug: dummyFunc,
info: dummyFunc,
warn: dummyFunc,
error: (err) => { throw err; },
isDebug: false,
isQuiet: false,
};
/**
* Class for generating narration.
* Instance of this class can be used to generate narration audio for scripts by calling the `narrate(...)` method.
*
* @example
* const ttsService = new AzureTtsService(...);
* const ttsNarrator = new TtsNarrator(ttsService, './output-folder');
* const script = await loadScript('./my-script.yml');
* await ttsNarrator.narrate(script);
* console.log(`One of the generated audio file is: ${script.chapters[0].sections[0].paragraphs[0].audioFilePath}`);
*/
class TtsNarrator extends script_processor_1.ScriptProcessor {
/**
* Constructor
* @param ttsService The TTS service to be used for generating audio
* @param audioFileFolder The folder that generated audio files will be placed
* @param options Optional settings
* @param cliConsole Optional logger
*/
constructor(ttsService, audioFileFolder, options, cliConsole = silentLogger) {
super('dummy-value', Object.assign({ debug: false, quiet: false, play: false, interactive: false, overwrite: false, ssml: false }, options), cliConsole);
this.audioFileFolder = audioFileFolder;
this.ttsService = ttsService;
}
async determineAudioFilePath(ssmlHash, _paragraph) {
if (!fs.existsSync(this.audioFileFolder)) {
fs.mkdirSync(this.audioFileFolder, { recursive: true });
}
const audioFilePath = node_path_1.default.join(this.audioFileFolder, `${ssmlHash}.mp3`);
return audioFilePath;
}
/**
* Generate narration for the script
* @param script the input script which will also be modified for recording audioFilePath
* @returns nothing
*/
async narrate(script) {
this._script = script;
await this.runWithoutCatch();
}
}
exports.TtsNarrator = TtsNarrator;