-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsilos.lua
959 lines (852 loc) · 30.1 KB
/
silos.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
-- live granular instrument
--
-- use a keyboard to
-- assign controls,
-- set parameter values,
-- and control a granular
-- engine.
--
engine.name = "Thresher"
local tabutil = require 'tabutil'
local a = arc.connect(1)
local g = grid.connect(1)
local m = midi.connect(1)
local alt = false
local grid_alt = false
local TRACKS = 4
-- for keyboard input
local my_string = ""
local history = {}
local history_index = nil
local new_line = false
-- for screen state
local track = 1
local show_info = false
local info_focus = 1
-- control names and shorts
controls = {
"gain",
"position",
"speed",
"jitter",
"size",
"pitch",
"fdbk",
"density",
"trig_mode",
"spread",
"cutoff",
"rq",
"send",
"gain_slew",
"jitter_slew",
"size_slew",
"density_slew",
"pitch_slew",
"cutoff"
}
control_shorts = {
"gai",
"pos",
"spe",
"jit",
"siz",
"pit",
"fdb",
"den",
"tmd",
"spr",
"cut",
"rq",
"sen",
"gsl",
"jsl",
"ssl",
"dsl",
"psl",
"csl"
}
fx_controls = {
"fx_gain",
"time",
"verbsize",
"damp",
"diff",
"mod_depth",
"mod_freq",
"lowx",
"midx",
"highx",
"quality"
}
fx_shorts = {
"fxg",
"tim",
"siz",
"dam",
"dif",
"mdp",
"mfq",
"low",
"mid",
"hig",
"qua"
}
local bit_depths = {4, 8, 10, 12, 24, 32}
local function set_quality(v)
engine.bit_depth(bit_depths[v])
end
-- for saving state
local silos = {}
silos.grid_mode = 1
silos.grid_modes = {"2xy", "snaps"}
-- current control choices for enc/arc/grid
silos.enc_choices = {"1gain", "1position", "1speed"}
silos.arc_choices = {"1jitter", "1spread", "1density", "1pitch"}
silos.gridx_choices = {"1spread", "2spread"}
silos.gridy_choices = {"1jitter", "2jitter"}
silos.midi_choices = {}
-- for parameter snapshots
silos.snaps = {}
for i = 1, TRACKS do
silos.snaps[i] = {}
for j = 1, 16 do
silos.snaps[i][j] = {}
end
end
-- for control macros
silos.macros = {}
silos.muls = {}
for i = 1, TRACKS do
silos.macros[i] = {}
silos.muls[i] = {}
end
silos.is_macro = {false, false, false}
-- for screen redraw
local is_dirty = true
local start_time = util.time()
local walk = 123
local function save_state(id)
if id == nil then id = "silos" end
tabutil.save(silos, paths.code .. "silos/lib/" .. id .. ".state")
end
local function load_state(id)
if id == nil then id = "silos" end
silos = tabutil.load(paths.code .. "silos/lib/" .. id .. ".state")
end
local function save_pset()
params:write()
end
local function load_pset()
params:read()
end
function split_string(input_string, sep)
-- seperates a string by whitespace
-- returns a table of the results
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(input_string, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function init()
screen.aa(0)
params:add_separator()
for i = 1, TRACKS do
params:add_group("track " .. i, 22)
params:add_number(i .. "gate", i .. " gate", 0, 1, 0)
params:set_action(i .. "gate", function(value) engine.gate(i, value) end)
params:add_number(i .. "record", i .. " record", 0, 1, 0)
params:set_action(i .. "record", function(value) engine.record(i, value) end)
params:add_taper(i .. "gain", i .. " gain", -60, 20, -16, 0, "dB")
params:set_action(i .. "gain", function(value) engine.gain(i, math.pow(10, value / 20)) end)
params:add_taper(i .. "position", i .. " position", 0, 1, 0.001, 0)
params:set_action(i .. "position", function(value) engine.seek(i, value) end)
params:add_taper(i .. "speed", i .. " speed", -4, 4, 0, 0, "")
params:set_action(i .. "speed", function(value) engine.speed(i, value) end)
params:add_taper(i .. "jitter", i .. " jitter", 0, 500, 0, 5, "ms")
params:set_action(i .. "jitter", function(value) engine.jitter(i, value / 1000) end)
params:add_taper(i .. "size", i .. " size", 1, 500, 150, 5, "ms")
params:set_action(i .. "size", function(value) engine.size(i, value / 1000) end)
params:add_taper(i .. "density", i .. " density", 0, 130, 32, 6, "hz")
params:set_action(i .. "density", function(value) engine.density(i, value) end)
params:add_number(i.."trig_mode", i.." trig mode", 0, 1, 0)
params:set_action(i.."trig_mode", function(v) engine.trig_mode(i, v) end)
params:add_taper(i .. "pitch", i .. " pitch", -4, 4, 1, 0, "")
params:set_action(i .. "pitch", function(value) engine.pitch(i, value) end)
params:add_taper(i .. "spread", i .. " spread", 0, 100, 35, 0, "%")
params:set_action(i .. "spread", function(value) engine.spread(i, value / 100) end)
params:add_control(i .. "cutoff", i .. " cutoff", controlspec.new(20, 18000, "exp", 0, 18000, "hz"))
params:set_action(i .. "cutoff", function(value) engine.cutoff(i, value) end)
params:add_control(i .. "rq", i .. " rq", controlspec.new(0.00, 1.00, "lin", 0.1, 1))
params:set_action(i .. "rq", function(value) engine.rq(i, value) end)
params:add_control(i .. "fdbk", i .." fdbk", controlspec.new(0.0, 1.0, "lin", 0.01, 0))
params:set_action(i .. "fdbk", function(value) engine.pre_level(i, value) end)
params:add_control(i .. "send", i .." send", controlspec.new(0.0, 1.0, "lin", 0.01, 0))
params:set_action(i .. "send", function(value) engine.send(i, value) end)
params:add_separator("slews " .. i)
params:add_control(i .. "gain_slew", i .. " gain slew", controlspec.new(0.0, 20.0, "lin", 0.1, 0))
params:set_action(i .. "gain_slew", function(v) engine.gain_slew(i, v) end)
params:add_control(i .. "jitter_slew", i .. " jitter slew", controlspec.new(0.0, 20.0, "lin", 0.1, 0))
params:set_action(i .. "jitter_slew", function(v) engine.jitter_slew(i, v) end)
params:add_control(i .. "size_slew", i .. " size slew", controlspec.new(0.0, 20.0, "lin", 0.1, 0))
params:set_action(i .. "size_slew", function(v) engine.size_slew(i, v) end)
params:add_control(i .. "density_slew", i .. " density slew", controlspec.new(0.0, 20.0, "lin", 0.1, 0))
params:set_action(i .. "density_slew", function(v) engine.density_slew(i, v) end)
params:add_control(i .. "pitch_slew", i .. " pitch slew", controlspec.new(0.0, 20.0, "lin", 0.1, 0))
params:set_action(i .. "pitch_slew", function(v) engine.pitch_slew(i, v) end)
params:add_control(i .. "cutoff_slew", i .. " cutoff slew", controlspec.new(0.0, 20.0, "lin", 0.1, 0))
params:set_action(i .. "cutoff_slew", function(v) engine.cutoff_slew(i, v) end)
end
params:add_group("fx", 13)
-- effect controls
-- delay time
params:add_taper("time", "*" .. "time", 0.0, 60.0, 8, 0, "")
params:set_action("time", function(value) engine.time(value) end)
-- delay size
params:add_taper("verbsize", "*" .. "size", 1, 5.0, 1.67, 0.01, "")
params:set_action("verbsize", function(value) engine.verbsize(value) end)
-- dampening
params:add_taper("damp", "*" .. "damp", 0.0, 1.0, 0.3144, 0, "")
params:set_action("damp", function(value) engine.damp(value) end)
-- diffusion
params:add_taper("diff", "*" .. "diff", 0.0, 1.0, 0.71, 0, "")
params:set_action("diff", function(value) engine.diff(value) end)
-- mod depth
params:add_taper("mod_depth", "*" .. "mod depth", 0.0, 1.0, .66, 0, "")
params:set_action("mod_depth", function(value) engine.mod_depth(value) end)
-- mod rate
params:add_taper("mod_freq", "*" .. "mod freq", 0.0, 10.0, 3.00, 0, "hz")
params:set_action("mod_freq", function(value) engine.mod_freq(value) end)
-- reverb eq
params:add_taper("lowx", "*" .. "lowx", 0.0, 1.0, 0.8, 0, "")
params:set_action("lowx", function(value) engine.low(value) end)
params:add_taper("midx", "*" .. "midx", 0.0, 1.0, 0.70, 0, "")
params:set_action("midx", function(value) engine.mid(value) end)
params:add_taper("highx", "*" .. "highx", 0.0, 1.0, 0.3, 0, "")
params:set_action("highx", function(value) engine.high(value) end)
params:add_taper("lowcross", "*" .. "low crossover", 100, 6000.0, 2450.0, 0, "")
params:set_action("lowcross", function(value) engine.lowcut(value) end)
params:add_taper("highcross", "*" .. "high crossover", 1000.0, 10000.0, 1024.0, 0, "")
params:set_action("highcross", function(value) engine.highcut(value) end)
-- bit depth
params:add_option("quality", "*quality", bit_depths, 5)
params:set_action("quality", function(value) set_quality(value) end)
-- reverb output volume
params:add_taper("fx_gain", "*" .. "gain", 0.0, 1.0, 0.50, 0, "")
params:set_action("fx_gain", function(value) engine.fxgain(value) end)
params:add_separator('config')
params:add_option("grid_mode", "grid mode", silos.grid_modes, 1)
params:set_action("grid_mode", function(value) silos.grid_mode = value end)
for i = 1, 4 do
params:add_control(i .."fade_time", i .. " fade time", controlspec.new(0, 30, "lin", 0.1, 0, "secs"))
params:set_action(i .. "fade_time", function(value) engine.envscale(i, value) end)
end
params:bang()
m.event = function(v) is_dirty = true end
-- arc redraw metro
local arc_redraw_timer = metro.init()
arc_redraw_timer.time = 0.025
arc_redraw_timer.event = function() arc_redraw() end
arc_redraw_timer:start()
-- norns redraw metro
local norns_redraw_timer = metro.init()
norns_redraw_timer.time = 0.025
norns_redraw_timer.event = function() if is_dirty then redraw() end end
norns_redraw_timer:start()
-- grid redraw metro
local grid_redraw_timer = metro.init()
grid_redraw_timer.time = 0.025
grid_redraw_timer.event = function() g.redraw() end
grid_redraw_timer:start()
end
-- norns hardware ----------
function key(n, z)
if n == 1 then alt = z == 1 and true or false end
if n == 2 and z == 1 then
-- gate on for selected track
if params:get(track .. "gate") == 0 then
params:set(track .. "gate", 1)
else
params:set(track .. "gate", 0)
end
elseif n == 3 and z == 1 then
if alt then
show_info = not show_info
-- recording on for selcted track
elseif params:get(track .. "record") == 0 then
params:set(track .. "record", 1)
else
params:set(track .. "record", 0)
end
end
is_dirty = true
end
function enc(n, d)
if alt then
if n == 2 then
track = util.clamp(track + d, 1, TRACKS)
elseif n == 3 then
info_focus = util.clamp(info_focus + d, 1, 7)
end
elseif silos.is_macro[n] then
for i = 1, #silos.macros[n] do
params:delta(silos.macros[n][i], d * silos.muls[n][i])
end
else
params:delta(silos.enc_choices[n], d)
end
is_dirty = true
end
-- screen ----------
local function draw_engine_params1()
screen.move(54, 10)
screen.text_center("-engine-")
screen.move(1, 18)
screen.text(1 .. " gain " .. string.format("%.1f", params:get(track .."gain")))
screen.move(1, 26)
screen.text(2 .. " position " .. string.format("%.1f", params:get(track .. "position")))
screen.move(1, 34)
screen.text(3 .. " speed " .. string.format("%.1f", params:get(track .. "speed")))
screen.move(1, 42)
screen.text(4 .. " jitter " .. string.format("%.1f", params:get(track .. "jitter")))
screen.move(1, 50)
screen.text(5 .. " size " .. string.format("%.1f", params:get(track .. "size")))
screen.move(65, 18)
screen.text(6 .. " pitch " .. string.format("%.1f", params:get(track .. "pitch")))
screen.move(65, 26)
screen.text(7 .. " fdbk " .. string.format("%.1f", params:get(track .. "fdbk")))
screen.move(65, 34)
screen.text(8 .. " density " .. string.format("%.1f", params:get(track .. "density")))
screen.move(65, 42)
screen.text(9 .. " trig mode "..string.format("%.1f", params:get(track .. "trig_mode")))
screen.move(65, 50)
screen.text(10 .. " spread " .. string.format("%.1f", params:get(track .. "spread")))
end
local function draw_engine_params2()
screen.move(54, 10)
screen.text_center("-engine-")
screen.move(1, 18)
screen.text("11 cutoff " .. string.format("%.1f", params:get(track .. "cutoff")))
screen.move(1, 26)
screen.text("12 rq " .. string.format("%.1f", params:get(track .. "rq")))
screen.move(1, 34)
screen.text("13 send " .. string.format("%.1f", params:get(track .. "send")))
end
local function draw_fx_params()
screen.move(54, 10)
screen.text_center("-fx-")
screen.move(1, 18)
screen.text("1 fx_gain " .. string.format("%.2f", params:get("fx_gain")))
screen.move(1, 26)
screen.text("2 time " .. string.format("%.1f", params:get("time")))
screen.move(1, 34)
screen.text("3 size " .. string.format("%.2f", params:get("verbsize")))
screen.move(1, 42)
screen.text("4 damp " .. string.format("%.3f", params:get("damp")))
screen.move(1, 50)
screen.text("5 diff " .. string.format("%.3f", params:get("diff")))
screen.move(65, 18)
screen.text("6 mod depth " .. string.format("%.2f", params:get("mod_depth")))
screen.move(65, 26)
screen.text("7 mod freq " .. string.format("%.2f", params:get("mod_freq")))
screen.move(65, 34)
screen.text("8 lowx " .. string.format("%.2f", params:get("lowx")))
screen.move(65, 42)
screen.text("9 midx " .. string.format("%.2f", params:get("midx")))
screen.move(65, 50)
screen.text("10 highx " .. string.format("%.2f", params:get("highx")))
end
local function draw_controls1()
screen.move(54, 10)
screen.text_center("-controls-")
screen.move(10, 20)
screen.text("enc:")
screen.move(30, 20)
screen.text(silos.enc_choices[1])
screen.move(94, 20)
screen.text_right(string.format("%.2f", params:get(silos.enc_choices[1])))
screen.move(30, 30)
screen.text(silos.enc_choices[2])
screen.move(94, 30)
screen.text_right(string.format("%.2f", params:get(silos.enc_choices[2])))
screen.move(30, 40)
screen.text(silos.enc_choices[3])
screen.move(94, 40)
screen.text_right(string.format("%.2f", params:get(silos.enc_choices[3])))
end
local function draw_controls2()
screen.move(54, 10)
screen.text_center("-controls-")
screen.move(10, 20)
screen.text("arc:")
screen.move(30, 20)
screen.text(silos.arc_choices[1])
screen.move(94, 20)
screen.text_right(string.format("%.2f", params:get(silos.arc_choices[1])))
screen.move(30, 30)
screen.text(silos.arc_choices[2])
screen.move(94, 30)
screen.text_right(string.format("%.2f", params:get(silos.arc_choices[2])))
screen.move(30, 40)
screen.text(silos.arc_choices[3])
screen.move(94, 40)
screen.text_right(string.format("%.2f", params:get(silos.arc_choices[3])))
screen.move(30, 50)
screen.text(silos.arc_choices[4])
screen.move(94, 50)
screen.text_right(string.format("%.2f", params:get(silos.arc_choices[4])))
end
local function draw_controls3()
screen.move(54, 10)
screen.text_center("-controls-")
screen.move(10, 20)
screen.text("grid 1:")
screen.move(40, 20)
screen.text("x - " .. silos.gridx_choices[1])
screen.move(114, 20)
screen.text_right(string.format("%.2f", params:get(silos.gridx_choices[1])))
screen.move(40, 30)
screen.text("y - " .. silos.gridy_choices[1])
screen.move(114, 30)
screen.text_right(string.format("%.2f", params:get(silos.gridy_choices[1])))
screen.move(10, 40)
screen.text("grid 2:")
screen.move(40, 40)
screen.text("x - " .. silos.gridx_choices[2])
screen.move(114, 40)
screen.text_right(string.format("%.2f", params:get(silos.gridx_choices[2])))
screen.move(40, 50)
screen.text("y - " .. silos.gridy_choices[2])
screen.move(114, 50)
screen.text_right(string.format("%.2f", params:get(silos.gridy_choices[2])))
end
local function draw_snaps()
-- show snap slots
-- dim for unused, bright for contains data
screen.move(64, 10)
screen.text_center("-snapshots-")
for i = 1, TRACKS do
for j = 1, 16 do
screen.level(#silos.snaps[i][j] > 0 and 10 or 2 )
screen.rect(0.5 + j * 8, 10 + i * 8, 4, 4)
screen.fill()
screen.stroke()
end
end
end
function redraw()
screen.clear()
-- splash screen type logo thing
if util.time() - start_time < 3.2 then
screen.display_png("/home/we/dust/code/silos/assets/silos4.png", 0, 0)
screen.aa(1)
screen.font_face(34)
screen.font_size(32)
screen.level(4)
screen.move(walk, 61)
screen.text("silos")
screen.stroke()
walk = walk - 2
else
screen.aa(0)
screen.font_face(1)
screen.font_size(8)
screen.level(10)
screen.move(126, 6)
screen.text_right(track)
screen.move(106, 6)
screen.level(params:get(track .. "gate") == 0 and 2 or 10)
screen.text_right("g")
screen.move(116, 6)
screen.level(params:get(track .. "record") == 0 and 2 or 10)
screen.text_right("r")
screen.level(10)
screen.move(5, 62)
screen.text("> " .. my_string)
if show_info then
screen.font_face(25)
screen.font_size(6)
screen.level(8)
if info_focus == 1 then
draw_engine_params1()
elseif info_focus == 2 then
draw_engine_params2()
elseif info_focus == 3 then
draw_fx_params()
elseif info_focus == 4 then
draw_controls1()
elseif info_focus == 5 then
draw_controls2()
elseif info_focus == 6 then
draw_controls3()
elseif info_focus == 7 then
draw_snaps()
end
end
screen.stroke()
end
screen.update()
if util.time() - start_time < 3.2 then
is_dirty = true
else
is_dirty = false
end
end
-- arc ------------
function a.delta(n, d)
params:delta(silos.arc_choices[n], d / 10)
is_dirty = true
end
function arc_redraw()
for i = 1, 4 do
local ring_choice = silos.arc_choices[i]
local low, high = params:get_range(ring_choice)[1], params:get_range(ring_choice)[2]
a:segment(i, util.degs_to_rads(210), util.degs_to_rads(util.linlin(low, high, 210, 309 + 210, params:get(ring_choice))), 15)
end
a:refresh()
end
-- grid ----------
function grid_2xy(x, y, z)
-- two 8x8 x/y pads
if x <= 8 and z == 1 then
-- left x/y pad
-- scale values and set selected params
local grid_choicex = silos.gridx_choices[1]
local grid_choicey = silos.gridy_choices[1]
local x_scaled = util.linlin(1, 8, params:get_range(grid_choicex)[1], params:get_range(grid_choicex)[2], x)
local y_scaled = util.linlin(1, 8, params:get_range(grid_choicey)[1], params:get_range(grid_choicey)[2], y)
params:set(grid_choicex, x_scaled)
params:set(grid_choicey, y_scaled)
elseif x >= 9 and z == 1 then
-- right x/y pad
-- scale values and set params
local grid_choicex = silos.gridx_choices[2]
local grid_choicey = silos.gridy_choices[2]
local x_scaled = util.linlin(9, 16, params:get_range(grid_choicex)[1], params:get_range(grid_choicex)[2], x)
local y_scaled = util.linlin(1, 8, params:get_range(grid_choicey)[1], params:get_range(grid_choicey)[2], y)
params:set(grid_choicex, x_scaled)
params:set(grid_choicey, y_scaled)
end
end
function grid_snap(x, y, z)
if y <= 4 and z == 1 then
if grid_alt then
local snap_id, t = x, y
silos.snaps[t][snap_id] = {}
for i = 1, #controls do
table.insert(silos.snaps[t][snap_id], params:get(t .. controls[i]))
end
elseif #silos.snaps[y][x] > 0 then
local snap_id, t = x, y
for i = 1, #controls do
params:set(t .. controls[i], silos.snaps[t][snap_id][i])
end
end
elseif x == 4 and y == 6 then
grid_alt = z == 1 and true or false
end
end
function g.key(x, y, z)
if silos.grid_mode == 1 then
grid_2xy(x, y, z)
elseif silos.grid_mode == 2 then
-- snapshot mode
grid_snap(x, y, z)
end
g.redraw()
is_dirty = true
end
function g.redraw()
g:all(0)
if silos.grid_mode == 1 then
local gxh, gxl = params:get_range(silos.gridx_choices[1])[1], params:get_range(silos.gridx_choices[1])[2]
local gyh, gyl = params:get_range(silos.gridy_choices[1])[1], params:get_range(silos.gridy_choices[1])[2]
local x_scaled = util.linlin(gxh, gxl, 1, 8, params:get(silos.gridx_choices[1]))
local y_scaled = util.linlin(gyh, gyl, 1, 8, params:get(silos.gridy_choices[1]))
gxh, gxl = params:get_range(silos.gridx_choices[2])[1], params:get_range(silos.gridx_choices[2])[2]
gyh, gyl = params:get_range(silos.gridy_choices[2])[1], params:get_range(silos.gridy_choices[2])[2]
local x_scaled2 = util.linlin(gxh, gxl, 9, 16, params:get(silos.gridx_choices[2]))
local y_scaled2 = util.linlin(gyh, gyl, 1, 8, params:get(silos.gridy_choices[2]))
x_scaled = math.floor(x_scaled + 0.5)
y_scaled = math.floor(y_scaled + 0.5)
x_scaled2 = math.floor(x_scaled2 + 0.5)
y_scaled2 = math.floor(y_scaled2 + 0.5)
for i = 1, 8 do
g:led(i, y_scaled, 2)
g:led(x_scaled, i, 2)
g:led(i + 8, y_scaled2, 2)
g:led(x_scaled2, i, 2)
end
g:led(x_scaled, y_scaled, 10)
g:led(x_scaled2, y_scaled2, 10)
elseif silos.grid_mode == 2 then
for i = 1, TRACKS do
for j = 1, 16 do
g:led(j, i, #silos.snaps[i][j] > 0 and 10 or 2 )
end
end
g:led(4, 6, grid_alt and 10 or 2)
end
g:refresh()
end
-- keyboard input ----------
local function get_index(key, table)
local index = {}
for k, v in pairs(table) do
index[v] = k
end
return index[key]
end
function keyboard.char(character)
if keyboard.ctrl() or keyboard.alt() then
else
my_string = my_string .. character -- add characters to my string
is_dirty = true
end
end
function keyboard.code(code,value)
--print(code)
if code == "ESC" then
if value == 1 or value == 2 then
if show_info then
show_info = false
else
show_info = true
end
end
end
if value == 1 or value == 2 then -- 1 is down, 2 is held, 0 is release
if keyboard.ctrl() then
-- hold control for script control keybinds
if code == "G" then
-- toggle gate for selected track
if params:get(track .. "gate") == 0 then
params:set(track .. "gate", 1)
else
params:set(track .. "gate", 0)
end
elseif code == "R" then
-- toggle record
if params:get(track .. "record") == 0 then
params:set(track .. "record", 1)
else
params:set(track .. "record", 0)
end
elseif code == "1" or code == "2" or code == "3" or code == "4" then
track = tonumber(code)
end
elseif code == "BACKSPACE" then
-- erase characters from my_string
my_string = my_string:sub(1, -2)
elseif code == "UP" then
-- make sure there's a history
if #history > 0 then
-- reset the history index after pressing enter
if new_line then
history_index = #history
new_line = false
else
-- decrement history_index
history_index = util.clamp(history_index - 1, 1, #history)
end
my_string = history[history_index]
end
elseif code == "DOWN" then
-- make sure there is a history, and we are accessing it
if #history > 0 and history_index ~= nil then
-- increment history_index
history_index = util.clamp(history_index + 1, 1, #history)
my_string = history[history_index]
end
elseif code == "RIGHT" then
if show_info then
info_focus = util.clamp(info_focus + 1, 1, 7)
end
elseif code == "LEFT" then
if show_info then
info_focus = util.clamp(info_focus - 1, 1, 7)
end
elseif code == "ENTER" then
-- parse string for commands
-- there is likely a better way to do this sort of thing
local command = split_string(my_string)
-- rand
if command[1] == "rand" then
if command[2] == "fx" then
local control = tonumber(command[3])
local p = fx_controls[control]
local low, high = tonumber(params:get_range(p)[1]), tonumber(params:get_range(p)[2])
local n = util.linlin(0, 1, low, high, math.random())
params:set(p, n)
else
local track, control = tonumber(command[2]), tonumber(command[3])
local p = track .. controls[control]
local low, high = tonumber(params:get_range(p)[1]), tonumber(params:get_range(p)[2])
local n = util.linlin(0, 1, low, high, math.random())
params:set(p, n)
end
-- rrand
elseif command[1] == "rrand" then
local low, high = tonumber(command[4]), tonumber(command[5])
if command[2] == "fx" then
local control = tonumber(command[3])
local p = fx_controls[control]
local n = util.linlin(0, 1, low, high, math.random())
params:set(p, n)
else
local track, control = tonumber(command[2]), tonumber(command[3])
local p = track .. controls[control]
local n = util.linlin(0, 1, low, high, math.random())
params:set(p, n)
end
-- assign controls
-- enc
elseif command[1] == "enc" then
local id = tonumber(command[2])
if command[3] == "macro" then
local state = tonumber(command[4])
silos.is_macro[id] = state == 1 and true or false
elseif command[3] == "fx" then
local control = tonumber(command[4])
silos.enc_choices[id] = fx_controls[control]
else
local track, control = tonumber(command[3]), tonumber(command[4])
silos.enc_choices[id] = track .. controls[control]
end
-- arc
elseif command[1] == "arc" then
local id = tonumber(command[2])
if command[3] == "fx" then
local control = tonumber(command[4])
silos.arc_choices[id] = fx_controls[control]
else
local track, control = tonumber(command[3]), tonumber(command[4])
silos.arc_choices[id] = track .. controls[control]
end
-- MIDI
-- assign CC's
elseif command[1] == "cc" then
-- syntax: cc midi_device midi_channel cc_number track/fx control_number
local id = command[2]..":"..command[3]..":"..command[4]
local midi_device = tonumber(command[2])
local midi_channel = tonumber(command[3])
local midi_cc = tonumber(command[4])
-- TODO deal with macro here
if command[5] == "fx" then
local control = tonumber(command[6])
silos.midi_choices[id] = fx_controls[control]
norns.pmap.remove(fx_controls[control])
norns.pmap.new(fx_controls[control])
norns.pmap.assign(fx_controls[control], midi_device, midi_channel, midi_cc)
else
local track, control = tonumber(command[5]), tonumber(command[6])
silos.midi_choices[id] = controls[track][control]
norns.pmap.remove(track .. controls[control])
norns.pmap.new(track .. controls[control])
norns.pmap.assign(track .. controls[control], midi_device, midi_channel, midi_cc)
end
-- gridx
elseif command[1] == "gridx" then
local id = tonumber(command[2])
if command[3] == "fx" then
local control = tonumber(command[4])
silos.gridx_choices[id] = fx_controls[control]
else
local track, control = tonumber(command[3]), tonumber(command[4])
silos.gridx_choices[id] = track .. controls[control]
end
-- gridy
elseif command[1] == "gridy" then
local id = tonumber(command[2])
if command[3] == "fx" then
local control = tonumber(command[4])
silos.gridy_choices[id] = fx_controls[control]
else
local track, control = tonumber(command[3]), tonumber(command[4])
silos.gridy_choices[id] = track .. controls[control]
end
-- set grid mode
elseif command[1] == "grid_mode" then
silos.grid_mode = tonumber(command[2])
-- set all gates
elseif command[1] == "g" or command[1] == "gate" then
for i = 1, TRACKS do
local state = tonumber(command[i + 1])
params:set(i .. "gate", state)
end
-- set all records
elseif command[1] == "r" or command[1] == "record" then
for i = 1, TRACKS do
local state = tonumber(command[i + 1])
params:set(i .. "record", state)
end
-- parameter snapshots
-- snap
elseif command[1] == "s" or command[1] == "snap" then
local snap_id, t = tonumber(command[2]), tonumber(command[3])
silos.snaps[t][snap_id] = {}
for i = 1, #controls do
table.insert(silos.snaps[t][snap_id], params:get(t .. controls[i]))
end
-- load
elseif command[1] == "l" or command[1] == "load" then
local snap_id, t = tonumber(command[2]), tonumber(command[3])
if #silos.snaps[t][snap_id] > 0 then
for i = 1, #controls do
params:set(t .. controls[i], silos.snaps[t][snap_id][i])
end
end
-- macro commands
elseif command[1] == "macro" then
local id = tonumber(command[2])
if command[3] == "clear" then
silos.macros[id] = {}
elseif command[3] == "fx" then
local control, mul = tonumber(command[4]), tonumber(command[5])
table.insert(silos.macros[id], fx_controls[control])
table.insert(silos.muls[id], mul)
else
local track, control, mul = tonumber(command[3]), tonumber(command[4]), tonumber(command[5])
table.insert(silos.macros[id], track .. controls[control])
table.insert(silos.muls[id], mul)
end
-- state/pset persistence
elseif command[1] == "save_state" then
save_state(command[2])
elseif command[1] == "load_state" then
load_state(command[2])
elseif command[1] == "save_pset" then
save_pset()
elseif command[1] == "load_pset" then
load_pset()
-- set single parameters
elseif tabutil.contains(controls, command[1]) then
local p, v = command[2] .. command[1], tonumber(command[3])
params:set(p, v)
elseif tabutil.contains(control_shorts, command[1]) then
local i = get_index(command[1], control_shorts)
local p, v = command[2] .. controls[i], tonumber(command[3])
params:set(p, v)
elseif tabutil.contains(fx_controls, command[1]) and command[2] == "fx" then
local c, v = command[1], tonumber(command[3])
params:set(c, v)
elseif tabutil.contains(fx_shorts, command[1]) and command[2] == "fx" then
local i = get_fx_index(command[1], fx_shorts)
local p, v = fx_controls[i], tonumber(command[3])
params:set(p, v)
else
print("syntax error!")
end
-- append the command to history
table.insert(history, my_string)
-- clear my_string
my_string = ""
new_line = true
end
is_dirty = true
end
end