-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostskript.lua
More file actions
526 lines (437 loc) · 11.7 KB
/
postskript.lua
File metadata and controls
526 lines (437 loc) · 11.7 KB
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
--
--
--
--
--
--
-- postskript
-- ...
-- v1.2 / imminent gloom
--
-- primitive sampler
--
-- arc recomended!
--
-- controls, arc:
-- E1 - rate
-- E2 - level
-- E3 - start
-- E4 - length
-- K1 - hold to record
--
-- controls, norns:
-- E1 - rate
-- E1 + K1 - level
-- E2 - start
-- E3 - length
-- K2 - hold to record
-- K3 - clear
-- setup
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
tab = require("tabutil")
a = arc.connect()
local save_on_exit = false
local s = screen
local fps = 120
local frame = 1
local recording = false
local stopped = true
local press_time = 5
local prev_press_time = press_time
local active_buffer = 1
local recording_voice = 1
local playing_voice = 2
local loop_start = 0
local loop_length = 320
local loop_end = 320
local playback_position = 1
local max_length = 320
local min_length = 0.01
local max_level = 0
local level = 1
local rate = 1
local waveform = {}
-- params
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
params:add_group("postskript", "postskript", 4)
params:add_control("rate", "rate", controlspec.new(1/2, 1, 0.001, 0.001, 1))
params:set_action("rate",
function(x)
rate = x
softcut.rate(playing_voice, x)
end
)
params:add_control("level", "level", controlspec.new(0, 2.0, 0.001, 0.001, 1))
params:set_action("level",
function(x)
level = x
softcut.level(playing_voice, x)
end
)
params:add_control("start", "start", controlspec.new(0.0, 1.0, 0.001, 0.001, 0))
params:set_action("start",
function(x)
loop_start = press_time * x
loop_end = util.clamp(loop_start + loop_length, min_length, press_time)
softcut.loop_start(playing_voice, loop_start)
softcut.loop_end(playing_voice, loop_end)
end
)
params:add_control("length", "length", controlspec.new(0.0, 1.0, 0.001, 0.001, 1))
params:set_action("length",
function(x)
loop_length = press_time * x
loop_start = util.clamp(loop_start, 0.0, loop_start)
loop_end = util.clamp(loop_start + loop_length, min_length, press_time)
softcut.loop_start(playing_voice, loop_start)
softcut.loop_end(playing_voice, loop_end)
end
)
-- functions
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
local function record()
recording = true
stopped = false
prev_press_time = press_time
press_time = 0
clk_press_timer:start()
softcut.buffer_clear_channel(active_buffer)
softcut.buffer(recording_voice, active_buffer)
softcut.position(recording_voice, 0)
softcut.position(playing_voice, 0)
softcut.loop_start(recording_voice, 0)
softcut.loop_end(recording_voice, 320)
softcut.rec(recording_voice, 1)
--params:bang()
end
local function play()
recording = false
playing = true
clk_press_timer:stop()
press_time = press_time - min_length
softcut.rec(recording_voice, 0)
softcut.position(recording_voice, 0)
softcut.position(playing_voice, 0)
softcut.loop_start(recording_voice, 0)
softcut.loop_end(recording_voice, press_time)
softcut.play(recording_voice, 1)
softcut.play(playing_voice, 0)
softcut.render_buffer(active_buffer, 0, press_time, 64)
params:set("rate", 1)
params:set("level", 1)
params:set("start", 0)
params:set("length", 1)
if active_buffer == 1 then
active_buffer = 2
recording_voice = 2
playing_voice = 1
else
active_buffer = 1
recording_voice = 1
playing_voice = 2
end
end
local function clear()
stopped = true
playing = false
softcut.play(1, 0)
softcut.play(2, 0)
softcut.position(1, 0)
softcut.position(2, 0)
softcut.buffer_clear()
params:set("rate", 1)
params:set("level", 1)
params:set("start", 0)
params:set("length", 1)
for n = 1, 64 do waveform[n] = 0 end
end
local function on_render(ch, start, i, s)
waveform = s
for n = 1, 32 do
table.insert(waveform, 1, waveform[#waveform])
table.remove(waveform, #waveform)
end
end
local function on_position(i, pos)
playback_position = pos
end
-- modified from Arc:segment()
local function arc_segment(ring, from, to, level)
local tau = math.pi * 2
local function overlap(a, b, c, d)
if a > b then
return overlap(a, tau, c, d) + overlap(0, b, c, d)
elseif c > d then
return overlap(a, b, c, tau) + overlap(a, b, 0, d)
else
return math.max(0, math.min(b, d) - math.max(a, c))
end
end
local function overlap_segments(a, b, c, d)
a = a % tau
b = b % tau
c = c % tau
d = d % tau
return overlap(a, b, c, d)
end
local m = {}
local sl = tau / 64
for i=1, 64 do
local sa = tau / 64 * (i - 1)
local sb = tau / 64 * i
local o = overlap_segments(math.rad(from), math.rad(to), sa, sb) -- added math.rad()
m[i] = util.round(o / sl * level)
if m[i] > 0 then -- skip updates if zero so we can draw over other things
a:led(ring, i, m[i])
end
end
end
-- clock events
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function ui_event_arc()
while true do
clock.sleep(1/fps)
if frame > 120 then
frame = 1
else
frame = frame + 1
end
arc_redraw()
end
end
function ui_event_screen()
while true do
clock.sleep(1/fps/4)
redraw()
end
end
function delayed_init_event()
clock.sleep(2)
params:set("gridkeys_nb_voice", 17)
-- params:set("sidv_nb_player", 17)
nb:add_player_params()
end
function press_timer_event()
if press_time < max_length then
press_time = press_time + 0.001 -- seconds
end
end
-- init
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function init()
clk_ui_arc = clock.run(ui_event_arc)
clk_ui_screen = clock.run(ui_event_screen)
clk_delayed_init = clock.run(delayed_init_event)
clk_press_timer = metro.init()
clk_press_timer.event = press_timer_event
clk_press_timer.time = 0.001
for n = 1, 2 do
softcut.enable(n, 1)
softcut.rate(n, 1)
softcut.loop(n, 1)
softcut.loop_start(n, 0)
softcut.loop_end(n, max_length)
softcut.level(n, 1)
softcut.rec_level(n, 1)
softcut.pre_level(n, 0)
softcut.pan(n, 0)
softcut.level_slew_time(n, 0.0)
softcut.recpre_slew_time(n, 0.0)
softcut.fade_time(n, min_length)
softcut.level_input_cut(n, 1, 0.5)
softcut.level_input_cut(n, 1, 0.5)
softcut.level_input_cut(n, 2, 0.5)
softcut.level_input_cut(n, 2, 0.5)
softcut.pre_filter_dry(n, 1)
softcut.pre_filter_lp(n, 0)
softcut.pre_filter_bp(n, 0)
softcut.pre_filter_hp(n, 0)
softcut.post_filter_dry(n, 1)
softcut.post_filter_lp(n, 0)
softcut.post_filter_bp(n, 0)
softcut.post_filter_hp(n, 0)
end
for n = 1, 64 do waveform[n] = 0 end
softcut.event_render(on_render)
softcut.event_position(on_position)
if save_on_exit then params:read(norns.state.data .. "state.pset") end
end
-- norns: keys
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function key(n, z)
if n == 1 then
if z == 1 then
e1_held = true
else
e1_held = false
end
end
if n == 2 then
if z == 1 then
record()
end
if z == 0 and recording then
play()
end
end
if n == 3 then
if z == 1 then
clear()
end
end
end
-- norns: encoders
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function enc(n, d)
-- d = d * 2.0
if n == 1 and not e1_held then
params:delta("rate", d)
end
if n == 1 and e1_held then
params:delta("level", d)
end
if n == 2 then
params:delta("start", d)
end
if n == 3 then
params:delta("length", d)
end
end
-- arc: key
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
a.key = function(n, z)
if n == 1 then
if z == 1 then
record()
end
if z == 0 and recording then
play()
end
end
end
-- arc: encoders
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
a.delta = function(n, d)
d = d * 0.135
if n == 1 then
params:delta("rate", d)
end
if n == 2 then
params:delta("level", d)
end
if n == 3 then
params:delta("start", d)
end
if n == 4 then
params:delta("length", d)
end
end
-- norns: drawing
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function redraw()
s.clear()
s.aa(0)
s.level(15)
s.move(63, 48)
s.font_face(3)
s.font_size(28)
s.text_center("postskript")
local start = params:get("start")
local length = params:get("length")
local rate = params:get_raw("rate")
-- length
s.move(util.clamp(10 + start * 89, 10, 99), 51)
s.line(util.clamp(10 + (start + length) * 89 + 1, 11, 100), 51)
if start + length > 1 then
s.move(105, 51)
s.line(util.clamp(105 + -1 * (1 - start - length) * 89, 105, 123), 51)
end
-- rate
s.move(105, 53)
s.line(105 + rate * 18, 53)
s.stroke()
-- playback
if recording then
if ((frame - 1) % 30) + 1 > 15 then
s.level(0)
else
s.level(15)
end
s.pixel(10, 53)
elseif playing then
s.pixel(util.clamp(10 + (util.clamp(playback_position / press_time, 0, 1) * 88), 10 + start * 89, 9 + (start + length) * 89 + 1), 53)
end
s.fill()
-- debug
-- s.move(0, 8)
-- s.font_face(1)
-- s.font_size(8)
-- s.text(playback_posiition)
s.update()
end
-- arc: drawing
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function arc_redraw()
-- one led = 5.625 degrees
a:all(0)
local br = 3
local start = params:get("start")
local length = params:get("length")
local length_full = length
if length + math.abs(start) > 1 then
length = 1 - math.abs(start)
end
-- e1
for n = 1, 64 do
local v = math.abs(waveform[n])
local scaled = v == 0 and 0 or math.log10(1 + 9 * v) * 15
a:led(1, n, math.floor(scaled))
end
a:led(1, 33, 0)
if recording then
if ((frame - 1) % 30) + 1 > 15 then
a:led(1, 33, 10)
else
a:led(1, 33, 15)
end
elseif stopped then
a:led(1, 33, 10)
elseif playing then
softcut.query_position(playing_voice)
a:led(1, math.floor(64 * util.clamp(playback_position / press_time, 0, 1)) + 32 % 64 + 1, 15)
end
-- e2
local s1 = 5.625 * -31
local s2 = util.clamp(5.625 * -31 + 5.625 * 63 * level * 0.5, 5.625 * -31, 5.625 * 63)
arc_segment(2, s1, s2, br)
a:led(2, 1, 15)
a:led(2, 33, 1)
-- e3
local s1 = 5.625 * -30 + start * 5.625 * 62
local s2 = util.clamp(s1 + 5.625 * 63 * length_full, 5.625 * -32, 5.625 * 128)
local s3 = 5.625 * -31 + start * 5.625 * 62
local s4 = util.clamp(s3 + 5.625 * 63 * length + 5.625, 5.625 * -32, 5.625 * 32)
arc_segment(3, s1, s2, 1)
arc_segment(3, s3, s4, br)
a:led(3, 33, 15)
-- e4
local s1 = 5.625 * -31 * length_full
local s2 = 5.625 * 0
arc_segment(4, s1, s2, 1)
local s1 = 5.625 * 1
local s2 = 5.625 * 1 + 5.625 * 31 * length_full
arc_segment(4, s1, s2, 1)
local s1 = 5.625 * -31 * length
local s2 = 5.625 * 0
arc_segment(4, s1, s2, br + 1)
local s1 = 5.625 * 1
local s2 = 5.625 * 1 + 5.625 * 31 * length
arc_segment(4, s1, s2, br + 1)
a:led(4, 1, 15)
a:refresh()
end
-- cleanup
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function cleanup()
if save_on_exit then params:write(norns.state.data .. "state.pset") end
end