aboutsummaryrefslogtreecommitdiffstats
path: root/solo-tool-project/test/midi_launchpad_mini_integrationtest.py
blob: 8fd09bbc8643d430a54f45d7af5e9efcdbc31669 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import pytest
from mido import Message

from solo_tool.midi_controller_launchpad_mini import MidiController
from solo_tool.solo_tool import SoloTool
from player_mock import Player as PlayerMock

LED_RED = 3
LED_YELLOW = 126
LED_GREEN = 124
LED_OFF = 0

nextSongButton = 55
fwd25PcButton = 54
fwd5PcButton = 53
fwd1PcButton = 52
rwd1PcButton = 51
rwd5PcButton = 50
rwd25PcButton = 49
previousSongButton = 48

playPauseButton = 112
stopButton = 96

nextLimitButton = 119
previousLimitButton = 118
abToggleButton = 98
jumpToAButton = 114
setAButton = 116
setBButton = 117

class MidiWrapperMock:
    def __init__(self):
        self.callback = None
        self.connectedDevice = None
        self.sentMessages = list()

    def connect(self, deviceName, callback):
        self.connectedDevice = deviceName
        self.callback = callback

    def disconnect(self):
        self.connectedDevice = None
    
    def sendMessage(self, note, velocity, channel):
        self.sentMessages.append((note, velocity, channel))
    
    def simulateInput(self, note, velocity=127, channel=0):
        if self.callback is not None:
            msg = Message("note_on", note=note, velocity=velocity, channel=channel)
            self.callback(msg)

    def getLatestMessage(self):
        return self.sentMessages[-1]

@pytest.fixture
def playerMock():
    return PlayerMock()

@pytest.fixture
def soloTool(playerMock):
    return SoloTool(playerMock)

@pytest.fixture
def midiWrapperMock():
    return MidiWrapperMock()

@pytest.fixture
def uut(soloTool, midiWrapperMock):
    return MidiController(soloTool, midiWrapperMock)

def test_startStopAndPauseButtons(uut, midiWrapperMock, playerMock):
    uut.connect()

    assert playerMock.state == PlayerMock.STOPPED
    
    midiWrapperMock.simulateInput(playPauseButton)
    assert playerMock.state == PlayerMock.PLAYING
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_GREEN, 0)

    midiWrapperMock.simulateInput(stopButton)
    assert playerMock.state == PlayerMock.STOPPED
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_YELLOW, 0)

    midiWrapperMock.simulateInput(playPauseButton)
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_GREEN, 0)
    
    midiWrapperMock.simulateInput(playPauseButton)
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_YELLOW, 0)
    assert playerMock.state == PlayerMock.PAUSED

    midiWrapperMock.simulateInput(playPauseButton)
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_GREEN, 0)
    assert playerMock.state == PlayerMock.PLAYING

    midiWrapperMock.simulateInput(playPauseButton)
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_YELLOW, 0)

    midiWrapperMock.simulateInput(stopButton)
    assert playerMock.state == PlayerMock.STOPPED

def test_startPauseButtonLed(uut, midiWrapperMock, playerMock, soloTool):
    uut.connect()

    assert playerMock.state == PlayerMock.STOPPED
    
    playerMock.state = PlayerMock.PLAYING
    playerMock.simulatePlayingStateChanged()
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_GREEN, 0)

    playerMock.state = PlayerMock.STOPPED
    playerMock.simulatePlayingStateChanged()
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_YELLOW, 0)

    playerMock.state = PlayerMock.PAUSED
    playerMock.simulatePlayingStateChanged()
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_YELLOW, 0)

    playerMock.state = PlayerMock.PLAYING
    playerMock.simulatePlayingStateChanged()
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, MidiController.LED_GREEN, 0)

def test_abToggleButton(uut, midiWrapperMock, soloTool):
    uut.connect()

    midiWrapperMock.simulateInput(abToggleButton)
    assert soloTool.isAbLimitEnabled()
    assert midiWrapperMock.getLatestMessage() == (abToggleButton, MidiController.LED_GREEN, 0)

    midiWrapperMock.simulateInput(abToggleButton)
    assert not soloTool.isAbLimitEnabled()
    assert midiWrapperMock.getLatestMessage() == (abToggleButton, MidiController.LED_RED, 0)

def test_abToggleButtonLed(uut, midiWrapperMock, soloTool):
    uut.connect()

    soloTool.setAbLimitEnable(True)
    assert midiWrapperMock.getLatestMessage() == (abToggleButton, MidiController.LED_GREEN, 0)

    soloTool.setAbLimitEnable(False)
    assert midiWrapperMock.getLatestMessage() == (abToggleButton, MidiController.LED_RED, 0)

