Changes made to Watson to populate voices, then used VSCode to reformat, so that should resolve the conflict

pull/1/head
guilevi 2021-04-07 11:14:31 +02:00
commit 70686c2ce0
8 changed files with 82 additions and 67 deletions

View File

@ -16,13 +16,13 @@ const db = new sqlite.Database(process.env.DB_FILE);
const api = { const api = {
db: db, db: db,
ttsEngines: (() => { ttsEngines: (() => {
let engines={}; let engines = {};
console.log(`Registering TTS engines...`); console.log(`Registering TTS engines...`);
const engineDirectories = fs.readdirSync('./tts'); const engineDirectories = fs.readdirSync('./tts');
engineDirectories.forEach((dir) => { engineDirectories.forEach((dir) => {
if(dir.startsWith('.')) return; if (dir.startsWith('.')) return;
eng=require(`./tts/${dir}/index.js`); eng = require(`./tts/${dir}/index.js`);
engines[dir]=new eng; engines[dir] = new eng;
console.log(`Loading ./tts/${dir}/index.js`) console.log(`Loading ./tts/${dir}/index.js`)
}) })
return engines; return engines;
@ -31,8 +31,8 @@ const api = {
announcementEngine: undefined, announcementEngine: undefined,
respond: (message, text, voiceText) => { respond: (message, text, voiceText) => {
let toSend = message.member.displayName+", "+(voiceText ? voiceText : text); let toSend = message.member.displayName + ", " + (voiceText ? voiceText : text);
if(message.member.voice.channel) { if (message.member.voice.channel) {
api.speak(message.member.voice.channel, toSend); api.speak(message.member.voice.channel, toSend);
} else { } else {
message.reply(text); message.reply(text);
@ -48,7 +48,7 @@ const api = {
}, },
generateVoice: async (string, engine, voice, params) => { generateVoice: async (string, engine, voice, params) => {
const hash = sha1(voice+string); const hash = sha1(voice + string);
const filepath = process.env.VOICE_TMP_PATH + hash + '.' + engine.fileExtension; const filepath = process.env.VOICE_TMP_PATH + hash + '.' + engine.fileExtension;
if (!fs.existsSync(filepath)) { if (!fs.existsSync(filepath)) {
await engine.getSpeechFile(string, filepath, voice, params); await engine.getSpeechFile(string, filepath, voice, params);
@ -70,7 +70,7 @@ const api = {
} }
}, },
speak: async (channel, message, engine=api.announcementEngine, voice=api.announcementVoice, params={}) => { speak: async (channel, message, engine = api.announcementEngine, voice = api.announcementVoice, params = {}) => {
const conn = api.getConnectionForVoiceChannel(channel); const conn = api.getConnectionForVoiceChannel(channel);
const filepath = await api.generateVoice(message, engine, voice, params); const filepath = await api.generateVoice(message, engine, voice, params);
if (conn) conn.play(filepath); if (conn) conn.play(filepath);
@ -85,7 +85,7 @@ function registerModules() {
console.log(`Registering modules...`); console.log(`Registering modules...`);
const moduleDirectories = fs.readdirSync('./modules'); const moduleDirectories = fs.readdirSync('./modules');
moduleDirectories.forEach((dir) => { moduleDirectories.forEach((dir) => {
if(dir.startsWith('.')) return; if (dir.startsWith('.')) return;
modules.push(require(`./modules/${dir}/index.js`)); modules.push(require(`./modules/${dir}/index.js`));
console.log(`Loading ./modules/${dir}/index.js`) console.log(`Loading ./modules/${dir}/index.js`)
}) })
@ -98,7 +98,7 @@ function handleMessage(message) {
const command = args[0].substr(1, args[0].length); const command = args[0].substr(1, args[0].length);
const execution = commandHandlers.get(command); const execution = commandHandlers.get(command);
if (command) { if (command) {
if(execution) execution(args, message); if (execution) execution(args, message);
} }
} }
} }

View File

@ -1,4 +1,4 @@
module.exports = function(bot, api) { module.exports = function (bot, api) {
bot.on('voiceStateUpdate', async (oldState, newState) => { bot.on('voiceStateUpdate', async (oldState, newState) => {
if (newState.member.user.bot) return; if (newState.member.user.bot) return;
if (oldState.channel && newState.channel) return; if (oldState.channel && newState.channel) return;

View File

@ -1,17 +1,17 @@
module.exports = function(bot, api) { module.exports = function (bot, api) {
api.registerCommand('announcevoice', (args, message) => { api.registerCommand('announcevoice', (args, message) => {
let channel = bot.voice.connections.first().channel; let channel = bot.voice.connections.first().channel;
if(args.length>3) { if (args.length > 3) {
return api.respond(message, "You tried to change my voice with too many arguments."); return api.respond(message, "You tried to change my voice with too many arguments.");
} }
if(api.ttsEngines[args[1]]) { if (api.ttsEngines[args[1]]) {
api.announcementEngine=api.ttsEngines[args[1]]; api.announcementEngine = api.ttsEngines[args[1]];
if(api.announcementEngine.validateVoice(args[2])) { if (api.announcementEngine.validateVoice(args[2])) {
api.announcementVoice=args[2]; api.announcementVoice = args[2];
api.respond(message, "My new voice is "+api.announcementVoice+" from "+api.announcementEngine.longName); api.respond(message, "My new voice is " + api.announcementVoice + " from " + api.announcementEngine.longName);
} else { } else {
api.announcementVoice=api.announcementEngine.getDefaultVoice(); api.announcementVoice = api.announcementEngine.getDefaultVoice();
api.respond(message, "Your voice name was invalid, so I switched to the default voice ("+api.announcementVoice+") for "+api.announcementEngine.longName+" instead."); api.respond(message, "Your voice name was invalid, so I switched to the default voice (" + api.announcementVoice + ") for " + api.announcementEngine.longName + " instead.");
} }
} }
}); });

View File

@ -1,4 +1,4 @@
module.exports = function(bot, api) { module.exports = function (bot, api) {
bot.on('ready', async () => { bot.on('ready', async () => {
console.log("Bot initialized and listening"); console.log("Bot initialized and listening");
const guild = await bot.guilds.fetch(process.env.GUILD); const guild = await bot.guilds.fetch(process.env.GUILD);

View File

@ -1,16 +1,16 @@
const fs=require('fs'); const fs = require('fs');
module.exports=class { module.exports = class {
constructor(longName, fileExtension, supportedParameters=[]) { constructor(longName, fileExtension, supportedParameters = []) {
this.longName=longName; this.longName = longName;
this.fileExtension=fileExtension; this.fileExtension = fileExtension;
} }
getInternalVoiceName(str) { getInternalVoiceName(str) {
return this.voices?this.voices[str]:str; return this.voices ? this.voices[str] : str;
} }
getDefaultVoice() {} getDefaultVoice() { }
validateVoice(voice) {return this.voices ? this.voices[voice] : true;} validateVoice(voice) { return this.voices ? this.voices[voice] : true; }
async getSpeech(text, voice=this.getDefaultVoice(), params) {} async getSpeech(text, voice = this.getDefaultVoice(), params) { }
async getSpeechFile(text, filepath, voice, params) { async getSpeechFile(text, filepath, voice, params) {
const data = await this.getSpeech(text, voice, params); const data = await this.getSpeech(text, voice, params);
const contents = await data.arrayBuffer(); const contents = await data.arrayBuffer();

View File

@ -1,15 +1,15 @@
const BaseEngine=require('../BaseEngine') const BaseEngine = require('../BaseEngine')
const {spawn} = require('child_process') const { spawn } = require('child_process')
module.exports=class extends BaseEngine { module.exports = class extends BaseEngine {
constructor() { constructor() {
super('ESpeak','wav') super('ESpeak', 'wav')
} }
getDefaultVoice() { getDefaultVoice() {
return 'en'; return 'en';
} }
async getSpeechFile(text, filepath, voice=this.getDefaultVoice(), params={}) { async getSpeechFile(text, filepath, voice = this.getDefaultVoice(), params = {}) {
let proc=await spawn('espeak', ['-v', voice, '-w',filepath, '--stdin']); let proc = await spawn('espeak', ['-v', voice, '-w', filepath, '--stdin']);
proc.stdin.end(text); proc.stdin.end(text);
} }
} }

View File

@ -1,16 +1,16 @@
const BaseEngine=require('../BaseEngine'); const BaseEngine = require('../BaseEngine');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const tts = require('google-tts-api'); const tts = require('google-tts-api');
module.exports= class extends BaseEngine { module.exports = class extends BaseEngine {
constructor() { constructor() {
super("Google Translate TTS","mp3"); super("Google Translate TTS", "mp3");
} }
getDefaultVoice() { getDefaultVoice() {
return 'en-us'; return 'en-us';
} }
async getSpeech(text, voice=this.getDefaultVoice(), params={}) { async getSpeech(text, voice = this.getDefaultVoice(), params = {}) {
const url = tts.getAudioUrl(text, {lang: voice}); const url = tts.getAudioUrl(text, { lang: voice });
return fetch(url); return fetch(url);
} }
}; };

View File

@ -5,6 +5,7 @@ const querystring = require('querystring');
module.exports = class extends BaseEngine { module.exports = class extends BaseEngine {
constructor() { constructor() {
super("IBM Watson TTS", "ogg"); super("IBM Watson TTS", "ogg");
<<<<<<< HEAD
this.voices = {}; this.voices = {};
this.populateVoiceList(); this.populateVoiceList();
} }
@ -16,6 +17,12 @@ module.exports = class extends BaseEngine {
headers: { headers: {
'Authorization': authorization 'Authorization': authorization
}, },
=======
this.voices = {
'Michael': 'en-US_MichaelV3Voice',
'Allison': 'en-US_AllisonV3Voice',
'Kevin': 'en-US_KevinV3Voice',
>>>>>>> 12a9f8fc5374d912e595cea204998daa4d7220ba
}; };
const res = await fetch(url, opts); const res = await fetch(url, opts);
const voices = await res.json(); const voices = await res.json();
@ -27,6 +34,7 @@ module.exports = class extends BaseEngine {
getDefaultVoice() { getDefaultVoice() {
return 'Michael'; return 'Michael';
} }
<<<<<<< HEAD
IBMAuthString() { IBMAuthString() {
let buff = new Buffer('apikey:' + process.env.watsonAPIKey); let buff = new Buffer('apikey:' + process.env.watsonAPIKey);
let b64auth = buff.toString('base64'); let b64auth = buff.toString('base64');
@ -35,6 +43,13 @@ module.exports = class extends BaseEngine {
async getSpeech(text, voice = this.getSpeechVoice(), params = {}) { async getSpeech(text, voice = this.getSpeechVoice(), params = {}) {
const url = process.env.watsonURL + "/v1/synthesize?voice=" + this.getInternalVoiceName(voice); const url = process.env.watsonURL + "/v1/synthesize?voice=" + this.getInternalVoiceName(voice);
const authorization = this.IBMAuthString(); const authorization = this.IBMAuthString();
=======
async getSpeech(text, voice = this.getSpeechVoice(), params = {}) {
const url = process.env.watsonURL + "/v1/synthesize?voice=" + this.getInternalVoiceName(voice);
let buff = new Buffer('apikey:' + process.env.watsonAPIKey);
let b64auth = buff.toString('base64');
const authorization = 'Basic ' + b64auth;
>>>>>>> 12a9f8fc5374d912e595cea204998daa4d7220ba
const opts = { const opts = {
method: "post", method: "post",
headers: { headers: {