midi: mtc support

This commit is contained in:
bordun 2020-11-18 23:37:16 +01:00
parent ea641956bf
commit 6a137df9a7
5 changed files with 115 additions and 23 deletions

View file

@ -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:

57
src/python/mtc.py Normal file
View file

@ -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]

View file

@ -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])