diff --git a/app/examples/sysex_device_inquiry.js b/app/examples/sysex_device_inquiry.js new file mode 100644 index 00000000..e4f2e1cf --- /dev/null +++ b/app/examples/sysex_device_inquiry.js @@ -0,0 +1,74 @@ +[ + { + "type": "root", + "tabs": [ + { + "type": "tab", + "id": "tab_1", + "label": "Sysex Example", + "color": "auto", + "css": "", + "layout": "", + "spacing": 0, + "value": "", + "precision": 0, + "address": "/tab_1", + "preArgs": [], + "target": [], + "variables": "@{parent.variables}", + "widgets": [ + { + "type": "push", + "top": 10, + "left": 20, + "id": "push_1", + "linkId": "", + "width": 200, + "height": "auto", + "label": "Send Device Inquiry", + "color": "auto", + "css": "", + "on": "f0 7e 7f 06 01 f7", + "norelease": true, + "precision": 0, + "address": "/sysex", + "target": [ + "midi:osc" + ], + "preArgs": [], + "off": 0 + }, + { + "type": "text", + "top": 80, + "left": 20, + "id": "text_1", + "width": 400, + "height": 60, + "label": "Device Inquiry Response", + "color": "auto", + "css": "", + "widgetId": "", + "vertical": false, + "preArgs": [], + "address": "/sysex", + "value": "" + } + ], + "tabs": [], + "scroll": true + } + ], + "color": "auto", + "css": "", + "value": "", + "precision": 0, + "address": "/root", + "preArgs": [], + "target": [], + "variables": {}, + "id": "root", + "scroll": true, + "label": false + } +] diff --git a/app/main/midi.py b/app/main/midi.py index ad0fd3be..cad180a8 100644 --- a/app/main/midi.py +++ b/app/main/midi.py @@ -1,6 +1,8 @@ from sys import argv, stdin, stdout, version_info +from binascii import hexlify, unhexlify import traceback import json as JSON +import re try: @@ -108,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: @@ -144,6 +153,10 @@ 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 sendMidi(name, event, *args): @@ -167,6 +180,22 @@ def sendMidi(name, event, *args): elif 'pitch' in event and len(args) == 2: 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)) + outputs[name].sendMessage(m) if debug: diff --git a/resources/docs/extras/midi.md b/resources/docs/extras/midi.md index 00a2155b..50e78c26 100644 --- a/resources/docs/extras/midi.md +++ b/resources/docs/extras/midi.md @@ -76,3 +76,12 @@ PitchWheel event. - `channel`: integer between 1 and 16 - `pitch`: integer between 0 and 16383 + +### `/sysex msg v1 .. vN` + +System exclusive message. + +- `msg`: hexadecimal sysex data string of the form `f0 ... f7`. You may include placeholders of the form `nn` which will be replaced by `v1, .., vN` respectively. +- `v1, .., vN`: values encoded as hexadecimal data strings to include in `msg`. Most probably, you will need to sepcify a [custom module](/extras/custom-module/) in order to convert numeric widget values into the required hexadecimal format. In general, this conversion will be different for each manufacturer / device. + +For a very simple example, refer to session 'sysex_device_inquiry.js' found in the application's sub folder 'resources/app/examples/'. Please remember to adjust the button's `target` to the `device_name` used in your MIDI setup.