From 6a137df9a758cdf2396355d844d2aa00950c6e19 Mon Sep 17 00:00:00 2001 From: bordun Date: Wed, 18 Nov 2020 23:37:16 +0100 Subject: [PATCH] midi: mtc support --- CHANGELOG.md | 3 +- resources/docs/docs/midi/midi-messages.md | 11 ++++- src/python/midi.py | 55 ++++++++++++++-------- src/python/mtc.py | 57 +++++++++++++++++++++++ src/python/utils.py | 12 ++++- 5 files changed, 115 insertions(+), 23 deletions(-) create mode 100644 src/python/mtc.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 774fb829..a5408480 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ As of this version, packages except the `node` package are bundled with a midi b - server: provide readable error when a file requested by the client file is not found - tab: content not drawn when changing visible property - matrix: addresses not generated property when matrix' address is `auto` - + - ui - add console bottom panel with a simple script interpreter @@ -34,6 +34,7 @@ As of this version, packages except the `node` package are bundled with a midi b - midi - accept sending sysex strings without spaces between the bytes - load prebuilt midi binary on 64bit linux/windows/osx + - add support for midi time code messages - misc - sessions converted from v0 will use the widget's html property to display the former label property diff --git a/resources/docs/docs/midi/midi-messages.md b/resources/docs/docs/midi/midi-messages.md index a79cee7e..393218a1 100644 --- a/resources/docs/docs/midi/midi-messages.md +++ b/resources/docs/docs/midi/midi-messages.md @@ -69,9 +69,18 @@ System exclusive message. - `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. +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. + +---- + +#### `/mtc timecode` + +MIDI time code message. + +- `timecode`: `hh:mm:ss:ff` string. +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 940607b0..7a4320c9 100644 --- a/src/python/midi.py +++ b/src/python/midi.py @@ -1,6 +1,7 @@ from head import * from list import * from utils import * +from mtc import * ipc_send('version', '1.7.0') @@ -13,6 +14,10 @@ if 'list-only' in argv: # option: act as if displayed program is between 1-128 instead of 0-127 PROGRAM_CHANGE_OFFSET = 'pc_offset' in argv +# option: parse sysex +ignore_sysex = 'sysex' not in argv +# option: parse mtc +ignore_mtc = 'mtc' not in argv inputs = {} outputs = {} @@ -108,7 +113,16 @@ def create_callback(name): osc['address'] = MIDI_TO_OSC[mtype] - if mtype == SYSTEM_EXCLUSIVE: + if not ignore_mtc and is_mtc(message): + + mtc = mtc_decode(message, name) + if mtc is None: + return + else: + osc['address'] = MIDI_TO_OSC[MIDI_TIME_CODE] + osc['args'].append({'type': 'string', 'value': mtc}) + + elif mtype == SYSTEM_EXCLUSIVE: # Parse the provided data into a hex MIDI data string of the form 'f0 7e 7f 06 01 f7'. v = ' '.join([hex(x).replace('0x', '').zfill(2) for x in message]) osc['args'].append({'type': 'string', 'value': v}) @@ -134,7 +148,7 @@ def create_callback(name): if debug: - ipc_send('log','(DEBUG, MIDI) in: %s From: midi:%s' % (midi_str(message), name)) + ipc_send('log','(DEBUG, MIDI) in: %s From: midi:%s' % (midi_str(message, name), name)) ipc_send('osc', osc) @@ -154,8 +168,6 @@ for name in inputs: inputs[name].set_callback(create_callback(name)) # sysex / mtc support - ignore_sysex = 'sysex' not in argv - ignore_mtc = 'mtc' not in argv inputs[name].ignore_types(ignore_sysex, ignore_mtc, True) @@ -188,22 +200,27 @@ def send_midi(name, event, *args): mtype = OSC_TO_MIDI[event] - if mtype == SYSTEM_EXCLUSIVE: + if mtype == SYSTEM_EXCLUSIVE or mtype == MIDI_TIME_CODE: - try: - m = [] - for arg in args: - if type(arg) is str: - arg = arg.replace(' ', '') # remove spaces - arg = [arg[i:i+2] for i in range(0, len(arg), 2)] # split in 2 chars bytes - arg = [int(x, 16) for x in arg] # parse hex bytes - for x in arg: - m.append(x) - else: - m.append(int(arg)) + if mtype == MIDI_TIME_CODE: - except: - pass + m = mtc_encode(args) + + else: + try: + m = [] + for arg in args: + if type(arg) is str: + arg = arg.replace(' ', '') # remove spaces + arg = [arg[i:i+2] for i in range(0, len(arg), 2)] # split in 2 chars bytes + arg = [int(x, 16) for x in arg] # parse hex bytes + for x in arg: + m.append(x) + else: + m.append(int(arg) & 0x7F) + + except: + pass else: @@ -231,7 +248,7 @@ def send_midi(name, event, *args): outputs[name].send_message(m) if debug: - ipc_send('log','(DEBUG, MIDI) out: %s To: midi:%s' % (midi_str(m), name)) + ipc_send('log','(DEBUG, MIDI) out: %s To: midi:%s' % (midi_str(m, name), name)) while True: diff --git a/src/python/mtc.py b/src/python/mtc.py new file mode 100644 index 00000000..69f5426a --- /dev/null +++ b/src/python/mtc.py @@ -0,0 +1,57 @@ +from head import * + + +def is_mtc(message): + + return message[0] == MIDI_TIME_CODE or (message[0] == SYSTEM_EXCLUSIVE and len(message) == 10 and message[1:5] == [0x7F, 0x7F, 0x01, 0x01]) + + +# quarter frame accumulator +mtc = {} + +def mtc_decode(message, port): + # with bits ©jeffmikels @ https://github.com/jeffmikels/timecode_tools/blob/master/tools.py (MIT) + + if message[0] == MIDI_TIME_CODE: + + piece = (message[1] >> 4) & 0xF + data = message[1] & 0xF + + if port not in mtc or mtc[port][4] == 8: + # 4th item is a piece counter + mtc[port] = [0, 0, 0, 0, 0] + + mtc_index = 3 - piece // 2 + if piece % 2 == 0: + # 'even' pieces came from the low nibble + mtc[port][mtc_index] += data + else: + # 'odd' pieces came from the high nibble + mtc[port][mtc_index] += data * 16 + + mtc[port][4] += 1 + + if mtc[port][4] == 8 and (piece == 1 or piece == 7): + mtc_bytes = mtc[port][0:4] + else: + return None + + else: + + mtc_bytes = message[5:-1] + mtc_bytes[0] = mtc_bytes[0] & 31 + + return ":".join([str(x).zfill(2) for x in mtc_bytes]) + +def mtc_encode(args): + + if len(args) != 1: + return None + + timecode = args[0] + data = str(timecode).split(':') + + if len(data) != 4: + return None + else: + return [0xF0, 0x7F, 0x7F, 0x01, 0x01] + [int(x) & 0x7F for x in data] + [0xF7] diff --git a/src/python/utils.py b/src/python/utils.py index 56fde861..70602a7d 100644 --- a/src/python/utils.py +++ b/src/python/utils.py @@ -1,4 +1,5 @@ from head import * +from mtc import * MIDI_TO_OSC = { NOTE_ON: '/note', @@ -7,6 +8,7 @@ MIDI_TO_OSC = { PROGRAM_CHANGE: '/program', PITCH_BEND: '/pitch', SYSTEM_EXCLUSIVE: '/sysex', + MIDI_TIME_CODE: '/mtc', CHANNEL_PRESSURE: '/channel_pressure', POLY_PRESSURE: '/key_pressure' } @@ -17,16 +19,22 @@ OSC_TO_MIDI = { '/program': PROGRAM_CHANGE, '/pitch': PITCH_BEND, '/sysex': SYSTEM_EXCLUSIVE, + '/mtc': MIDI_TIME_CODE, '/channel_pressure': CHANNEL_PRESSURE, '/key_pressure': POLY_PRESSURE } -def midi_str(message): +def midi_str(message, port): mtype = message[0] & 0xF0 s = 'UNKNOWN' - if mtype == SYSTEM_EXCLUSIVE: + + if is_mtc(message): + + s = 'MTC: timecode: %s' % mtc_decode(message, None) + + elif mtype == SYSTEM_EXCLUSIVE: s = 'SYSTEM_EXCLUSIVE: sysex=%s' % ' '.join([hex(x).replace('0x', '').zfill(2) for x in message])