UNPKG

tts-narrator

Version:

Generate narration with Text-To-Speech technology

53 lines (52 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureTtsService = void 0; const microsoft_cognitiveservices_speech_sdk_1 = require("microsoft-cognitiveservices-speech-sdk"); const tts_service_1 = require("./tts-service"); class AzureTtsService extends tts_service_1.BaseTtsService { constructor(options) { super(); this.options = options; } buildSpeakStartTag(voiceSettings) { var _a; return `<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="${(_a = voiceSettings.language) !== null && _a !== void 0 ? _a : 'en-US'}">`; } generateAudio(ssml, options) { var _a; const allOptions = Object.assign(Object.assign({}, this.options), options); let speechConfig; if (allOptions.subscriptionKey && allOptions.serviceRegion) { speechConfig = microsoft_cognitiveservices_speech_sdk_1.SpeechConfig.fromSubscription(allOptions.subscriptionKey, allOptions.serviceRegion); } else { throw new Error('Can\'t find Azure service region and/or subscription key'); } speechConfig.speechSynthesisOutputFormat = (_a = allOptions.outputFormat) !== null && _a !== void 0 ? _a : microsoft_cognitiveservices_speech_sdk_1.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3; const audioConfig = microsoft_cognitiveservices_speech_sdk_1.AudioConfig.fromAudioFileOutput(allOptions.outputFilePath); const synthesizer = new microsoft_cognitiveservices_speech_sdk_1.SpeechSynthesizer(speechConfig, audioConfig); // The event synthesis completed signals that the synthesis is completed. /* synthesizer.synthesisCompleted = function (_s, e) { console.log('(synthesized) Reason: ' + ResultReason[e.result.reason] + ' Audio length: ' + e.result.audioData.byteLength); }; */ return new Promise((resolve, reject) => { synthesizer.speakSsmlAsync(ssml, result => { // console.log('result', result); synthesizer.close(); if (result.reason === microsoft_cognitiveservices_speech_sdk_1.ResultReason.SynthesizingAudioCompleted) { resolve(result); } else { reject(result); } }, error => { console.log('error', error); synthesizer.close(); reject(error); }); }); } } exports.AzureTtsService = AzureTtsService;