def test_jumpToAButton(uut, midiWrapperMock, soloTool, playerMock):
    ab = (0.5, 0.6)
    uut.connect()
    
    soloTool.setAbLimits(ab[0], ab[1])
    assert playerMock.position == 0.0

    midiWrapperMock.simulateInput(jumpToAButton)
    assert playerMock.position == ab[0]

def test_previousAndNextSongButtons(uut, midiWrapperMock, soloTool, playerMock):
    songs = [
        "test.flac",
        "test.mp3"
    ]
    for s in songs:
        soloTool.addSong(s)
    uut.connect()

    assert playerMock.currentSong == None
    midiWrapperMock.simulateInput(nextSongButton)
    assert playerMock.currentSong == songs[0]

    midiWrapperMock.simulateInput(nextSongButton)
    assert playerMock.currentSong == songs[1]

    midiWrapperMock.simulateInput(previousSongButton)
    assert playerMock.currentSong == songs[0]

    midiWrapperMock.simulateInput(previousSongButton)
    assert playerMock.currentSong == songs[0]

def test_previousAndNextAbButtons(uut, midiWrapperMock, soloTool, playerMock):
    song = "test.flac"
    abLimits = [
        [0.2, 0.4],
        [0.1, 0.3]
    ]

    soloTool.addSong(song)
    soloTool.setSong(0)
    soloTool.setAbLimitEnable(True)

    for ab in abLimits:
        soloTool.storeAbLimits(ab[0], ab[1])

    uut.connect()

    def checkLimit(aLimit, bLimit):
        playerMock.position = bLimit - 0.1
        soloTool.tick()
        assert playerMock.position == bLimit - 0.1

        playerMock.position = bLimit + 0.1
        soloTool.tick()
        assert playerMock.position == aLimit

    checkLimit(0.0, 0.0)

    midiWrapperMock.simulateInput(nextLimitButton)
    checkLimit(abLimits[0][0], abLimits[0][1])

    midiWrapperMock.simulateInput(nextLimitButton)
    checkLimit(abLimits[1][0], abLimits[1][1])

    midiWrapperMock.simulateInput(nextLimitButton)
    checkLimit(abLimits[1][0], abLimits[1][1])

    midiWrapperMock.simulateInput(previousLimitButton)
    checkLimit(abLimits[0][0], abLimits[0][1])

    midiWrapperMock.simulateInput(previousLimitButton)
    checkLimit(abLimits[0][0], abLimits[0][1])

def test_playbackRateButtons(uut, midiWrapperMock, soloTool, playerMock):
    playbackRateOptions = {
        16 : (0.5, [LED_YELLOW] * 1 + [LED_OFF] * 7),
        17 : (0.6, [LED_YELLOW] * 2 + [LED_OFF] * 6),
        18 : (0.7, [LED_YELLOW] * 3 + [LED_OFF] * 5),
        19 : (0.8, [LED_YELLOW] * 4 + [LED_OFF] * 4),
        20 : (0.9, [LED_YELLOW] * 5 + [LED_OFF] * 3),
        21 : (1.0, [LED_YELLOW] * 6 + [LED_OFF] * 2),
        22 : (1.1, [LED_YELLOW] * 7 + [LED_OFF] * 1),
        23 : (1.2, [LED_YELLOW] * 8)
    }
    uut.connect()
    assert playerMock.rate == 1.0

    for t, button in enumerate(playbackRateOptions):
        midiWrapperMock.sentMessages.clear()

        midiWrapperMock.simulateInput(button)
        assert playerMock.rate == playbackRateOptions[button][0]

        for i, colour in enumerate(playbackRateOptions[button][1]):
            assert midiWrapperMock.sentMessages[i] == (16 + i, colour, 0)

