From 6a6df458cab4e0a3efe52c33a52c73bdcb274de9 Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Sun, 3 Sep 2017 13:09:48 +0200 Subject: [PATCH 1/5] Add the possibility to send MIDI sysex messages --- app/main/midi.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/main/midi.py b/app/main/midi.py index ad0fd3be..14f74f9a 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 unhexlify import traceback import json as JSON +import re try: @@ -145,6 +147,27 @@ for name in inputs: inputs[name].setCallback(createCallbackFunction(name)) +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: @@ -167,6 +190,23 @@ 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 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. + 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():] + i += 1 + + if debug: + ipcSend('log', 'Sysex string: %s' % midiBytes) + + m = rtmidi.MidiMessage(unhexlify(midiBytes)) + outputs[name].sendMessage(m) if debug: From cf30c9fb024d9187de3ce8b3b769763c91918333 Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Sun, 3 Sep 2017 13:31:44 +0200 Subject: [PATCH 2/5] Add sysex receive support, and align sysex send --- app/main/midi.py | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) 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: From 0daa7d6daff31e4fc62d07aec80d7ab0e375addb Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Sun, 3 Sep 2017 13:47:04 +0200 Subject: [PATCH 3/5] Add MIDI sysex example --- app/examples/sysex_device_inquiry.js | 74 ++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 app/examples/sysex_device_inquiry.js diff --git a/app/examples/sysex_device_inquiry.js b/app/examples/sysex_device_inquiry.js new file mode 100644 index 00000000..2e1e1958 --- /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:virtual" + ], + "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 + } +] \ No newline at end of file From 0310a008cb8125b488c6b552b7a316f8ec422515 Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Sun, 3 Sep 2017 17:57:57 +0200 Subject: [PATCH 4/5] Add MIDI sysex documentation --- resources/docs/extras/midi.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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. From 7450aacafa5dc68e99a80f3e252b03b88b49c708 Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Sun, 3 Sep 2017 17:58:36 +0200 Subject: [PATCH 5/5] Change MIDI device name to avoid confusion --- app/examples/sysex_device_inquiry.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/examples/sysex_device_inquiry.js b/app/examples/sysex_device_inquiry.js index 2e1e1958..e4f2e1cf 100644 --- a/app/examples/sysex_device_inquiry.js +++ b/app/examples/sysex_device_inquiry.js @@ -33,7 +33,7 @@ "precision": 0, "address": "/sysex", "target": [ - "midi:virtual" + "midi:osc" ], "preArgs": [], "off": 0 @@ -71,4 +71,4 @@ "scroll": true, "label": false } -] \ No newline at end of file +]