-
Notifications
You must be signed in to change notification settings - Fork 0
/
ricochet.lua
456 lines (400 loc) · 10.4 KB
/
ricochet.lua
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
-- Ricochet
-- Generative sequencer based on cellular automation.
-- Inspired by Bongo and Batuhan Bozkurt
y_max = 8
x_max = 16
local cells = {}
local temp_cells = {}
local MusicUtil = require "musicutil"
local BeatClock = require 'beatclock'
local cs = require 'controlspec'
local clk = BeatClock.new()
local clk_midi = midi.connect()
local running = false
local reset = false
local alt = false
local transpose = 60
engine.name = 'PolyPerc'
local scale_notes = {}
local grid_scale = {}
local note_queue = {}
local note_off_queue = {}
local min_note = 0
local max_note = 127
local m = midi.connect()
local g = grid.connect()
function init()
g:all(0)
clk_midi.event = clk.process_midi
clk.on_step = step
clk.on_select_internal = function() clk:stop() end
clk.on_select_external = reset_pattern
clk:add_clock_params()
params:add_separator()
local scales = {}
for i=1,#MusicUtil.SCALES do
scales[i] = MusicUtil.SCALES[i].name
end
params:add_option("scale", "scale", scales)
params:set_action("scale", build_scale)
params:add_option("root", "root", MusicUtil.NOTE_NAMES)
params:set_action("root", build_scale)
params:add_separator()
cs.AMP = cs.new(0,1,'lin',0,0.5,'')
params:add_control("amp", "amp", cs.AMP)
params:set_action("amp",
function(x) engine.amp(x) end)
cs.PW = cs.new(0,100,'lin',0,80,'%')
params:add_control("pw", "pw", cs.PW)
params:set_action("pw",
function(x) engine.pw(x/100) end)
cs.REL = cs.new(0.1,3.2,'lin',0,0.2,'s')
params:add_control("release", "release", cs.REL)
params:set_action("release",
function(x) engine.release(x) end)
cs.CUT = cs.new(50,5000,'exp',0,555,'hz')
params:add_control("cutoff", "cutoff", cs.CUT)
params:set_action("cutoff",
function(x) engine.cutoff(x) end)
cs.GAIN = cs.new(0,4,'lin',0,1,'')
params:add_control("gain", "gain", cs.GAIN)
params:set_action("gain",
function(x) engine.gain(x) end)
params:bang()
end
function reset_pattern()
reset = true
clk:reset()
end
function step()
count()
end
function play_notes()
-- send note off for previously played notes
while #note_off_queue > 0 do
m.send({type='note_off', note=table.remove(note_off_queue)})
end
-- play queued notes
while #note_queue > 0 do
local n = table.remove(note_queue)
engine.hz(MusicUtil.note_num_to_freq(grid_scale[n]+transpose))
m.send({type='note_on', note=n})
table.insert(note_off_queue, n)
end
end
function grid_scale_copy ()
local new_grid_scale = {}
local i, v = next(scale_notes, nil)
local k = 0
while i <= x_max do
new_grid_scale[i] = v
i, v = next(scale_notes, i)
k = k + 1
end
grid_scale = new_grid_scale
end
function build_scale()
scale_notes = MusicUtil.generate_scale_of_length(params:get("root") - 1, params:get("scale"), x_max+1)
grid_scale_copy()
end
function enqueue_note(n)
table.insert(note_queue, n)
end
function g.key(x,y,z)
if running == false and z == 1 then
exists = false
for i=1,#cells do
--check to see if this cell exists
if cells[i].x == x and cells[i].y == y then
exists = true
exist_cell = i
break
end
end
if exists == false then
add_cell(x, y)
if y > 1 then
add_temp_cell(x, y-1)
end
elseif exists == true and cells[exist_cell].direction ~= 1 then
change_direction(exist_cell)
else
remove_cell(exist_cell)
end
end
if running == false and z == 0 then
for i=1,#cells do
--check to get cell
if cells[i].x == x and cells[i].y == y then
exist_cell = i
-- then check the direction of the cell
local direction = cells[exist_cell].direction
local chk_x
local chk_y
if direction == 3 then
chk_x = x
chk_y = y-1
elseif direction == 2 then
chk_x = x+1
chk_y = y
elseif direction == 4 then
chk_x = x
chk_y = y+1
elseif direction == 1 then
chk_x = x-1
chk_y = y
end
for i=1, #temp_cells do
if temp_cells[i].x == chk_x and temp_cells[i].y == chk_y then
remove_temp_cell(i)
break
end
end
break
end
end
end
end
function add_cell(new_x, new_y)
cells[#cells+1] = {
x = new_x,
y = new_y,
direction = 3,
brightness = 8,
initial = {x = new_x, y = new_y}
}
grid_redraw()
end
function add_temp_cell(new_x, new_y)
temp_cells[#temp_cells+1] = {
x = new_x,
y = new_y,
}
grid_redraw()
end
function evaluate_cells()
for i=1, #cells do
for k=i+1, #cells do
if cells[i].x == cells[k].x and cells[i].y == cells[k].y then
collision(i)
collision(k)
cells[i].brightness = 15
cells[k].brightness = 15
elseif cells[i].direction == 1 and cells[k].direction == 2 then
if cells[i].x - 1 == cells[k].x and cells[i].y == cells[k].y then
collision(i)
collision(k)
cells[i].brightness = 15
cells[k].brightness = 15
end
elseif cells[i].direction == 2 and cells[k].direction == 1 then
if cells[i].x + 1 == cells[k].x and cells[i].y == cells[k].y then
collision(i)
collision(k)
cells[i].brightness = 15
cells[k].brightness = 15
end
elseif cells[i].direction == 3 and cells[k].direction == 4 then
if cells[i].y - 1 == cells[k].y and cells[i].x == cells[k].x then
collision(i)
collision(k)
cells[i].brightness = 15
cells[k].brightness = 15
end
elseif cells[i].direction == 4 and cells[k].direction == 3 then
if cells[i].y + 1 == cells[k].y and cells[i].x == cells[k].x then
collision(i)
collision(k)
cells[i].brightness = 15
cells[k].brightness = 15
end
end
end
end
end
function change_direction(exist_cell)
if cells[exist_cell].direction == 3 then
cells[exist_cell].direction = 2
if not (cells[exist_cell].x >= x_max) then
add_temp_cell(cells[exist_cell].x+1, cells[exist_cell].y)
end
elseif cells[exist_cell].direction == 2 then
cells[exist_cell].direction = 4
if not (cells[exist_cell].y >= y_max) then
add_temp_cell(cells[exist_cell].x, cells[exist_cell].y+1)
end
elseif cells[exist_cell].direction == 4 then
cells[exist_cell].direction = 1
if not (cells[exist_cell].x <= 1) then
add_temp_cell(cells[exist_cell].x-1, cells[exist_cell].y)
end
end
end
function collision(exist_cell)
if cells[exist_cell].direction == 1 then
cells[exist_cell].direction = 2
elseif cells[exist_cell].direction == 2 then
cells[exist_cell].direction = 1
elseif cells[exist_cell].direction == 3 then
cells[exist_cell].direction = 4
elseif cells[exist_cell].direction == 4 then
cells[exist_cell].direction = 3
end
end
function remove_cell(cell_num)
local tmp = {}
for i=1,#cells do
if i ~= cell_num then
tmp[#tmp+1] = cells[i]
end
end
cells = tmp
grid_redraw()
end
function remove_last()
local cell = #cells
remove_cell(cell)
end
function remove_all()
cells = {}
grid_redraw()
end
function remove_temp_cell(cell_num)
local tmp = {}
for i=1,#temp_cells do
if i ~= cell_num then
tmp[#tmp+1] = temp_cells[i]
end
end
temp_cells = tmp
grid_redraw()
end
function reset_to_initial()
for i=1,#cells do
cells[i].x = cells[i].initial.x
cells[i].y = cells[i].initial.y
end
grid_redraw()
end
function grid_redraw()
g:all(0)
for i=1,#cells do
g:led(cells[i].x, cells[i].y, cells[i].brightness)
end
for i=1,#temp_cells do
g:led(temp_cells[i].x, temp_cells[i].y, 15)
end
g:refresh()
end
function count()
cell_logic()
evaluate_cells()
grid_redraw()
play_notes()
end
function cell_logic()
for i=1,#cells do
cells[i].brightness = 8
if cells[i].x == 1 and cells[i].direction == 1 then
cells[i].direction = 2
cells[i].x = cells[i].x + 1
enqueue_note(cells[i].y)
elseif cells[i].x == x_max and cells[i].direction == 2 then
cells[i].direction = 1
cells[i].x = cells[i].x - 1
enqueue_note(cells[i].y)
elseif cells[i].y == 1 and cells[i].direction == 3 then
cells[i].direction = 4
cells[i].y = cells[i].y + 1
enqueue_note(cells[i].x)
elseif cells[i].y == y_max and cells[i].direction == 4 then
cells[i].direction = 3
cells[i].y = cells[i].y - 1
enqueue_note(cells[i].x)
elseif cells[i].direction == 4 then
cells[i].y = cells[i].y + 1
elseif cells[i].direction == 3 then
cells[i].y = cells[i].y - 1
elseif cells[i].direction == 2 then
cells[i].x = cells[i].x + 1
elseif cells[i].direction == 1 then
cells[i].x = cells[i].x - 1
end
if cells[i].x == x_max and cells[i].direction == 2 then
cells[i].brightness = 15
elseif cells[i].x == 1 and cells[i].direction == 1 then
cells[i].brightness = 15
elseif cells[i].y == y_max and cells[i].direction == 4 then
cells[i].brightness = 15
elseif cells[i].y == 1 and cells[i].direction == 3 then
cells[i].brightness = 15
end
end
end
function enc(n,d)
if n==1 then
params:delta("bpm", d)
end
redraw()
end
function key(n,z)
if n == 1 and z == 1 then
alt = true
redraw()
elseif n == 1 and z == 0 then
alt = false
redraw()
end
if n == 2 and z == 1 and alt == false then
reset_to_initial()
end
if n == 2 and z == 1 and alt == true then
remove_last()
end
if n == 3 and z == 1 and alt == false then
if running == true then
clk:stop()
running = false
redraw()
else
clk:start()
running = true
redraw()
end
end
if alt == true and n == 3 and z == 1 then
remove_all()
end
end
function redraw()
screen.aa(0)
screen.clear()
screen.move(0,10)
screen.level(4)
if params:get("clock") == 1 then
screen.text("bpm: " .. params:get("bpm"))
end
if alt == false then
screen.move(100,60)
screen.level(4)
if running then
screen.text("stop")
else
screen.text("start")
end
screen.move(50,60)
screen.level(4)
screen.text("reset")
else
screen.move(10,60)
screen.level(1)
screen.text("remove:")
screen.move(100,60)
screen.level(4)
screen.text("all")
screen.move(50,60)
screen.level(4)
screen.text("last")
end
screen.update()
end