From 542dfee8cc4c9cf345be3e84625600e9d225f1c4 Mon Sep 17 00:00:00 2001 From: Cedric Godin Date: Tue, 30 Jan 2018 11:25:22 -0500 Subject: [PATCH] First working version of separated sources recording --- recordings.js | 154 ++++++++++++++++++++++++++++++ ressources/js/recordings_model.js | 34 +++++-- ressources/js/tcp_link.js | 4 + views/recordings.html | 2 + 4 files changed, 188 insertions(+), 6 deletions(-) diff --git a/recordings.js b/recordings.js index a3ea9cd..0872895 100644 --- a/recordings.js +++ b/recordings.js @@ -1,11 +1,27 @@ const electron = require('electron') +const fs = require('fs') const path = require('path') const url = require('url') +const net = require('net') +const wav = require('wav') +const reverser = require('buffer-reverse') + +/* Some hardcoded parameters for now + Should be dynamic... +*/ +const bitNumber = 16 +const nChannels = 4 +const sampleRate = 44100 +/* + End of parameters +*/ const BrowserWindow = electron.BrowserWindow const ipcMain = electron.ipcMain let odasStudio +let workspacePath +let record = false exports.register = (_odasStudio) => { @@ -29,6 +45,144 @@ function createWindow () { // Emitted when the window is closed. odasStudio.recordingsWindow.on('closed', function () { + audioRecorders.forEach((recorder)=>{recorder.stopRecording()}) odasStudio.recordingsWindow = null }) } + +// 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.writer = undefined + + 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(typeof(this.writer)!=='undefined') { + this.writer.write(data) + } + } + + startRecording(id) { + console.log('recorder started') + + if(typeof(this.writer) == 'undefined' && record) { + + let filename = path.join(workspacePath, `ODAS_${id}_${new Date().toLocaleString()}.wav`) + this.writer = new wav.FileWriter(filename,{channels:1, sampleRate:sampleRate, bitDepth:bitNumber}) + } + } + + stopRecording() { + //console.log('recorder stopped') + + if(typeof(this.writer) !== 'undefined') { + + this.writer.end() + this.writer.on('header',(header) => { + if(typeof(odasStudio.recordingsWindow)!='undefined') { + odasStudio.recordingsWindow.webContents.send('add-recording', this.writer.path) + } + + this.writer = undefined + }) + } + } +} + +const audioRecorders = [] +for(i=0; i { + + var remoteAddress = conn.remoteAddress + ':' + conn.remotePort + console.log('new client connection from %s', remoteAddress) + + console.log(this) + conn.on('data', (data) => { + + let offset = 0 + let jump = bitNumber/8 + let delta = jump-1 + + if(record) { + + try { + + while(offset < data.length) { + + audioRecorders.forEach((recorder, index) => { + + recorder.receive(new Buffer.from(data.slice(offset + index*jump, offset + index*jump + delta +1))) + //recorder.receive(new Buffer.from([data[offset+index*jump],data[offset+index*jump+delta]])) + }) + + offset += (bitNumber / 8)*nChannels + } + + } catch(e) { + console.log(e) + } + } + + }) + + conn.once('close', () => { + console.log('connection from %s closed', remoteAddress) + audioRecorders.forEach((recorder) => { recorder.stopRecording() }) + }) + + conn.on('error', (err) => { + console.log('Connection %s error: %s', remoteAddress, err.message) + }) + }) + + this.server.listen(this.port, () => { + console.log('server listening to %j', this.server.address()) + }) + } + +} + +const audioServer = new AudioSocket() diff --git a/ressources/js/recordings_model.js b/ressources/js/recordings_model.js index d5bce4b..7d1f5d3 100644 --- a/ressources/js/recordings_model.js +++ b/ressources/js/recordings_model.js @@ -8,13 +8,13 @@ const dialog = require('electron').remote.dialog // Single recording class class Recording { - constructor(filename) { + constructor(fullPath) { // Init audio recording this.isPlaying = false this.timestamp = new Date() - this.filename = filename - this.path = path.join(os.homedir(), 'Music', this.filename) + this.path = fullPath + this.filename = path.basename(this.path) this.duration = 0 this.audio = undefined @@ -24,10 +24,23 @@ class Recording { this.duration = info.duration this.timestamp = new Date(info.stats.birthtime) + + RecordingsModel.recordings.sort((a,b) => { + if(a.timestamp < b.timestamp) + return 1 + + else if(a.timestamp > b.timestamp) + return -1 + + else { + return 0 + } + }) } else { this.duration = -1 + console.log(err) } }) } @@ -65,6 +78,15 @@ const RecordingsModel = new Vue({ recordings: [], workspacePath: localStorage.workspacePath, recordingEnabled: false + }, + methods: { + removeRecording(filepath) { + + console.log(`Deleting ${filepath}`) + fs.unlinkSync(filepath) + + this.recordings = this.recordings.filter((rec) => {return rec.path !== filepath}) + } } }) @@ -99,7 +121,7 @@ const createList = function(workspace) { console.log(file) if(path.extname(file).toLowerCase() === '.wav') - RecordingsModel.recordings.push(new Recording(file)) + RecordingsModel.recordings.push(new Recording(path.join(RecordingsModel.workspacePath,file))) }) }) } @@ -124,9 +146,9 @@ const recordControl = function() { } } -ipcRenderer.on('new-recording', (event, filename) => { +ipcRenderer.on('add-recording', (event, filename) => { - RecordingsModel.recordings.push(new Recording(filename)) + RecordingsModel.recordings.unshift(new Recording(filename)) }) // Close window and stop recording diff --git a/ressources/js/tcp_link.js b/ressources/js/tcp_link.js index 5e411b0..7df2908 100644 --- a/ressources/js/tcp_link.js +++ b/ressources/js/tcp_link.js @@ -65,6 +65,8 @@ const processTracking = function(event, msg) { currentFrame.sources[newMap[src.id]].id = src.id; hasNewSource = true; + + ipcRenderer.send('new-recording',newMap[src.id],src.id) } currentFrame.sources[newMap[src.id]].x = src.x; @@ -89,6 +91,8 @@ const processTracking = function(event, msg) { currentFrame.sources[index].active = false; currentFrame.sources[index].selected = true; + + ipcRenderer.send('end-recording',index) }); // Trigger update diff --git a/views/recordings.html b/views/recordings.html index e83ace7..71045be 100644 --- a/views/recordings.html +++ b/views/recordings.html @@ -40,6 +40,7 @@ Timestamp Duration Filename + @@ -47,6 +48,7 @@ {{recording.timestamp.toLocaleString()}} {{recording.duration.toFixed(2)}} s {{recording.filename}} + -- 2.34.1