From 45b102d404d01dc7f06270838d54a16d7b77700b Mon Sep 17 00:00:00 2001 From: Cedric Godin Date: Fri, 2 Feb 2018 10:47:39 -0500 Subject: [PATCH] First iteration of live speech recognition --- recordings.js | 20 +++++++- resources/js/recordings_model.js | 88 +++++++++++++++++++++++++++++--- stream-to-text.js | 55 ++++++++++++++++++++ views/recordings.html | 2 +- 4 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 stream-to-text.js diff --git a/recordings.js b/recordings.js index 7b6fb3d..fad7579 100644 --- a/recordings.js +++ b/recordings.js @@ -4,6 +4,7 @@ const path = require('path') const url = require('url') const net = require('net') const wav = require('wav') +const streamToText = require('./stream-to-text.js') /* Some hardcoded parameters for now Should be dynamic... @@ -79,6 +80,16 @@ class AudioRecorder { this.index = index this.active = false this.writer = undefined + this.path = undefined + + this.transcripter = new streamToText.Transcripter() + this.transcripter.on('data', data => { + console.log(data) + odasStudio.recordingsWindow.webContents.send('fuzzy-transcript', this.path, data) + }) + this.transcripter.on('error', err => { + console.error(err) + }) ipcMain.on('new-recording', (event, index, id) => { if(index == this.index) { @@ -101,17 +112,22 @@ class AudioRecorder { if(typeof(this.writer)!=='undefined') { this.writer.write(data) + this.transcripter.putData(data) } } startRecording(id) { - console.log('recorder started') if(typeof(this.writer) == 'undefined' && record) { + console.log('recorder started') + let filename = path.join(workspacePath, `ODAS_${id}_${new Date().toLocaleString()}.wav`) + this.path = filename this.writer = new wav.FileWriter(filename,{channels:1, sampleRate:sampleRate, bitDepth:bitNumber}) odasStudio.recordingsWindow.webContents.send('fuzzy-recording', filename) + + this.transcripter.start() } } @@ -121,6 +137,8 @@ class AudioRecorder { if(typeof(this.writer) !== 'undefined') { this.writer.end() + this.transcripter.stop() + this.writer.on('header',(header) => { if(typeof(odasStudio.recordingsWindow)!='undefined') { odasStudio.recordingsWindow.webContents.send('add-recording', this.writer.path) diff --git a/resources/js/recordings_model.js b/resources/js/recordings_model.js index 2b5f230..bec840a 100644 --- a/resources/js/recordings_model.js +++ b/resources/js/recordings_model.js @@ -13,13 +13,15 @@ class FuzzyRecording { this.filename = path.basename(fullPath) this.path = fullPath this.timestamp = new Date() + this.transcription = '' + this.potentialTranscription = '' } } // Single recording class class Recording { - constructor(fullPath) { + constructor(fullPath, transcription) { // Init audio recording this.isPlaying = false @@ -30,9 +32,21 @@ class Recording { this.audio = undefined this.deleting = 'hidden' this.transcription = 'Not available' + this.potentialTranscription = '' this.readInfo(true) - this.createTranscript(); + + if(typeof(transcription) !== 'undefined') { + this.transcription = transcription + const txtPath = this.path.slice(0, -4)+'.txt'; + fs.writeFile(txtPath, transcription, (err) => { + if(err) console.log(err); + }) + } + + else { + this.createTranscript(); + } } @@ -118,6 +132,13 @@ class Recording { } } + saveTranscription() { + const txtPath = this.path.slice(0, -4)+'.txt' + fs.writeFile(txtPath, this.transcription + this.potentialTranscription, (err) => { + if(err) console.log(err); + }) + } + } // Recording model @@ -132,7 +153,7 @@ const RecordingsModel = new Vue({ }, computed: { transcription: function() { - if(this.hovering) return this.hovering.transcription; + if(this.hovering) return this.hovering.transcription + this.hovering.potentialTranscription; else return ''; } }, @@ -144,6 +165,7 @@ const RecordingsModel = new Vue({ fs.unlinkSync(filepath.slice(0, -4)+'.txt') this.recordings = this.recordings.filter((rec) => {return rec.path !== filepath}) + this.hovering = null } } }) @@ -204,12 +226,16 @@ const recordControl = function() { } } +// Receive recordings from main process + ipcRenderer.on('add-recording', (event, filename) => { - RecordingsModel.recordings.unshift(new Recording(filename)) - RecordingsModel.fuzzyRecordings = RecordingsModel.fuzzyRecordings.filter((recording) => { - return recording.path !== filename - }) + const fuzzy = RecordingsModel.fuzzyRecordings.filter(recording => recording.path === filename)[0] + RecordingsModel.recordings.unshift(new Recording(filename, fuzzy.transcription + fuzzy.potentialTranscription)) + + RecordingsModel.fuzzyRecordings = RecordingsModel.fuzzyRecordings.filter((recording) => { + return recording.path !== filename + }) }) ipcRenderer.on('fuzzy-recording', (event, filename) => { @@ -217,6 +243,54 @@ ipcRenderer.on('fuzzy-recording', (event, filename) => { RecordingsModel.fuzzyRecordings.unshift(new FuzzyRecording(filename)) }) +ipcRenderer.on('fuzzy-transcript', (event, filename, data) => { + + let isFuzzy = true + + console.log('fuzzy data ' + data + ' at ' + filename) + let target = RecordingsModel.fuzzyRecordings.filter(recording => recording.path === filename) + + console.log(target) + if(target.length < 1) { + target = RecordingsModel.recordings.filter(recording => recording.path === filename) + isFuzzy = false + } + + console.log(target[0]) + + if(isFuzzy) { + + let transcript = target[0].transcription + let potentialTranscript = '' + + data.map(result => { + + if(result.is_final) { + transcript += result.alternatives[0].transcript + } + + else { + potentialTranscript += result.alternatives[0].transcript + } + }) + + target[0].transcription += transcript + target[0].potentialTranscription = potentialTranscript + } + + else { + + data.map(result => { + + if(result.is_final) { + target[0].transcription += result.alternatives[0].transcript + } + }) + + target[0].saveTranscription() + } +}) + // Close window and stop recording const quit = function() { ipcRenderer.send('stop-recording') diff --git a/stream-to-text.js b/stream-to-text.js new file mode 100644 index 0000000..04dbf0a --- /dev/null +++ b/stream-to-text.js @@ -0,0 +1,55 @@ +const EventEmitter = require('events'); +const speech = require('@google-cloud/speech'); + +const client = new speech.SpeechClient(); + +const languageCode = "fr-CA"; + +exports.setLanguage = function(code) { + languageCode = code; +} + +const request = { + config: { + encoding: 'LINEAR16', + sampleRateHertz: 44100, + languageCode: languageCode + }, + interimResults: true +}; + +exports.Transcripter = class Transcripter extends EventEmitter { + constructor() { + super() + this.stream = null; + this.dataBuffer = Buffer.alloc(0); + } + + start() { + this.stream = client.streamingRecognize(request) + .on('error', (err) => { + this.emit('error', err); + }) + .on('data', (data) => { + if(data.error) + this.emit('error', data.error); + else if(data.results) + this.emit('data', data.results); + }); + } + + stop() { + this.stream.end(); + this.stream = null; + this.dataBuffer = Buffer.alloc(0); + } + + putData(data) { + this.dataBuffer = Buffer.concat([this.dataBuffer, data]); + + if(this.dataBuffer.length > 882) { + this.stream.write(this.dataBuffer); + this.dataBuffer = Buffer.alloc(0); + } + } +} diff --git a/views/recordings.html b/views/recordings.html index bcbdf75..e3ffa5b 100644 --- a/views/recordings.html +++ b/views/recordings.html @@ -64,7 +64,7 @@
- + -- 2.34.1
{{recording.timestamp.toLocaleString()}}