midi: dry & add some helpful debug messages

This commit is contained in:
jean-emmanuel 2019-04-04 19:18:35 +02:00
parent 00e2d6ad66
commit 38eed39d7d
2 changed files with 77 additions and 41 deletions

View file

@ -1,5 +1,6 @@
from head import *
from list import *
from utils import *
if 'list' in argv:
list()
@ -51,18 +52,9 @@ for arg in argv:
ipc_send('error', 'can\'t connect to output port %i: %s' % (out_port, out_dev.get_port_name(out_port)))
MIDI_TO_OSC = {
NOTE_ON: '/note',
NOTE_OFF: '/note',
CONTROL_CHANGE: '/control',
PROGRAM_CHANGE: '/program',
PITCH_BEND: '/pitch',
SYSTEM_EXCLUSIVE: '/sysex'
}
def create_callback(name):
def callback(event, data):
def receive_midi(event, data):
osc = {}
osc['args'] = []
@ -98,13 +90,13 @@ def create_callback(name):
ipc_send('osc', osc)
if debug:
ipc_send('log','MIDI received: %s' % message)
ipc_send('log','MIDI received: %s' % midi_str(message))
def callback_error_wrapper(event, data):
try:
callback(event, data)
receive_midi(event, data)
except:
ipc_send('log', 'ERROR: MIDI: %s' % traceback.format_exc())
@ -140,30 +132,16 @@ def send_midi(name, event, *args):
ipc_send('log','ERROR: MIDI: unknown output (%s)' % name)
return
if event not in OSC_TO_MIDI:
ipc_send('log','ERROR: MIDI: unknown output (%s)' % name)
return
m = None
if 'note' in event and len(args) == 3:
args = [int(round(x)) for x in args]
if args[2] > 0:
m = midi_message(NOTE_ON, args[0], args[1], args[2])
else:
m = midi_message(NOTE_OFF, args[0], args[1])
mtype = OSC_TO_MIDI[event]
elif 'control' in event and len(args) == 3:
args = [int(round(x)) for x in args]
m = midi_message(CONTROL_CHANGE, *args)
if mtype is SYSTEM_EXCLUSIVE:
elif 'program' in event and len(args) == 2:
args = [int(round(x)) for x in args]
m = midi_message(PROGRAM_CHANGE, *args)
elif 'pitch' in event and len(args) == 2:
args = [int(round(x)) for x in args]
channel = args[0]
value = args[1]
m = midi_message(PITCH_BEND, channel, value & 0x7f, (value >> 7) & 0x7f)
elif 'sysex' in event:
# 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
@ -182,6 +160,20 @@ def send_midi(name, event, *args):
else:
ipc_send('log', 'ERROR: MIDI: Invalid sysex string: %s' % msg)
else:
args = [int(round(x)) for x in args]
m = [mtype, args[0]]
if mtype is NOTE_ON:
if args[2] is 0:
mtype = NOTE_OFF
elif mtype is PITCH_BEND:
args = args[:1] + [args[1] & 0x7f, (args[1] >> 7) & 0x7f] # convert 0-16384 -> 0-127 pair
m = midi_message(mtype, *args)
if m is None:
ipc_send('log','ERROR: MIDI: could not convert osc args to midi message (%s %s)' % (event, " ".join([str(x) for x in args])))
@ -192,15 +184,7 @@ def send_midi(name, event, *args):
if debug:
if 'sysex' in event:
repr = 'sysex: %s' % [int(x) for x in m]
else:
repr = 'status=%s' % m[0]
repr += ', data1=%s' % m[1]
if len(m)==3:
repr += ', data2=%s' % m[2]
ipc_send('log','MIDI sent: %s' % repr)
ipc_send('log','MIDI sent: %s' % midi_str(m))
while True:

52
src/python/utils.py Normal file
View file

@ -0,0 +1,52 @@
from head import *
MIDI_TO_OSC = {
NOTE_ON: '/note',
NOTE_OFF: '/note',
CONTROL_CHANGE: '/control',
PROGRAM_CHANGE: '/program',
PITCH_BEND: '/pitch',
SYSTEM_EXCLUSIVE: '/sysex'
}
OSC_TO_MIDI = {
'/note': NOTE_ON,
'/control': CONTROL_CHANGE,
'/program': PROGRAM_CHANGE,
'/pitch': PITCH_BEND,
'/sysex': SYSTEM_EXCLUSIVE
}
def midi_str(message):
mtype = message[0] & 0xF0
s = 'UNKNOWN'
if mtype is SYSTEM_EXCLUSIVE:
s = 'SYSTEM_EXCLUSIVE: sysex=%s' % ' '.join([hex(x).replace('0x', '').zfill(2) for x in message])
else:
status = message[0]
channel = (status & 0xF) + 1
try:
if mtype is NOTE_ON:
s = 'NOTE_ON: channel=%i, note=%i, velocity=%i' % (channel, message[1], message[2])
elif mtype is NOTE_OFF:
s = 'NOTE_OFF: channel=%i, note=%i' % (channel, message[1])
elif mtype is CONTROL_CHANGE:
s = 'CONTROL_CHANGE: channel=%i, cc=%i, value=%i'% (channel, message[1], message[2])
elif mtype is PROGRAM_CHANGE:
s = 'PROGRAM_CHANGE: channel=%i, program=%i' % (channel, message[1])
elif mtype is PITCH_BEND:
print(channel, message)
s = 'PITCH_BEND: channel=%i, pitch=%i' % (channel, message[1] + message[2] * 128)
except IndexError:
s = 'NONE (ERROR: wrong number of argument for %s)' % MIDI_TO_OSC[mtype]
return s