-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidiController_example.py
More file actions
39 lines (32 loc) · 1.32 KB
/
Copy pathMidiController_example.py
File metadata and controls
39 lines (32 loc) · 1.32 KB
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
# install "usb-device-midi" using Tools > Manage packages...
import usb.device
from usb.device.midi import MIDIInterface
import time
# global variables
channel = 0
controller = 64
control_val = 0
note = 60
velocity_min = 0
velocity_max = 127
# instantiate the MIDI interface
midi = MIDIInterface()
# set builtin_driver=False if you don't want the MicroPython serial REPL available.
usb.device.get().init(midi, builtin_driver=True)
while not midi.is_open():
time.sleep_ms(100)
while midi.is_open():
midi.note_on(channel, note, velocity_max)
time.sleep(0.5)
midi.note_off(channel, note, velocity_min)
time.sleep(1)
midi.control_change(channel, controller, control_val)
control_val += 1
if control_val == 0x7F:
control_val = 0
time.sleep(1)
# note_on(channel, note, velocity) Triggers a note. (e.g., m.note_on(0, 60, 127) plays Middle C at max volume).
# note_off(channel, note, velocity) Stops a note. (e.g., m.note_off(0, 60, 0)).
# control_change(channel, control, value) Sends CC messages (like your encoder knob uses).
# program_change(channel, program) Changes the active instrument patch (0–127).
# pitch_bend(channel, value) Modulates pitch. Note that pitch bend values are usually 14-bit (0 to 16383, centered at 8192)