Skip to content
Permalink
df5288289f
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
57 lines (53 sloc) 1.98 KB
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg')
const wav = require('wav');
const deepSpeech = require('deepspeech');
const { sendMsg } = require("./sendMsg");
const config = require('./config.json');
const { getAmplitude } = require('./getAmplitude');
const modelPath = './dependencies/deepspeech-models.pbmm';
const scorerPath = './dependencies/deepspeech-models.scorer';
const model = new deepSpeech.Model(modelPath);
exports.model = model;
model.enableExternalScorer(scorerPath);
let desiredSampleRate = model.sampleRate();
const bufferSize = 512;
// Gathers any speech from a WAV audio file and sends it to the logs channel
function speechToText(nickname, mp3path) {
// Convert the mp3 audio file into wav
const path = mp3path.replace('.mp3', '.wav');
ffmpeg(mp3path).format('wav').save(path)
.on('error', (err) => {
console.log(err);
})
.on ('end', () => {
fs.unlinkSync(mp3path);
transcribe(path, nickname);
});
}
exports.speechToText = speechToText;
// Stream the audio file into the speech recognition algorithm and then send output
function transcribe(path, nickname) {
let modelStream = model.createStream();
const fileStream = fs.createReadStream(path, { highWaterMark: bufferSize });
const reader = new wav.Reader();
let transcription = null;
reader.on('error', (err) => {
console.log(err);
});
reader.on('format', (format) => {
if (format.sampleRate !== model.sampleRate()) {
console.log(`invalid sample rate: ${format.sampleRate} needs to be: ${model.sampleRate()}`);
}
reader.on('data', (data) => {
modelStream.feedAudioContent(data);
});
reader.on('end', () => {
sendMsg(nickname, `${config.prefix}voice 0`);
transcription = modelStream.finishStream();
fs.unlinkSync(path);
sendMsg(nickname, transcription);
});
});
fileStream.pipe(reader);
}