|
23 | 23 | import threading |
24 | 24 | from chunk import Chunk |
25 | 25 | import struct |
26 | | -import rtmidi_python as rtmidi |
| 26 | +try: |
| 27 | + import rtmidi_python as rtmidi |
| 28 | + RTMIDI = False |
| 29 | +except ModuleNotFoundError: |
| 30 | + import rtmidi |
| 31 | + RTMIDI = True |
27 | 32 | import samplerbox_audio |
28 | 33 |
|
29 | 34 | ######################################### |
@@ -165,7 +170,18 @@ def AudioCallback(outdata, frame_count, time_info, status): |
165 | 170 | b *= globalvolume |
166 | 171 | outdata[:] = b.reshape(outdata.shape) |
167 | 172 |
|
168 | | -def MidiCallback(message, time_stamp): |
| 173 | + |
| 174 | +def midi_callback(message, data=None): |
| 175 | + message, deltatime = message |
| 176 | + |
| 177 | + return midi_callback_common(message, None) |
| 178 | + |
| 179 | + |
| 180 | +def MidiCallback(message, timestamp): |
| 181 | + return midi_callback_common(message, timestamp) |
| 182 | + |
| 183 | + |
| 184 | +def midi_callback_common(message, timestamp): |
169 | 185 | global playingnotes, sustain, sustainplayingnotes |
170 | 186 | global preset |
171 | 187 | messagetype = message[0] >> 4 |
@@ -424,14 +440,27 @@ def MidiSerialCallback(): |
424 | 440 | # MAIN LOOP |
425 | 441 | ######################################### |
426 | 442 |
|
427 | | -midi_in = [rtmidi.MidiIn(b'in')] |
428 | | -previous = [] |
429 | | -while True: |
430 | | - for port in midi_in[0].ports: |
431 | | - if port not in previous and b'Midi Through' not in port: |
432 | | - midi_in.append(rtmidi.MidiIn(b'in')) |
433 | | - midi_in[-1].callback = MidiCallback |
434 | | - midi_in[-1].open_port(port) |
435 | | - print('Opened MIDI: ' + str(port)) |
436 | | - previous = midi_in[0].ports |
437 | | - time.sleep(2) |
| 443 | +if RTMIDI: |
| 444 | + midi_in = [rtmidi.MidiIn()] |
| 445 | + previous = [] |
| 446 | + while True: |
| 447 | + for num_port, port in enumerate(midi_in[0].get_ports()): |
| 448 | + if port not in previous and 'Midi Through' not in port: |
| 449 | + midi_in.append(rtmidi.MidiIn()) |
| 450 | + midi_in[-1].set_callback(midi_callback) |
| 451 | + midi_in[-1].open_port(num_port) |
| 452 | + print('Opened MIDI: ' + str(port)) |
| 453 | + previous = midi_in[0].get_ports() |
| 454 | + time.sleep(2) |
| 455 | +else: |
| 456 | + midi_in = [rtmidi.MidiIn(b'in')] |
| 457 | + previous = [] |
| 458 | + while True: |
| 459 | + for port in midi_in[0].ports: |
| 460 | + if port not in previous and b'Midi Through' not in port: |
| 461 | + midi_in.append(rtmidi.MidiIn(b'in')) |
| 462 | + midi_in[-1].callback = MidiCallback |
| 463 | + midi_in[-1].open_port(port) |
| 464 | + print('Opened MIDI: ' + str(port)) |
| 465 | + previous = midi_in[0].ports |
| 466 | + time.sleep(2) |
0 commit comments