-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (79 loc) · 2.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Jupiter XM CC to internal Roland SysEx
"""
import adafruit_midi
import adafruit_midi.control_change
import adafruit_midi.system_exclusive
import usb_midi
import busio
import board
from time import sleep
from adafruit_midi.control_change import ControlChange
from adafruit_midi.system_exclusive import SystemExclusive
from constants import (
ROLAND_MANUFACTURER_CODE,
JUPITER_XM_DEVICE_ID,
DT1_CODE,
MIDI_BAUD_RATE,
MIDI_TIMEOUT,
TONE_PARTIAL_CUTOFF_FREQ,
ZC_TONE_PARTIAL_1,
ZCORE_1,
)
from jupiterlib import RelativeParameter, build_parameter_address
from midiconfig import MidiConfig
from midistate import MidiState
def dbg(*args, **kwargs):
if MidiConfig.LOG_DEBUG:
print(*args, **kwargs)
uart = busio.UART(board.TX, board.RX, baudrate=MIDI_BAUD_RATE, timeout=MIDI_TIMEOUT)
dbg("UART is ready", uart.baudrate)
def setup_midi_listeners():
uart_midi = adafruit_midi.MIDI(
midi_in=uart, midi_out=uart, debug=MidiConfig.MIDI_DEBUG
)
usb_amidi = adafruit_midi.MIDI(
midi_in=usb_midi.ports[0],
midi_out=usb_midi.ports[1],
debug=MidiConfig.MIDI_DEBUG,
)
if MidiConfig.UART_MIDI_IN:
MidiState.midi_receivers.append(uart_midi)
if MidiConfig.USB_MIDI_IN:
MidiState.midi_receivers.append(usb_amidi)
if MidiConfig.UART_MIDI_OUT:
MidiState.midi_emitters.append(uart_midi)
if MidiConfig.USB_MIDI_OUT:
MidiState.midi_emitters.append(usb_amidi)
def handle_midi(message: adafruit_midi.MIDIMessage = None):
if message is None:
return
if message.__class__ == adafruit_midi.control_change.ControlChange:
channel, cc_code, value = message.channel, message.control, message.value
for handler in MidiState.parameter_handlers.get((cc_code, channel), []):
handler.change(value)
elif message.__class__ == adafruit_midi.system_exclusive.SystemExclusive:
print([hex(a) for a in message.data])
if message.data[5] == 0x12:
addr = tuple(list(message.data[6:10]))
print("addr", [a for a in addr], MidiState.rq1_handlers)
for client in MidiState.rq1_handlers.get(addr, []):
client.read_rq1(message)
# setup_usb_midi()
setup_midi_listeners()
mr = MidiState.midi_receivers
cb = MidiState.callbacks
sleep(1)
rel_test = RelativeParameter(
110,
10,
build_parameter_address(ZCORE_1, ZC_TONE_PARTIAL_1, TONE_PARTIAL_CUTOFF_FREQ),
0,
1023,
4,
multiplicator=4,
)
while True:
for m in mr:
handle_midi(m.receive())
for c in cb:
c.send()