From: Cedric Godin Date: Wed, 21 Feb 2018 18:03:31 +0000 (-0500) Subject: Multi-threaded recording X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=99217a352af28598c3fc758f866fa7fce1a10a9a;p=yn%2Fyn-odasweb.git Multi-threaded recording --- diff --git a/recordings.js b/recordings.js index a43a429..8d4f3b6 100644 --- a/recordings.js +++ b/recordings.js @@ -3,8 +3,6 @@ const fs = require('fs') 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... @@ -20,8 +18,13 @@ const BrowserWindow = electron.BrowserWindow const ipcMain = electron.ipcMain let odasStudio -let workspacePath -let record = false + +let audioRecorders = []; +const recorderUrl = url.format({ + pathname: path.join(__dirname, 'views', 'audio-recorder.html'), + protocol: 'file', + slashes: true +}); exports.register = (_odasStudio) => { @@ -52,190 +55,22 @@ function createWindow () { // Emitted when the window is closed. odasStudio.recordingsWindow.on('closed', function () { - audioRecorders.forEach((recorder)=>{recorder.stopRecording()}) + audioRecorders.forEach((recorder)=>{recorder.webContents.send('terminate-recording')}) odasStudio.recordingsWindow = null }) odasStudio.recordingsWindow.on('ready-to-show', function() { odasStudio.recordingsWindow.show() }) -} - -// Receive recording control -ipcMain.on('start-recording', (event, workspace) => { - workspacePath = workspace - record = true -}) - -ipcMain.on('stop-recording', (event) => { - record = false - workspacePath = undefined -}) - -// Audio Recorder -class AudioRecorder { - - constructor(index) { - - this.index = index - this.active = false - this.hold = false - this.buffer = undefined - 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) { - this.startRecording(id) - } - }) - - ipcMain.on('end-recording', (event, index) => { - if(index == this.index) { - this.stopRecording() - } - }) - - ipcMain.on('stop-recording', (event) => { - this.stopRecording() - }) - } - - receive(data) { - - if(this.active) { - - if(this.hold) { - /* - if(typeof(this.buffer) !== 'undefined') { - - this.buffer = Buffer.concat([this.buffer, data]) - } - - else { - - this.buffer = data - } - */ - } - - else { - - try { - /* - if(typeof(this.buffer) !== 'undefined') { - data = Buffer.concat([this.buffer, data]) - this.buffer = undefined - - console.log(`Wrote samples buffered in writer ${this.index}`) - } - */ - if( !this.writer.write(data)) { - //let error = new Error(`Write stream ${this.index} is full`) - //throw error - console.warn(`Write stream ${this.index} is full\nHolding samples...`) - this.hold = true - } - } + audioRecorders = []; + for(i=0; i { - this.startRecording(id) - console.log(`Recorder ${id} was defined. Retrying...`) - }, 100) - - return - } - - console.log(`Recorder ${this.index} started`) - console.log(`Recorder ${this.index} was ${this.active} active`) - - let filename = path.join(workspacePath, `ODAS_${id}_${new Date().toLocaleString()}.wav`) - this.path = filename - - try { - this.writer = new wav.FileWriter(filename,{channels:1, sampleRate:sampleRate, bitDepth:bitNumber}) - odasStudio.recordingsWindow.webContents.send('fuzzy-recording', filename) - - this.writer.on('drain', () => { - console.log(`Writer ${this.index} is empty.\nResuming...`) - this.hold = false - }) - - this.active = true - this.hold = false - this.buffer = undefined - //this.transcripter.start() - } - - catch(err) { - console.error(`Failed to start recorder ${this.index}`) - console.log(err); - - this.writer = undefined - } - } - } - - stopRecording() { - //console.log('recorder stopped') - - if(this.active) { - - this.active = false; - console.log(`Recorder ${this.index} ended`) - //this.transcripter.stop() - - console.log(`Registering header on recorder ${this.index}`) - this.writer.end() - - this.writer.on('header',(header) => { - - console.log(`Registered header on recorder ${this.index}`) - - if(typeof(odasStudio.recordingsWindow)!='undefined') { - odasStudio.recordingsWindow.webContents.send('add-recording', this.writer.path) - } - - this.writer = undefined - console.log(`Recorder ${this.index} undefined`) - - - }) - } - } -} - -const audioRecorders = [] -for(i=0; i { - recorder.receive(new Buffer.from(data.slice(offset + index*jump, offset + index*jump + delta +1))) + recorder.webContents.send('audio-data', receive(new Buffer.from(data.slice(offset + index*jump, offset + index*jump + delta +1)))); }) offset += (bitNumber / 8)*nChannels @@ -279,11 +114,12 @@ class AudioSocket { conn.once('close', () => { console.log('connection from %s closed', remoteAddress) - audioRecorders.forEach((recorder) => { recorder.stopRecording() }) + audioRecorders.forEach((recorder) => { recorder.webContents.send('stop-recording'); }); }) conn.on('error', (err) => { console.log('Connection %s error: %s', remoteAddress, err.message) + audioRecorders.forEach((recorder) => { recorder.webContents.send('stop-recording'); }); }) }) diff --git a/resources/js/audio-recorder-worker.js b/resources/js/audio-recorder-worker.js new file mode 100644 index 0000000..b356ad1 --- /dev/null +++ b/resources/js/audio-recorder-worker.js @@ -0,0 +1,6 @@ +const electron = require('electron'); +const AudioRecorder = require('./audio-recorder.js'); + +const ipcRenderer = electron.ipcRenderer; + +const recorder = new AudioRecorder(); diff --git a/resources/js/audio-recorder.js b/resources/js/audio-recorder.js new file mode 100644 index 0000000..f8aa8fa --- /dev/null +++ b/resources/js/audio-recorder.js @@ -0,0 +1,191 @@ +const streamToText = require('./stream-to-text.js'); +const electron = require('electron'); +const path = require('path'); +const wav = require('wav') + +const ipcRenderer = electron.ipcRenderer; +const ipcMain = electron.remote.ipcMain; + +// Audio Recorder +exports.AudioRecorder = class AudioRecorder { + + constructor() { + + this.active = false; + this.hold = false; + this.index = window.minWidth; + + this.recordingEnabled = false; + this.workspacePath = ''; + + this.buffer = undefined; + this.writer = undefined; + this.path = undefined; + + this.transcripter = new streamToText.Transcripter(); + + this.transcripter.on('data', data => { + console.log(data); + ipcRenderer.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 && this.recordingEnabled) { + this.startRecording(id); + } + }); + + ipcMain.on('end-recording', (event, index) => { + if(index == this.index) { + this.stopRecording(); + } + }); + + ipcMain.on('stop-recording', (event) => { + this.stopRecording(); + }); + + ipcMain.on('start-recording', (event, workspace) => { + this.recordingEnabled = true; + this.workspacePath = workspace; + }); + + ipcMain.on('stop-recording', (event) => { + this.stopRecording(); + }); + + ipcRenderer.on('stop-recording', (event) => { + this.stopRecording(); + }); + + ipcRenderer.on('terminate-recording', (event) => { + this.stopRecording(true); + }); + + ipcRenderer.on('audio-data', (event, data) => { + this.receive(data); + }); + } + + receive(data) { + + if(this.active) { + + if(this.hold) { + + if(typeof(this.buffer) !== 'undefined') { + + this.buffer = Buffer.concat([this.buffer, data]); + } + + else { + + this.buffer = data; + } + } + + else { + + try { + + if(typeof(this.buffer) !== 'undefined') { + + data = Buffer.concat([this.buffer, data]); + this.buffer = undefined; + + console.log(`Wrote samples buffered in writer ${this.index}`); + } + + if( !this.writer.write(data)) { + + console.warn(`Write stream ${this.index} is full\nHolding samples...`); + this.hold = true; + } + } + + catch(err) { + + console.error(`Couldn't write to recorder ${this.index}`); + console.warn(err); + + this.stopRecording(); + } + + } + + this.transcripter.putData(data); + + } + } + + startRecording(id) { + + if(typeof(this.writer) !== 'undefined') { // Verify that previous recording is cleared + + setTimeout(() => { + + this.startRecording(id); + console.log(`Recorder ${id} was defined. Retrying...`); + }, 100); + + return; + } + + console.log(`Recorder ${this.index} started`) + console.log(`Recorder ${this.index} was ${this.active} active`) + + let filename = path.join(this.workspacePath, `ODAS_${id}_${new Date().toLocaleString()}.wav`) + this.path = filename + + try { + this.writer = new wav.FileWriter(filename,{channels:1, sampleRate:sampleRate, bitDepth:bitNumber}); + ipcRenderer.send('fuzzy-recording', filename); + + this.writer.on('drain', () => { // Release hold when write stream is cleared + console.log(`Writer ${this.index} is empty.\nResuming...`); + this.hold = false; + }); + + this.active = true; + this.hold = false; + this.buffer = undefined; + this.transcripter.start(); + } + + catch(err) { + console.error(`Failed to start recorder ${this.index}`); + console.log(err); + this.writer = undefined; + } + } + + stopRecording(terminate) { + + if(this.active) { + + this.active = false; + console.log(`Recorder ${this.index} ended`); + this.transcripter.stop(); + + console.log(`Registering header on recorder ${this.index}`); + this.writer.end(); + + this.writer.on('header',(header) => { + + console.log(`Registered header on recorder ${this.index}`); + ipcRenderer.send('add-recording', this.writer.path); + + this.writer = undefined; + console.log(`Recorder ${this.index} undefined`); + + }); + } + + if(terminate) + window.close(); + } +} diff --git a/resources/js/stream-to-text.js b/resources/js/stream-to-text.js new file mode 100644 index 0000000..0aaa0fd --- /dev/null +++ b/resources/js/stream-to-text.js @@ -0,0 +1,63 @@ +const EventEmitter = require('events'); +const speech = require('@google-cloud/speech'); + +const client = new speech.SpeechClient({ + keyFilename: 'google-api-key.json' +}); + +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/stream-to-text.js b/stream-to-text.js deleted file mode 100644 index a20e88b..0000000 --- a/stream-to-text.js +++ /dev/null @@ -1,63 +0,0 @@ -const EventEmitter = require('events'); -const speech = require('@google-cloud/speech'); - -const client = new speech.SpeechClient({ - keyFilename: 'google-api-key.json' -}); - -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/audio-recorder.html b/views/audio-recorder.html new file mode 100644 index 0000000..0b13901 --- /dev/null +++ b/views/audio-recorder.html @@ -0,0 +1,6 @@ + + +