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...
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) => {
// 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<nChannels; i++) {
- catch(err) {
- console.error(`Couldn't write to recorder ${this.index}`)
- console.warn(err)
-
- this.stopRecording()
- }
-
- }
-
- //this.transcripter.putData(data)
- }
+ let recorder = new BrowserWindow({width: 150, height: 150, minWidth: i, show: true});
+ recorder.loadURL(recorderUrl);
+ audioRecorders.push(recorder);
+ console.log(recorder);
}
-
- startRecording(id) {
-
- if(record) {
-
- 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(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<nChannels; i++) {
- audioRecorders.push(new AudioRecorder(i))
}
// TCP socket
audioRecorders.forEach((recorder, index) => {
- 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
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'); });
})
})
--- /dev/null
+const electron = require('electron');
+const AudioRecorder = require('./audio-recorder.js');
+
+const ipcRenderer = electron.ipcRenderer;
+
+const recorder = new AudioRecorder();
--- /dev/null
+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();
+ }
+}
--- /dev/null
+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);
+ }
+
+ }
+}
+++ /dev/null
-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);
- }
-
- }
-}
--- /dev/null
+<html>
+ <script>
+ const AudioRecorder = require('../resources/js/audio-recorder.js').AudioRecorder;
+ const recorder = new AudioRecorder();
+ </script>
+</html>