From c91afb6dab83102954ab941d0e3da6a85beeeb70 Mon Sep 17 00:00:00 2001 From: bordun Date: Mon, 21 Sep 2020 13:25:43 +0200 Subject: [PATCH] midi: remove variable args in sysex messages (can be handled many ways now); allow sending any system message; add support for receiving mtc (as sysex); add support for sending sysex bytes as integers --- CHANGELOG.md | 7 +++++ .../docs/docs/midi/midi-configuration.md | 4 +++ resources/docs/docs/midi/midi-messages.md | 9 ++++-- src/python/midi.py | 31 ++++++++----------- src/python/utils.py | 3 ++ src/server/options.js | 2 +- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93774908..6687808f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ - editor: hide impossible actions from context-menu (eg adding widgets in tab containers) - panel: layout issue with tabs & lineWidth property +- midi + - remove variable args in sysex messages (dynamic properties and scripting can be used to this effect) + - add support for sending sysex bytes as integers + - allow sending any system message (eg MIDI time code) + - add support for receiving MIDI time code (as raw sysex) + + ## 1.2.1 - scripting diff --git a/resources/docs/docs/midi/midi-configuration.md b/resources/docs/docs/midi/midi-configuration.md index 0ff446d6..7f040302 100644 --- a/resources/docs/docs/midi/midi-configuration.md +++ b/resources/docs/docs/midi/midi-configuration.md @@ -30,6 +30,10 @@ Create a virtual MIDI device that will translate OSC messsages to MIDI messages Enable parsing of system exclusive messages (disabled by default). +**`mtc`** + +Enable parsing of midi time code messages (disabled by default). These will be received as raw sysex messages. + **`pc_offset`** Send program changes with a `-1` offset to match some software/hardware implementations diff --git a/resources/docs/docs/midi/midi-messages.md b/resources/docs/docs/midi/midi-messages.md index 92692d77..11ce6265 100644 --- a/resources/docs/docs/midi/midi-messages.md +++ b/resources/docs/docs/midi/midi-messages.md @@ -63,12 +63,15 @@ PitchWheel event. ---- -#### `/sysex msg v1 .. vN` +#### `/sysex msg` 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](/docs/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. +- `msg`: hexadecimal sysex data string of the form `f0 ... f7`. + +Other system messages may be sent (midi time code for example: `f1 `). Instead of a single hexadecimal string, arguments can be separated and defined either as hexadecimal bytes or as integers. When received, system messages are always formatted as a single hexadecimal string. + + ---- diff --git a/src/python/midi.py b/src/python/midi.py index 8bd9e149..5d0f720a 100644 --- a/src/python/midi.py +++ b/src/python/midi.py @@ -153,9 +153,10 @@ for name in inputs: inputs[name].set_callback(create_callback(name)) - # Activate sysex support - if 'sysex' in argv: - inputs[name].ignore_types(False, True, True) + # 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): @@ -189,22 +190,16 @@ def send_midi(name, event, *args): if mtype == SYSTEM_EXCLUSIVE: - # 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. try: - midiBytes = args[0].replace(' ', '') - i = 1 - for m in sysexRegex.finditer(midiBytes): - midiBytes = midiBytes[:m.start()] + args[i].replace(' ', '') + midiBytes[m.end():] - i += 1 - - msg = unhexlify(midiBytes) - - if (msg and msg.startswith(b'\xF0') and msg.endswith(b'\xF7') and - all((val <= 0x7F for val in msg[1:-1]))): - m = msg + m = [] + for arg in args: + if type(arg) is str: + arg = arg.split(' ') + arg = [int(x, 16) for x in arg] + for x in arg: + m.append(x) + else: + m.append(int(arg)) except: pass diff --git a/src/python/utils.py b/src/python/utils.py index 81a8e518..1ab479fc 100644 --- a/src/python/utils.py +++ b/src/python/utils.py @@ -11,6 +11,9 @@ MIDI_TO_OSC = { POLY_PRESSURE: '/key_pressure' } +for i in range(SYSTEM_EXCLUSIVE + 1, 256): + MIDI_TO_OSC[i] = '/sysex' + OSC_TO_MIDI = { '/note': NOTE_ON, '/control': CONTROL_CHANGE, diff --git a/src/server/options.js b/src/server/options.js index c667ce4c..0f265e28 100644 --- a/src/server/options.js +++ b/src/server/options.js @@ -51,7 +51,7 @@ module.exports = { }, 'm': {alias: 'midi', type: 'array', describe: 'midi router settings', check: (o)=>{ - var err = o.filter(item=>!item.match(/^(list|debug|jack|sysex|pc_offset|path=(.*)|[^:]+:(virtual|[^,]+,[^,]+))$/)) + var err = o.filter(item=>!item.match(/^(list|debug|jack|sysex|mtc|pc_offset|path=(.*)|[^:]+:(virtual|[^,]+,[^,]+))$/)) return err.length ? `Invalid option${err.length > 1 ? 's' : ''}: ${err.join(', ')}` : true } },