-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrumcrowOLD.lua
422 lines (422 loc) · 14.8 KB
/
drumcrowOLD.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
--- drumcrow
note_min = -8.03127 -- (-8.03127, 1Hz, 1sec) (-11.3532, 0.1Hz, 10sec) (-14.67513, 0.01Hz, 100sec) (-17.997, 0.001Hz, 1000sec)
states = {}
ratios = {}
caw_mult = {1, 1, 1, 1}
flaps = {1, 1, 1, 1}
channel = 1
parameter = 0
c2 = {}
act = 0
shapes = {'linear','sine','logarithmic','exponential','now','wait','over','under','rebound'}
param_list = {}
clock_ID = {}
clock_on = {0,0,0,0}
function setup_state(ch)
states[ch] = {
tlenA = 1, trepA = 2, tlenB = 2, trepB = 2, flaps = 4, caw1 = 1, caw2 = 1, caw3 = 1, caw4 = 1,
ant = 0, aamp = 1, apw = 0, apw2 = 0, abit = 0, afr = 4, asy = -1, acr = 3, atype = 0, aph = 1,
lnt = 0, lamp = 0, lpw = 0, lpw2 = 0, lbit = 0, lfr = 5, lsy = 0, lcr = 0, ltype = 1, lph = -1,
ent = 0, eamp = 0, epw = 0, epw2 = 0, ebit = 0, efr = 1, esy = -1, ecr = 4, etype = 0, eph = 1,
nte = 0, amp = 4, pw = 0, pw2 = 4, bit = 0, cawfr3 = 0, cawfr4 = 0, cawnte = 1, splash = 0, mdl = 1,
}
end
function setup_ratio(ch)
ratios[ch] = {
tlenA = 0, trepA = 0, tlenB = 0, trepB = 0, flaps = 0, caw1 = 0, caw2 = 0, caw3 = 0, caw4 = 0,
ant = 0, aamp = 0, apw = 0, apw2 = 0, abit = 0, afr = 0, asy = 0, acr = 0, atype = 0, aph = 0,
lnt = 0, lamp = 0, lpw = 0, lpw2 = 0, lbit = 0, lfr = 0, lsy = 0, lcr = 0, ltype = 0, lph = 0,
ent = 0, eamp = 0, epw = 0, epw2 = 0, ebit = 0, efr = 0, esy = 0, ecr = 0, etype = 0, eph = 0,
nte = 0, amp = 0, pw = 0, pw2 = 0, bit = 0, cawfr3 = 0, cawfr4 = 0, cawnte = 0, splash = 0, mdl = 0,
}
end
function v10_to_int(v) return (v >= 1) and (v - v % 1) or (v <= -1) and (-1*(v + (-1*v) % 1)) or 1 end
function v10_to_ratio(v) return (v >= 1) and math.floor(v) or (v >= -9) and 1/(-1*(math.floor(v)-1)) or 0 end
function make_divide(divisor) return function (x) return x / divisor end end
function make_rectify_right(thresh, out_coeff, static)
return function (v) return (v <= thresh) and (2 ^ (v*out_coeff)) or (v > thresh) and static end
end
function make_rectify_left(thresh, out_coeff, static)
return function (v) return (v >= thresh) and (2 ^ (v*out_coeff)) or (v < thresh) and static end
end
function setup_hof_param (index, param_name, fn)
param_list[index] = param_name
c2[index] = function (ch, v) set_state(ch, param_name, fn(v)) end
end
local bad_param = function (ch, v) end
setup_hof_param(11, 'nte', make_divide(1))
setup_hof_param(12, 'amp', make_divide(2))
setup_hof_param(13, 'pw', make_divide(10))
setup_hof_param(14, 'pw2', make_divide(1))
setup_hof_param(15, 'bit', make_divide(1))
setup_hof_param(16, 'cawfr4', make_divide(5))
setup_hof_param(17, 'cawfr3', make_divide(5))
setup_hof_param(18, 'cawnte', make_divide(1))
setup_hof_param(19, 'splash', make_divide(2))
setup_hof_param(21, 'ent', make_divide(1))
setup_hof_param(22, 'eamp', make_divide(3))
setup_hof_param(23, 'epw', make_divide(5))
setup_hof_param(24, 'epw2', make_divide(1))
setup_hof_param(25, 'ebit', make_divide(1))
setup_hof_param(26, 'efr', make_rectify_right(9.5, -0.7, 0.0000000002328))
setup_hof_param(27, 'esy', make_divide(1))
setup_hof_param(28, 'ecr', make_divide(2))
setup_hof_param(29, 'etype', make_divide(1))
setup_hof_param(31, 'lnt', make_divide(1))
setup_hof_param(32, 'lamp', make_divide(3))
setup_hof_param(33, 'lpw', make_divide(5))
setup_hof_param(34, 'lpw2', make_divide(1))
setup_hof_param(35, 'lbit', make_divide(1))
setup_hof_param(36, 'lfr', make_rectify_left(-9.5, 1, 0.0000000002328))
setup_hof_param(37, 'lsy', make_divide(5))
setup_hof_param(38, 'lcr', make_divide(2))
setup_hof_param(39, 'ltype', make_divide(1))
setup_hof_param(41, 'ant', make_divide(1))
setup_hof_param(42, 'aamp', make_divide(3))
setup_hof_param(43, 'apw', make_divide(5))
setup_hof_param(44, 'apw2', make_divide(1))
setup_hof_param(45, 'abit', make_divide(1))
setup_hof_param(46, 'afr', make_rectify_right(9.5, -0.7, 0.0000000002328))
setup_hof_param(47, 'asy', make_divide(5))
setup_hof_param(48, 'acr', make_divide(2))
setup_hof_param(49, 'atype', make_divide(1))
setup_hof_param(51, 'tlenA', v10_to_ratio)
setup_hof_param(52, 'trepA', v10_to_int)
setup_hof_param(53, 'tlenB', v10_to_ratio)
setup_hof_param(54, 'trepB', v10_to_int)
param_list[55] = 'flaps'
c2[55] = function (ch, v)
v = math.floor((((v+10)/20)*3.9+1))
set_state(ch, 'flaps', v)
end
setup_hof_param(56, 'caw1', v10_to_ratio)
setup_hof_param(57, 'caw2', v10_to_ratio)
setup_hof_param(58, 'caw3', v10_to_ratio)
setup_hof_param(59, 'caw4', v10_to_ratio)
param_list[81] = 'tempo'
c2[81] = function (ch, v) clock.tempo = (v+10.1) * 100 end
param_list[82] = 'update_time'
c2[82] = function (ch, v)
v = (v + 10) / 20 * (0.1 - 0.002) + 0.002
input[1]{mode = 'stream', time = v}
end
function u16_to_v10(u16) return u16/16384*10 end
function get_digits(b1)
local digits = {}
for i = 1, 5 do digits[i] = b1 % 10; b1 = (b1 - digits[i]) / 10 end
local action = (digits[5] * 10) + digits[4]
local param = (digits[3] * 10) + digits[2]
local ch = digits[1] % 5
return digits, action, param, ch
end
function setup_input()
input[1].stream = function (v)
v = (v <= 0) and 0 or (v >= 10) and 10 or v
v = (math.min(math.max(v, -10), 10) - 5) * 2
if act == 1 then set_ratio(channel, param_list[parameter], v) end
;(c2[parameter] or bad_param)(channel,v)--KEEP SEMICOLON!
for i = 1, 4 do if i ~= nil then update_synth(i) end end
end
input[1]{mode = 'stream', time = 0.005}
end
function setup_synth(ch, model, shape)
function var_saw(shape)
return loop{to(dyn{amp=2}, dyn{cyc=1/440} * dyn{pw=1/2}, shape), to(0-dyn{amp=2}, dyn{cyc=1/440} * (1-dyn{pw=1/2}), shape)}
end
function bytebeat(shape)
return loop{to(dyn{x=1}:step(dyn{pw=1}):wrap(-20,20) * dyn{amp=2}, dyn{cyc=1}, shape)}
end
function noise(shape)
return loop{to(dyn{x=1}:mul(dyn{pw2=1}):step(dyn{pw=1}):wrap(-10,10) * dyn{amp=2}, dyn{cyc=1}/2, shape)}
end
function FMstep(shape)
return loop{
to( dyn{amp=2}, dyn{x=1}:step(dyn{pw2=1}):wrap(1,2) * dyn{cyc=1} * dyn{pw=1}, shape),
to(0-dyn{amp=2}, dyn{x=1} * dyn{cyc=1} * (1-dyn{pw=1}), shape)
}
end
function ASLsine(shape)
return loop{to((dyn{x=0}:step(dyn{pw=0.314}):wrap(-3.14,3.14) + 0.101321 * dyn{x=0} * dyn{x=0} * dyn{x=0}) * dyn{amp=2}, dyn{cyc=1}, shape)}
end
function ASLharmonic(shape)
return loop{to((dyn{x=0}:step(dyn{pw=1}):mul(-1):wrap(-3.14,3.14) + 0.101321 * dyn{x=0} * dyn{x=0} * dyn{x=0}) * dyn{amp=2}, dyn{cyc=1}, shape)}
end
function bytebeat5(shape)
return loop{to(dyn{x=0}:step(dyn{pw=0.1}):wrap(0, 10) % dyn{pw2=1} * dyn{amp=2}, dyn{cyc=1}, shape)}
end
states[ch].mdl = model
if model == 1 then output[ch]( var_saw(shapes[shape]) )
elseif model == 2 then output[ch]( bytebeat(shapes[shape]) )
elseif model == 3 then output[ch]( noise(shapes[shape]) )
elseif model == 4 then output[ch]( FMstep(shapes[shape]) )
elseif model == 5 then output[ch]( ASLsine(shapes[shape]) )
elseif model == 6 then output[ch]( ASLharmonic(shapes[shape]) )
elseif model == 7 then output[ch]( bytebeat5(shapes[shape]) )
else output[ch]( var_saw(shapes[shape]) ) end
end
function setup_i2c()
ii.self.call1 = function (b1)
if b1 == nil then print("CAW!") return end
digits, action, param, ch = get_digits(b1)
process_action(digits, action, param, ch, 1)
end
ii.self.call2 = function (b1, v)
if b1 == nil or v == nil then print("CAW!") return end
digits, action, param, ch = get_digits(b1)
v = u16_to_v10(math.min(math.max(v, -16384), 16384))
print(" action "..action.." param "..param.." ch "..ch.." v "..v)
process_action(digits, action, param, ch, 2, v)
end
ii.self.call3 = function (ch, note, amp)
local function trig_234(ch, note, amp)
if ratios[ch].nte == 0 then set_state(ch, 'nte', u16_to_v10(note), 3) end
if ratios[ch].amp == 0 then set_state(ch, 'amp', u16_to_v10(amp), 3) end
trigger_note(ch)
end
local function trig_1(ch, note, amp)
set_state(ch, 'nte', u16_to_v10(note), 3)
set_state(ch, 'amp', u16_to_v10(amp), 3)
trigger_note(ch)
end
if ch == nil or note == nil or amp == nil then print("CAW!") return end
ch = ch % 5;
note = math.min(math.max(note, -16384), 16384)
amp = math.min(math.max(amp, -16384), 16384)
if ch == 0 then
trig_1(1, note, amp)
for i = 2,4 do
trig_234(i, note, amp)
end
elseif ch ~= 1 then
trig_234(ch, note, amp)
else
trig_1(ch, note, amp)
end
end
end
function process_action(digits, action, param, ch, cmd, v)
v = v or 0
if action == 2 then -- 2: set new ASL construct (shape, model) keep act the same
if ch == 0 then
for i = 1, 4 do
setup_synth(i, digits[2], digits[3])
end
else
setup_synth(ch, digits[2], digits[3])
end
print("Setup Synth, Shape: "..digits[3].." Engine: "..digits[2].." Channel: "..ch)
elseif action == 1 then -- 1: set ratio
if param == 86 then -- init
if ch == 0 then
for i = 2, 4 do
setup_ratio(i)
print("Init ratio: "..i)
end
elseif ch ~= 1 then
setup_ratio(ch)
print("Init ratio: "..ch)
end
elseif ch ~= 1 then
if cmd == 1 then
channel = ch; parameter = param; act = action
else
set_ratio(ch, param_list[param], v)
end
print("Ratio to Ch1, Param: "..param.." Channel: "..ch)
else
channel = 1; parameter = 0; act = 0
print("Deselect")
end
elseif action == 0 then -- 0: set state
if param == 86 then -- init
if ch == 0 then
for i = 1, 4 do
setup_state(i); setup_synth(i, 1, 1)
print("Init Channel: "..i)
end
else
setup_state(ch); setup_synth(ch, 1, 1)
print("Init Channel: "..ch)
end
elseif param == 85 then -- reset tseq
if ch == 0 then
for i = 1, 4 do
trig_enable(i, true)
print("CLK Reset Channel: "..i)
end
else
trig_enable(ch, true)
print("CLK Reset Channel: "..ch)
end
elseif param == 50 then -- TRIG enable/disable
if ch == 0 then
for i = 1, 4 do
trig_enable(i, false)
print("CLK ON/OFF Channel: "..i)
end
else
trig_enable(ch, false)
print("CLK ON/OFF Channel: "..ch)
end
elseif param_list[param] ~= nil then -- set state
if cmd == 2 then
-- set_state(ch, param_list[param], v, 2)
;(c2[param] or bad_param)(ch,v)
else
channel = ch; parameter = param; act = action
end
print("INPUT to Param: "..param.." Channel: "..ch)
else
channel = 1; parameter = 0; act = 0
print("Deselect")
end
else
channel = 1; parameter = 0; act = 0
print("Deselect")
end
end
function set_state(ch, key, v, cmd)
local function check_ratio_zero(ch, key, value)
if ratios[ch][key] == 0 then
states[ch][key] = value
elseif key ~= 'nte' then
states[ch][key] = states[1][key] * ratios[ch][key]
else
states[ch][key] = states[1][key]
end
end
local cmd = cmd or 0
if ch == 0 then
if act == 1 then
for i = 2,4 do
check_ratio_zero(i, key, 0)
end
else
for i = 1,4 do
if i == 1 then
states[i][key] = v
else
check_ratio_zero(i, key, v)
end
end
end
elseif ch ~= 1 then
if act == 1 then
if cmd == 3 then
check_ratio_zero(ch, key, v)
else
check_ratio_zero(ch, key, 0)
end
else
check_ratio_zero(ch, key, v)
end
elseif ch == 1 then
states[ch][key] = v
for i = 2,4 do
if ratios[i][key] ~= 0 then
if key ~= 'nte' then
states[i][key] = states[1][key] * ratios[i][key]
else
states[i][key] = states[1][key]
end
end
end
end
end
function set_ratio(ch, key, v)
ratio_keys = { nte = true, efr = true, afr = true, lfr = true, tlenA = true, trepA = true, tlenB = true, trepB = true, caw1 = true, caw2 = true, caw3 = true, caw4 = true }
local function check_ratio(chan, key)
if ratios[chan][key] ~= nil then
ratios[chan][key] = ratio_keys[key] and v10_to_ratio(v) or math.floor(5*v)/10
end
end
if ch == 0 then for i = 2,4 do check_ratio(i, key) end
elseif ch ~= 1 then check_ratio(ch, key) end
end
function acc(phase, freq, sec, looping)
phase = phase + (freq * sec)
phase = looping and (1 + phase) % 2 - 1 or math.max(math.min(1, phase), -1)
return phase
end
function peak(ph, pw, curve)
local value = ph < pw and (1 + ph) / (1 + pw) or ph > pw and (1 - ph) / (1 - pw) or 1
value = value ^ (2 ^ curve)
return value
end
function update_synth(i)
local s = states[i]
local sec = input[1].time
s.aph = acc(s.aph, s.cawfr4 > 0 and s.afr * caw_mult[i] / s.cawfr4 or s.afr, sec, s.atype > 0)
local ampenv = peak(s.aph, s.asy, s.acr)
s.lph = acc(s.lph, s.cawfr3 > 0 and s.lfr * caw_mult[i] * s.cawfr3 or s.lfr, sec, s.ltype > 0)
local lfo = peak(s.lph, s.lsy, s.lcr)
s.eph = acc(s.eph, s.efr, sec, s.etype > 0)
local modenv = peak(s.eph, s.esy, s.ecr)
local note = s.nte + (modenv * s.ent) + (lfo * s.lnt) + (ampenv * s.ant)
local volume = (modenv * s.eamp * s.amp) + (lfo * s.lamp * s.amp) + (ampenv * s.aamp * s.amp)
local pw = s.pw + (modenv * s.epw) + (lfo * s.lpw) + (ampenv * s.apw)
local pw2 = s.pw2 + (modenv * s.epw2) + (lfo * s.lpw2) + (ampenv * s.apw2)
local bitz = s.bit + (modenv * s.ebit) + (lfo * s.lbit) + (ampenv * s.abit)
local freq = note > note_min and note < 6.25643 and math.min(math.max(267.9 * (2 ^ note), 1), 20000) or (note >= 6.25643 and 20000 or 1)
if i ~= 1 and ratios[i].nte ~= 0 then freq = freq * ratios[i].nte end
local cyc = 1/(freq * (s.cawnte > 0 and caw_mult[i] or 1))
output[i].dyn.cyc = s.splash > 0 and (math.random()*0.1 < cyc/0.1 and cyc + (cyc * 0.2 * math.random()*s.splash) or cyc + math.random()*0.002*s.splash) or cyc
output[i].dyn.amp = math.min(math.max(volume, -10), 10)
if bitz > 0 then output[i].scale({}, 2, bitz * 3) else output[i].scale('none') end
pw = (math.min(math.max(pw, -1), 1) + 1) / 2
if s.mdl == 2 or s.mdl == 5 or s.mdl == 6 then output[i].dyn.pw = pw * pw2
elseif s.mdl == 3 or s.mdl == 7 then
output[i].dyn.pw = pw
output[i].dyn.pw2 = pw2
elseif s.mdl == 4 then
output[i].dyn.pw = pw
output[i].dyn.pw2 = pw2 / 50
else output[i].dyn.pw = pw end
end
function trigger_note(ch)
if states[ch].aph >= states[ch].asy then states[ch].aph = -1 end
if states[ch].eph >= states[ch].esy then states[ch].eph = -1 end
end
function trigger_seq(ch)
local sq_caw = sequins{56,57,58,59}
local sq_time = sequins{51,53}
local sq_reps = sequins{52,54}
local _time = states[ch][param_list[sq_time()]]
local _reps = states[ch][param_list[sq_reps()]]
local rep_ct = 0;
while clock_on[ch] == 1 do
if rep_ct >= _reps then
rep_ct = 0;
_time = states[ch][param_list[sq_time()]]
_reps = states[ch][param_list[sq_reps()]]
sq_caw:step(math.floor(states[ch].flaps)%4+1)
end
rep_ct = rep_ct + 1
cmult = states[ch][param_list[sq_caw()]]
caw_mult[ch] = (cmult == 0) and 0.1 or cmult
trigger_note(ch)
clock.sync(_time)
end
end
function trig_enable(ch, reset)
if reset == false then
if clock_on[ch] == 0 then
clock_on[ch] = 1
clock_ID[ch] = clock.run(trigger_seq, ch)
else
clock_on[ch] = 0
clock.cancel(clock_ID[ch])
end
else
if clock_on[ch] == 1 then clock.cancel(clock_ID[ch]) end
clock_ID[ch] = clock.run(trigger_seq, ch)
end
end
function init()
clock.tempo = 300
setup_state(1); setup_synth(1, 1, 1);
for i = 2, 4 do setup_state(i); setup_ratio(i); setup_synth(i, 1, 1); end
setup_i2c()
setup_input()
print("setup complete!")
end