From: Cedric Godin Date: Fri, 26 Jan 2018 16:31:09 +0000 (-0500) Subject: Refactored main to split server and odas in separated files X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=29b56783e2d5b042d9c9624c2de40b3893cedbf7;p=yn%2Fyn-odasweb.git Refactored main to split server and odas in separated files --- diff --git a/main.js b/main.js index e1de87e..548b969 100644 --- a/main.js +++ b/main.js @@ -2,7 +2,7 @@ const electron = require('electron') // Module to control application life. const app = electron.app -app.commandLine.appendSwitch('--ignore-gpu-blacklist'); +app.commandLine.appendSwitch('--ignore-gpu-blacklist'); // Allows Web GL on Ubuntu // Module to create native browser window. const BrowserWindow = electron.BrowserWindow @@ -12,11 +12,11 @@ const url = require('url') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. -let mainWindow +let odasStudio = {} function createWindow () { // Create the browser window. - mainWindow = new BrowserWindow({width: 1200, height: 800, + odasStudio.mainWindow = new BrowserWindow({width: 1200, height: 800, 'web-preferences': { 'web-security': false, "webgl": true @@ -24,7 +24,7 @@ function createWindow () { // and load the index.html of the app. - mainWindow.loadURL(url.format({ + odasStudio.mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'views/live_data.html'), protocol: 'file:', slashes: true @@ -34,11 +34,11 @@ function createWindow () { //mainWindow.webContents.openDevTools() // Emitted when the window is closed. - mainWindow.on('closed', function () { + odasStudio.mainWindow.on('closed', function () { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. - mainWindow = null + odasStudio.mainWindow = null }) } @@ -59,7 +59,7 @@ app.on('window-all-closed', function () { app.on('activate', function () { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. - if (mainWindow === null) { + if (odasStudio.mainWindow === null) { createWindow() } }) @@ -67,136 +67,8 @@ app.on('activate', function () { // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here. -/* - * Create TCP server for source tracking - */ +const sockets = require('./servers.js') +odasStudio.odas = require('./odas.js') -// Load modules -const StringDecoder = require('string_decoder').StringDecoder; -var net = require('net'); - -var server = net.createServer(); -server.on('connection', handleConnection); - -server.listen(9000, function() { - console.log('server listening to %j', server.address()); -}); - -function handleConnection(conn) { - var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; - console.log('new client connection from %s', remoteAddress); - - conn.on('data', onConnData); - conn.once('close', onConnClose); - conn.on('error', onConnError); - - function onConnData(d) { - - var decoder = new StringDecoder(); - - // Decode received string - var str = decoder.write(d); - - try { - mainWindow.webContents.send('newTracking',str); - if(typeof odas_process == 'undefined') { - mainWindow.webContents.send('remote-online'); - } - } - - catch(err) { - console.log('Window was closed'); - } - - } - - function onConnClose() { - console.log('connection from %s closed', remoteAddress); - mainWindow.webContents.send('remote-offline'); - } - - function onConnError(err) { - console.log('Connection %s error: %s', remoteAddress, err.message); - } -} - - -/* - * Create TCP server for potential sources - */ - -var potentialServer = net.createServer(); -potentialServer.on('connection', handlePotConnection); - -potentialServer.listen(9001, function() { - console.log('server listening to %j', potentialServer.address()); -}); - -function handlePotConnection(conn) { - var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; - console.log('new client connection from %s', remoteAddress); - - conn.on('data', onConnData); - conn.once('close', onConnClose); - conn.on('error', onConnError); - - function onConnData(d) { - - var decoder = new StringDecoder(); - - // Decode received string - var str = decoder.write(d); - - try { - mainWindow.webContents.send('newPotential',str); - if(typeof odas_process == 'undefined') { - mainWindow.webContents.send('remote-online', conn.remoteAddress); - } - } - - catch(err) { - - console.log('Window was closed'); - } - - } - - function onConnClose() { - console.log('connection from %s closed', remoteAddress); - mainWindow.webContents.send('remote-offline'); - } - - function onConnError(err) { - console.log('Connection %s error: %s', remoteAddress, err.message); - } -} - - -/* - * ODAS Control - */ - -const ipcMain = electron.ipcMain -const child_process = require('child_process') - -let odas_process - -ipcMain.on('launch-odas', function(event, core, config) { - - console.log('received launch command') - console.log(core) - console.log(config) - - odas_process = child_process.spawn(core, ['-c', config]) - - event.sender.send('launched-odas', true) -}) - - -ipcMain.on('stop-odas', function(event) { - - odas_process.kill('SIGINT') - odas_process = undefined - - console.log('received stop command') -}) +sockets.startTrackingServer(odasStudio) +sockets.startPotentialServer(odasStudio) diff --git a/odas.js b/odas.js new file mode 100644 index 0000000..010383b --- /dev/null +++ b/odas.js @@ -0,0 +1,29 @@ +/* + * ODAS Control + */ + +exports.odas_process + +const electron = require('electron') +const ipcMain = electron.ipcMain +const child_process = require('child_process') + +ipcMain.on('launch-odas', function(event, core, config) { + + console.log('received launch command') + console.log(core) + console.log(config) + + exports.odas_process = child_process.spawn(core, ['-c', config]) + + event.sender.send('launched-odas', true) +}) + + +ipcMain.on('stop-odas', function(event) { + + exports.odas_process.kill('SIGINT') + exports.odas_process = undefined + + console.log('received stop command') +}) diff --git a/odas_interface/web_interface.ios b/odas_interface/web_interface.ios deleted file mode 100644 index 8749432..0000000 --- a/odas_interface/web_interface.ios +++ /dev/null @@ -1,59 +0,0 @@ -# Input - -input: -{ - -# raw: { type = "file"; path = "in.raw"; format = "bin16"; } - raw: { type = "soundcard"; card = 1; device = 0; format = "bin16"; } - -}; - -# Output: - -output: -{ - raw: - ( -# { type = "file"; path = "recorded_raw_16.raw"; format = "bin16"; } -# { type = "file"; path = "recorded_raw_32.raw"; format = "bin32"; }, -# { type = "file"; path = "recorded_raw_24.raw"; format = "bin24"; } - ); - - mapping: - ( -# { type = "file"; path = "recorded_mapping_16.raw"; format = "bin16"; } -# { type = "file"; path = "recorded_mapping_32.raw"; format = "bin32"; } - ); - - resample: - ( -# { type = "file"; path = "recorded_resample_16.raw"; format = "bin16"; } -# { type = "file"; path = "recorded_resample_32.raw"; format = "bin32"; } - ); - - stft: - ( -# { type = "file"; path = "recorded_stft.bin"; format = "float"; } - ); - - ssl: - ( - { type = "socket"; ip = "127.0.0.1"; port = 9001; format = "json"; } - ); - - sst: - ( - { type = "socket"; ip = "127.0.0.1"; port = 9000; format = "json"; } - ); - - sss: - ( -# { type = "file"; path = "separated_stft.bin"; format = "float"; } - ); - - sspf: - ( -# { type = "file"; path = "postfiltered_stft.bin"; format = "float"; } - ); - -} diff --git a/package.json b/package.json index cc4fcdc..21f9620 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,22 @@ { - "name": "electron-quick-start", + "name": "ODAS_Desktop", "version": "1.0.0", - "description": "A minimal Electron application", + "description": "A desktop GUI for the ODAS library", "main": "main.js", "scripts": { "start": "electron ." }, - "repository": "https://github.com/electron/electron-quick-start", - "keywords": [ - "Electron", - "quick", - "start", - "tutorial", - "demo" - ], - "author": "GitHub", - "license": "CC0-1.0", - "devDependencies": { + "author": "Cedric Godin", + "license": "GPL-3.0", + "dependencies": { "electron": "~1.7.8", - "express": "^4.15.2", - "express-ws": "^3.0.0", "ip": "^1.1.5", - "systeminformation": "^3.17.3" + "speaker": "^0.4.0", + "systeminformation": "^3.17.3", + "wav": "^1.0.1" + }, + "repository" : + { "type" : "git" + , "url" : "https://github.com/introlab/odas_web.git" } } diff --git a/ressources/js/audio_stream.js b/ressources/js/audio_stream.js index 63c1346..bfae0bd 100644 --- a/ressources/js/audio_stream.js +++ b/ressources/js/audio_stream.js @@ -10,48 +10,64 @@ var streamStarted = false; var watchdog = null; +/* + * Audio socket + */ + + +const processAudio = function(msg) { + + var reader = new FileReader(); + + reader.onloadend = function() { + document.dispatchEvent(new CustomEvent('audioData', { 'detail': reader.result})); + }; + + reader.readAsArrayBuffer(msg.data); +}; + document.addEventListener('audioData',function(ev) { - + var data = new Int16Array(ev.detail); var dataNormalized = new Float32Array(data.length); - + for(var i=0; i { + + trackingServer = net.createServer(); + trackingServer.on('connection', handleConnection); + + trackingServer.listen(9000, function() { + console.log('server listening to %j', trackingServer.address()); + }); + + function handleConnection(conn) { + var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; + console.log('new client connection from %s', remoteAddress); + + conn.on('data', onConnData); + conn.once('close', onConnClose); + conn.on('error', onConnError); + + function onConnData(d) { + + var decoder = new StringDecoder(); + + // Decode received string + var str = decoder.write(d); + + try { + odasStudio.mainWindow.webContents.send('newTracking',str); + if(typeof odasStudio.odas.odas_process == 'undefined') { + odasStudio.mainWindow.webContents.send('remote-online'); + } + } + + catch(err) { + console.log('Window was closed'); + } + + } + + function onConnClose() { + console.log('connection from %s closed', remoteAddress); + odasStudio.mainWindow.webContents.send('remote-offline'); + } + + function onConnError(err) { + console.log('Connection %s error: %s', remoteAddress, err.message); + } + } + + } + + +/* + * Create TCP server for potential sources + */ + + exports.startPotentialServer = (odasStudio) => { + + potentialServer = net.createServer(); + potentialServer.on('connection', handlePotConnection); + + potentialServer.listen(9001, function() { + console.log('server listening to %j', potentialServer.address()); + }); + + function handlePotConnection(conn) { + var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; + console.log('new client connection from %s', remoteAddress); + + conn.on('data', onConnData); + conn.once('close', onConnClose); + conn.on('error', onConnError); + + function onConnData(d) { + + var decoder = new StringDecoder(); + + // Decode received string + var str = decoder.write(d); + + try { + odasStudio.mainWindow.webContents.send('newPotential',str); + if(typeof odasStudio.odas.odas_process == 'undefined') { + odasStudio.mainWindow.webContents.send('remote-online', conn.remoteAddress); + } + } + + catch(err) { + + console.log('Window was closed'); + } + + } + + function onConnClose() { + console.log('connection from %s closed', remoteAddress); + odasStudio.mainWindow.webContents.send('remote-offline'); + } + + function onConnError(err) { + console.log('Connection %s error: %s', remoteAddress, err.message); + } + } + } diff --git a/views/live_data.html b/views/live_data.html index b759be6..e9bea13 100644 --- a/views/live_data.html +++ b/views/live_data.html @@ -182,6 +182,7 @@ +