aboutsummaryrefslogtreecommitdiffstats
path: root/midi_controller_launchpad_mini.py
blob: 960dd7d6f7465618792c02c33b9f86b3488eb00f (plain)
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
from midi_wrapper_mido import MidiWrapper

class MidiController:
    DEVICE_NAME = "Launchpad Mini MIDI 1"

    def __init__(self, soloTool, midiWrapperOverride=None):
        self._soloTool = soloTool
        if midiWrapperOverride is not None:
            self._midiWrapper = midiWrapperOverride
        else:
            self._midiWrapper = MidiWrapper()

        self._handlers = {
            96  : self._soloTool.stop,
            112 : self._playPause,
            101 : self._soloTool.jumpToA,
            102 : self._soloTool.previousStoredAbLimits,
            103 : self._soloTool.nextStoredAbLimits,
            118 : self._soloTool.previousSong,
            119 : self._soloTool.nextSong
        }
        
        for i in range(16, 24):
            rate = round(0.5 + (i - 16) * 0.1, 1)
            self._handlers[i] = self._createSetPlaybackRateCallback(rate)

    def connect(self):
        self._midiWrapper.connect(MidiController.DEVICE_NAME)
        self._midiWrapper.setCallback(self._callback)

    def _callback(self, msg):
        if msg.velocity < 127:
            return

        if msg.note in self._handlers:
            handler = self._handlers[msg.note]()

    def _playPause(self):
        if self._soloTool.isPlaying():
            self._soloTool.pause()
        else:
            self._soloTool.play()

    def _createSetPlaybackRateCallback(self, rate):
        def f():
            self._soloTool.setPlaybackRate(rate)
        return f