aboutsummaryrefslogtreecommitdiffstats
path: root/midi_controller_launchpad_mini.py
blob: cb3bc3721ca5ae88d9a93435f1f31c0caf59033d (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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from midi_wrapper_mido import MidiWrapper

class MidiController:
    DEVICE_NAME = "Launchpad Mini MIDI 1"
    LIGHT_CONTROL_CHANNEL = 0
    LED_GREEN  = 124
    LED_YELLOW = 126
    LED_RED    = 3
    LED_OFF    = 0
    BUTTON_MATRIX = [[x for x in range(y * 16, y * 16 + 8)] for y in range(0,8)] # address as [row][col]

    MIN_PLAYBACK_RATE = 0.5
    MAX_PLAYBACK_RATE = 1.2
    PLAYBACK_RATE_STEP = 0.1

    MIN_PLAYBACK_VOLUME = 0.125
    MAX_PLAYBACK_VOLUME = 1.0
    PLAYBACK_VOLUME_STEP = 0.125

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

        self._registerHandlers()
        self._soloTool.registerPlayingStateCallback(self._updatePlayPauseButton)

    def _registerHandlers(self):
        self._handlers = {
            96  : self._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(0, 8):
            volume = round(MidiController.MIN_PLAYBACK_VOLUME + i * MidiController.PLAYBACK_VOLUME_STEP, 3)
            self._handlers[i] = self._createSetPlaybackVolumeCallback(volume, i)

        for i, button in enumerate(range(16, 24)):
            rate = round(MidiController.MIN_PLAYBACK_RATE + MidiController.PLAYBACK_RATE_STEP * i, 1)
            self._handlers[button] = self._createSetPlaybackRateCallback(rate, i)

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

    def _callback(self, msg):
        if msg.type != "note_on" or 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 _stop(self):
        self._soloTool.stop()

    def _updatePlayPauseButton(self):
        if self._soloTool.isPlaying():
            self._setButtonLED(7, 0, MidiController.LED_GREEN)
        else:
            self._setButtonLED(7, 0, MidiController.LED_YELLOW)

    def _createSetPlaybackRateCallback(self, rate, column):
        def f():
            self._soloTool.setPlaybackRate(rate)
            self._lightRowUntilColumn(1, column, MidiController.LED_YELLOW)
        return f

    def _createSetPlaybackVolumeCallback(self, volume, column):
        def f():
            self._soloTool.setPlaybackVolume(volume)
            self._lightRowUntilColumn(0, column, MidiController.LED_GREEN)
        return f

    def _setButtonLED(self, row, col, colour):
        self._midiWrapper.sendMessage(MidiController.BUTTON_MATRIX[row][col], colour, MidiController.LIGHT_CONTROL_CHANNEL)

    def _lightRowUntilColumn(self, row, column, litColour):
        colours = [litColour] * (column + 1) + [MidiController.LED_OFF] * (7 - column)
        for col in range(0, 8):
            self._setButtonLED(row, col, colours[col])

    def _allLEDsOff(self):
        for row in range(0, 8):
            for col in range(0, 8):
                self._setButtonLED(row, col, MidiController.LED_OFF)

    def _initialiseButtonLEDs(self):
        self._allLEDsOff()

        # volume buttons
        for col in range(0, 8):
            self._setButtonLED(0, col, MidiController.LED_GREEN)

        # playback rate buttons
        for col in range(0, 6):
            self._setButtonLED(1, col, MidiController.LED_YELLOW)

        # playback control
        self._setButtonLED(6, 0, MidiController.LED_RED)
        self._setButtonLED(7, 0, MidiController.LED_YELLOW)

        # AB control
        self._setButtonLED(6, 5, MidiController.LED_YELLOW)
        self._setButtonLED(6, 6, MidiController.LED_RED)
        self._setButtonLED(6, 7, MidiController.LED_GREEN)

        # Song control
        self._setButtonLED(7, 6, MidiController.LED_RED)
        self._setButtonLED(7, 7, MidiController.LED_GREEN)