open-stage-control/src/python/midi.py
bordun 82361e2f60 git: ugly reset to remove 3000 commits that don't exist
--- relevant commits ---
commit fa63c7ec3596c9afb4f07f75583b2357f4ef6c85 (HEAD -> master, origin/master, origin/HEAD)
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 19:53:00 2020 +0100

    console: don't submit onBlur

commit b653c2edd3be4991dfcd6ed82c76dd3ed2526edb
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 19:48:24 2020 +0100

    ui: restore left & right panel minWidth

commit 3a1af3f9ec70857548d412ce2971992650fa2899
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 19:45:42 2020 +0100

    console: seamless input

commit ec2d49379431a5a6b54968f4dc15bc28286e3741
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 15:08:22 2020 +0100

    console: css tweaks

commit 5248fbe4241134ddf56697180a62f8f89c5f382c
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 14:58:03 2020 +0100

    console: history tweak

commit 8c49baf8d7f9546ab3ab19f9296e5acbf60a364f
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 14:57:47 2020 +0100

    server: allow console osc out

commit 152c6f7276055492bf4a637d5a7a678164d6513b
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 12:32:14 2020 +0100

    lint

commit 1158118db89bb3b14fb2972d020371aac3d8f198
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 12:30:18 2020 +0100

    ipc: set samesite attr on client_id cookie

commit 25fd79474da4ecba8e80e8d00ac63d4dfb62ca66
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 12:24:55 2020 +0100

    console: tweaks

commit 3d39ea9c0a3a28a7344abb023acb76df75f59ba9
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 11:57:45 2020 +0100

    console: add interpreter

commit 961ebe7518
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Thu Nov 12 08:16:22 2020 +0100

    widget: lazy load uiConsole

commit 96f0d3c00f (tag: v1.7.0-beta2)
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Wed Nov 11 22:46:38 2020 +0100

    ios: clone widget not laid out properly in horizontal panels

commit a8e0ac344a
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Wed Nov 11 19:56:35 2020 +0100

    console: unified widget error, with quick edit link from the console

commit 6000d1a67a
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Wed Nov 11 17:31:33 2020 +0100

    package: beta 2

commit d8b1fc298f
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Wed Nov 11 17:30:06 2020 +0100

    ui: overflow hidden on workspaces introduces some overflow scroll with the console panel

commit 35374a7c1c
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Wed Nov 11 17:24:22 2020 +0100

    session: compat: increment toolbar() usages (new console item)

commit ff78df42b6
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Wed Nov 11 17:17:33 2020 +0100

    console: tweaks

commit 33581804ac
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Wed Nov 11 16:28:18 2020 +0100

    ui: move console to full bottom panel

commit dd6b85f6ff
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 16:09:31 2020 +0100

    session: v0 label compat tweak

commit 42c4b0f6cd
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 15:50:56 2020 +0100

    session: v0 label conversion fix on tabs

commit 9431fd722e (tag: v1.7.0-beta1)
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 13:05:06 2020 +0100

    docs, log & restore user rpi build

commit c9e3641332
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 11:13:56 2020 +0100

    python: resolve bin path

commit 8030744325
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 11:13:45 2020 +0100

    docs: add link

commit 4ce960139a
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 10:18:31 2020 +0100

    package: wrong path on osx

commit 2bc507a17b
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 09:48:14 2020 +0100

    package: 1.7.0 beta 1

commit 16579241ce
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 09:45:32 2020 +0100

    package: document midi support and install process

commit b8173294aa
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 09:24:54 2020 +0100

    package: reduce number of prebuilt binaries (keep 64bit linux/windows/osx + node)

commit df215612c5
Author: bordun <jean-emmanuel.doucet@groolot.net>
Date:   Tue Nov 10 08:39:27 2020 +0100

    midi: bump version
2020-11-12 20:02:32 +01:00

249 lines
7 KiB
Python

