From 110b78c76213d07be8a7b1ac3ba9b13879e1f2c7 Mon Sep 17 00:00:00 2001 From: guilevi Date: Tue, 6 Apr 2021 02:16:09 +0200 Subject: [PATCH] fuck --- .DS_Store | Bin 6148 -> 6148 bytes .env.example | 6 ------ index.js | 35 +++++++++++++++++++++-------------- modules/.DS_Store | Bin 0 -> 6148 bytes modules/welcomer/index.js | 2 +- package-lock.json | 1 - tts/.DS_Store | Bin 0 -> 8196 bytes tts/BaseEngine/index.js | 16 ++++++++++++++++ tts/espeak/index.js | 11 +++++++++++ tts/gtranslate/index.js | 13 +++++++++++++ tts/watson/index.js | 26 ++++++++++++++++++++++++++ 11 files changed, 88 insertions(+), 22 deletions(-) delete mode 100644 .env.example create mode 100644 modules/.DS_Store create mode 100644 tts/.DS_Store create mode 100644 tts/BaseEngine/index.js create mode 100644 tts/espeak/index.js create mode 100644 tts/gtranslate/index.js create mode 100644 tts/watson/index.js diff --git a/.DS_Store b/.DS_Store index c435fb72c20b2f4824116a18c943e9c642078df7..94ae3489b920161ef4a2c7148a73ee0c64654670 100644 GIT binary patch literal 6148 zcmeHK!A{#i5S=Ayai~ypK(xob^p-K@ubm2vyOJwfkmwW_B~r-rXz!i1sjS0n`D&!9rMF!D^0>oyv?X zaSz1_Jw^;Y$b(Uy1@Vlx?XV132L3k&XzeaT0t!MH!kzW20<`=IvN+0%X7gGsRjMyu zE;}`RR-GfSuSQ9;3Q57ig;jL@YQs?BRQm(-nzV!%BaL?{y@(E;S z=U%k*=Oh}Rw!9OSWH%oO;vBC;bvi+;>2N;&`tJT+{k{8;XZT3ZtpAvqr(V^apZSz3 zFBj+4Qx0m!vJ6-T7J>oVA0!q+m$5Xcw+^iI6#%h{-C9tmy9C8ZMwhWPh!zxKLJ>`< z;46kO;n*+P&t)tPns5+&`4Bv_;2Vn2v*Y+uyMu5I+S)Q;8Mx0t#Wd@5{(pM-{(nEn zwk!jdfrVl~R6Aa$jVbZjI+GlowGzuQ783F+4eAiA_&C-I9mQ|4XhEAK1<_?J4Pp<9 P{Ue}hu$5)tSsC~TTXg(o delta 138 zcmZoMXfc=|#>B`mu~2NHo}wr#0|Nsi1A_nqLn=c&LkUAJLqSgZ#KPr_tdj-V#U?Yd z8*wCNX>T_YKpQp-a(ri= V%r9aIG#qRR6O?Az93irX832@69wPt% diff --git a/.env.example b/.env.example deleted file mode 100644 index fbe1127..0000000 --- a/.env.example +++ /dev/null @@ -1,6 +0,0 @@ -TOKEN= -GUILD= -CHANNEL=
-VOICE_TMP_PATH=./voice_tmp/ -DB_FILE=./database/tardis.db -PREFIX=! \ No newline at end of file diff --git a/index.js b/index.js index f7114be..0358e33 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ const Discord = require('discord.js'); require('dotenv').config(); -const tts = require('google-tts-api'); const fetch = require('node-fetch'); const fs = require('fs'); const sha1 = require('sha1'); @@ -16,7 +15,19 @@ const db = new sqlite.Database(process.env.DB_FILE); const api = { 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) => { return joinedVoiceChannels.includes(channel); }, @@ -25,16 +36,11 @@ const api = { return bot.voice.connections.find((conn) => conn.channel === channel); }, - generateVoice: async (string) => { - const hash = sha1(string); - const filepath = process.env.VOICE_TMP_PATH + hash + ".mp3"; + generateVoice: async (string, engine, voice, params) => { + const hash = sha1(voice+string); + const filepath = process.env.VOICE_TMP_PATH + hash + '.' + engine.fileExtension; if (!fs.existsSync(filepath)) { - const url = tts.getAudioUrl(string, {lang: "en-us"}); - console.log("Generated url: " + url); - const data = await fetch(url); - const contents = await data.arrayBuffer(); - const buf = Buffer.from(contents); - fs.writeFileSync(filepath, buf); + await engine.getSpeechFile(string, filepath, voice, params); } 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 filepath = await api.generateVoice(message); + const filepath = await api.generateVoice(message, engine, voice, params); if (conn) conn.play(filepath); }, @@ -65,9 +71,10 @@ const api = { } function registerModules() { - console.log(`Registering modules...`); + console.log(`Registering modules...`); const moduleDirectories = fs.readdirSync('./modules'); moduleDirectories.forEach((dir) => { + if(dir.startsWith('.')) return; modules.push(require(`./modules/${dir}/index.js`)); console.log(`Loading ./modules/${dir}/index.js`) }) diff --git a/modules/.DS_Store b/modules/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..786160700ae6a8975e7cea08783b58d2458b9d7f GIT binary patch literal 6148 zcmeHK!A=4(5S;?*uExm0sK;KsaR4M54<;MLgEx~kdQbzqLS&O=OJH%MD`x*f|Hx19 zcbsX95y6WGV@PMyzG%XC!+>GnzcD~}*9H&oZ3F_o+}~l}4~37`H@_bS!?;|2XSsY~X?ex6@msfU z+^$I6*o#NQx;MC@(U})_eXXAh5q|2Pe-U(&W^waaM6nk{oq==?+8tbSeHlb;k<`T~ zY7gZ;t}@U$cC)yZrU%s$-z$$xJUy(IOT4yU8;@;kd$)3Q+Uh+#rcdMN7sV{YL?mTH z;{;w&nG@)}JBUIN-Jz8{El4nZg_y(y^i(2SL?ULw{6@osG=q!GfW9i$+9C&OVlxaF z27bi=fg5>EeUwlbNnim^>Z#=W;nPN1^Er1BQWV2J&iHr1$^i=lXxz$&3sGhJl4*fE5~U zqmCizw{<2sdTTkh7B&)EmnoDk*y-n3D)bcZV3UG2P70!$DCS2%(qKBnz@IYk E0dT08djJ3c literal 0 HcmV?d00001 diff --git a/modules/welcomer/index.js b/modules/welcomer/index.js index edd4001..684aba2 100644 --- a/modules/welcomer/index.js +++ b/modules/welcomer/index.js @@ -5,6 +5,6 @@ module.exports = function(bot, api) { const channel = await bot.channels.fetch(process.env.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); }) } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index fe09d29..68ff3b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1046,7 +1046,6 @@ "hasInstallScript": true, "dependencies": { "node-addon-api": "^3.0.0", - "node-gyp": "3.x", "node-pre-gyp": "^0.11.0" }, "optionalDependencies": { diff --git a/tts/.DS_Store b/tts/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1256189858c6d9a0cec2e1b05e94e5058aabaf6c GIT binary patch literal 8196 zcmeHMzi-n(6n@u+)G4aSfJ#R%3`iN0Qh^wdj7bMJBqNY0sI^Iy7;K+qJ46tIvM@6O ze@1sU24*HCBo-tj{tVvzNU@!>=oCcrb)l@u5#~(LA7CFM7=Vv$f5BQ0Xa+O`ngPv#W`Q$2@@hm=Z0CGlAR{uPL$zB3+40F^F)-d6StV zS`udhg*y=84n#&KGD9IeI{KO19Ec@Qx0(UXz#;=Ici#jXTu3oz&*kslIE}r4dN}{t zivvH++U>7oZM}A`zF{=*wPieZhIH&?ZZ=A7H#uPM?z-8~ll^;?1}A;jyYKhMhs}$( zX_UEs)K9px-|M5vqX&M}qhp(nqF&0c`Q4+SFMs#^%~m0o zGJZ+6I*hqgDNu-3Q?9}i6*3j(^YXks{`mdZ{O-xol$WK-Mj=NbPDzH`o~ye{W?)^! z+hO_te)aSJC4VX%PBWkxSSACc)^)lzMx6h0E(d41wu$u!D=W-yCXl;e!_#p@o{l4) l{9%ZF6HS?qC2=MYN3i^h2LUW{;ZV}&f27ZU{&hSv&2K}23hn>^ literal 0 HcmV?d00001 diff --git a/tts/BaseEngine/index.js b/tts/BaseEngine/index.js new file mode 100644 index 0000000..4a9aa73 --- /dev/null +++ b/tts/BaseEngine/index.js @@ -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; + } +} \ No newline at end of file diff --git a/tts/espeak/index.js b/tts/espeak/index.js new file mode 100644 index 0000000..ca4acb2 --- /dev/null +++ b/tts/espeak/index.js @@ -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]); + } +} \ No newline at end of file diff --git a/tts/gtranslate/index.js b/tts/gtranslate/index.js new file mode 100644 index 0000000..5847aba --- /dev/null +++ b/tts/gtranslate/index.js @@ -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); + } +}; \ No newline at end of file diff --git a/tts/watson/index.js b/tts/watson/index.js new file mode 100644 index 0000000..3900e5a --- /dev/null +++ b/tts/watson/index.js @@ -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); + } +}; \ No newline at end of file