watson
guilevi 2021-04-06 02:16:09 +02:00
parent b444508fb8
commit 110b78c762
11 changed files with 88 additions and 22 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -1,6 +0,0 @@
TOKEN=<BOT TOKEN>
GUILD=<Guild ID>
CHANNEL=<Main voice channel>
VOICE_TMP_PATH=./voice_tmp/
DB_FILE=./database/tardis.db
PREFIX=!

View File

@ -1,6 +1,5 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
require('dotenv').config(); require('dotenv').config();
const tts = require('google-tts-api');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const fs = require('fs'); const fs = require('fs');
const sha1 = require('sha1'); const sha1 = require('sha1');
@ -16,6 +15,18 @@ const db = new sqlite.Database(process.env.DB_FILE);
const api = { const api = {
db: db, db: db,
ttsEngines: (() => {
let engines={};
console.log(`Registering TTS engines...`);
const engineDirectories = fs.readdirSync('./tts');
engineDirectories.forEach((dir) => {
if(dir.startsWith('.')) return;
eng=require(`./tts/${dir}/index.js`);
engines[dir]=new eng;
console.log(`Loading ./tts/${dir}/index.js`)
})
return engines;
})(),
isInVoiceChannel: (channel) => { isInVoiceChannel: (channel) => {
return joinedVoiceChannels.includes(channel); return joinedVoiceChannels.includes(channel);
@ -25,16 +36,11 @@ const api = {
return bot.voice.connections.find((conn) => conn.channel === channel); return bot.voice.connections.find((conn) => conn.channel === channel);
}, },
generateVoice: async (string) => { generateVoice: async (string, engine, voice, params) => {
const hash = sha1(string); const hash = sha1(voice+string);
const filepath = process.env.VOICE_TMP_PATH + hash + ".mp3"; const filepath = process.env.VOICE_TMP_PATH + hash + '.' + engine.fileExtension;
if (!fs.existsSync(filepath)) { if (!fs.existsSync(filepath)) {
const url = tts.getAudioUrl(string, {lang: "en-us"}); await engine.getSpeechFile(string, filepath, voice, params);
console.log("Generated url: " + url);
const data = await fetch(url);
const contents = await data.arrayBuffer();
const buf = Buffer.from(contents);
fs.writeFileSync(filepath, buf);
} }
return filepath; return filepath;
}, },
@ -53,9 +59,9 @@ const api = {
} }
}, },
speak: async (channel, message) => { speak: async (channel, message, engine=api.ttsEngines.gtranslate, voice='en-us', params={}) => {
const conn = api.getConnectionForVoiceChannel(channel); const conn = api.getConnectionForVoiceChannel(channel);
const filepath = await api.generateVoice(message); const filepath = await api.generateVoice(message, engine, voice, params);
if (conn) conn.play(filepath); if (conn) conn.play(filepath);
}, },
@ -65,9 +71,10 @@ const api = {
} }
function registerModules() { 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;
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`)
}) })

BIN
modules/.DS_Store vendored 100644

Binary file not shown.

View File

@ -5,6 +5,6 @@ module.exports = function(bot, api) {
const channel = await bot.channels.fetch(process.env.CHANNEL); const channel = await bot.channels.fetch(process.env.CHANNEL);
await api.joinChannel(channel); await api.joinChannel(channel);
api.speak(channel, `Hi! I'm alive. It is now ${new Date().toLocaleTimeString()} on ${new Date().toLocaleDateString()}`); api.speak(channel, `Hi! I'm alive. It is now ${new Date().toLocaleTimeString()} on ${new Date().toLocaleDateString()}`,api.ttsEngines.watson);
}) })
} }

1
package-lock.json generated
View File

@ -1046,7 +1046,6 @@
"hasInstallScript": true, "hasInstallScript": true,
"dependencies": { "dependencies": {
"node-addon-api": "^3.0.0", "node-addon-api": "^3.0.0",
"node-gyp": "3.x",
"node-pre-gyp": "^0.11.0" "node-pre-gyp": "^0.11.0"
}, },
"optionalDependencies": { "optionalDependencies": {

BIN
tts/.DS_Store vendored 100644

Binary file not shown.

View File

@ -0,0 +1,16 @@
const fs=require('fs');
module.exports=class {
constructor(longName, fileExtension, supportedParameters=[]) {
this.longName=longName;
this.fileExtension=fileExtension;
}
async getSpeech(text, voice, params) {}
async getSpeechFile(text, filepath, voice, params) {
const data = await this.getSpeech(text, voice, params);
const contents = await data.arrayBuffer();
const buf = Buffer.from(contents);
fs.writeFileSync(filepath, buf);
return filepath;
}
}

View File

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

View File

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

View File

@ -0,0 +1,26 @@
const BaseEngine=require('../BaseEngine');
const fetch = require('node-fetch');
const querystring = require('querystring');
module.exports= class extends BaseEngine {
constructor() {
super("IBM Watson TTS","ogg");
}
async getSpeech(text, voice='en-us', params={}) {
const url = process.env.watsonURL+"/v1/synthesize";
let buff=new Buffer('apikey:'+process.env.watsonAPIKey);
let b64auth=buff.toString('base64');
const authorization='Basic '+b64auth;
const opts={
method: "post",
headers: {
'Content-Type': 'application/json',
'Authorization': authorization
},
body: {
text: text
}
};
return fetch(url);
}
};