from head import *
from list import *
from utils import *
ipc_send('version', '1.7.0')
if 'list' in argv:
list()
if 'list-only' in argv:
list()
exit()
# option: act as if displayed program is between 1-128 instead of 0-127
PROGRAM_CHANGE_OFFSET = 'pc_offset' in argv
inputs = {}
outputs = {}
for arg in argv:
if 'python/midi.py' in arg:
pass # in case we're using pre-compiled binaries
elif type(arg) == str and ':' in arg:
name, *ports = arg.split(':')
ports = ':'.join(ports) # port names may contain colons
inputs[name] = rtmidi.MidiIn(API, name if not JACK else name + '_in')
outputs[name] = rtmidi.MidiOut(API, name if not JACK else name + '_out')
if debug:
ipc_send('log','(DEBUG, MIDI) device "%s" created' % name)
if ports == 'virtual':
try:
inputs[name].open_virtual_port('midi_in')
outputs[name].open_virtual_port('midi_out')
if debug:
ipc_send('log','(DEBUG, MIDI) virtual ports opened for device "%s"' % name)
except:
ipc_send('error', 'can\'t open virtual port "%s"' % name)
elif ',' in ports:
in_port, out_port = ports.split(',')
if in_port.isdigit() or in_port == '-1':
in_port = int(in_port)
else:
for i in range(in_dev.get_port_count()):
if in_port.lower() in in_dev.get_port_name(i).lower():
in_port = i
break
if out_port.isdigit() or out_port == '-1':
out_port = int(out_port)
else:
for i in range(out_dev.get_port_count()):
if out_port.lower() in out_dev.get_port_name(i).lower():
out_port = i
break
if type(in_port) != int or in_port >= in_dev.get_port_count():
ipc_send('error', 'can\'t connect to input port "%s"' % in_port)
break
if type(out_port) != int or out_port >= out_dev.get_port_count():
ipc_send('error', 'can\'t connect to output port "%s"' % out_port)
break
if in_port != -1:
try:
inputs[name].open_port(in_port, 'midi_in')
if debug:
ipc_send('log','(DEBUG, MIDI) device "%s" connected to input port %s (%s)' % (name, in_port, in_dev.get_port_name(in_port)))
except:
ipc_send('error', 'can\'t connect input to port %i: %s' % (in_port, in_dev.get_port_name(in_port)))
if out_port != -1:
try:
outputs[name].open_port(out_port, 'midi_out')
if debug:
ipc_send('log','(DEBUG, MIDI) device "%s" connected to output port %s (%s)' % (name, out_port, out_dev.get_port_name(out_port)))
except:
ipc_send('error', 'can\'t connect to output port %i: %s' % (out_port, out_dev.get_port_name(out_port)))
def create_callback(name):
def receive_midi(event, data):
osc = {}
osc['args'] = []
osc['host'] = 'midi'
osc['port'] = name
message, deltatime = event
mtype = message[0] & 0xF0
if mtype in MIDI_TO_OSC:
osc['address'] = MIDI_TO_OSC[mtype]
if mtype == SYSTEM_EXCLUSIVE:
# Parse the provided data into a hex MIDI data string of the form 'f0 7e 7f 06 01 f7'.
v = ' '.join([hex(x).replace('0x', '').zfill(2) for x in message])
osc['args'].append({'type': 'string', 'value': v})
else:
status = message[0]
channel = (status & 0x0F) + 1
osc['args'].append({'type': 'i', 'value': channel})
if mtype == NOTE_OFF:
message[2] = 0
elif mtype == PITCH_BEND:
message = message[:1] + [message[1] + message[2] * 128] # convert 0-127 pair -> 0-16384 ->
elif mtype == PROGRAM_CHANGE and PROGRAM_CHANGE_OFFSET:
message[-1] = message[-1] + 1
for data in message[1:]:
osc['args'].append({'type': 'i', 'value': data})
if debug:
ipc_send('log','(DEBUG, MIDI) in: %s From: midi:%s' % (midi_str(message), name))
ipc_send('osc', osc)
def callback_error_wrapper(event, data):
try:
receive_midi(event, data)
except:
ipc_send('log', '(ERROR, MIDI) %s' % traceback.format_exc())
return callback_error_wrapper
for name in inputs:
inputs[name].set_callback(create_callback(name))
# sysex / mtc support
ignore_sysex = 'sysex' not in argv
ignore_mtc = 'mtc' not in argv
inputs[name].ignore_types(ignore_sysex, ignore_mtc, True)
def midi_message(status, channel, data1=None, data2=None):
msg = [(status & 0xF0) | (channel - 1 & 0x0F)]
if data1 != None:
msg.append(data1 & 0x7F)
if data2 != None:
msg.append(data2 & 0x7F)
return msg
sysexRegex = re.compile(r'([^0-9A-Fa-f])\1(\1\1)*')
def send_midi(name, event, *args):
if name not in outputs:
ipc_send('log','(ERROR, MIDI) unknown output (%s)' % name)
return
if event not in OSC_TO_MIDI:
ipc_send('log','(ERROR, MIDI) invalid address (%s)' % event)
return
m = None
mtype = OSC_TO_MIDI[event]
if mtype == SYSTEM_EXCLUSIVE:
try:
m = []
for arg in args:
if type(arg) is str:
arg = arg.replace(' ', '') # remove spaces
arg = [arg[i:i+2] for i in range(0, len(arg), 2)] # split in 2 chars bytes
arg = [int(x, 16) for x in arg] # parse hex bytes
for x in arg:
m.append(x)
else:
m.append(int(arg))
except:
pass
else:
args = [int(round(x)) for x in args]
m = [mtype, args[0]]
if mtype == NOTE_ON:
if args[2] == 0:
mtype = NOTE_OFF
elif mtype == PITCH_BEND:
args = args[:1] + [args[1] & 0x7F, (args[1] >> 7) & 0x7F] # convert 0-16384 -> 0-127 pair
elif mtype == PROGRAM_CHANGE and PROGRAM_CHANGE_OFFSET:
args[-1] = args[-1] - 1
m = midi_message(mtype, *args)
if m == None:
ipc_send('log','(ERROR, MIDI) could not convert osc to midi (%s %s)' % (event, " ".join([str(x) for x in args])))
else:
outputs[name].send_message(m)
if debug:
ipc_send('log','(DEBUG, MIDI) out: %s To: midi:%s' % (midi_str(m), name))
while True:
try:
msg = raw_input()
except:
break
try:
msg = JSON.loads(msg)
msg[1] = msg[1].lower()
send_midi(*msg)
except:
ipc_send('log', '(ERROR, MIDI) %s' % traceback.format_exc())