def test_playbackRateLeds(uut, midiWrapperMock, soloTool, playerMock):
    playbackRateOptions = [
        (0.00, [LED_OFF] * 8),
        (0.49, [LED_OFF] * 8),

        (0.50, [LED_YELLOW] * 1 + [LED_OFF] * 7),
        (0.59, [LED_YELLOW] * 1 + [LED_OFF] * 7),

        (0.60, [LED_YELLOW] * 2 + [LED_OFF] * 6),
        (0.69, [LED_YELLOW] * 2 + [LED_OFF] * 6),
        
        (0.70, [LED_YELLOW] * 3 + [LED_OFF] * 5),
        (0.79, [LED_YELLOW] * 3 + [LED_OFF] * 5),

        (0.80, [LED_YELLOW] * 4 + [LED_OFF] * 4),
        (0.89, [LED_YELLOW] * 4 + [LED_OFF] * 4),

        (0.90, [LED_YELLOW] * 5 + [LED_OFF] * 3),
        (0.99, [LED_YELLOW] * 5 + [LED_OFF] * 3),

        (1.00, [LED_YELLOW] * 6 + [LED_OFF] * 2),
        (1.09, [LED_YELLOW] * 6 + [LED_OFF] * 2),

        (1.10, [LED_YELLOW] * 7 + [LED_OFF] * 1),
        (1.19, [LED_YELLOW] * 7 + [LED_OFF] * 1),

        (1.2, [LED_YELLOW] * 8),
        (1.5, [LED_YELLOW] * 8)
    ]
    uut.connect()
    assert playerMock.rate == 1.0

    for t, (rate, leds) in enumerate(playbackRateOptions):
        midiWrapperMock.sentMessages.clear()

        soloTool.setPlaybackRate(rate)
        assert playerMock.rate == rate

        for i, colour in enumerate(leds):
            assert midiWrapperMock.sentMessages[i] == (16 + i, colour, 0)

def test_playbackVolumeButtons(uut, midiWrapperMock, soloTool, playerMock):
    playbackVolumeOptions = {
        0 : (0.5, [LED_GREEN] * 1 + [LED_OFF] * 7),
        1 : (0.6, [LED_GREEN] * 2 + [LED_OFF] * 6),
        2 : (0.7, [LED_GREEN] * 3 + [LED_OFF] * 5),
        3 : (0.8, [LED_GREEN] * 4 + [LED_OFF] * 4),
        4 : (0.9, [LED_GREEN] * 5 + [LED_OFF] * 3),
        5 : (1.0, [LED_GREEN] * 6 + [LED_OFF] * 2),
        6 : (1.1, [LED_GREEN] * 7 + [LED_OFF] * 1),
        7 : (1.2, [LED_GREEN] * 8)
    }
    uut.connect()
    assert playerMock.volume == 1.0
    
    for t, button in enumerate(playbackVolumeOptions):
        midiWrapperMock.sentMessages.clear()

        midiWrapperMock.simulateInput(button)
        assert playerMock.volume == playbackVolumeOptions[button][0]

        for i, colour in enumerate(playbackVolumeOptions[button][1]):
            assert midiWrapperMock.sentMessages[i] == (i, colour, 0)

def test_playbackVolumeLeds(uut, midiWrapperMock, soloTool, playerMock):
    playbackVolumeOptions = [
        (0.00, [LED_OFF] * 8),
        (0.49, [LED_OFF] * 8),

        (0.50, [LED_GREEN] * 1 + [LED_OFF] * 7),
        (0.59, [LED_GREEN] * 1 + [LED_OFF] * 7),

        (0.60, [LED_GREEN] * 2 + [LED_OFF] * 6),
        (0.69, [LED_GREEN] * 2 + [LED_OFF] * 6),
        
        (0.70, [LED_GREEN] * 3 + [LED_OFF] * 5),
        (0.79, [LED_GREEN] * 3 + [LED_OFF] * 5),

        (0.80, [LED_GREEN] * 4 + [LED_OFF] * 4),
        (0.89, [LED_GREEN] * 4 + [LED_OFF] * 4),

        (0.90, [LED_GREEN] * 5 + [LED_OFF] * 3),
        (0.99, [LED_GREEN] * 5 + [LED_OFF] * 3),

        (1.00, [LED_GREEN] * 6 + [LED_OFF] * 2),
        (1.09, [LED_GREEN] * 6 + [LED_OFF] * 2),

        (1.10, [LED_GREEN] * 7 + [LED_OFF] * 1),
        (1.19, [LED_GREEN] * 7 + [LED_OFF] * 1),

        (1.2, [LED_GREEN] * 8),
        (1.5, [LED_GREEN] * 8)
    ]
    uut.connect()
    assert playerMock.volume == 1.0

    for t, (volume, leds) in enumerate(playbackVolumeOptions):
        midiWrapperMock.sentMessages.clear()

        soloTool.setPlaybackVolume(volume)
        assert playerMock.volume == volume

        for i, colour in enumerate(leds):
            assert midiWrapperMock.sentMessages[i] == (i, colour, 0)

def test_unassignedButton(uut, midiWrapperMock):
    unassignedButton = 48
    uut.connect()

    # expect no crash
    midiWrapperMock.simulateInput(unassignedButton)
    # XXX would be better to assert that nothing changed in the solo tool

