-
Notifications
You must be signed in to change notification settings - Fork 0
/
lissadron.lua
491 lines (410 loc) · 11.5 KB
/
lissadron.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
-- lissadron
--
--
-- seeding fields forever
--
--
--
-- @LFSaw [20200516]
--
-- E1 amp -- legato
-- E2 x0 -- meta parameter
-- E3 x1 -- meta parameter
--
-- K2 seed--
-- K3 seed++
-- <shift>-K2 seed - 131
-- <shift>-K3 seed + 131
--
-- <shift>-E1 note
-- <shift>-E2 seq_steps
-- <shift>-E3 seq_freq
--
-- K1 <shift>
--
-- additional parameters include
-- control of euclidean
-- sequencer patterns and
-- lazyness of control updates
--
-- https://llllllll.co/t/lissadron/
engine.name = "Lissadron"
bjorklund = include("lissadron/lib/bjorklund")
state = {shift = false, last_touched_ctl = nil, last_touched_counter = 0, last_seed_offset = 0, seed_offset = 0, seq_pattern = nil}
-- knobs and encoders
local kn = { k1=1, k2=2, k3=3 }
local en = { e1=1, e2=2, e3=3 }
local specs = {}
local viewport = { width = 128, height = 64, c_width = 64, c_height = 32, frame = 0, hilight = 15, lolight = 2 }
local symbols = {"@", "*", "#", "-", "+", "\\", "/", "|", "«", "»", "≠", "$", "i", "!", "\'", "\"", "=", "0"}
local intFormats = {seed = 1, seq_steps = 1, seq_freq = 1, note = 1}
--- inits ----------------------------------
function init_ctls()
make_synth_ctl("amp", -90, 0, "linear", 0.0, -90, "")
make_synth_ctl("attack", 0.01, 0.8, "linear", 0.0, 0.02, "")
make_synth_ctl("decay", 0, 1, "linear", 0.0, 0.3, "")
make_seq_dependant_ctl("note", 0, 127, "linear", 0.25, 52, "")
make_seq_dependant_ctl("seed", 0, 16383, "linear", 1, 3079, "") -- 14bit
params:add_separator()
make_synth_ctl("x0", 0, 1, "linear", 0.0, 0.5, "")
make_synth_ctl("x1", 0, 1, "linear", 0.0, 0.5, "")
params:add_separator()
make_ctl("seq_freq", 1, 40, "linear", 1, 1, "")
make_ctl("seq_steps", 1, 24, "linear", 1, 1)
-- make_ctl("seq_pulses", 1, 24, "linear", 1, 24)
make_ctl("seq_fill", 0, 1, "linear", 0, 1)
make_ctl("seq_shift", 0, 1, "linear", 0, 0)
make_synth_ctl("lazy", 0.01, 1, "exp", 0.0, 0.1)
make_synth_ctl("grit", 0, 1, "lin", 0.0, 0.8)
make_synth_ctl("trigOnSeed", 0, 1, "linear", 1, 1, "")
params:bang()
end
function init_screenTimer()
local screen_timer = metro.init()
screen_timer.time = 1 / 15
screen_timer.event = function() redraw() end
screen_timer:start()
local releaseFunc = function ()
while true do
if state.last_touched_counter > 0 then
state.last_touched_counter = state.last_touched_counter - 1
end
-- print(state.last_touched_counter)
clock.sleep(0.1);
end
end
state.last_touched_counter = 0
clock.run(releaseFunc)
end
--- sequencing ----------------------------------
local seq_id -- clock id for sequencer position
function init_seq()
state.seed_offset = -1
update_seq_pattern()
-- state.seq_pattern = bjorklund.bjorklund(1, 1, 0)
seq_id = clock.run(sequencer)
end
function sequencer()
while true do
local steps = params:get("seq_steps")
local freq = 1/params:get("seq_freq")
if (steps > 1) then
update_seq_pattern()
state.last_seed_offset = state.seed_offset
state.seed_offset = (state.seed_offset + 1)%steps
else
state.seed_offset = 0
state.last_seed_offset = 0
engine.seedOffset(0)
-- seq_id = nil
-- break
end
if (state.last_seed_offset ~= state.seed_offset) then
if (state.seq_pattern[state.seed_offset+1] == 1) then
-- print(state.seed_offset)
engine.note(params:get("note"))
engine.seed(params:get("seed"))
engine.trig(1)
engine.seedOffset(state.seed_offset)
local offFunc = function()
clock.sleep(0.01)
engine.trig(0)
end
clock.run(offFunc)
end
end
redraw()
clock.sync(freq)
end
end
function clock.transport.start()
if (seq_id ~= nil) then
clock.cancel(seq_id)
end
print("clock-start")
init_seq()
end
function clock.transport.stop()
if seq_id ~= nil then
clock.cancel(seq_id)
seq_id = nil
print("clock-stop")
end
end
--- MIDI ----------------------------------
local midi_signal_in
function midi_connect()
midi_signal_in = midi.connect(1)
midi_signal_in.event = on_midi_event
end
function on_midi_event(data)
msg = midi.to_msg(data)
update_from_midi(msg)
-- print(msg)
end
function update_from_midi(msg)
if msg.type == 'note_on' then
params:set("note", msg.note)
if (seq_id == nil) or (params:get("seq_steps") == 1) then -- trigger if sequencer is not running
engine.trig(1)
-- print(msg.note)
local offFunc = function()
clock.sleep(0.01)
engine.trig(0)
end
clock.run(offFunc)
end
end
-- OP-1 fix for transport
if msg.type == 'song_position' then
clock.transport.start()
end
end
----------------------------------------------
function init()
midi_connect()
init_ctls()
init_screenTimer()
init_seq()
screen.level(viewport.hilight)
screen.line_width(1)
screen.font_size(10)
end
--- drawing ----------------------------------
function draw_xy(x, y, size, brightness, x0, x1)
local w = x0 * (size-4)
local h = x1 * (size-4)
screen.level(brightness)
screen.line_width(1)
screen.rect(x, y, size, size)
screen.stroke()
screen.rect(x + w + 1, y + size - h - 3, 1, 1)
screen.fill()
end
function draw_state(x, y, size, brightness, amp, lazy, x0, x1, selected, seed)
-- screen.aa(0)
math.randomseed(seed)
screen.line_width(1)
local level
level = math.random(1, math.max(1, brightness))
local w
local h
if math.random() >= 0.5 then
w = math.random(1, math.floor(size * (x0 + 1)))
h = math.random(1, math.floor(size * (x1 + 1)))
else
h = math.random(1, math.floor(size * (x0 + 1)))
w = math.random(1, math.floor(size * (x1 + 1)))
end
-- -- sequencer cursor
-- if selected then
-- screen.level(16)
-- screen.rect(x - 0.5, y-(size/2)-2, 1, 1)
-- screen.rect(x - 0.5, y+(size/2), 1, 1)
-- screen.fill()
-- end
-- element
if brightness > 0 then
screen.level(level)
screen.rect(x - (w/2), y - (h/2), w, h)
screen.fill()
-- screen.aa(1)
local f_size = math.random(5, 20)
screen.move(x-1, y - 20 + (f_size/2))
screen.font_size(f_size)
if selected then
screen.level(16)
else
screen.level(math.max(2, 12-level))
end
screen.text_center(symbols[math.random(1, #symbols)])
-- screen.aa(0)
end
end
function draw_value(x, y, size, brightness, value)
screen.level(brightness)
screen.move(x, y)
screen.aa(0)
screen.font_size(size)
screen.text(tostring(value))
end
-- draw everything
function draw_params()
local amp = params:get_raw("amp")
local seed = params:get("seed")
local x0 = params:get_raw("x0")
local x1 = params:get_raw("x1")
local seq_freq = 1 - params:get_raw("seq_freq")
local seq_steps = params:get("seq_steps")
local lazy = params:get_raw("lazy")
math.randomseed(seed)
screen.aa(0)
-- screen.level(math.random(1, 15))
-- screen.rect(0, viewport.c_height, viewport.width, 14)
-- screen.fill()
-- screen.level(math.random(1, 15))
-- screen.rect(0, viewport.c_height-14, viewport.width, 14)
-- screen.fill()
-- screen.level(math.floor((1-seq_freq) * 16))
-- screen.rect(0, viewport.c_height-28, viewport.width, 14)
-- screen.fill()
-- screen.level(5)
-- screen.rect(0, viewport.c_height, viewport.width, 1)
-- screen.fill()
seq_steps = math.min(seq_steps, 24) -- prevent too many objects to be drawn
local size = 10
local dt = viewport.width/seq_steps
if seq_steps > 1 then
-- make sure to have the right pattern size
if #state.seq_pattern ~= seq_steps then
update_seq_pattern()
end
for i=1,seq_steps do
if state.seq_pattern[i] > 0 then
draw_state(
dt * i - (dt*0.5),
viewport.c_height,
size, 16,
amp, lazy, x0, x1, ((state.seed_offset+1) == i),
seed + (i-1)
)
else
draw_state(
dt * i - (dt*0.5),
viewport.c_height,
size, 0,
amp, lazy, x0, x1, ((state.seed_offset+1) == i),
seed + (i-1)
)
end
-- draw_value(
-- dt * i - (dt*0.5),
-- viewport.c_height,
-- size, 16,
-- i
-- )
end
else
draw_state(
dt/2,
viewport.c_height,
size, 16,
amp, lazy, x0, x1, false,
seed
)
end
draw_xy(viewport.width - 14, viewport.height - 14, 10, 16, x0, x1)
if state.last_touched_counter > 0 then
local val = params:get(state.last_touched_ctl)
if intFormats[state.last_touched_ctl] == nil then
val = string.format("%.3f", val)
else
val = string.format("%i", math.floor(val))
end
local brightness = math.min(state.last_touched_counter, 16)
draw_value(5 , viewport.height - 7, 10, brightness, val)
draw_value(viewport.c_width - 10, viewport.height - 7, 10, brightness, state.last_touched_ctl)
end
end
function redraw()
screen.clear()
draw_params()
screen.update()
end
--- knobs and pots ----------------------------------
function key(n, val)
if n == kn.k1 then
state.shift = (val == 1)
end
local seed = params:get("seed")
if n == kn.k2 and val == 1 then
if state.shift then
params:set("seed", (seed-131) % 18013, false)
else
params:set("seed", seed-1, false)
end
elseif n == kn.k3 and val == 1 then
if state.shift then
params:set("seed", (seed+131) % 18013, false)
else
params:set("seed", seed+1, false)
end
end
end
function enc(n, delta)
local delta = delta
if n == en.e1 then
if state.shift then
params:delta("note", delta)
else
params:delta("amp", delta)
end
end
if n == en.e2 then
if state.shift then
params:delta("seq_steps", delta)
else
params:delta("x0", delta)
end
end
if n == en.e3 then
if state.shift then
params:delta("seq_freq", delta)
else
params:delta("x1", delta)
end
end
end
--- helpers ----------------------------------
function make_synth_ctl(name, min, max, warp, default, start, label)
local spec = controlspec.new(min, max, warp, default, start, label)
specs[name] = spec
local update_func = function(val)
state.last_touched_ctl = name
state.last_touched_counter = 32
engine[name](val)
end
params:add{
type="control",
id=name,
controlspec=spec,
action=update_func
}
end
function make_seq_dependant_ctl(name, min, max, warp, default, start, label)
local spec = controlspec.new(min, max, warp, default, start, label)
specs[name] = spec
local update_func = function(val)
state.last_touched_ctl = name
state.last_touched_counter = 32
if ( ((params:get("seq_steps") < 2) or (seq_id == nil)) ) then -- only update if sequencer is not running
engine[name](val)
end
end
params:add{
type="control",
id=name,
controlspec=spec,
action=update_func
}
end
function update_seq_pattern()
local steps = params:get("seq_steps")
state.seq_pattern = bjorklund.bjorklund(steps, math.max(1, math.ceil(params:get("seq_fill") * steps)), math.floor(params:get("seq_shift") * (steps-1)))
end
function make_ctl(name, min, max, warp, default, start, label)
local spec = controlspec.new(min, max, warp, default, start, label)
specs[name] = spec
local update_func = function(val)
state.last_touched_ctl = name
state.last_touched_counter = 32
update_seq_pattern()
-- engine[name](val)
end
params:add{
type="control",
id=name,
controlspec=spec,
action=update_func
}
end