midi: implement backend restart feature

This commit is contained in:
jean-emmanuel 2025-12-15 09:41:17 +01:00
parent 31f4119442
commit e4eae76bdf
7 changed files with 84 additions and 18 deletions

View file

@ -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)})
})

View file

@ -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')

View file

@ -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) {

View file

@ -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),