-
Notifications
You must be signed in to change notification settings - Fork 19
/
looper.py
571 lines (508 loc) · 21.8 KB
/
looper.py
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import logging
import mido
import time
import colors
from gridgets import Gridget, Surface
from palette import palette
from persistence import persistent_attrs, persistent_attrs_init
on_off_colors = palette.SWITCH
class Note(object):
def __init__(self, note, velocity, duration):
self.note = note
self.velocity = velocity
self.duration = duration
def __repr__(self):
return ("Note(note={}, velocity={}, duration={})"
.format(self.note, self.velocity, self.duration))
@persistent_attrs(
notes={}, channel=None, tick_in=0, tick_out=0, teach_interval=0)
class Loop(object):
def __init__(self, looper, cell):
self.looper = looper
self.cell = cell
persistent_attrs_init(self, "{},{}".format(*cell))
self.next_tick = 0 # next "position" to be played in self.notes
def __repr__(self):
return "Loop({})".format(self.cell)
class Flash(Gridget):
def __init__(self, grid):
self.grid = grid
self.surface = Surface(grid.surface)
def flash(self, color):
for led in self.surface:
if isinstance(led, tuple):
self.surface[led] = color
self.grid.focus(self)
time.sleep(0.3)
self.grid.focus(self.grid.notepickers[self.grid.channel])
class Teacher(object):
def __init__(self, looper):
self.looper = looper
self.teacher_loop = None
self.student_loop = Loop(looper, "student")
self.phase = "STOP" # "TEACHER" "STUDENT"
def select(self, loop):
self.teacher_loop = loop
self.student_loop.channel = loop.channel
for grid in self.looper.griode.grids:
grid.channel = loop.channel
grid.focus(grid.notepickers[grid.channel])
grid.flash = Flash(grid) # FIXME
self.tick_interval = loop.teach_interval or 24 * 8
self.tick_in = 0
self.tick_out = self.tick_in + self.tick_interval
loop.tick_in = 0
loop.tick_out = 0 # Do not loop that!
self.flash(colors.YELLOW)
self.teacher()
def flash(self, color):
for grid in self.looper.griode.grids:
grid.flash.flash(color)
def stop(self):
self.phase = "STOP"
logging.debug("phase=STOP")
self.looper.playing = False
self.looper.loops_playing.clear()
self.looper.loops_recording.clear()
def teacher(self):
self.phase = "TEACHER"
logging.debug("phase=TEACHER")
self.looper.playing = False
self.looper.loops_recording.clear()
self.looper.loops_playing.clear()
self.looper.loops_playing.add(self.teacher_loop)
self.teacher_loop.next_tick = self.tick_in
self.teacher_notes = []
for tick in range(self.tick_in, self.tick_out):
for note in self.teacher_loop.notes.get(tick, []):
self.teacher_notes.append(note.note)
if self.teacher_notes == []:
# A silence long enough will be interpreted as end of song
self.stop()
else:
self.flash(palette.BLACK)
self.looper.playing = True
def student(self):
self.phase = "STUDENT"
logging.debug("phase=STUDENT")
self.looper.playing = False
self.student_loop.notes.clear()
self.student_loop.next_tick = 0
self.looper.loops_recording.add(self.student_loop)
self.looper.loops_playing.clear()
self.flash(colors.YELLOW)
self.looper.playing = True
def tick(self, tick):
if self.phase == "TEACHER":
if self.teacher_loop.next_tick >= self.tick_out:
self.student()
if self.phase == "STUDENT":
# Once per beat, check how we did in this loop
if tick%24 == 0:
student_notes = []
for tick in self.student_loop.notes:
for note in self.student_loop.notes.get(tick, []):
if note.duration > 0:
student_notes.append(note.note)
if student_notes == self.teacher_notes:
# Yay!
logging.info("Got the right notes!")
self.flash(colors.GREEN)
self.tick_in += self.tick_interval
self.tick_out += self.tick_interval
self.teacher()
elif (any(x!=y for x, y in zip(self.teacher_notes, student_notes))
or
len(student_notes) > len(self.teacher_notes)
or
self.student_loop.next_tick >= 2*self.tick_interval):
# Bzzzt wrong
logging.info("Bzzzt try again!")
logging.info("Teacher notes: {}"
.format(self.teacher_notes))
logging.info("Student notes: {}"
.format(student_notes))
self.flash(colors.RED)
self.teacher()
@persistent_attrs(beats_per_bar=4)
class Looper(object):
def __init__(self, griode):
self.griode = griode
persistent_attrs_init(self)
self.playing = False
self.last_tick = 0 # Last (=current) tick
self.loops_playing = set() # Contains instances of Loop
self.loops_recording = set() # Also instances of Loop
self.notes_recording = {} # note -> (Note(), tick_when_started)
self.notes_playing = [] # (stop_tick, channel, note)
self.loops = {}
self.teacher = Teacher(self)
for row in range(1, 9):
for column in range(1, 9):
self.loops[row, column] = Loop(self, (row, column))
def send(self, message):
if self.playing and message.type == "note_on":
for loop in self.loops_recording:
if loop.channel == message.channel:
if message.velocity > 0:
note = Note(message.note, message.velocity, 0)
logging.info("recording {}...".format(note))
if loop.next_tick not in loop.notes:
loop.notes[loop.next_tick] = []
loop.notes[loop.next_tick].append(note)
self.notes_recording[message.note] = (note, self.last_tick)
else:
note, tick_started = self.notes_recording.pop(message.note)
note.duration = self.last_tick - tick_started
logging.info("recorded {}".format(note))
# No matter what: let the message through the chain
self.output(message)
def output(self, message):
channel = message.channel
devicechain = self.griode.devicechains[channel]
devicechain.send(message)
def tick(self, tick):
self.last_tick = tick
# First, check if there are notes that should be stopped.
notes_to_stop = [note for note in self.notes_playing if note[0] <= tick]
for note in notes_to_stop:
message = mido.Message(
"note_on", channel=note[1], note=note[2], velocity=0)
self.output(message)
self.notes_playing.remove(note)
# Light off notepickers
for grid in self.griode.grids:
grid.notepickers[note[1]].send(message, self)
# Only play stuff if we are really playing (i.e. not paused)
if not self.playing:
return
# OK now, for each loop that is playing...
for loop in self.loops_playing:
# Figure out which notes should be started *now*
for note in loop.notes.get(loop.next_tick, []):
logging.info("Play {} from {}".format(note, loop))
self.notes_playing.append(
(tick+note.duration, loop.channel, note.note))
message = mido.Message(
"note_on", channel=loop.channel,
note=note.note, velocity=note.velocity)
self.output(message)
# Light up notepickers
for grid in self.griode.grids:
grid.notepickers[loop.channel].send(message, self)
# Advance each loop that is currently playing or recording
for loop in self.loops_playing | self.loops_recording:
loop.next_tick += 1
# If we're past the end of the loop, jump to begin of loop
if loop.tick_out > 0 and loop.next_tick >= loop.tick_out:
loop.next_tick = loop.tick_in
# Teacher logic
self.teacher.tick(tick)
class LoopController(Gridget):
"""
^ v < > = PLAY REC REWIND PLAY/PAUSE
Then the 64 pads are available for loops
Press more than 1 second on a pad to edit it
Press less than 1 second to select/deselect it for play/rec
"""
def __init__(self, grid):
self.grid = grid
self.surface = Surface(grid.surface)
self.loopeditor = LoopEditor(grid)
self.stepsequencer = StepSequencer(grid)
self.mode = "LEARN" # or "REC" or "PLAY"
self.pads_held = {} # maps pad to time when pressed
self.draw()
@property
def looper(self):
return self.grid.griode.looper
def blink(self, color, play, rec):
tick = self.looper.last_tick
# If play: slow blink (once per quarter note)
# If rec: fast blink (twice per quarter note)
# If play and rec: alternate slow and fast blink
if play and rec:
if tick % 48 > 24:
rec = False
else:
play = False
if play:
if tick % 24 > 18:
return palette.BLACK
else:
return color
if rec:
if tick % 12 > 4:
return palette.BLACK
else:
return color
return color
def draw(self):
for led in self.surface:
if isinstance(led, tuple):
color = colors.GREY_LO
loop = self.looper.loops[led]
if loop.channel is not None:
color = palette.CHANNEL[loop.channel]
# If that loop is selected for play or rec, show it
# (With fancy blinking)
color = self.blink(
color,
loop in self.looper.loops_playing,
loop in self.looper.loops_recording)
self.surface[led] = color
# UP = playback, DOWN = record
self.surface["UP"] = on_off_colors[self.mode == "PLAY"]
self.surface["DOWN"] = on_off_colors[self.mode == "REC"]
self.surface["UP"] = self.blink(
self.surface["UP"], self.looper.loops_playing, False)
self.surface["DOWN"] = self.blink(
self.surface["DOWN"], False, self.looper.loops_recording)
# LEFT = rewind all loops (but keep playing if we're playing)
# (but also used to delete a loop!)
self.surface["LEFT"] = on_off_colors[bool(self.pads_held)]
# RIGHT = play/pause
self.surface["RIGHT"] = on_off_colors[self.looper.playing]
def tick(self, tick):
for cell, time_pressed in self.pads_held.items():
if time.time() > time_pressed + 1.0:
self.pads_held.clear()
# Enter edit mode for that pad
loop = self.looper.loops[cell]
self.loopeditor.loop = loop
self.grid.focus(self.loopeditor)
break
self.draw()
self.loopeditor.draw()
self.stepsequencer.draw()
def pad_pressed(self, row, column, velocity):
# We don't act when the pad is pressed, but when it is released.
# (When the pad is pressed, we record the time, so we can later
# detect when a pad is held more than 1s to enter edit mode.)
if velocity > 0:
self.pads_held[row, column] = time.time()
return
# When pad is released, if "something" removed it from the
# pads_held dict, ignore the action.
if (row, column) not in self.pads_held:
return
del self.pads_held[row, column]
if self.mode == "PLAY":
loop = self.looper.loops[row, column]
# Does that loop actually exist?
if loop.channel is not None:
if loop in self.looper.loops_playing:
self.looper.loops_playing.remove(loop)
else:
self.looper.loops_playing.add(loop)
if self.mode == "REC":
loop = self.looper.loops[row, column]
# If we tapped an empty cell, create a new loop
if loop.channel is None:
loop.channel = self.grid.channel
if loop in self.looper.loops_recording:
self.looper.loops_recording.remove(loop)
else:
self.looper.loops_recording.add(loop)
# FIXME: stop recording other loops on the same channel
if self.mode == "LEARN":
loop = self.looper.loops[row, column]
if loop.channel is not None:
self.looper.teacher.select(loop)
# Update all loopcontrollers to show new state
for grid in self.grid.griode.grids:
grid.loopcontroller.draw()
def button_pressed(self, button):
if button == "UP":
self.mode = "PLAY"
if button == "DOWN":
self.mode = "REC"
if button == "LEFT":
if self.pads_held:
# Delete!
for cell in self.pads_held:
# OK, this is hackish.
# We can't easily wipe out an object from the persistence
# system, so we re-initialize it to empty values instead.
loop = self.looper.loops[cell]
loop.channel = None
loop.tick_in = loop.tick_out = 0
loop.notes.clear()
self.looper.loops_playing.discard(loop)
self.looper.loops_recording.discard(loop)
self.pads_held.clear()
else:
# Rewind
for loop in self.looper.loops_playing:
loop.next_tick = loop.tick_in
for loop in self.looper.loops_recording:
loop.next_tick = loop.tick_in
# FIXME should we also undo the last recording?
if button == "RIGHT":
if self.looper.playing:
for loop in self.looper.loops_recording:
# Save! Save all the things!
loop.db.sync()
# I'm not sure that this logic should be here,
# but it should be somewhere, so here we go...
# When stopping, if any loop doesn't have a
# tick_out point, add one. (By rounding up to
# the end of the bar.)
if loop.tick_out == 0:
loop.tick_out = 24 * ((loop.tick_out + 23) // 24)
# And then toggle the playing flag.
self.looper.playing = not self.looper.playing
for grid in self.grid.griode.grids:
grid.loopcontroller.draw()
class CellPicker(Gridget):
def __init__(self, grid):
self.grid = grid
self.surface = Surface(grid.surface)
self.ticks_per_cell = 12
self._loop = None
@property
def loop(self):
return self._loop
@loop.setter
def loop(self, value):
self._loop = value
self.draw()
def rc2cell(self, row, column):
# Map row,column to a cell number (starting at zero)
return (8-row)*8 + column-1
def rc2ticks(self, row, column):
# Return list of ticks in a given cell
cell = self.rc2cell(row, column)
return range(cell*self.ticks_per_cell, (cell+1)*self.ticks_per_cell)
class LoopEditor(CellPicker):
def __init__(self, grid):
super().__init__(grid)
self.action = None
def draw(self):
if self.loop is None:
return
for led in self.surface:
if isinstance(led, tuple):
row, column = led
color = palette.BLACK
ticks = self.rc2ticks(row, column)
for tick in ticks:
if tick in self.loop.notes:
color = colors.GREY_LO
if self.loop.looper.playing:
if self.loop in (self.loop.looper.loops_playing |
self.loop.looper.loops_recording):
if self.loop.next_tick in ticks:
color = palette.CHANNEL[self.loop.channel]
if self.loop.tick_in in ticks:
color = colors.PINK_HI
if self.loop.tick_out-1 in ticks:
color = colors.PINK_HI
self.surface[led] = color
def pad_pressed(self, row, column, velocity):
if velocity == 0:
return
if self.action == "SET_TICK_IN":
tick = self.rc2ticks(row, column)[0]
logging.info("Moving tick_in for loop {} to {}"
.format(self.loop, tick))
self.loop.tick_in = tick
self.action = None
elif self.action == "SET_TICK_OUT":
tick = self.rc2ticks(row, column)[-1] + 1
logging.info("Moving tick_out for loop {} to {}"
.format(self.loop, tick))
self.loop.tick_out = tick
self.action = None
else:
ticks = self.rc2ticks(row, column)
if self.loop.tick_out == 0 and 0 in ticks:
logging.info("Action is now SET_TICK_OUT (initial)")
self.action = "SET_TICK_OUT"
elif self.loop.tick_out-1 in ticks:
logging.info("Action is now SET_TICK_OUT")
self.action = "SET_TICK_OUT"
elif self.loop.tick_in in ticks:
logging.info("Action is now SET_TICK_IN")
self.action = "SET_TICK_IN"
else:
for tick in ticks:
for note in self.loop.notes.get(tick, []):
# FIXME: Send note_off message when the pad is released
message = mido.Message(
"note_on", channel=self.loop.channel,
note=note.note, velocity=note.velocity)
self.grid.griode.synth.send(message)
self.grid.griode.synth.send(message.copy(velocity=0))
self.draw()
def button_pressed(self, button):
if button == "UP":
self.grid.focus(self.grid.loopcontroller)
if button == "DOWN":
self.grid.loopcontroller.stepsequencer.loop = self.loop # FIXME urgh
self.grid.focus(self.grid.loopcontroller.stepsequencer)
class StepSequencer(CellPicker):
def __init__(self, grid):
super().__init__(grid)
self.note = None
@property
def notepicker(self):
return self.grid.notepickers[self.grid.channel]
def draw(self):
if self.loop is None:
return
for led in self.surface:
if isinstance(led, tuple):
row, column = led
if row in [1, 2, 3, 4]:
color = self.notepicker.surface[led]
if self.note is not None:
if self.note == self.notepicker.led2note[row, column]:
color = colors.PINK_HI
else:
color = palette.BLACK
ticks = self.rc2ticks(row, column)
# Show if there are notes in this cell
has_first = bool(self.loop.notes.get(ticks[0]))
has_other = any(bool(self.loop.notes.get(tick))
for tick in ticks[1:])
if has_first and has_other:
color = colors.GREY_LO
if has_first and not has_other:
color = colors.WHITE
if not has_first and has_other:
color = colors.GREY_LO
# And now, override that color if the current note is there
for note in self.loop.notes.get(ticks[0], []):
if note.note == self.note:
color = colors.PINK_HI
# But override even more to show the current play position
if self.loop.looper.playing:
if self.loop in (self.loop.looper.loops_playing |
self.loop.looper.loops_recording):
if self.loop.next_tick in ticks:
color = colors.AMBER_HI
self.surface[led] = color
def pad_pressed(self, row, column, velocity):
if row in [1, 2, 3, 4]:
self.notepicker.pad_pressed(row, column, velocity)
self.note = self.notepicker.led2note[row, column]
self.draw()
else:
if velocity == 0:
return
if self.note is None:
return
ticks = self.rc2ticks(row, column)
for note in self.loop.notes.get(ticks[0], []):
if note.note == self.note:
self.loop.notes[ticks[0]].remove(note)
break
else:
if ticks[0] not in self.loop.notes:
self.loop.notes[ticks[0]] = []
self.loop.notes[ticks[0]].append(
Note(note=self.note, velocity=velocity, duration=self.ticks_per_cell))
def button_pressed(self, button):
if button == "UP":
self.grid.focus(self.grid.loopcontroller.loopeditor)