-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttc.py
463 lines (379 loc) · 16.3 KB
/
ttc.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
import tkinter as tk
import time
import random
base = tk.Tk()
size = (10, 5)
cell_size = 100
frame = 30
num_tap_avgs = 6
class Cell:
def __init__(self, canvas, x, y):
self.canvas = canvas
self.mode = "expand"
self.reverse = True
self.c_x = x * cell_size + cell_size/2
self.c_y = y * cell_size + cell_size/2
self.active = True
self.bbox = (self.c_x - cell_size/2, self.c_y - cell_size/2,
self.c_x + cell_size/2, self.c_y + cell_size/2)
self.ID = 0
self.create_object()
self.bkg_colour = "black"
self.fore_colour = "white"
self.colour = "white"
self.flipped = False
self.one_or_two = True
def do_colours(self):
if not self.flipped:
self.fore_colour = self.colour
self.bkg_colour = "black"
else:
self.fore_colour = "black"
self.bkg_colour = self.colour
self.canvas.itemconfigure(self.backgroundID, fill=self.bkg_colour)
self.canvas.itemconfigure(self.ID, fill=self.fore_colour)
def create_object(self):
if self.ID:
self.canvas.delete(self.ID) # clear old object
self.backgroundID = self.canvas.create_oval(*self.bbox)
if self.mode == "expand":
self.ID = self.canvas.create_oval(*self.bbox)
elif self.mode == "pie":
self.ID = self.canvas.create_arc(*self.bbox, start=90, extent = 0)
else:
self.ID = self.canvas.create_oval(*self.bbox)
def tick(self, phase): # phase is 0-1
if self.reverse:
phase = 1 - phase
if self.mode == "expand":
self.canvas.coords(self.ID, self.c_x - phase*cell_size/2, self.c_y - phase*cell_size/2,
self.c_x + phase*cell_size/2, self.c_y + phase*cell_size/2)
self.canvas.itemconfigure(self.ID)
elif self.mode == "pie":
self.canvas.itemconfigure(self.ID, start=90, extent = - phase * 360)
else:
pass
def beat(self):
self.tick
print("do we ever end up here?")
def disable(self):
self.canvas.itemconfig(self.ID, state="hidden")
self.active = False
self.canvas.itemconfigure(self.backgroundID, state="hidden")
def enable(self):
self.canvas.itemconfig(self.ID, state="")
self.active = True
self.canvas.itemconfigure(self.backgroundID, state="")
def clock():
#return int(time.perf_counter_ns() // 10e5)
return int(time.time()*1000)
def avg_interval(l):
diffs = [l[i] - l[i+1] for i in range(0,len(l)-1)]
#print(diffs)
return int(sum(diffs)/len(diffs))
class Situation:
def __init__(self):
self.canvas = tk.Canvas(base, bg="black", height=size[1]*cell_size, width=size[0]*cell_size)
self.canvas.pack()
self.time = 0
self.cells = []
self.tempo = 5000
self.last_taps = [-1000]
self.beat_start_time = 0
self.beat_counter = 0
self.colour_mode = "odds"
self.colour_one = "blue"
self.colour_two = "red"
self.pattern = "LCR"
for x in range(0, size[0]):
self.cells.append([])
for y in range(0, size[1]):
self.cells[x].append(Cell(self.canvas, x, y))
self.beat(False)
self.set_colours()
self.setup_control()
def set_cells(self):
if self.pattern == "odds":
for x, col in enumerate(self.cells):
for y, cell in enumerate(col):
if (x+y+self.beat_counter)%2:
cell.enable()
else:
cell.disable()
elif self.pattern == "RL":
for x, col in enumerate(self.cells):
if (x+self.beat_counter)%size[0] == 0:
[cell.enable() for cell in col]
else:
[cell.disable() for cell in col]
elif self.pattern == "LR":
for x, col in enumerate(reversed(self.cells)):
if (x+self.beat_counter)%size[0] == 0:
[cell.enable() for cell in col]
else:
[cell.disable() for cell in col]
elif self.pattern == "LCR":
num = int((size[0]+1)//2)
print(self.beat_counter%num)
for col in self.cells:
[cell.disable() for cell in col]
[cell.enable() for cell in self.cells[self.beat_counter%num]]
[cell.enable() for cell in self.cells[size[0]-1-self.beat_counter%num]]
elif self.pattern == "rand1":
for col in self.cells:
for cell in col:
if random.random() > 0.90:
cell.enable()
else:
cell.disable()
elif self.pattern == "rand2":
for col in self.cells:
for cell in col:
if random.random() > 0.75:
cell.enable()
else:
cell.disable()
elif self.pattern == "rand3":
for col in self.cells:
for cell in col:
if random.random() > 0.50:
cell.enable()
else:
cell.disable()
elif self.pattern == "down":
for col in self.cells:
for y, cell in enumerate(col):
if not (y+self.beat_counter)%size[1]:
cell.enable()
else:
cell.disable()
elif self.pattern == "alt lr":
for x, col in enumerate(self.cells):
if x < size[0]/2:
if self.beat_counter%2:
[cell.enable() for cell in col]
else:
[cell.disable() for cell in col]
else:
if self.beat_counter%2:
[cell.disable() for cell in col]
else:
[cell.enable() for cell in col]
elif self.pattern == "alt tb":
for col in self.cells:
for y, cell in enumerate(col):
if y < size[1]/2:
if self.beat_counter%2:
cell.enable()
else:
cell.disable()
else:
if self.beat_counter%2:
cell.disable()
else:
cell.enable()
else:
for row in self.cells:
[cell.enable() for cell in row]
def beat(self, auto):
cl = clock()
#print(self.tempo,cl - self.beat_start_time)
lateness = 0
if not auto and (cl - self.beat_start_time) < 70: #this prevents two beats firing when user taps just after the last automatic beat hit
pass
else:
#following lines moved from the top of tap()
if hasattr(self, 'tick_job'): # these ifs only fail right at the start of a run
self.canvas.after_cancel(self.tick_job) # cancel current tick
if hasattr(self, 'beat_job'):
self.canvas.after_cancel(self.beat_job) # cancel current beat
if auto: # if this beat was triggered automatically, let's calculate how late it was
lateness = cl - self.beat_start_time - self.tempo
self.beat_job = self.canvas.after(self.tempo - lateness,self.beat,True)
self.beat_start_time = cl
#make right cells active
self.set_cells()
self.animate()
self.beat_counter += 1
def animate(self): # this is a one shot function, it plays one animation then stops
time = clock() - self.beat_start_time
if time < self.tempo:
for col in self.cells:
for cell in col:
if cell.active:
cell.tick( time / self.tempo )
self.tick_job = self.canvas.after(frame, self.animate)
else:
print("animate else",time)
def tap(self, params):
# do tap tempo logic
now = clock()
if now - self.last_taps[0] < 2000: # adding to an already started series of taps
self.last_taps.insert(0, now)
self.tempo = avg_interval(self.last_taps)
if len(self.last_taps) > num_tap_avgs + 1:
self.last_taps.pop()
#self.beat(False) # call new beat
else: # starting a new tap after a while running automatically
self.last_taps = [now]
self.beat(False) # call new beat
def set_colours(self):
if self.colour_mode == "odds":
for x, col in enumerate(self.cells):
for y, cell in enumerate(col):
if (x+y)%2:
cell.colour = self.colour_one
cell.one_or_two = True
else:
cell.colour = self.colour_two
cell.one_or_two = False
elif self.colour_mode == "rows":
for col in self.cells:
for y, cell in enumerate(col):
if y&2:
cell.colour = self.colour_one
cell.one_or_two = True
else:
cell.colour = self.colour_two
cell.one_or_two = False
elif self.colour_mode == "cols":
for x, col in enumerate(self.cells):
if x%2:
for cell in col:
cell.colour=self.colour_one
cell.one_or_two = True
else:
for cell in col:
cell.colour=self.colour_two
cell.one_or_two = False
else:
for col in self.cells:
for cell in col:
cell.colour=self.colour_one
cell.one_or_two = True
for col in self.cells:
for cell in col:
cell.do_colours()
def set_pattern(self,pattern):
self.pattern = pattern
def set_spawn(self,spawn):
for col in self.cells:
for cell in col:
cell.mode = spawn
cell.create_object()
def set_colour_mode(self,mode):
self.colour_mode = mode
self.set_colours()
def set_colour_one(self,col):
self.colour_one = col
self.set_colours()
def set_colour_two(self,col):
self.colour_two = col
self.set_colours()
def flip_colour_one(self,*d):
for col in self.cells:
for cell in col:
if cell.one_or_two:
cell.flipped = not cell.flipped
def flip_colour_two(self,*d):
for col in self.cells:
for cell in col:
if not cell.one_or_two:
cell.flipped = not cell.flipped
def reverse_spawn(self,*d):
for col in self.cells:
for cell in col:
cell.reverse = not cell.reverse
def setup_control(self):
self.control = tk.Toplevel(base)
self.control.bind("<space>",self.tap)
pattern = tk.Label(self.control, text="pattern").grid(row=0, column=0)
all_ = tk.Button(self.control, text="all")
all_.grid(row=1, column=0)
all_.bind("<1>", lambda d:self.set_pattern("all"))
p_odds = tk.Button(self.control, text="odds/evens")
p_odds.grid(row=2, column=0)
p_odds.bind("<1>", lambda d:self.set_pattern("odds"))
LR = tk.Button(self.control, text="L to R")
LR.grid(row=3, column=0)
LR.bind("<1>", lambda d:self.set_pattern("LR"))
RL = tk.Button(self.control, text="R to L")
RL.grid(row=4, column=0)
RL.bind("<1>", lambda d:self.set_pattern("RL"))
LCR = tk.Button(self.control, text="in to centre")
LCR.grid(row=5, column=0)
LCR.bind("<1>", lambda d:self.set_pattern("LCR"))
rand1 = tk.Button(self.control, text="random 10%")
rand1.grid(row=6, column=0)
rand1.bind("<1>", lambda d:self.set_pattern("rand1"))
rand2 = tk.Button(self.control, text="random 25%")
rand2.grid(row=7, column=0)
rand2.bind("<1>", lambda d:self.set_pattern("rand2"))
rand3 = tk.Button(self.control, text="random 50%")
rand3.grid(row=8, column=0)
rand3.bind("<1>", lambda d:self.set_pattern("rand3"))
down = tk.Button(self.control, text="top to bottom")
down.grid(row=9, column=0)
down.bind("<1>", lambda d:self.set_pattern("down"))
down = tk.Button(self.control, text="alternate L/R")
down.grid(row=10, column=0)
down.bind("<1>", lambda d:self.set_pattern("alt lr"))
down = tk.Button(self.control, text="alt. top/bottom")
down.grid(row=11, column=0)
down.bind("<1>", lambda d:self.set_pattern("alt tb"))
spawn = tk.Label(self.control, text="spawn").grid(row=0, column=1)
on = tk.Button(self.control, text="on")
on.grid(row=1, column=1)
on.bind("<1>", lambda d:self.set_spawn("on"))
expand = tk.Button(self.control, text="expand")
expand.grid(row=2, column=1)
expand.bind("<1>", lambda d:self.set_spawn("expand"))
pie = tk.Button(self.control, text="pie")
pie.grid(row=3, column=1)
pie.bind("<1>", lambda d:self.set_spawn("pie"))
reverse_spawn = tk.Checkbutton(self.control, text="reverse")
reverse_spawn.grid(row=4, column=1)
reverse_spawn.bind("<1>", self.reverse_spawn)
col_mode = tk.Label(self.control, text="colour mode").grid(row=0, column=2)
single = tk.Button(self.control, text="single")
single.grid(row=1, column=2)
single.bind("<1>", lambda d:self.set_colour_mode("single"))
c_odds = tk.Button(self.control, text="odds/evens")
c_odds.grid(row=2, column=2)
c_odds.bind("<1>", lambda d:self.set_colour_mode("odds"))
rows = tk.Button(self.control, text="rows")
rows.grid(row=3, column=2)
rows.bind("<1>", lambda d:self.set_colour_mode("rows"))
cols = tk.Button(self.control, text="columns")
cols.grid(row=4, column=2)
cols.bind("<1>", lambda d:self.set_colour_mode("cols"))
col_one = tk.Label(self.control, text="colour one").grid(row=0, column=3)
one_white = tk.Button(self.control, text="white")
one_white.grid(row=1, column=3)
one_white.bind("<1>", lambda d:self.set_colour_one("white"))
one_red = tk.Button(self.control, text="red")
one_red.grid(row=2, column=3)
one_red.bind("<1>", lambda d:self.set_colour_one("red"))
one_blue = tk.Button(self.control, text="blue")
one_blue.grid(row=3, column=3)
one_blue.bind("<1>", lambda d:self.set_colour_one("blue"))
flip_cols_butt = tk.Checkbutton(self.control, text="flip colours")
flip_cols_butt.grid(row=5, column=3)
flip_cols_butt.bind("<1>", self.flip_colour_one)
col_two = tk.Label(self.control, text="colour two").grid(row=0, column=4)
two_white = tk.Button(self.control, text="white")
two_white.grid(row=1, column=4)
two_white.bind("<1>", lambda d:self.set_colour_two("white"))
two_red = tk.Button(self.control, text="red")
two_red.grid(row=2, column=4)
two_red.bind("<1>", lambda d:self.set_colour_two("red"))
two_blue = tk.Button(self.control, text="blue")
two_blue.grid(row=3, column=4)
two_blue.bind("<1>", lambda d:self.set_colour_two("blue"))
flip_cols_butt2 = tk.Checkbutton(self.control, text="flip colours")
flip_cols_butt2.grid(row=5, column=4)
flip_cols_butt2.bind("<1>", self.flip_colour_two)
tk.Label(self.control, text="space for tap tempo").grid(row=6, column=2)
situ = Situation()
#base.bind("<space>",situ.tap)
base.mainloop()