diff --git a/app/main/midi.py b/app/main/midi.py index 14f74f9a..cad180a8 100644 --- a/app/main/midi.py +++ b/app/main/midi.py @@ -1,5 +1,5 @@ from sys import argv, stdin, stdout, version_info -from binascii import unhexlify +from binascii import hexlify, unhexlify import traceback import json as JSON import re @@ -110,24 +110,31 @@ def createCallbackFunction(name): osc['args'].append({'type': 'i', 'value': midi.getNoteNumber()}) osc['args'].append({'type': 'i', 'value': midi.getVelocity()}) - if midi.isNoteOff(): + elif midi.isNoteOff(): osc['address'] = '/note' osc['args'].append({'type': 'i', 'value': midi.getNoteNumber()}) osc['args'].append({'type': 'i', 'value': 0}) - if midi.isController(): + elif midi.isController(): osc['address'] = '/control' osc['args'].append({'type': 'i', 'value': midi.getControllerNumber()}) osc['args'].append({'type': 'i', 'value': midi.getControllerValue()}) - if midi.isProgramChange(): + elif midi.isProgramChange(): osc['address'] = '/program' osc['args'].append({'type': 'i', 'value': midi.getProgramChangeNumber()}) - if midi.isPitchWheel(): + 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: @@ -146,28 +153,11 @@ def createCallbackFunction(name): 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 midify(value, noOfBytes): - # Convert value into noOfBytes MIDI bytes. Given a length of 2 bytes, the lower - # 7bit of an 8bit value will go into the first byte, and the most-significant - # bit will go into the second byte (with the left-most bit being 0 in each byte). - # For example, 200 (11001000) will be converted into 48 40 (01001000 01000000). - # TODO: Please note that there is no standard conversion. This specific conversion - # may only be relevant for certain manufacturers (Keith McMillen, for example). - currValue = int(value) - result = [] - - for i in range(0, noOfBytes): - if i % 2 == 0: - result.append(format(currValue & 0x7f, '02x')) - else: - result.append(format((currValue & 0x80) >> 1, '02x')) - currValue >>= 8 - - return ''.join(result) - def sendMidi(name, event, *args): if name not in outputs: @@ -193,13 +183,12 @@ def sendMidi(name, event, *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 args[0] to be a string of the form 'f0 7e 7f 06 01 f7', and - # args[1..n] to be integer values. Strings of the form 'nn' in args[0] are - # then replaced using midified args[1..n] to create the final data string. + # 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()] + midify(args[i], (m.end() - m.start()) // 2) + midiBytes[m.end():] + midiBytes = midiBytes[:m.start()] + args[i].replace(' ', '') + midiBytes[m.end():] i += 1 if debug: