-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstjoernuithrott.lua
571 lines (455 loc) · 15.2 KB
/
stjoernuithrott.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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
--
-- --- --- ---- --- ---
-- --- ---- ---
-- --- --- ---
-- --- --- --- ---
-- --- --- --- ---
-- --- --- ---
--
--
-- Stjoernuithrott
-- 1.1.1
-- llllllll.co/t/33889
--
-- K2 Toggle sub-octave
-- K3 Randomize parmes
--
-- E1 Cycle through params
-- E4 Pitch (hz) [Fates only]
-- E2-E3 Adjust highlight params
--
-- Twiddle the knobs and revel
-- in the chaos... or check out
-- the README for more details
--
engine.name = "SCgazer"
local passthrough = include("lib/passthrough")
local music_util = require "musicutil"
-- constants
local VERSION = "1.1.0"
local VIEWPORT = {
width = 128,
height = 64
}
local FIRST_PAGE = 0
local OFF = 2
local ON = 15
local LFO_TYPE = {
"Sine",
"Tri",
"Saw",
"Pulse"
}
-- Add parameters
function add_params()
-- Parameter page
params:add_number("page", "page", FIRST_PAGE - 1, 13, 1)
params:hide("page")
params:add_separator()
-- Pitch
params:add_control("freq", "Primary osc. pitch", controlspec.new(16.35, 1046.5, "exp", 1, 440, "hz"))
params:set_action("freq", function(x) engine.freq(x) end)
-- Waveform
params:add_control("wave", "Waveform", controlspec.new(0, 89, "lin", 1, 0, ""))
params:set_action("wave", function(x) engine.wave(x) end)
-- Detune parameter of the 2nd oscillator
params:add_control("osc2", "Detune second osc.", controlspec.new(-5, 5, "lin", .1, 1, ""))
params:set_action("osc2", function(x) engine.osc2(x) end)
-- Detunes second oscillator 1 octave lower, 0 for using detune as it is
params:add_control("sub", "Suboctave 2nd osc.", controlspec.new(0, 1, "lin", 1, 0, ""))
params:set_action("sub", function(x) engine.sub(x) end)
-- Mix for 2 oscillator. -1 is 1st oscillator and 1 for the 2nd oscillator only 0 is the middle
params:add_control("mix", "Mix", controlspec.new(-1, 1, "lin", .1, 0, ""))
params:set_action("mix", function(x) engine.mix(x) end)
params:add_separator()
-- Sample rate reduction in Hz
params:add_control("alias", "Sample rate", controlspec.new(100, 44100, "lin", 100, 44100, "hz"))
params:set_action("alias", function(x) engine.alias(x) end)
-- Bit rate reduction between 0-24 bits
params:add_control("redux", "Bit rate reduction", controlspec.new(0, 24, "lin", .1, 24, ""))
params:set_action("redux", function(x) engine.redux(x) end)
-- Gain stage for distortion kinda effect -1 is clean, 1 is dirty
params:add_control("gain", "Gain", controlspec.new(-1, 1, "lin", .01, -1, ""))
params:set_action("gain", function(x) engine.gain(x) end)
params:add_separator()
-- Cutoff frequency for the 1st filter
params:add_control("freq1", "Filter 1 cutoff", controlspec.new(80, 5000, "exp", 1, 880, "hz"))
params:set_action("freq1", function(x) engine.freq1(x) end)
-- LFO of 1st filter choose between 4 waveforms: 0 for Sine-ish, 1 for Triangle, 2 for Saw, 3 for Pulse
params:add_control("lfo1type1", "LFO 1 type", controlspec.new(0, 3, "lin", 1, 1, ""))
params:set_action("lfo1type1", function(x) engine.lfo1type1(x) end)
-- Rate of 1st LFO in Hz
params:add_control("rate1", "LFO 1 rate", controlspec.new(0.05, 100.0, "exp", .05, 100, ""))
params:set_action("rate1", function(x) engine.rate1(x / 100) end)
-- Res of 1st LFO
params:add_control("res1", "LFO 1 res", controlspec.new(0, 1, "lin", .01, .0, ""))
params:set_action("res1", function(x) engine.depth1(x) end)
-- Depth of 1st LFO in Hz, 1 means no modulation, 0 is max
params:add_control("depth1", "LFO 1 depth", controlspec.new(0, 1, "lin", .01, .0, ""))
params:set_action("depth1", function(x) engine.depth1(x) end)
params:add_separator()
-- Cutoff frequency for the 2nd filter
params:add_control("freq2", "Filter 2 cutoff", controlspec.new(80, 5000, "exp", 1, 880, "hz"))
params:set_action("freq2", function(x) engine.freq2(x) end)
-- LFO of 2nd filter choose between 4 waveforms: 0 for Sine-ish, 1 for Triangle, 2 for Saw, 3 for Pulse
params:add_control("lfo1type2", "LFO 2 type", controlspec.new(0, 3, "lin", 1, 3, ""))
params:set_action("lfo1type2", function(x) engine.lfo1type2(x) end)
-- Rate of 2nd LFO in Hz
params:add_control("rate2", "LFO 2 rate", controlspec.new(0.05, 100.0, "exp", .05, 50, ""))
params:set_action("rate2", function(x) engine.rate2(x / 100) end)
-- Res of 2nd LFO
params:add_control("res2", "LFO 2 res", controlspec.new(0, 1, "lin", .01, .0, ""))
params:set_action("res2", function(x) engine.depth1(x) end)
-- Depth of 2nd LFO in Hz, 1 means no modulation, 0 is max
params:add_control("depth2", "LFO 2 depth", controlspec.new(0, 1, "lin", .01, .5, ""))
params:set_action("depth2", function(x) engine.depth2(x) end)
params:add_separator()
-- LFO of amplitude choose between 4 waveforms: 0 for Sine-ish, 1 for Triangle, 2 for Saw, 3 for Pulse
params:add_control("lfo1type3", "LFO 3 type", controlspec.new(0, 3, "lin", 1, 2, ""))
params:set_action("lfo1type3", function(x) engine.lfo1type3(x) end)
-- Rate of 3rd LFO in Hz
params:add_control("rate3", "LFO 3 rate", controlspec.new(0.05, 100.0, "exp", .05, 10, ""))
params:set_action("rate3", function(x) engine.rate3(x / 100) end)
-- Depth of 3rd LFO in Hz, 1 means no modulation, 0 is max
params:add_control("depth3", "LFO 3 depth", controlspec.new(0, 1, "lin", .01, .33, ""))
params:set_action("depth3", function(x) engine.depth3(x) end)
params:add_separator()
-- Volume
params:add_control("amp", "Volume", controlspec.new(0, 100, "lin", 1, 100, "%"))
params:set_action("amp", function(x) engine.amp(x / 100) end)
-- Load saved params
params:read()
end
-- Randomize parameter values
function randomize_params()
params:set("freq", random(16.35, 1046.5))
-- params:set("osc2", random(-5, 5)) -- uncomment to randomize waveforms
params:set("gain", random(-1, 1))
params:set("sub", math.floor(random(0, 2)))
-- params:set("wave", random(0, 89)) -- uncomment to randomize waveforms
-- params:set("mix", random(-1, 1x)) -- uncomment to randomize mix
params:set("freq1", random(80, 5000))
params:set("freq2", random(80, 5000))
params:set("lfo1type1", math.floor(random(0, 4)))
params:set("rate1", random(0.05, 100.0))
params:set("res1", random(0, 1))
params:set("depth1", random(0, 1))
params:set("lfo1type2", math.floor(random(0, 4)))
params:set("rate2", random(0.05, 100.0))
params:set("res2", random(0, 1))
params:set("depth2", random(0, 1))
params:set("lfo1type3", math.floor(random(0, 4)))
params:set("rate3", random(0.05, 100.0))
params:set("depth3", random(0, 1))
params:set("alias", random(100, 44100))
params:set("redux", random(0, 24))
end
-- Initialize
function init()
print("Stjörnuíþrótt " .. VERSION)
init_midi()
add_params()
screen.aa(0)
redraw()
end
-- Midi event, fires when Midi data receieved
function midi_device_event(data)
local msg = midi.to_msg(data)
if msg.type == "note_on" then
hz = music_util.note_num_to_freq(msg.note)
params:set("freq", hz)
params:set("amp", (msg.vel / 127) * 100)
end
redraw()
end
-- Initialize Midi
function init_midi()
passthrough.init()
passthrough.user_device_event = midi_device_event
end
function enc(n, delta)
if n == 1 then
params:delta("page", delta)
if (params:get("page") > 12) then
params:set("page", FIRST_PAGE)
elseif (params:get("page") < FIRST_PAGE) then
params:set("page", 12)
end
end
if (#norns.encoders.accel == 4) then
-- Fates only; persistent pitch adjustment
if n == 4 then
params:delta("freq", delta)
end
else
-- Norns; pitch only on page 0
if params:get("page") == 0 then
if n == 2 then
params:delta("freq", delta)
end
end
end
if params:get("page") == 0 then
if n == 3 then
params:delta("osc2", delta)
end
elseif params:get("page") == 1 then
if n == 2 then
params:delta("amp", delta)
elseif n == 3 then
params:delta("gain", delta)
end
elseif params:get("page") == 2 then
if n == 2 then
params:delta("wave", delta)
elseif n == 3 then
params:delta("mix", delta)
end
elseif params:get("page") == 3 then
if n == 2 then
params:delta("freq1", delta)
elseif n == 3 then
params:delta("freq2", delta)
end
elseif params:get("page") == 4 then
if n == 2 then
params:delta("lfo1type1", delta)
elseif n == 3 then
params:delta("rate1", delta)
end
elseif params:get("page") == 5 then
if n == 2 then
params:delta("lfo1type1", delta)
elseif n == 3 then
params:delta("res1", delta)
end
elseif params:get("page") == 6 then
if n == 2 then
params:delta("lfo1type1", delta)
elseif n == 3 then
params:delta("depth1", delta)
end
elseif params:get("page") == 7 then
if n == 2 then
params:delta("lfo1type2", delta)
elseif n == 3 then
params:delta("rate2", delta)
end
elseif params:get("page") == 8 then
if n == 2 then
params:delta("lfo1type2", delta)
elseif n == 3 then
params:delta("res2", delta)
end
elseif params:get("page") == 9 then
if n == 2 then
params:delta("lfo1type2", delta)
elseif n == 3 then
params:delta("depth2", delta)
end
elseif params:get("page") == 10 then
if n == 2 then
params:delta("lfo1type3", delta)
elseif n == 3 then
params:delta("rate3", delta)
end
elseif params:get("page") == 11 then
if n == 2 then
params:delta("lfo1type3", delta)
elseif n == 3 then
params:delta("depth3", delta)
end
elseif params:get("page") == 12 then
if n == 2 then
params:delta("alias", delta)
elseif n == 3 then
params:delta("redux", delta)
end
end
redraw()
end
function key(n, state)
if n == 2 and state == 1 then
if (params:get("sub") == 1) then
params:set("sub", 0)
else
params:set("sub", 1)
end
elseif n == 3 and state == 1 then
randomize_params()
end
redraw()
end
function redraw()
screen.clear()
-- Page marker
screen.level(ON)
screen.move(VIEWPORT.width * .80, VIEWPORT.height * .5)
screen.text_center("P" .. params:get("page"))
screen.line_width(1)
screen.rect((VIEWPORT.width * .80) - 8, (VIEWPORT.height * .5) - 6, 15, 8)
screen.stroke()
-- page 0
page = 0
if (#norns.encoders.accel == 4) then
screen.level(ON)
else
highlight(ON, OFF, {page})
end
screen.move(VIEWPORT.width * 0, 10)
screen.text(params:get("freq"))
screen.move(VIEWPORT.width * 0, 20)
screen.text(note_octave(music_util.freq_to_note_num(params:get("freq"))))
led(VIEWPORT.width * .75, 17, params:get("sub"))
screen.move(VIEWPORT.width, 10)
highlight(ON, OFF, {page})
screen.text_right(params:get("freq") * params:get("osc2"))
detune_note = note_octave(music_util.freq_to_note_num(params:get("freq") * params:get("osc2")))
-- screen.text_center(math.floor(params:get("detune") * 100) .. "%")
screen.move(VIEWPORT.width, 20)
screen.text_right(detune_note)
-- page 1
page = 1
screen.move(VIEWPORT.width * .25, 20)
highlight(ON, OFF, {page})
screen.text_center(params:get("amp") .. "%")
screen.move(VIEWPORT.width * .5, 20)
highlight(ON, OFF, {page})
screen.text( math.ceil(((params:get("gain") + 1) / 2) * 100) .. "%" )
-- page 2
page = 2
screen.move(VIEWPORT.width * .25, 10)
highlight(ON, OFF, {page})
screen.text_center("W" .. params:get("wave") + 1)
highlight(ON, OFF, {page})
screen.rect (VIEWPORT.width * .5, 11, 10 * params:get("mix"), 1)
screen.fill()
mix_str = {"", ""}
if (params:get("mix") == -1) then
mix_str[1] = "o1◀"
mix_str[2] = ""
elseif (params:get("mix") == 1) then
mix_str[1] = ""
mix_str[2] = "▶o2"
else
mix_str[1] = "o1+"
mix_str[2] = "o2"
end
screen.move(VIEWPORT.width * .5 - 10, 10)
screen.text(mix_str[1])
screen.move(VIEWPORT.width * .5 + 11, 10)
screen.text_right(mix_str[2])
-- page 3
page = 3
screen.move(VIEWPORT.width * 0, 44)
highlight(ON, OFF, {page})
screen.text(params:get("freq1"))
screen.move(VIEWPORT.width * 0, 54)
highlight(ON, OFF, {page})
screen.text(params:get("freq2"))
-- page 4
page = 4
screen.move(VIEWPORT.width * .33, 44)
highlight(ON, OFF, {page, page + 1, page + 2})
screen.text_center(LFO_TYPE[params:get("lfo1type1") + 1])
screen.move(VIEWPORT.width * .66, 44)
highlight(ON, OFF, {page})
screen.text_center(params:get("rate1"))
led((VIEWPORT.width * .66) - 17.5, 41, 1)
-- page 5
page = 5
screen.move(VIEWPORT.width * .75, 49)
highlight(ON, OFF, {page})
screen.text(params:get("res1"))
-- page 6
page = 6
screen.move(VIEWPORT.width, 44)
highlight(ON, OFF, {page})
screen.text_right(params:get("depth1"))
-- page 7
page = 7
screen.move(VIEWPORT.width * .33, 54)
highlight(ON, OFF, {page, page + 1, page + 2})
screen.text_center(LFO_TYPE[params:get("lfo1type2") + 1])
screen.move(VIEWPORT.width * .66, 54)
highlight(ON, OFF, {page})
screen.text_center(params:get("rate2"))
led((VIEWPORT.width * .66) - 17.5, 51, 1)
-- page 8
page = 8
screen.move(VIEWPORT.width * .75, 59)
highlight(ON, OFF, {page})
screen.text(params:get("res2"))
-- page 9
page = 9
screen.move(VIEWPORT.width, 54)
highlight(ON, OFF, {page})
screen.text_right(params:get("depth2"))
-- page 10
page = 10
screen.move(VIEWPORT.width * 0, 64)
highlight(ON, OFF, {page, page + 1})
screen.text(LFO_TYPE[params:get("lfo1type3") + 1])
screen.move(VIEWPORT.width * .5, 64)
highlight(ON, OFF, {page})
screen.text_center(params:get("rate3"))
led((VIEWPORT.width * .5) - 17, 61, 1)
-- page 11
page = 11
screen.move(VIEWPORT.width, 64)
highlight(ON, OFF, {page})
screen.text_right(params:get("depth3"))
-- page 12
page = 12
screen.move(VIEWPORT.width * .25, VIEWPORT.height * .5)
highlight(ON, OFF, {page})
screen.text_center(params:get("alias"))
screen.move(VIEWPORT.width * .5, VIEWPORT.height * .5)
highlight(ON, OFF, {page})
screen.text_center(params:get("redux"))
screen.update()
end
function cleanup()
params:write()
end
-- Displays note name and octave based on Midi note number
-- num: Midi note number
function note_octave(num)
octave = math.floor((num / 12) - 1)
notes = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}
return notes[(num % 12) + 1] .. octave
end
-- Toggles the brightness of an element based on page
-- on: brightnless level for "on" state
-- off: brightnless level for "off" state
-- page_nums: array of page numbers to toggle "on" state
function highlight(on, off, page_nums)
for i = 1, #page_nums do
if params:get("page") == page_nums[i] then
screen.level(on)
break
else
screen.level(off)
end
end
end
-- Creates activity element to signify status of parameter
-- x: X-coordinate of element
-- y: Y-coordinate of element
-- state: 1 is active, 0 is inactive
function led(x, y, state)
screen.move(x + 3, y) -- hacky way to fix visual glitch
screen.circle(x, y, 3)
if state == 1 then
screen.fill()
else
screen.stroke()
end
end
-- Generates random number between given min and max
-- min: minmum range
-- max: maximum range
-- returns random number as float
function random(min, max)
return (min + math.random() * (max - min));
end