From 4a37af079541bce00c524e21c29dc5af4f24fed3 Mon Sep 17 00:00:00 2001 From: Talon Date: Fri, 9 Apr 2021 15:56:53 +0200 Subject: [PATCH] Add azure tts --- tts/azure/index.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tts/azure/index.js diff --git a/tts/azure/index.js b/tts/azure/index.js new file mode 100644 index 0000000..c9191b9 --- /dev/null +++ b/tts/azure/index.js @@ -0,0 +1,58 @@ +const BaseEngine = require('../BaseEngine'); +const sdk = require("microsoft-cognitiveservices-speech-sdk"); +const fetch = require('node-fetch'); + +module.exports = class AzureTTS extends BaseEngine { + constructor() { + super("azure", "Microsoft Azure TTS", "wav"); + this.voices = {}; + this.populateVoiceList(); + } + + getDefaultVoice() { + return ""; + } + + getSpeechFile(text, filepath, voice = this.getDefaultVoice(), params = {}) { + return new Promise((resolve, reject) => { + const speechConfig = sdk.SpeechConfig.fromSubscription(process.env.AZURE_API_KEY, process.env.AZURE_REGION); + speechConfig.speechSynthesisOutputFormat = sdk.SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm; + speechConfig.speechSynthesisVoiceName = this.voices[voice]; + const audioConfig = sdk.AudioConfig.fromAudioFileOutput(filepath); + const synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig); + synthesizer.speakTextAsync( + text, + result => { + synthesizer.close(); + if (result) { + // return result as stream + resolve(filepath); + } + }, + error => { + console.log(error); + synthesizer.close(); + reject(error); + }); + }) + } + + async populateVoiceList() { + const opts = { + headers: { + 'Ocp-Apim-Subscription-Key': process.env.AZURE_API_KEY + } + } + const res = await fetch(process.env.AZURE_LIST_ENDPOINT, opts); + const json = await res.json(); + json.forEach((voice) => { + if (this.voices[voice.DisplayName]) { + if (voice.Name.includes('Neural')) { + this.voices[voice.DisplayName] = voice.ShortName; + } + } else { + this.voices[voice.DisplayName] = voice.ShortName; + } + }); + } +} \ No newline at end of file