-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgrd.lua
executable file
·283 lines (252 loc) · 7.81 KB
/
grd.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
-- grd
--
-- :::..
--
-- k3 start/stop
-- k2 page
-- k1+k2 random params
-- enc1 section
-- enc2,3 params
local sc = include('lib/sc')
engine.name = 'Grd'
screen.font_face(1)
screen.font_size(8)
local playing = false
local section = 0
local nsections = 2
local page = 0
local npages = 4
local nsounds = 9
local random_mode = {0,2,4,6,8,9,11} -- lydian augmented
local metro_draw
-- see https://monome.org/docs/norns/reference/lib/lfo
_lfos = require 'lfo'
local grd1 = {}
grd1['xn'] = {}
grd1['n'] = 64
grd1.fill = function(self)
for i=1,self.n do self.xn[i] = math.random()*2-1 end
end
grd1.next = function(self,r,g,fx,map)
local prev = { table.unpack(self.xn) }
local halfG = g * 0.5
local rx = map(r)
local n = self.n
for i=1,n do
self.xn[i] = sc.fold2(
((1.0 - g) * fx(rx, prev[i]))
+
(halfG * (fx(rx, prev[(i+1)%n+1]) + fx(rx, prev[(i-1)%n+1])))
)
end
end
local fx = function(r,x) return 1 - (r*(x*x)) end
local map = function(r) return sc.lin1(r,1,2) end
grd1:fill()
local function sendsc()
local _r = params:get('_r')
local _g = params:get('_g')
grd1:next(_r, _g, fx, map)
engine.ping(table.unpack(grd1.xn))
end
-- MIDI SETUP
local clk_midi = midi.connect()
clk_midi.event = function(data)
local d = midi.to_msg(data)
if d.type == "start" then
if not playing then
clock.transport.reset()
clock.transport.start()
end
elseif d.type == "continue" then
if playing then
clock.transport.stop()
else
clock.transport.start()
end
end
if d.type == "stop" then
clock.transport.stop()
end
-- placeholder for MIDI CC messages
if d.type == "cc" then
-- print("ch:".. d.ch .. " " .. d.type .. ":".. d.cc.. " ".. d.val)
end
end
-- clock
function pulse()
while true do
clock.sync(params:get('_delta'))
sendsc()
end
end
function clock.transport.start()
print("transport.start")
id = clock.run(pulse)
playing = true
end
function clock.transport.stop()
print("transport.stop")
clock.cancel(id)
playing = false
end
function clock.transport.reset()
print("transport.reset")
end
function init()
params:add_group('LFOs', 15*7) -- rows * #lfo
r_lfo = _lfos:add{min = 0, max = 1}
r_lfo:add_params('r_lfo', 'R')
r_lfo:set('action', function(s) params:set('_r', s) end)
g_lfo = _lfos:add{min = 0, max = 1}
g_lfo:add_params('g_lfo', 'G')
g_lfo:set('action', function(s) params:set('_g', s) end)
delta_lfo = _lfos:add{min = 0.02, max = 2}
delta_lfo:add_params('delta_lfo', 'delta')
delta_lfo:set('action', function(s) params:set('_delta', s) end)
duration_lfo = _lfos:add{min = 0.05, max = 23}
duration_lfo:add_params('duration_lfo', 'duration')
duration_lfo:set('action', function(s) params:set('_duration', s) end)
root_lfo = _lfos:add{min = 20, max = 90}
root_lfo:add_params('root_lfo', 'root')
root_lfo:set('action', function(s) params:set('_root', s) end)
mode_lfo = _lfos:add{min = 0, max = 6}
mode_lfo:add_params('mode_lfo', 'mode')
mode_lfo:set('action', function(s) params:set('_mode', s) end)
sound_lfo = _lfos:add { min=0, max=nsounds-1 }
sound_lfo:add_params('sound_lfo', 'sound')
sound_lfo:set('action', function(s) params:set('_sound', s) end)
params:add_control('_r','R',controlspec.new(0,1,'lin',0.001,0.7,'',0.001,false))
params:add_control('_g','G',controlspec.new(0,1,'lin',0.001,0.1,'',0.001,false))
params:add_control('_delta','delta',controlspec.new(0.02,2,'lin',0.01,0.3,'',0.01,false))
params:add_control('_duration','dur',controlspec.new(0.05,23,'exp',0.05,0.6,'',0.05,false))
params:set_action('_duration', function(duration) engine.pong(duration) end)
params:add_control('_root','root',controlspec.new(0,127,'lin',1,50,''))
params:set_action('_root', function(root) engine.set_root(root) end)
params:add_control('_mode','mode',controlspec.new(0,6,'lin',1,0,''))
params:set_action('_mode', function(mode) engine.set_mode(mode) end)
params:add_control('_sound','sound',controlspec.new(0,nsounds-1,'lin',1,0,''))
params:set_action('_sound', function(sound) engine.set_sound(sound) end)
metro_draw = metro.init(function() redraw() end, 1/60)
metro_draw:start()
end
redraw_pages = {} -- page, section, element
for i = 0, npages-1 do
redraw_pages[i] = {}
for j = 0, nsections-1 do
redraw_pages[i][j] = {}
end
end
redraw_pages[0][0][1] = function() screen.text('R: ' .. sc.round(params:get('_r'),3)) end
redraw_pages[0][0][2] = function() screen.text('G: ' .. sc.round(params:get('_g'),3)) end
redraw_pages[1][0][1] = function() screen.text('delta: ' .. sc.round(params:get('_delta'),3)) end
redraw_pages[1][0][2] = function() screen.text('dur: ' .. sc.round(params:get('_duration'),3)) end
redraw_pages[2][0][1] = function() screen.text('root: ' .. params:get('_root')) end
redraw_pages[2][0][2] = function() screen.text('mode: ' .. params:get('_mode')) end
redraw_pages[3][0][1] = function() screen.text('sound: ' .. params:get('_sound')) end
redraw_pages[3][0][2] = function() end
for i = 1,3 do
for j = 1,2 do
redraw_pages[i][1][j] = function() end
end
end
redraw_pages[0][1][1] = function() screen.text('tempo: ' .. params:get('clock_tempo')) end
redraw_pages[0][1][2] = function() screen.text('reverb: ' .. params:string('reverb')) end
-- should be global
function redraw()
local index = 1
screen.clear()
for i=1,8 do
for j=1,8 do
screen.level(math.floor(sc.lin2(grd1.xn[index],0,15)))
screen.rect(i*6, j*6, 5, 5)
screen.fill()
index = index + 1
end
end
offset = 3
row = 8 + offset
if section == 0 then
for i = 0,3 do
screen.level(page == i and 15 or 2)
screen.move(64, row)
redraw_pages[i][0][1]()
row = row + 6
screen.move(64,row)
redraw_pages[i][0][2]()
row = row + 6
end
elseif section == 1 then
if page > 1 then page = 0 end
for i = 0,1 do
screen.level(page == i and 15 or 2)
screen.move(64,row)
redraw_pages[i][1][1]()
row = row + 6
screen.move(64,row)
redraw_pages[i][1][2]()
row = row + 6
end
end
screen.update()
end
function key(n,z)
if n == 1 then
alt = z==1
elseif n == 3 and z == 1 then
if not playing then
clock.transport.start()
else
clock.transport.stop()
end
elseif n == 2 and z == 1 then
if not alt==true then
local pdiv = section==0 and npages or 2
page = (page+1)%pdiv
else
params:set('_r', sc.round(math.random(), 3));
params:set('_g', sc.round(math.random(), 3));
params:set('_delta', sc.round(math.random(), 2));
params:set('_duration', sc.round(math.random()*7, 2));
params:set('_root', math.random(40,55));
params:set('_mode', math.random(0,7));
end
end
end
enc_update = {} -- page, section, enc
for i = 0, npages-1 do
enc_update[i] = {}
for j = 0, nsections-1 do
enc_update[i][j] = {}
end
end
enc_update[0][0][2] = function(d) params:delta('_r', d) end
enc_update[0][0][3] = function(d) params:delta('_g', d) end
enc_update[1][0][2] = function(d) params:delta('_delta', d) end
enc_update[1][0][3] = function(d) params:delta('_duration', d) end
enc_update[2][0][2] = function(d) params:delta('_root', d) end
enc_update[2][0][3] = function(d) params:delta('_mode', d) end
enc_update[3][0][2] = function(d) params:delta('_sound', d) end
enc_update[3][0][3] = function(d) end
enc_update[0][1][2] = function(d) params:delta('clock_tempo', d) end
enc_update[0][1][3] = function(d)
local rev = d > 0 and 2 or 1
params:set('reverb', rev)
if rev == 1 then audio.rev_off() else audio.rev_on() end
norns.state.mix.aux = rev
end
for i=2,3 do
enc_update[i][1][2] = function(d) end
enc_update[i][1][3] = function(d) end
end
function enc(n,d)
if n == 1 then
section = util.clamp(section + d, 0, nsections-1)
else
enc_update[page][section][n](d)
end
end
function cleanup()
metro_draw:stop()
metro.free_all()
end