aboutsummaryrefslogtreecommitdiffstats
path: root/midi.py
diff options
context:
space:
mode:
authorEddy Pedroni <eddy@0xf7.com>2021-11-04 22:02:06 +0100
committerEddy Pedroni <eddy@0xf7.com>2021-11-04 22:02:06 +0100
commitfa5b0f760892bcbc2548ec0e516a716bd854ceef (patch)
treed90a9ecc50b6c445697b9440fa0e6c356594ea97 /midi.py
parent27f908340a5b8d3c8a9f3354f99712be5a0f739c (diff)
Added MIDI support, test files, removed mediaplayer example
Diffstat (limited to 'midi.py')
-rw-r--r--midi.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/midi.py b/midi.py
new file mode 100644
index 0000000..81b6703
--- /dev/null
+++ b/midi.py
@@ -0,0 +1,47 @@
+import mido
+
+lp_key = [[j for j in range(i * 16, i * 16 + 8)] for i in range(0,8)]
+
+GREEN = 124
+YELLOW = 126
+RED = 3
+
+_light_ctrl_msg = mido.Message('note_on', channel=0)
+_inport = None
+_outport = None
+
+_callbacks_on_press = dict()
+_callbacks_on_release = dict()
+
+def midi_init(device_name="Launchpad Mini MIDI 1"):
+ global _inport, _outport
+ _inport = mido.open_input(device_name)
+ _outport = mido.open_output(device_name)
+
+ _inport.callback = _callback
+
+def button_on(button, colour):
+ print(f"outport: {_outport}")
+ if _outport:
+ msg = _light_ctrl_msg.copy(note=button, velocity=colour)
+ print(f"sending {msg}")
+ _outport.send(msg)
+
+def button_off(button):
+ if _outport:
+ msg = _light_ctrl_msg.copy(note=button, velocity=0)
+ _outport.send(msg)
+
+def on_press(button, function):
+ _callbacks_on_press[button] = function
+
+def on_release(button, function):
+ _callbacks_on_release[button] = function
+
+def _callback(msg):
+ if msg.velocity == 0:
+ if msg.note in _callbacks_on_release:
+ _callbacks_on_release[msg.note]()
+ elif msg.velocity == 127:
+ if msg.note in _callbacks_on_press:
+ _callbacks_on_press[msg.note]()