2021-04-10 21:45:02 +00:00
|
|
|
const BaseEngine = require('../BaseEngine')
|
|
|
|
const sdk = require('@google-cloud/text-to-speech');
|
|
|
|
const fs = require('fs');
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
module.exports = class GoogleCloudTTS extends BaseEngine {
|
|
|
|
constructor() {
|
|
|
|
super('google', 'Google Cloud TTS', 'wav');
|
|
|
|
this.client = new sdk.TextToSpeechClient();
|
|
|
|
this.client.initialize();
|
|
|
|
this.voices = {};
|
|
|
|
this.populateVoiceList();
|
|
|
|
}
|
|
|
|
async populateVoiceList() {
|
|
|
|
const [result] = await this.client.listVoices({});
|
|
|
|
const voiceList = result.voices;
|
|
|
|
voiceList.forEach((voice) => {
|
2021-04-10 22:26:03 +00:00
|
|
|
|
2021-04-10 21:45:02 +00:00
|
|
|
this.voices[voice.name.toLowerCase()] = { name: voice.name, lang: voice.languageCodes[0] };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
getDefaultVoice() {
|
|
|
|
return 'en-US-Wavenet-A';
|
|
|
|
}
|
|
|
|
async getSpeechFile(text, filepath, voice = this.getDefaultVoice(), params = {}) {
|
|
|
|
const request = {
|
|
|
|
input: { text: text },
|
|
|
|
voice: { name: this.voices[voice].name, languageCode: this.voices[voice].lang },
|
|
|
|
audioConfig: { audioEncoding: 'OGG_OPUS', effectsProfileId: ['medium-bluetooth-speaker-class-device'] },
|
|
|
|
};
|
|
|
|
let [response] = await this.client.synthesizeSpeech(request);
|
|
|
|
const writeFile = util.promisify(fs.writeFile);
|
|
|
|
await writeFile(filepath, response.audioContent, 'binary');
|
|
|
|
return filepath;
|
|
|
|
}
|
|
|
|
}
|