First working version of separated sources recording
authorCedric Godin <cedric.godin@usherbrooke.ca>
Tue, 30 Jan 2018 16:25:22 +0000 (11:25 -0500)
committerCedric Godin <cedric.godin@usherbrooke.ca>
Tue, 30 Jan 2018 16:25:22 +0000 (11:25 -0500)
recordings.js
ressources/js/recordings_model.js
ressources/js/tcp_link.js
views/recordings.html

index a3ea9cd20185284f1e118f9458b44d19f6065f4b..0872895bb6ce3e14ea30800ddeef7a3ad5ce14a7 100644 (file)
@@ -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<nChannels; i++) {
+  audioRecorders.push(new AudioRecorder(i))
+}
+
+// TCP socket
+class AudioSocket {
+
+  constructor() {
+
+    this.port = 10000
+    this.server = net.createServer()
+
+    this.server.on('connection', (conn) => {
+
+      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()
index d5bce4b545e824504afa9f1f4604723d423d6437..7d1f5d3a8c02f3f60fc0ca127f2e619de4c3536f 100644 (file)
@@ -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
index 5e411b07efa965d72be2a86215fbd2f37c6bf6c1..7df2908eca8aad62d315ca458996365d7374cde5 100644 (file)
@@ -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
index e83ace7f725449d444c5a6b7a007ea2de63a4d35..71045be6cc2c9eccb2ab5e362b38837642f8e61d 100644 (file)
@@ -40,6 +40,7 @@
                     <th>Timestamp</th>
                     <th>Duration</th>
                     <th>Filename</th>
+                    <th/>
                   </tr>
                   <tr v-for = "recording in recordings">
                     <td v-if="!recording.isPlaying" class="glyphicon glyphicon-play" v-on:click = "recording.play()"/>
@@ -47,6 +48,7 @@
                     <td>{{recording.timestamp.toLocaleString()}}</td>
                     <td>{{recording.duration.toFixed(2)}} s</td>
                     <td>{{recording.filename}}</td>
+                    <td class="glyphicon glyphicon-remove" v-on:click = "removeRecording(recording.path)"/>
                   </tr>
                 </table>
               </div>