-
Notifications
You must be signed in to change notification settings - Fork 6
/
playfair.lua
270 lines (239 loc) · 4.92 KB
/
playfair.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
-- euclidean drummer
-- 2.0.0 @tehn
-- llllllll.co/t/21349
--
-- E1 select
-- E2 density
-- E3 length
-- K2 reset phase
-- K3 start/stop
--
-- K1 = ALT
-- ALT-E1 = bpm
--
-- add samples via param menu
er = require 'er'
engine.name = 'Ack'
g = grid.connect()
ack = require 'ack/lib/ack'
BeatClock = require 'beatclock'
clk = BeatClock.new()
clk_midi = midi.connect()
clk_midi.event = function(data)
clk:process_midi(data)
end
reset = false
alt = false
running = true
track_edit = 1
current_pattern = 0
current_pset = 0
track = {}
for i=1,4 do
track[i] = {
k = 0,
n = 9 - i,
pos = 1,
s = {}
}
end
pattern = {}
for i=1,112 do
pattern[i] = {
data = 0,
k = {},
n = {}
}
for x=1,4 do
pattern[i].k[x] = 0
pattern[i].n[x] = 0
end
end
function reer(i)
if track[i].k == 0 then
for n=1,32 do track[i].s[n] = false end
else
track[i].s = er.gen(track[i].k,track[i].n)
end
end
function trig()
for i=1,4 do
if track[i].s[track[i].pos] then
engine.trig(i-1)
end
end
end
function init()
for i=1,4 do reer(i) end
screen.line_width(1)
clk.on_step = step
clk.on_select_internal = function() clk:start() end
clk.on_select_external = reset_pattern
clk:add_clock_params()
for channel=1,4 do
ack.add_channel_params(channel)
end
ack.add_effects_params()
params:default()
playfair_load()
clk:start()
end
function reset_pattern()
reset = true
clk:reset()
end
function step()
if reset then
for i=1,4 do track[i].pos = 1 end
reset = false
else
for i=1,4 do track[i].pos = (track[i].pos % track[i].n) + 1 end
end
trig()
redraw()
end
function key(n,z)
if n==1 then alt = z
elseif n==2 and z==1 then reset_pattern()
elseif n==3 and z==1 then
if running then
clk:stop()
running = false
else
clk:start()
running = true
end
end
redraw()
end
function enc(n,d)
if n==1 then
if alt==1 then
params:delta("bpm", d)
else
track_edit = util.clamp(track_edit+d,1,4)
end
elseif n == 2 then
track[track_edit].k = util.clamp(track[track_edit].k+d,0,track[track_edit].n)
elseif n==3 then
track[track_edit].n = util.clamp(track[track_edit].n+d,1,32)
track[track_edit].k = util.clamp(track[track_edit].k,0,track[track_edit].n)
end
reer(track_edit)
redraw()
end
function redraw()
screen.aa(0)
screen.clear()
screen.move(0,10)
screen.level(4)
if params:get("clock") == 1 then
screen.text(params:get("bpm"))
else
for i=1,clk.beat+1 do
screen.rect(i*2,1,1,2)
end
screen.fill()
end
for i=1,4 do
screen.level((i == track_edit) and 15 or 4)
screen.move(5, i*10 + 10)
screen.text_center(track[i].k)
screen.move(20,i*10 + 10)
screen.text_center(track[i].n)
for x=1,track[i].n do
screen.level((track[i].pos==x and not reset) and 15 or 2)
screen.move(x*3 + 30, i*10 + 10)
if track[i].s[x] then
screen.line_rel(0,-8)
else
screen.line_rel(0,-2)
end
screen.stroke()
end
end
screen.update()
end
keytimer = 0
function g.key(x,y,z)
print(x,y,z)
local id = x + (y-1)*16
if z==1 then
if id > 16 then
keytimer = util.time()
elseif id < 17 then
params:read("tehn/playfair-" .. string.format("%02d",id) .. ".pset")
params:bang()
current_pset = id
end
else
if id > 16 then
id = id - 16
local elapsed = util.time() - keytimer
if elapsed < 0.5 and pattern[id].data == 1 then
-- recall pattern
current_pattern = id
for i=1,4 do
track[i].n = pattern[id].n[i]
track[i].k = pattern[id].k[i]
reer(i)
end
--reset_pattern()
elseif elapsed > 0.5 then
-- store pattern
current_pattern = id
for i=1,4 do
pattern[id].n[i] = track[i].n
pattern[id].k[i] = track[i].k
pattern[id].data = 1
end
end
end
gridredraw()
end
end
function gridredraw()
g:all(0)
if current_pset > 0 and current_pset < 17 then
g:led(current_pset,1,9)
end
for x=1,16 do
for y=2,8 do
local id = x + (y-2)*16
if pattern[id].data == 1 then
g:led(x,y,id == current_pattern and 15 or 4)
end
end
end
g:refresh()
end
function playfair_save()
local fd=io.open(norns.state.data .. "playfair.data","w+")
io.output(fd)
for i=1,112 do
io.write(pattern[i].data .. "\n")
for x=1,4 do
io.write(pattern[i].k[x] .. "\n")
io.write(pattern[i].n[x] .. "\n")
end
end
io.close(fd)
end
function playfair_load()
local fd=io.open(norns.state.data .. "playfair.data","r")
if fd then
print("found datafile")
io.input(fd)
for i=1,112 do
pattern[i].data = tonumber(io.read())
for x=1,4 do
pattern[i].k[x] = tonumber(io.read())
pattern[i].n[x] = tonumber(io.read())
end
end
io.close(fd)
end
end
cleanup = function()
playfair_save()
end