From e4eae76bdfc17de16cf9c0b393b8874e082d550a Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 15 Dec 2025 09:41:17 +0100 Subject: [PATCH] midi: implement backend restart feature --- CHANGELOG.md | 2 + .../docs/docs/custom-module/custom-module.md | 4 ++ .../docs/docs/user-interface/launcher.md | 4 +- src/launcher/toolbar.mjs | 30 ++++++++--- src/server/electron/index.mjs | 7 ++- src/server/node/midi.mjs | 54 ++++++++++++++++--- src/server/node/osc/index.mjs | 1 + 7 files changed, 84 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d48c2cb..e994ee36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,8 @@ - midi - add `no_auto_note_off` option + - add `Restart backend` action to the launcher's menu + - add `restartMidiBackend()` function to custom module context - breaking change - dropped support for iOS 10.3 diff --git a/resources/docs/docs/custom-module/custom-module.md b/resources/docs/docs/custom-module/custom-module.md index 3615e4fc..8bb3081f 100644 --- a/resources/docs/docs/custom-module/custom-module.md +++ b/resources/docs/docs/custom-module/custom-module.md @@ -167,6 +167,10 @@ for (var ip in tcpServer.clients) { ``` +#### `restartMidiBackend()` + +This function can be used to restart the midi backend (if already running) without restarting the server + #### Other javascript globals - `console` diff --git a/resources/docs/docs/user-interface/launcher.md b/resources/docs/docs/user-interface/launcher.md index 304aa33b..87ba9d67 100644 --- a/resources/docs/docs/user-interface/launcher.md +++ b/resources/docs/docs/user-interface/launcher.md @@ -7,7 +7,9 @@ - New window: open a new client window (hidden when stopped) - Load: load server configuration from file - Save / Save as: save server configuration to file -- List MIDI devices: list available MIDI devices in the console +- MIDI + - List devices: list available MIDI devices in the console + - Restart backend: restart midi backend without restarting the server - Console: - Clear: clear console - Autoscroll: enable/disable console autoscroll diff --git a/src/launcher/toolbar.mjs b/src/launcher/toolbar.mjs index 8c80fc9d..a6677b4d 100644 --- a/src/launcher/toolbar.mjs +++ b/src/launcher/toolbar.mjs @@ -9,7 +9,7 @@ import {DOM, PXSCALE} from '../client/globals.mjs' var {Menu, MenuItem} = remote.require('electron'), menu = new Menu(), - {midilist, openDocs} = remote.getGlobal('launcherSharedGlobals'), + {midiList, midiRestart, openDocs} = remote.getGlobal('launcherSharedGlobals'), serverStarting = false, serverStart = ()=>{ if (serverStarting) return @@ -77,13 +77,25 @@ menu.append(new MenuItem({ menu.append(new MenuItem({ type: 'separator' })) -menu.append(new MenuItem({ - label: 'List MIDI Devices', - click: ()=>{ - midilist() - }, - accelerator: 'ctrl + m' -})) + +menu.append(new MenuItem({type: 'submenu' , label: 'MIDI', submenu: [ + new MenuItem({ + label: 'List devices', + click: ()=>{ + midiList() + }, + accelerator: 'ctrl + m' + }), + new MenuItem({ + label: 'Restart backend', + id: 'midiRestart', + click: ()=>{ + midiRestart() + }, + enabled: false + }), + +]})) menu.append(new MenuItem({type: 'submenu' , label: 'Console', submenu: [ new MenuItem({ label: 'Clear', @@ -211,6 +223,8 @@ class Toolbar { stop.visible = !!serverStarting qrcode.visible = !!serverStarting newWindow.visible = !!serverStarting + menu.getMenuItemById('midiRestart').enabled = !!serverStarting + menu.popup({window: remote.getCurrentWindow(), x: parseInt(PXSCALE), y: parseInt(40 * PXSCALE)}) }) diff --git a/src/server/electron/index.mjs b/src/server/electron/index.mjs index 567c34f8..f75f121d 100644 --- a/src/server/electron/index.mjs +++ b/src/server/electron/index.mjs @@ -169,7 +169,12 @@ function startLauncher() { global.launcherSharedGlobals = { settings: settings, openDocs: openDocs, - midilist: midi.list + midiList: midi.list, + midiRestart: ()=>{ + if (serverProcess) { + serverProcess.send('midiRestart') + } + } } var address = 'file://' + path.resolve(__dirname + '/../../launcher/' + 'index.html') diff --git a/src/server/node/midi.mjs b/src/server/node/midi.mjs index 6ff6048d..9cb7ee17 100644 --- a/src/server/node/midi.mjs +++ b/src/server/node/midi.mjs @@ -1,10 +1,11 @@ import {PythonShell} from 'python-shell' import fs from 'fs' import path from 'path' +import {EventEmitter} from 'events' import * as settings from './settings.mjs' -var scriptPath = __dirname.includes('server/node') ? +var scriptPath = __dirname.includes('server/node') ? path.resolve(__dirname, '../python') : // src folder structure path.resolve(__dirname, './python') // app folder structure with rollup chunking @@ -32,12 +33,33 @@ if (midiBinaries[process.arch] && midiBinaries[process.arch][process.platform]) } } -class MidiConverter { +class MidiConverter extends EventEmitter{ constructor() { + super() + if (expectMidiBinariesError) MidiConverter.midiBinariesError() + this.running = false + this.restarting = false + this.py = {} + + this.start() + + + process.on('message', (m) => { + if (m == 'midiRestart') { + this.restart() + } + }) + + } + + start() { + + if (this.running) return + this.py = new PythonShell('midi.py', Object.assign({ args: [ '--params', @@ -55,12 +77,32 @@ class MidiConverter { } }) - this.running = true this.py.childProcess.on('exit', (code)=>{ - if (code === null) console.error('(ERROR, MIDI) Midi bridge process crashed') + if (code === null && !this.restarting) console.error('(ERROR, MIDI) Midi bridge process crashed') this.running = false + this.emit('stopped') }) + this.running = true + + } + + stop() { + + if (this.running) this.py.childProcess.kill() + + } + + restart() { + + if (this.running) { + this.restarting = true + this.once('stopped', ()=>{ + this.start() + this.restarting = false + }) + this.stop() + } } send(data) { @@ -76,11 +118,7 @@ class MidiConverter { } - stop() { - this.py.childProcess.kill() - - } init(receiveOsc) { diff --git a/src/server/node/osc/index.mjs b/src/server/node/osc/index.mjs index a3f27274..89870658 100644 --- a/src/server/node/osc/index.mjs +++ b/src/server/node/osc/index.mjs @@ -19,6 +19,7 @@ export default class OscServer { this.customModuleEventEmitter = new EventEmitter() this.customModule = settings.read('custom-module') ? new CustomModule(settings.read('custom-module'), { app: this.customModuleEventEmitter, + restartMidiBackend: ()=>{if (this.midiServer) this.midiServer.restart()}, tcpServer: this.oscTCPServer, sendOsc: this.sendOsc.bind(this), receiveOsc: this.receiveOsc.bind(this),