midi: move python assets in src, add launcher context menu action for midi device listing
This commit is contained in:
parent
1d298b574e
commit
3eb73eca74
10 changed files with 125 additions and 77 deletions
|
|
@ -2,6 +2,7 @@ var {remote, ipcRenderer, shell} = eval('require(\'electron\')'),
|
|||
{dialog, Menu, MenuItem, app} = remote.require('electron'),
|
||||
menu = new Menu(),
|
||||
settings = remote.getGlobal('settings'),
|
||||
midilist = remote.getGlobal('midilist'),
|
||||
packageInfos = remote.require('./package.json'),
|
||||
packageVersion = packageInfos.version,
|
||||
packageUrl = packageInfos.repository.url,
|
||||
|
|
@ -210,16 +211,16 @@ $(document).ready(()=>{
|
|||
|
||||
// Fake console
|
||||
var terminal = $('<div class="terminal"></div>').appendTo(form).hide(),
|
||||
autoscoll = true
|
||||
autoscroll = true
|
||||
|
||||
ipcRenderer.on('stdout', function(e, msg){
|
||||
terminal.append(`<div class="log">${ansiHTML(msg)}</div>`)
|
||||
if (autoscoll) scrollBottom()
|
||||
if (autoscroll) scrollBottom()
|
||||
})
|
||||
|
||||
ipcRenderer.on('stderr', function(e, msg){
|
||||
terminal.append(`<div class="error">${ansiHTML(msg)}</div>`)
|
||||
if (autoscoll) scrollBottom()
|
||||
if (autoscroll) scrollBottom()
|
||||
})
|
||||
|
||||
|
||||
|
|
@ -227,6 +228,15 @@ $(document).ready(()=>{
|
|||
|
||||
menu.append(new MenuItem({role: 'copy'}))
|
||||
menu.append(new MenuItem({role: 'paste'}))
|
||||
menu.append(new MenuItem({type: 'submenu' , label: 'Midi', submenu: [
|
||||
new MenuItem({
|
||||
label: 'List Devices',
|
||||
click: ()=>{
|
||||
terminal.show()
|
||||
midilist()
|
||||
}
|
||||
}),
|
||||
]}))
|
||||
menu.append(new MenuItem({type: 'submenu' , label: 'Console', submenu: [
|
||||
new MenuItem({
|
||||
label: 'Clear',
|
||||
|
|
@ -239,7 +249,7 @@ $(document).ready(()=>{
|
|||
type: 'checkbox',
|
||||
checked: true,
|
||||
click: function(e){
|
||||
autoscoll = e.checked
|
||||
autoscroll = e.checked
|
||||
}
|
||||
})
|
||||
]}))
|
||||
|
|
|
|||
26
src/python/head.py
Normal file
26
src/python/head.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from sys import argv, stdin, stdout, version_info, version, exit
|
||||
from binascii import hexlify, unhexlify
|
||||
import traceback
|
||||
import json as JSON
|
||||
import re
|
||||
|
||||
def ipcSend(name, data):
|
||||
print(JSON.dumps([name, data]))
|
||||
stdout.flush()
|
||||
|
||||
try:
|
||||
import rtmidi
|
||||
API = rtmidi.RtMidiIn.UNIX_JACK if 'jack' in argv else rtmidi.RtMidiIn.UNSPECIFIED
|
||||
JACK = API == rtmidi.RtMidiIn.UNIX_JACK
|
||||
in_dev = rtmidi.RtMidiIn(API, 'MIDI->OSC probe')
|
||||
out_dev = rtmidi.RtMidiOut(API, 'OSC->MIDI probe')
|
||||
except:
|
||||
ipcSend('error', 'pyrtmidi not found (or wrong version)\nRunning with python version %s' % version)
|
||||
exit()
|
||||
|
||||
if version_info.major == 3:
|
||||
raw_input = input
|
||||
|
||||
debug = False
|
||||
if 'debug' in argv:
|
||||
debug = True
|
||||
24
src/python/list.py
Normal file
24
src/python/list.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
from head import *
|
||||
|
||||
def list():
|
||||
|
||||
message = []
|
||||
|
||||
message.append('===========')
|
||||
message.append('MIDI Inputs')
|
||||
|
||||
for i in range(in_dev.getPortCount()):
|
||||
message.append('%i: %s' % (i, in_dev.getPortName(i)))
|
||||
|
||||
message.append('===========')
|
||||
message.append('MIDI Outputs')
|
||||
|
||||
for i in range(out_dev.getPortCount()):
|
||||
message.append('%i: %s' % (i, out_dev.getPortName(i)))
|
||||
|
||||
ipcSend('log', '\n'.join(message))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
list()
|
||||
184
src/python/midi.py
Normal file
184
src/python/midi.py
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
from head import *
|
||||
from list import *
|
||||
|
||||
if 'list' in argv:
|
||||
list()
|
||||
|
||||
inputs = {}
|
||||
outputs = {}
|
||||
|
||||
for arg in argv:
|
||||
|
||||
if type(arg) == str and ':' in arg:
|
||||
|
||||
name, ports = arg.split(':')
|
||||
|
||||
inputs[name] = rtmidi.RtMidiIn(API, name if not JACK else name + '_in')
|
||||
outputs[name] = rtmidi.RtMidiOut(API, name if not JACK else name + '_out')
|
||||
|
||||
if ports == 'virtual':
|
||||
|
||||
try:
|
||||
inputs[name].openVirtualPort('midi_in')
|
||||
outputs[name].openVirtualPort('midi_out')
|
||||
except:
|
||||
ipcSend('error', 'can\'t open virtual port "%s"' % name)
|
||||
|
||||
elif ',' in ports:
|
||||
|
||||
in_port, out_port = [int(x) for x in ports.split(',')]
|
||||
|
||||
if in_port >= in_dev.getPortCount():
|
||||
ipcSend('error', 'can\'t connect to input port %i' % in_port)
|
||||
break
|
||||
|
||||
if out_port >= out_dev.getPortCount():
|
||||
ipcSend('error', 'can\'t connect to output port %i' % in_port)
|
||||
break
|
||||
|
||||
if in_port != -1:
|
||||
|
||||
try:
|
||||
inputs[name].openPort(in_port, 'midi_in')
|
||||
except:
|
||||
ipcSend('error', 'can\'t connect input to port %i: %s' % (in_port, in_dev.getPortName(in_port)))
|
||||
|
||||
if out_port != -1:
|
||||
|
||||
try:
|
||||
outputs[name].openPort(out_port, 'midi_out')
|
||||
except:
|
||||
ipcSend('error', 'can\'t connect to output port %i: %s' % (out_port, out_dev.getPortName(out_port)))
|
||||
|
||||
def createCallbackFunction(name):
|
||||
|
||||
def callback(midi):
|
||||
|
||||
osc = {}
|
||||
osc['args'] = []
|
||||
osc['host'] = 'midi'
|
||||
osc['port'] = name
|
||||
|
||||
if midi.getChannel() != 0:
|
||||
osc['args'].append({'type': 'i', 'value': midi.getChannel()})
|
||||
|
||||
if midi.isNoteOn():
|
||||
osc['address'] = '/note'
|
||||
osc['args'].append({'type': 'i', 'value': midi.getNoteNumber()})
|
||||
osc['args'].append({'type': 'i', 'value': midi.getVelocity()})
|
||||
|
||||
elif midi.isNoteOff():
|
||||
osc['address'] = '/note'
|
||||
osc['args'].append({'type': 'i', 'value': midi.getNoteNumber()})
|
||||
osc['args'].append({'type': 'i', 'value': 0})
|
||||
|
||||
elif midi.isController():
|
||||
osc['address'] = '/control'
|
||||
osc['args'].append({'type': 'i', 'value': midi.getControllerNumber()})
|
||||
osc['args'].append({'type': 'i', 'value': midi.getControllerValue()})
|
||||
|
||||
elif midi.isProgramChange():
|
||||
osc['address'] = '/program'
|
||||
osc['args'].append({'type': 'i', 'value': midi.getProgramChangeNumber()})
|
||||
|
||||
elif midi.isPitchWheel():
|
||||
osc['address'] = '/pitch'
|
||||
osc['args'].append({'type': 'i', 'value': midi.getPitchWheelValue()})
|
||||
|
||||
elif midi.isSysEx():
|
||||
osc['address'] = '/sysex'
|
||||
# Parse the provided data into a hex MIDI data string of the form 'f0 7e 7f 06 01 f7'.
|
||||
v = hexlify(midi.getSysExData()).decode()
|
||||
v = 'f0 ' + ' '.join([v[i:i+2] for i in range(0, len(v), 2)]) + ' f7'
|
||||
osc['args'].append({'type': 'string', 'value': v})
|
||||
|
||||
ipcSend('osc', osc)
|
||||
|
||||
if debug:
|
||||
ipcSend('log','MIDI received: %s' % midi)
|
||||
|
||||
|
||||
def errorLoggedCallback(midi):
|
||||
|
||||
try:
|
||||
callback(midi)
|
||||
except:
|
||||
ipcSend('log', 'ERROR: MIDI: %s' % traceback.format_exc())
|
||||
|
||||
return errorLoggedCallback
|
||||
|
||||
for name in inputs:
|
||||
|
||||
inputs[name].setCallback(createCallbackFunction(name))
|
||||
# Activate sysex support
|
||||
inputs[name].ignoreTypes(False, False, True)
|
||||
|
||||
sysexRegex = re.compile(r'([^0-9A-Fa-f])\1(\1\1)*')
|
||||
|
||||
def sendMidi(name, event, *args):
|
||||
|
||||
if name not in outputs:
|
||||
return
|
||||
|
||||
m = None
|
||||
|
||||
if 'note' in event and len(args) == 3:
|
||||
args = [int(round(x)) for x in args]
|
||||
if args[2] > 0:
|
||||
m = rtmidi.MidiMessage.noteOn(*args)
|
||||
else:
|
||||
m = rtmidi.MidiMessage.noteOff(args[0], args[1])
|
||||
|
||||
elif 'control' in event and len(args) == 3:
|
||||
args = [int(round(x)) for x in args]
|
||||
m = rtmidi.MidiMessage.controllerEvent(*args)
|
||||
|
||||
elif 'program' in event and len(args) == 2:
|
||||
args = [int(round(x)) for x in args]
|
||||
m = rtmidi.MidiMessage.programChange(*args)
|
||||
|
||||
elif 'pitch' in event and len(args) == 2:
|
||||
args = [int(round(x)) for x in args]
|
||||
m = rtmidi.MidiMessage.pitchWheel(*args)
|
||||
|
||||
elif 'sysex' in event:
|
||||
# rtmidi.MidiMessage(unhexlify('f0 7e 7f 06 01 f7')) creates a sysex message
|
||||
# from hex MIDI data string 'f0 7e 7f 06 01 f7'.
|
||||
# We expect all args to be hex strings! args[0] may contain placeholders of
|
||||
# the form 'nn' that are replaced using args[1..N] to create the final message.
|
||||
midiBytes = args[0].replace(' ', '')
|
||||
i = 1
|
||||
for m in sysexRegex.finditer(midiBytes):
|
||||
midiBytes = midiBytes[:m.start()] + args[i].replace(' ', '') + midiBytes[m.end():]
|
||||
i += 1
|
||||
|
||||
if debug:
|
||||
ipcSend('log', 'Sysex string: %s' % midiBytes)
|
||||
|
||||
m = rtmidi.MidiMessage(unhexlify(midiBytes))
|
||||
|
||||
if m is None:
|
||||
|
||||
ipcSend('log','ERROR: MIDI: could not convert osc args to midi message (%s %s)' % (event, " ".join([str(x) for x in args])))
|
||||
|
||||
else:
|
||||
|
||||
outputs[name].sendMessage(m)
|
||||
|
||||
if debug:
|
||||
ipcSend('log','MIDI sent: %s' % m)
|
||||
|
||||
|
||||
while True:
|
||||
|
||||
try:
|
||||
msg = raw_input()
|
||||
except:
|
||||
break
|
||||
|
||||
try:
|
||||
msg = JSON.loads(msg)
|
||||
msg[1] = msg[1].lower()
|
||||
sendMidi(*msg)
|
||||
except:
|
||||
ipcSend('log', 'ERROR: MIDI: %s' % traceback.format_exc())
|
||||
|
|
@ -98,6 +98,7 @@ if (settings.cli) {
|
|||
|
||||
app.on('ready',function(){
|
||||
global.settings = settings
|
||||
global.midilist = require('./midi').list
|
||||
launcher = require('./electron-window')({address:address, shortcuts:dev, width:680, height:(100 + 8*3 + 29 * Object.keys(settings.options).filter(x=>settings.options[x].launcher !== false).length), node:true, color:'#253040'})
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -1,18 +1,21 @@
|
|||
var {PythonShell} = require('python-shell'),
|
||||
settings = require('./settings')
|
||||
|
||||
class Midi {
|
||||
var pythonOptions = {
|
||||
scriptPath:__dirname,
|
||||
mode:'text',
|
||||
}
|
||||
|
||||
class MidiConverter {
|
||||
|
||||
constructor() {
|
||||
|
||||
this.py = new PythonShell('midi.py', {
|
||||
scriptPath:__dirname,
|
||||
mode:'text',
|
||||
this.py = new PythonShell('python/midi.py', Object.assign({
|
||||
args: [
|
||||
settings.read('debug') ? 'debug' : '',
|
||||
...settings.read('midi')
|
||||
]
|
||||
})
|
||||
}, pythonOptions))
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -34,31 +37,46 @@ class Midi {
|
|||
}
|
||||
|
||||
init(receiveOsc) {
|
||||
this.py.on('message', function(message) {
|
||||
// console.log(message)
|
||||
var name, data
|
||||
try {
|
||||
[name, data] = JSON.parse(message)
|
||||
} catch (err) {
|
||||
// console.log(err)
|
||||
}
|
||||
if (name == 'log') {
|
||||
if (data.indexOf('ERROR') === 0) {
|
||||
console.error(data)
|
||||
} else {
|
||||
console.log(data)
|
||||
}
|
||||
} else if (name == 'osc') {
|
||||
receiveOsc(data)
|
||||
} else if (name == 'error') {
|
||||
console.error('ERROR: MIDI: ' + data)
|
||||
this.stop()
|
||||
}
|
||||
|
||||
this.py.on('message', (message)=>{
|
||||
MidiConverter.parseIpc(message, this)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
static parseIpc(message, instance) {
|
||||
|
||||
// console.log(message)
|
||||
var name, data
|
||||
try {
|
||||
[name, data] = JSON.parse(message)
|
||||
} catch (err) {
|
||||
// console.log(err)
|
||||
}
|
||||
if (name == 'log') {
|
||||
if (data.indexOf('ERROR') === 0) {
|
||||
console.error(data)
|
||||
} else {
|
||||
console.log(data)
|
||||
}
|
||||
} else if (name == 'osc') {
|
||||
receiveOsc(data)
|
||||
} else if (name == 'error') {
|
||||
console.error('ERROR: MIDI: ' + data)
|
||||
if (instance) instance.stop()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static list() {
|
||||
|
||||
PythonShell.run('python/list.py', pythonOptions, function (err, results) {
|
||||
if (err) console.error(err)
|
||||
MidiConverter.parseIpc(results)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var midi = new Midi()
|
||||
|
||||
module.exports = midi
|
||||
module.exports = MidiConverter
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ var ipc = require('../server').ipc,
|
|||
EventEmitter = require('events').EventEmitter
|
||||
|
||||
|
||||
if (midi) midi = new midi()
|
||||
|
||||
class OscServer {
|
||||
|
||||
constructor(){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue