working speech detection and translation (only french rn lol)

This commit is contained in:
Paoda
2018-08-31 14:33:32 +01:00
parent 0d47a9c655
commit 8e2246cde2
6 changed files with 363 additions and 35 deletions

98
google-cloud/Speech.js Normal file
View File

@@ -0,0 +1,98 @@
const speech = require("@google-cloud/speech");
const Translate = require('./Translate');
const translate = new Translate('node-polyglot');
/**
* Class Responsible for dealing with Google's Speech Recognition API
*/
class Speech {
constructor() {
/** @type {speech.SpeechClient} */
this.client = new speech.SpeechClient();
/** @type {Stream} */
this.recognize = null;
/** @type {boolean} */
this.enabled = false;
}
/**
* Getter for the Google API Stream we can write to.
* @returns {Stream} - this.recognize
*/
getStream() {
return this.recognize;
}
/**
* Stops the GoogleAPI Stream.
*/
stopRecognition() {
if (this.recognize) this.recognize.end();
this.recognize = null;
this.enabled = false;
}
/**
* Starts the Google API Stream
* @param {string} lang - Language Code e.g en-CA
*/
startRecognition(lang) {
this.lang = lang;
this.enabled = true;
const request = {
config: {
encoding: "LINEAR16",
sampleRateHertz: 16000,
languageCode: lang,
profanityFilter: false,
enableWordTimeOffsets: true
},
interimResults: true // If you want interim results, set this to true
};
this.recognize = this.client
.streamingRecognize(request)
.on("error", console.error)
.on("data", data => {
process.stdout.write(
data.results[0] && data.results[0].alternatives[0]
? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
: `\n\nReached transcription time limit, press Ctrl+C\n`
);
//client.emit("speechData", data);
if (data.results[0].alternatives[0] !== undefined) {
let text = data.results[0].alternatives[0].transcript;
translate.speech(text, "fr").then(translation => {
console.log("Translation: " + translation);
}).catch(err => console.error(err));
// translate
// .translate(text, target)
// .then(results => {
// const translation = results[0];
// //client.emit("translateData", translation);
// console.log(`Text: ${text}`);
// console.log(`Translation: ${translation}`);
// })
// .catch(err => {
// console.error("ERROR:", err);
// });
}
// if end of utterance, let's restart stream
// this is a small hack. After 65 seconds of silence, the stream will still throw an error for speech length limit
if (data.results[0] && data.results[0].isFinal) {
this.stopRecognition();
this.startRecognition(this.lang);
// console.log('restarted stream serverside');
}
});
}
}
module.exports = Speech;

29
google-cloud/Translate.js Normal file
View File

@@ -0,0 +1,29 @@
const Trans = require('@google-cloud/translate');
class Translate {
/** @param {string} id - ID of Project for Google Translate */
constructor(id) {
this.id = id;
this.client = new Trans({
projectId: id
})
}
/**
* As in Translate.speech()
* @param {string} text - Text to Translate
* @param {string} lang - Target Language
* @returns {Promise<Array<String>|Error>}
* @async
*/
async speech(text, lang) {
return new Promise((res, rej) => {
this.client.translate(text, lang).then(results => {
res(results[0]); // Translation
}).catch(err => rej(err));
});
}
}
module.exports = Translate;