From e90a8db4ee9c5c9f74cca16862de59844cfdac6d Mon Sep 17 00:00:00 2001 From: Max Litruv Boonzaayer Date: Wed, 24 Apr 2019 22:10:56 +1000 Subject: [PATCH] Added in TTS --- 1SecSilence.mp3 | Bin 0 -> 8586 bytes commands/tts.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 1SecSilence.mp3 create mode 100644 commands/tts.js diff --git a/1SecSilence.mp3 b/1SecSilence.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..e4f601ebb73854ea17cdb71329c7b3b1f9459342 GIT binary patch literal 8586 zcmeH`KWM^G5Qnd5Cx_yeNh-}y5XATgy9h1lU{DCPb<0o!f=jby%24d$=H%p5>ENV` zTNVW&lUpXIOm=c|5{EQV0#ZB(eHlu*HzfD+%S+yU`BE+CqTr32r$(dVp05BRwHK{b zxJzdXX(5w2q{oA^zn`5rMi{74sHRGGQ?u>ILC2`1kdxSlpqMjVyRRrmqk&O zq-L`z%Zj43+p4N+ny%}ePPg0b_4@t(U@#mGN2Bq0Je?YbF`t{Jxm;S7wOZM>y|W@##*l0rGDE(b_}3lJnYo z4nBUfCiv3>-|6fh7xMd$egdyPz3~q<^g#`sei^@}FXWR5_w4;cq}*Aiq}*97%oj;H z6d@^xqG1Y1ITRl$hoWH$NI4WADTktA3P?E=A1Q~TVG2k&6dx&vqG1Y1ITRl$hoWH$ zNI4WADTktA3P?E=A1Q~TVG2k&6dx&vqG1Y1ITRl$hoWH$!c&fE8{sYfcMW|5wHo3u literal 0 HcmV?d00001 diff --git a/commands/tts.js b/commands/tts.js new file mode 100644 index 0000000..ae4f8c9 --- /dev/null +++ b/commands/tts.js @@ -0,0 +1,82 @@ +const fs = require('fs'); +const textToSpeech = require('@google-cloud/text-to-speech'); +var FfmpegCommand = require('fluent-ffmpeg'); + +var pid = JSON.parse(fs.readFileSync("./googleapi.json")).project_id + +const clientz = new textToSpeech.TextToSpeechClient({ + projectId: pid, + keyFilename: './googleapi.json' +}); + +module.exports = { + name: "Text To Speech", + alias: ['tts'], + helptext: "Uses google api's to run text to speech stuffs", + helphide: false, + permissions: ['READ_MESSAGES'], + args: ['message'], + category: 'general', + command: (client, msg) => { + if (msg.suffix == "" || msg.suffix == undefined) + return; + + const text = msg.suffix; + + // Construct the request + const request = { + input: { text: text }, + // Select the language and SSML Voice Gender (optional) + voice: { name: 'en-US-Wavenet-D', languageCode: 'en-US' }, + // Select the type of audio encoding + audioConfig: { audioEncoding: 'MP3', speakingRate: '0.85' }, + }; + + // Performs the Text-to-Speech request + clientz.synthesizeSpeech(request, (err, response) => { + if (err) { + console.error('ERROR:', err); + return; + } + + + + // Write the binary audio content to a local file + fs.writeFile('output.mp3', response.audioContent, 'binary', err => { + if (err) { + console.error('ERROR:', err); + return; + } + + var command = FfmpegCommand('./output.mp3').addInput('./1SecSilence.mp3') + .on('error', function (err) { + console.log('An error occurred: ' + err.message); + }) + .on('end', function () { + msg.member.voiceChannel.join() + .then(connection => { + const dispatcher = connection.playFile('./merged.mp3'); + + dispatcher.on('end', () => { + const timeoutObj = setTimeout(() => { + const dispatcher = null; + connection.disconnect(); + }, 60000) + }); + + dispatcher.on('error', error => { + console.log(error) + }); + }) + .catch(console.error); + }) + .mergeToFile('./merged.mp3', './tempDir'); + + + + + console.log('Audio content written to file: output.mp3'); + }); + }); + } +}