From 885886d0c99be14e7170a7f7dd1e23d62be28e8d Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 31 Jan 2023 16:09:57 +0100 Subject: [PATCH] midi: optional nrpn/rpn packing/unpacking --- src/python/head.py | 2 ++ src/python/midi.py | 30 ++++++++++++++++++++++++++++-- src/python/utils.py | 7 +++++++ src/server/options.js | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/python/head.py b/src/python/head.py index 6bf01d90..be39bd42 100644 --- a/src/python/head.py +++ b/src/python/head.py @@ -42,6 +42,8 @@ program_change_offset = 'pc_offset' in argv ignore_sysex = 'sysex' not in argv # option: parse mtc ignore_mtc = 'mtc' not in argv +# option: parse (n)rpn +ignore_rpn = 'rpn' not in argv and 'nrpn' not in argv # option: parse active sensing ignore_active_sensing = 'active_sensing' not in argv # option: note off velocity diff --git a/src/python/midi.py b/src/python/midi.py index 825b846a..1fcd36f7 100644 --- a/src/python/midi.py +++ b/src/python/midi.py @@ -2,6 +2,7 @@ from head import * from list import * from utils import * from mtc import * +from rpn import * ipc_send('version', '1.17.1') @@ -130,6 +131,22 @@ def create_callback(name): v = ' '.join([hex(x).replace('0x', '').zfill(2) for x in message]) osc['args'].append({'type': 'string', 'value': v}) + + elif mtype == CONTROL_CHANGE and not ignore_rpn and is_rpn(message): + rpn = rpn_decode(message, name) + if rpn is not None: + osc['address'] = MIDI_TO_OSC[rpn[0]] + osc['args'].append({'type': 'i', 'value': rpn[1]}) + osc['args'].append({'type': 'i', 'value': rpn[2]}) + osc['args'].append({'type': 'i', 'value': rpn[3]}) + ipc_send('osc', osc) + if debug: + t = 'NRPN' if rpn[0] == NRPN else 'RPN' + s = '%s: channel=%i, parameter=%i, value=%i'% (t, rpn[1], rpn[2], rpn[3]) + ipc_send('debug','in: %s From: midi:%s' % (s, name)) + # bypass (n)rpn CCs + return + else: status = message[0] @@ -232,6 +249,16 @@ def send_midi(name, event, *args): except: pass + elif mtype == NRPN or mtype == RPN: + m = rpn_encode(mtype, args) + if m is not None: + for x in m: + outputs[name].send_message(x) + if debug: + t = 'NRPN' if mtype == NRPN else 'RPN' + s = '%s: channel=%i, parameter=%i, value=%i'% (t, args[0], args[1], args[2]) + ipc_send('debug','out: %s To: midi:%s' % (s, name)) + return else: args = [int(round(x)) for x in args] @@ -242,7 +269,7 @@ def send_midi(name, event, *args): mtype = NOTE_OFF elif mtype == PITCH_BEND: - args = args[:1] + [args[1] & 0x7F, (args[1] >> 7) & 0x7F] # convert 0-16384 -> 0-127 pair + args = args[:1] + [args[1] & 0x7F, (args[1] >> 7) & 0x7F] # convert 0-16383 -> 0-127 pair elif mtype == PROGRAM_CHANGE and program_change_offset: args[-1] = args[-1] - 1 @@ -256,7 +283,6 @@ def send_midi(name, event, *args): else: outputs[name].send_message(m) - if debug: ipc_send('debug','out: %s To: midi:%s' % (midi_str(m, name), name)) diff --git a/src/python/utils.py b/src/python/utils.py index b911ff1d..951f7d4c 100644 --- a/src/python/utils.py +++ b/src/python/utils.py @@ -1,5 +1,6 @@ from head import * from mtc import * +from rpn import * MIDI_TO_OSC = { NOTE_ON: '/note', @@ -28,6 +29,12 @@ OSC_TO_MIDI = { if note_off_velocity: MIDI_TO_OSC[NOTE_OFF] = '/note_off' +if not ignore_rpn: + MIDI_TO_OSC[NRPN] = '/nrpn' + MIDI_TO_OSC[RPN] = '/rpn' + OSC_TO_MIDI['/nrpn'] = NRPN + OSC_TO_MIDI['/rpn'] = RPN + def midi_str(message, port): diff --git a/src/server/options.js b/src/server/options.js index eacae2a6..1071d752 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|mtc|active_sensing|note_off_velocity|pc_offset|path=(.*)|[^:]+:(virtual|[^,]+,[^,]+))$/)) + var err = o.filter(item=>!item.match(/^(list|debug|jack|sysex|mtc|rpn|nrpn|active_sensing|note_off_velocity|pc_offset|path=(.*)|[^:]+:(virtual|[^,]+,[^,]+))$/)) return err.length ? `Invalid option${err.length > 1 ? 's' : ''}: ${err.join(', ')}` : true } },