def test_connectDisconnect(uut, midiWrapperMock):
    startupMessages = list(
        [(int(i / 8) * 16 + (i % 8), LED_OFF, 0) for i in range(0, 64)] + # clear all
        [(i, LED_GREEN, 0) for i in range(0, 6)] +                      # volume row
        [(i, LED_YELLOW, 0) for i in range(16, 22)] +                   # playback rate row
        [
            (stopButton,          LED_RED, 0),
            (playPauseButton,     LED_YELLOW, 0),
            (abToggleButton,      LED_RED, 0),
            (jumpToAButton,       LED_YELLOW, 0),
            (previousLimitButton, LED_RED, 0),
            (nextLimitButton,     LED_GREEN, 0),
            (setAButton,          LED_YELLOW, 0),
            (setBButton,          LED_YELLOW, 0),
            (previousSongButton,  LED_RED, 0),
            (rwd1PcButton,        LED_RED, 0),
            (rwd5PcButton,        LED_RED, 0),
            (rwd25PcButton,       LED_RED, 0),
            (nextSongButton,      LED_GREEN, 0),
            (fwd1PcButton,        LED_GREEN, 0),
            (fwd5PcButton,        LED_GREEN, 0),
            (fwd25PcButton,       LED_GREEN, 0),
        ])

    teardownMessages = [(int(i / 8) * 16 + (i % 8), LED_OFF, 0) for i in range(0, 64)] # clear all

    expectedDevice = "Launchpad Mini MIDI 1"
    uut.connect()

    assert midiWrapperMock.connectedDevice == expectedDevice
    assert set(midiWrapperMock.sentMessages) == set(startupMessages)

    midiWrapperMock.sentMessages.clear()

    uut.disconnect()

    assert set(midiWrapperMock.sentMessages) == set(teardownMessages)

def test_playingFeedbackWhenChangingSong(uut, midiWrapperMock, soloTool, playerMock):
    songs = [
        "test.flac",
        "test.mp3"
    ]
    for s in songs:
        soloTool.addSong(s)
    uut.connect()

    soloTool.setSong(0)
    soloTool.play()
    assert playerMock.state == PlayerMock.PLAYING
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, LED_GREEN, 0)

    soloTool.nextSong()
    assert playerMock.state == PlayerMock.STOPPED
    assert midiWrapperMock.getLatestMessage() == (playPauseButton, LED_YELLOW, 0)

def test_setAbButtons(uut, midiWrapperMock, soloTool, playerMock):
    song = "test.flac"
    soloTool.addSong(song)
    soloTool.setSong(0)
    soloTool.setAbLimitEnable(True)
    abLimits = (0.6, 0.8)
    soloTool.storeAbLimits(abLimits[0], abLimits[1])

    uut.connect()

    def checkLimit(aLimit, bLimit):
        playerMock.position = bLimit - 0.1
        soloTool.tick()
        assert playerMock.position == bLimit - 0.1

        playerMock.position = bLimit + 0.1
        soloTool.tick()
        assert playerMock.position == aLimit

    # Set A limit
    playerMock.position = 0.3
    midiWrapperMock.simulateInput(setAButton)
    playerMock.position = 0.5
    midiWrapperMock.simulateInput(jumpToAButton)

    assert playerMock.position == 0.3

    # Set B limit
    playerMock.position = 0.4
    midiWrapperMock.simulateInput(setBButton)
    checkLimit(0.3, 0.4)

    # Selecting preset overrides manually set limits
    midiWrapperMock.simulateInput(nextLimitButton)
    checkLimit(abLimits[0], abLimits[1])

def test_seekButtons(uut, midiWrapperMock, soloTool, playerMock):
    song = "test.flac"
    soloTool.addSong(song)
    soloTool.setSong(0)

    uut.connect()
    
    assert playerMock.position == 0.0

    midiWrapperMock.simulateInput(fwd25PcButton)
    assert playerMock.position == 0.25

    midiWrapperMock.simulateInput(fwd5PcButton)
    assert playerMock.position == 0.30

    midiWrapperMock.simulateInput(fwd1PcButton)
    assert playerMock.position == 0.31

    midiWrapperMock.simulateInput(fwd25PcButton)
    midiWrapperMock.simulateInput(fwd25PcButton)
    midiWrapperMock.simulateInput(fwd25PcButton)
    assert playerMock.position == 1.0

    midiWrapperMock.simulateInput(rwd25PcButton)
    assert playerMock.position == 0.75

    midiWrapperMock.simulateInput(rwd5PcButton)
    assert playerMock.position == 0.70

    midiWrapperMock.simulateInput(rwd1PcButton)
    assert playerMock.position == 0.69

    midiWrapperMock.simulateInput(rwd25PcButton)
    midiWrapperMock.simulateInput(rwd25PcButton)
    midiWrapperMock.simulateInput(rwd25PcButton)
    assert playerMock.position == 0.0