-
Notifications
You must be signed in to change notification settings - Fork 5
/
mlre.lua
3086 lines (2765 loc) · 91.2 KB
/
mlre.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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- mlre v2.0.1 @sonocircuit
-- llllllll.co/t/mlre
--
-- an adaption of
-- mlr v2.2.4 @tehn
-- llllllll.co/t/21145
--
-- for docs go to:
-- >> github.com
-- /sonocircuit/mlre
--
-- or smb into:
-- >> code/mlre/docs
--
norns.version.required = 231114
m = midi.connect()
a = arc.connect()
g = grid.connect()
mu = require 'musicutil'
textentry = require 'textentry'
fileselect = require 'fileselect'
lattice = require 'lattice'
--_lfo = require 'lfo'
ui = include 'lib/ui_mlre'
grd = include 'lib/grid_mlre'
_lfo = include 'lib/lfo_mlre'
scales = include 'lib/scales_mlre'
pattern_time = include 'lib/pattern_time_mlre'
--------- user variables --------
pset_load = false -- if true default pset loaded at launch
rotate_grid = false -- zero only. if true will rotate 90° CW
autofocus = true -- zero only. if true norns screen automatically changes to last used grid layout
--------- other variables --------
mlre_path = _path.audio .. "mlre/"
-- constants
GRID_SIZE = 0
FADE_TIME = 0.01
TAPE_GAP = 1
MAX_TAPELENGTH = 57
DEFAULT_SPLICELEN = 4
DEFAULT_BEATNUM = 4
-- ui
main_pageNum = 1
lfo_pageNum = 1
env_pageNum = 1
patterns_pageNum = 1
track_focus = 1
lfo_focus = 1
env_focus = 1
pattern_focus = 1
held_focus = 0
alt = 0
mod = 0
shift = 0
cutview_hold = false
lfo_trksel = 1
lfo_dstview = 0
lfo_dstsel = 1
-- viz variables
pulse_key_fast = 1
pulse_key_mid = 1
pulse_key_slow = 1
pulse_bar = false
pulse_beat = false
view_message = ""
-- oneshot recording variables
amp_threshold = 1
armed_track = 1
oneshot_rec = false
transport_run = false
autolength = false
loop_pos = 1
rec_dur = 0
-- options variables
stop_all_active = true
macro_slot_mode = 1
loading_pset = false
current_scale = 1
autorand_at_cycle = false
rnd_stepcount = 16
-- arc variables
arc_pageNum = 1
arc_is = false
enc2_wait = false
arc_off = 0
arc_inc1 = 0
arc_inc2 = 0
arc_inc3 = 0
arc_inc4 = 0
arc_inc5 = 0
arc_render = 0
arc_lfo_focus = 1
arc_track_focus = 1
arc_splice_focus = 1
scrub_sens = 100
tau = math.pi * 2
-- main page variables
main_page_params_l = {"vol", "rec", "cutoff", "filter_type", "detune","rate_slew", "play_mode", "reset_active"}
main_page_params_r = {"pan", "dub", "filter_q", "post_dry", "transpose", "level_slew", "start_launch", "reset_count"}
main_page_names_l = {"volume", "rec level", "cutoff", "filter type", "detune", "rate slew", "play mode", "track reset"}
main_page_names_r = {"pan", "dub level", "filter q", "dry level", "transpose", "level slew", "track launch", "reset count"}
-- lfo page variables
lfo_rate_params = {"lfo_clocked_lfo_", "lfo_free_lfo_"}
lfo_page_params_l = {"lfo_depth_lfo_", "lfo_shape_lfo_", "lfo_mode_lfo_"}
lfo_page_params_r = {"lfo_offset_lfo_", "lfo_phase_lfo_", "lfo_free_lfo_"}
lfo_page_names_l = {"depth", "shape", "mode"}
lfo_page_names_r = {"offset", "phase", "rate"}
-- pattern page variables
patterns_page_params_l = {"patterns_meter", "patterns_countin"}
patterns_page_params_r = {"patterns_barnum", "patterns_playback"}
patterns_page_names_l = {"meter", "launch"}
patterns_page_names_r = {"length", "play mode"}
-- tape page variables
tape_actions = {"load", "clear", "save", "copy", "paste"}
tape_action = 1
copy_track = nil
copy_splice = nil
resize_values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 28, 32, MAX_TAPELENGTH}
resize_options = {"1/4", "2/4", "3/4", "4/4", "5/4", "6/4", "7/4", "8/4", "9/4", "10/4", "11/4", "12/4", "14/4", "16/4", "18/4", "20/4", "22/4", "24/4", "28/4", "32/4", "MAX"}
view_splice_info = false
view_track_send = false
sends_focus = 1
silent_load_tempo = false
view_presets = false
pset_focus = 1
pset_list = {}
-- pattern page variables and tables
pattern_playback = {"loop", "oneshot"}
pattern_countin = {"beat", "bar"}
pattern_meter = {"2/4", "3/4", "4/4", "5/4", "6/4", "7/4", "9/4", "11/4"}
pattern_meter_val = {2/4, 3/4, 4/4, 5/4, 6/4, 7/4, 9/4, 11/4}
-- key quantization variables and tables
quantize_events = {}
quantizing = false
quant_options = {"1bar", "1/2", "1/3", "1/4", "1/6", "1/8", "1/16", "1/32"}
quant_values = {1, 1/2, 1/3, 1/4, 1/6, 1/8, 1/16, 1/32}
q_rate = 16
-- key logic
held = {}
heldmax = {}
first = {}
second = {}
for i = 1, 8 do
held[i] = 0
heldmax[i] = 0
first[i] = 0
second[i] = 0
end
--------------------- EVENTS -----------------------
-- event variables
eCUT = 1
eSTOP = 2
eSTART = 3
eLOOP = 4
eSPEED = 5
eREV = 6
eMUTE = 7
eTRSP = 8
ePATTERN = 9
eUNLOOP = 10
eGATEON = 11
eGATEOFF = 12
eSPLICE = 13
eROUTE = 14
-- event funtions
function event_record(e)
for i = 1, 8 do
pattern[i]:watch(e)
end
recall_watch(e)
end
function event(e)
if quantizing and e.sync == nil then
table.insert(quantize_events, e)
else
if e.t ~= ePATTERN then
event_record(e)
end
event_exec(e)
end
end
function update_q_clock()
while true do
clock.sync(q_rate)
if #quantize_events > 0 then
for k, e in pairs(quantize_events) do
if e.t ~= ePATTERN then event_record(e) end
event_exec(e)
end
quantize_events = {}
end
end
end
function loop_event(i, lstart, lend)
local e = {}
e.t = eLOOP
e.i = i
e.loop = 1
e.loop_start = lstart
e.loop_end = lend
event(e)
end
function recalc_time_factor()
for i = 1, 8 do
if pattern[i].tempo_map == true and pattern[i].bpm ~= nil then -- pattern tempo map default set to true.
pattern[i].time_factor = pattern[i].bpm / current_tempo
end
end
end
-- exec function
function event_exec(e)
if e.t == eCUT then
if track[e.i].loop == 1 then
clear_loop(e.i)
end
local cut = (e.pos / 16) * clip[e.i].l + clip[e.i].s
local q = track[e.i].rev == 1 and clip[e.i].l / 16 or 0
softcut.position(e.i, cut + q)
if track[e.i].play == 0 then
track[e.i].play = 1
track[e.i].beat_count = 0
set_rec(e.i)
set_level(e.i)
toggle_transport()
end
dirtygrid = true
elseif e.t == eSTOP then
stop_track(e.i)
elseif e.t == eSTART then
softcut.position(e.i, e.pos or track[e.i].cut)
track[e.i].play = 1
track[e.i].beat_count = 0
set_rec(e.i)
set_level(e.i)
toggle_transport()
dirtygrid = true
elseif e.t == eLOOP then
make_loop(e.i, e.loop_start, e.loop_end)
elseif e.t == eUNLOOP then
clear_loop(e.i)
elseif e.t == eSPEED then
track[e.i].speed = e.speed
update_rate(e.i)
grid_page(vREC)
elseif e.t == eREV then
track[e.i].rev = e.rev
update_rate(e.i)
dirtygrid = true
elseif e.t == eMUTE then
track[e.i].mute = e.mute
set_level(e.i)
elseif e.t == eTRSP then
params:set(e.i.."transpose", e.val)
grid_page(vCUT)
grid_page(vTRSP)
elseif e.t == eGATEON then
if env[e.i].active then
env_gate_on(e.i)
end
elseif e.t == eGATEOFF then
if env[e.i].active then
env_gate_off(e.i)
end
elseif e.t == eSPLICE then
track[e.i].splice_active = e.active
set_clip(e.i)
render_splice()
dirtygrid = true
elseif e.t == eROUTE then
if e.ch == 5 then
track[e.i].t5 = e.route
else
track[e.i].t6 = e.route
end
set_track_sends(e.i)
grid_page(vTAPE)
elseif e.t == ePATTERN then
if e.action == "stop" then
pattern[e.i]:stop()
elseif e.action == "start" then
pattern[e.i]:start()
elseif e.action == "rec_stop" then
pattern[e.i]:rec_stop()
pattern_rec = false
elseif e.action == "rec_start" then
pattern[e.i]:rec_start()
pattern_rec = true
elseif e.action == "clear" then
pattern[e.i]:clear()
elseif e.action == "overdub_on" then
pattern[e.i]:set_overdub(1)
pattern_rec = true
elseif e.action == "overdub_off" then
pattern[e.i]:set_overdub(0)
pattern_rec = false
elseif e.action == "overdub_undo" then
pattern[e.i]:set_overdub(-1)
pattern_rec = false
end
end
end
-- patterns and recall
patterns_only = false
pattern_rec = false
pattern = {}
for i = 1, 8 do
pattern[i] = pattern_time.new("pattern "..i)
pattern[i].process = event_exec
end
recall = {}
for i = 1, 8 do
recall[i] = {}
recall[i].recording = false
recall[i].has_data = false
recall[i].active = false
recall[i].event = {}
end
function recall_watch(e)
for i = 1, 8 do
if recall[i].recording == true then
table.insert(recall[i].event, e)
recall[i].has_data = true
end
end
end
function recall_exec(i)
for _,e in pairs(recall[i].event) do
event_exec(e)
end
end
function randomize(i)
if params:get("rnd_transpose") == 2 then
params:set(i.."transpose", math.random(1, 15))
end
if params:get("rnd_vol") == 2 then
params:set(i.."vol", math.random(20, 100) / 100)
end
if params:get("rnd_pan") == 2 then
params:set(i.."pan", (math.random() * 20 - 10) / 10)
end
if params:get("rnd_dir") == 2 then
local e = {} e.t = eREV e.i = i e.rev = math.random(0, 1)
event(e)
end
if params:get("rnd_loop") == 2 then
local lstart = math.random(1, 15)
local lend = autorand_at_cycle and math.random(lstart + 1, 16) or math.random(lstart, 16)
loop_event(i, lstart, lend)
end
if params:get("rnd_speed") == 2 then
local e = {} e.t = eSPEED e.i = i e.speed = math.random(- params:get("rnd_loct"), params:get("rnd_uoct"))
event(e)
end
if params:get("rnd_cut") == 2 then
params:set(i.. "cutoff", math.random(params:get("rnd_lcut"), params:get("rnd_ucut")) )
end
track[i].step_count = 0
end
--------------------- TRACK, TAPE AND CLIPS -----------------------
-- track variables
track = {}
for i = 1, 6 do
track[i] = {}
track[i].start_launch = 1
track[i].play_mode = 1
track[i].play = 0
track[i].sel = 0
track[i].rec = 0
track[i].oneshot = 0
track[i].level = 1
track[i].prev_level = 1
track[i].pan = 0
track[i].mute = 0
track[i].rate_slew = 0
track[i].rec_level = 1
track[i].pre_level = 0
track[i].dry_level = 0
track[i].t5 = 0
track[i].t6 = 0
track[i].send_t5 = 1
track[i].send_t6 = 1
track[i].loop = 0
track[i].loop_start = 1
track[i].loop_end = 16
track[i].dur = 4
track[i].splice_active = 1
track[i].splice_focus = 1
track[i].cut = TAPE_GAP * i + (i - 1) * MAX_TAPELENGTH
track[i].pos_abs = TAPE_GAP * i + (i - 1) * MAX_TAPELENGTH
track[i].pos_rel = 0
track[i].pos_clip = 0
track[i].pos_hi_res = 1
track[i].pos_lo_res = 1
track[i].pos_arc = 1
track[i].pos_grid = 1
track[i].step_count = 0
track[i].speed = 0
track[i].warble = 0
track[i].rev = 0
track[i].tempo_map = 0
track[i].resize_val = 4
track[i].detune = 0
track[i].transpose = 0
track[i].fade = 0
track[i].loaded = true
track[i].reset = false
track[i].beat_count = 0
track[i].beat_reset = 4
end
-- tape variables -> six slices of tape, one for each track
tp = {}
for i = 1, 6 do
tp[i] = {}
tp[i].input = 1
tp[i].side = 1
tp[i].s = TAPE_GAP * i + (i - 1) * MAX_TAPELENGTH
tp[i].e = tp[i].s + MAX_TAPELENGTH
tp[i].splice = {}
for j = 1, 8 do
tp[i].splice[j] = {}
tp[i].splice[j].s = tp[i].s + (DEFAULT_SPLICELEN + 0.01) * (j - 1)
tp[i].splice[j].e = tp[i].splice[j].s + DEFAULT_SPLICELEN
tp[i].splice[j].l = tp[i].splice[j].e - tp[i].splice[j].s
tp[i].splice[j].name = "-"
tp[i].splice[j].info = "length: "..string.format("%.2f", DEFAULT_SPLICELEN).."s"
tp[i].splice[j].init_start = tp[i].splice[j].s
tp[i].splice[j].init_len = DEFAULT_SPLICELEN
tp[i].splice[j].init_beatnum = DEFAULT_BEATNUM
tp[i].splice[j].beatnum = DEFAULT_BEATNUM
tp[i].splice[j].bpm = 60
end
end
-- clip variables -> six clips define the active playback window, one for each track
clip = {}
for i = 1, 6 do
clip[i] = {}
clip[i].s = tp[i].splice[1].s
clip[i].e = tp[i].splice[1].e
clip[i].l = tp[i].splice[1].l
clip[i].bpm = tp[i].splice[1].bpm
end
function set_clip(i)
-- set playback window
clip[i].s = tp[i].splice[track[i].splice_active].s
clip[i].l = tp[i].splice[track[i].splice_active].l
clip[i].e = clip[i].s + clip[i].l
clip[i].bpm = tp[i].splice[track[i].splice_active].bpm
-- set softcut
softcut.loop_start(i, clip[i].s)
softcut.loop_end(i, clip[i].e)
local q = (clip[i].l / 64)
local off = util.round((math.ceil(clip[i].s / q) * q) - clip[i].s, 0.001)
softcut.phase_quant(i, q)
softcut.phase_offset(i, off)
if track[i].loop == 1 then
make_loop(i, track[i].loop_start, track[i].loop_end)
end
update_rate(i)
end
function splice_resize(i, focus, length)
-- if no length argument recalculate
if length == nil then
if track[i].tempo_map == 0 then
length = tp[i].splice[focus].beatnum
elseif track[i].tempo_map == 1 then
length = beat_sec * tp[i].splice[focus].beatnum
elseif track[i].tempo_map == 2 then
length = tp[i].splice[focus].l
end
end
-- set splice variables
if tp[i].splice[focus].s + length <= tp[i].e then
tp[i].splice[focus].e = tp[i].splice[focus].s + length
tp[i].splice[focus].l = length
tp[i].splice[focus].bpm = 60 / length * tp[i].splice[focus].beatnum
if track[i].splice_focus == track[i].splice_active then
set_clip(i)
end
set_info(i, focus)
else
show_message("splice too long")
end
end
function splice_reset(i, focus) -- reset splice to default length
local focus = focus or track[i].splice_focus
-- reset variables
tp[i].splice[focus].s = tp[i].splice[focus].init_start
tp[i].splice[focus].l = tp[i].splice[focus].init_len
tp[i].splice[focus].e = tp[i].splice[focus].s + tp[i].splice[focus].l
tp[i].splice[focus].beatnum = tp[i].splice[focus].init_beatnum
tp[i].splice[focus].bpm = 60 / tp[i].splice[focus].l * tp[i].splice[focus].beatnum
-- set clip
if track[i].splice_focus == track[i].splice_active then
set_clip(i)
end
set_info(i, focus)
end
function clear_splice(i) -- clear focused splice
local buffer = tp[i].side
local start = tp[i].splice[track[i].splice_focus].s
local length = tp[i].splice[track[i].splice_focus].l + FADE_TIME
softcut.buffer_clear_region_channel(buffer, start, length)
render_splice()
show_message("track "..i.." splice "..track[i].splice_focus.." cleared")
end
function clear_tape(i) -- clear tape and reset splices
local buffer = tp[i].side
local start = tp[i].s
softcut.buffer_clear_region_channel(buffer, start, MAX_TAPELENGTH)
track[i].loop = 0
init_splices(i)
render_splice()
show_message("track "..i.." tape cleared")
dirtygrid = true
end
function clear_buffers() -- clear both buffers and reset splices
softcut.buffer_clear()
for i = 1, 6 do
track[i].loop = 0
init_splices(i)
end
render_splice()
show_message("buffers cleared")
dirtygrid = true
end
function init_splices(i)
for j = 1, 8 do
tp[i].splice[j] = {}
tp[i].splice[j].s = tp[i].s + (DEFAULT_SPLICELEN + 0.01) * (j - 1)
tp[i].splice[j].e = tp[i].splice[j].s + DEFAULT_SPLICELEN
tp[i].splice[j].l = tp[i].splice[j].e - tp[i].splice[j].s
tp[i].splice[j].init_start = tp[i].splice[j].s
tp[i].splice[j].init_len = DEFAULT_SPLICELEN
tp[i].splice[j].beatnum = DEFAULT_BEATNUM
tp[i].splice[j].bpm = 60
tp[i].splice[j].name = "-"
set_info(i, j)
end
track[i].splice_active = 1
set_clip(i)
end
function save_all_markers()
for t = 1, 6 do
for s = 1, 8 do
tp[t].splice[s].init_len = tp[t].splice[s].l
tp[t].splice[s].init_start = tp[t].splice[s].s
tp[t].splice[s].init_beatnum = tp[t].splice[s].beatnum
end
end
end
function set_info(i, n)
if track[i].tempo_map == 2 then
tp[i].splice[n].info = "repitch factor: "..string.format("%.2f", current_tempo / tp[i].splice[n].bpm)
else
tp[i].splice[n].info = "length: "..string.format("%.2f", tp[i].splice[n].l).."s"
end
if view == vTAPE and view_splice_info then dirtyscreen = true end
end
function set_tempo_map(i)
if track[i].tempo_map == 1 then
for n = 1, 8 do
splice_resize(i, n)
end
else
for n = 1, 8 do
splice_reset(i, n)
end
end
render_splice()
page_redraw(vTAPE)
end
function recalc_splices()
for i = 1, 6 do
if track[i].tempo_map > 0 and track[i].loaded then
for j = 1, 8 do
splice_resize(i, j) -- resize clip according to tempo settings
end
render_splice()
end
end
end
--------------------- SNAPSHOTS -----------------------
snapshot_mode = false
snapshot_playback = false
snapshot_cut = false
snap = {}
for i = 1, 8 do -- 8 snapshot slots
snap[i] = {}
snap[i].data = false
snap[i].active = false
snap[i].play = {}
snap[i].mute = {}
snap[i].loop = {}
snap[i].loop_start = {}
snap[i].loop_end = {}
snap[i].cut = {}
snap[i].speed = {}
snap[i].rev = {}
snap[i].transpose_val = {}
for j = 1, 6 do -- 6 tracks
snap[i].play[j] = 0
snap[i].mute[j] = 0
snap[i].loop[j] = 0
snap[i].loop_start[j] = 1
snap[i].loop_end[j] = 16
snap[i].cut[j] = 1
snap[i].speed[j] = 0
snap[i].rev[j] = 0
snap[i].transpose_val[j] = 8
end
end
function save_snapshot(n)
for i = 1, 6 do
softcut.query_position(i)
snap[n].play[i] = track[i].play
snap[n].mute[i] = track[i].mute
snap[n].loop[i] = track[i].loop
snap[n].loop_start[i] = track[i].loop_start
snap[n].loop_end[i] = track[i].loop_end
snap[n].speed[i] = track[i].speed
snap[n].rev[i] = track[i].rev
snap[n].transpose_val[i] = params:get(i.."transpose")
clock.run(
function()
clock.sleep(0.05) -- give get_pos() some time
snap[n].cut[i] = track[i].cut
end
)
end
snap[n].data = true
end
function load_snapshots(snapshot)
for track = 1, 6 do
load_snapshot(snapshot, track)
end
end
function load_snapshot(n, i)
local e = {} e.t = eMUTE e.i = i e.mute = snap[n].mute[i] event(e)
local e = {} e.t = eREV e.i = i e.rev = snap[n].rev[i] event(e)
local e = {} e.t = eSPEED e.i = i e.speed = snap[n].speed[i] event(e)
local e = {} e.t = eTRSP e.i = i e.val = snap[n].transpose_val[i] event(e)
if snap[n].loop[i] == 1 then
loop_event(i, snap[n].loop_start[i], snap[n].loop_end[i])
elseif snap[n].loop[i] == 0 then
local e = {} e.t = eUNLOOP e.i = i event(e)
end
if snapshot_playback then
if snap[n].play[i] == 0 then
local e = {} e.t = eSTOP e.i = i event(e)
else
if snapshot_cut then track[i].cut = snap[n].cut[i] end
local e = {} e.t = eSTART e.i = i event(e)
end
end
end
--------------------- SOFTCUT FUNCTIONS -----------------------
function toggle_rec(i) -- toggle recording and trigger chop function
track[i].rec = 1 - track[i].rec
set_rec(i)
chop(i)
grid_page(vREC)
end
function set_rec(i) -- set softcut rec and pre levels
if track[i].fade == 0 then
if track[i].rec == 1 and track[i].play == 1 then
softcut.pre_level(i, track[i].pre_level)
softcut.rec_level(i, track[i].rec_level)
else
softcut.pre_level(i, 1)
softcut.rec_level(i, 0)
end
elseif track[i].fade == 1 then
if track[i].rec == 1 and track[i].play == 1 then
softcut.pre_level(i, track[i].pre_level)
softcut.rec_level(i, track[i].rec_level)
else
softcut.pre_level(i, track[i].pre_level)
softcut.rec_level(i, 0)
end
end
page_redraw(vMAIN, 2)
end
function set_level(i) -- set track volume and mute track
if track[i].mute == 0 and track[i].play == 1 then
softcut.level(i, track[i].level)
set_track_sends(i)
else
softcut.level(i, 0)
softcut.level_cut_cut(i, 5, 0)
softcut.level_cut_cut(i, 6, 0)
end
page_redraw(vMAIN, 1)
end
function set_track_sends(i) -- internal softcut routing
if track[i].t5 == 1 and track[i].play == 1 then
softcut.level_cut_cut(i, 5, track[i].send_t5 * track[i].level)
else
softcut.level_cut_cut(i, 5, 0)
end
if track[i].t6 == 1 and track[i].play == 1 then
softcut.level_cut_cut(i, 6, track[i].send_t6 * track[i].level)
else
softcut.level_cut_cut(i, 6, 0)
end
end
function get_pos(i, pos) -- get and store softcut position (callback)
track[i].cut = pos
if track[i].play_mode == 2 then
if track[i].rev == 0 and track[i].pos_hi_res == 64 then
track[i].cut = clip[i].s
track[i].pos_arc = 1
elseif track[i].rev == 1 and track[i].pos_hi_res == 1 then
track[i].cut = clip[i].e
track[i].pos_arc = 64
end
end
end
function stop_track(i)
softcut.query_position(i)
track[i].play = 0
trig[i].tick = 0
set_level(i)
set_rec(i)
dirtygrid = true
end
function make_loop(i, lstart, lend)
track[i].loop = 1
track[i].loop_start = lstart
track[i].loop_end = lend
local s = clip[i].s + (lstart - 1) / 16 * clip[i].l
local e = clip[i].s + (lend) / 16 * clip[i].l
softcut.loop_start(i, s)
softcut.loop_end(i, e)
enc2_wait = false
dirtygrid = true
end
function clear_loop(i)
track[i].loop = 0
softcut.loop_start(i, clip[i].s)
softcut.loop_end(i, clip[i].e)
end
function copy_buffer(i, src, dst) -- copy splice to the other buffer
local n = track[i].splice_focus
softcut.buffer_copy_mono(src, dst, tp[i].splice[n].s, tp[i].splice[n].s, tp[i].splice[n].l, 0.01)
local dst_name = dst == 1 and "main" or "temp"
show_message("splice copied to "..dst_name.." buffer")
end
function set_track_source(option) -- select audio source
audio.level_adc_cut(option == 3 and 0 or 1)
audio.level_eng_cut(option == 2 and 0 or 1)
audio.level_tape_cut(option == 1 and 0 or 1)
end
function set_softcut_input(i) -- select softcut input
if tp[i].input == 1 then -- L&R
softcut.level_input_cut(1, i, 0.707)
softcut.level_input_cut(2, i, 0.707)
elseif tp[i].input == 2 then -- L IN
softcut.level_input_cut(1, i, 1)
softcut.level_input_cut(2, i, 0)
elseif tp[i].input == 3 then -- R IN
softcut.level_input_cut(1, i, 0)
softcut.level_input_cut(2, i, 1)
elseif tp[i].input == 4 then -- OFF
softcut.level_input_cut(1, i, 0)
softcut.level_input_cut(2, i, 0)
end
end
function filter_select(i, option)
softcut.post_filter_lp(i, option == 1 and 1 or 0)
softcut.post_filter_hp(i, option == 2 and 1 or 0)
softcut.post_filter_bp(i, option == 3 and 1 or 0)
softcut.post_filter_br(i, option == 4 and 1 or 0)
softcut.post_filter_dry(i, option == 5 and 1 or track[i].dry_level)
end
function phase_poll(i, pos)
-- calc softcut positon
local pp = ((pos - clip[i].s) / clip[i].l)
local pc = ((pos - tp[i].s) / MAX_TAPELENGTH)
local g_pos = math.floor(pp * 16)
local a_pos = math.floor(pp * 64)
-- calc positions
track[i].pos_abs = pos -- absoulute position on buffer
track[i].pos_hi_res = util.clamp(a_pos + 1 % 64, 1, 64) -- fine mesh for arc
track[i].pos_lo_res = util.clamp(g_pos + 1 % 16, 1, 16) -- coarse mesh for grid
if track[i].play == 1 then
if track[i].pos_lo_res ~= track[i].pos_grid then
track[i].pos_grid = track[i].pos_lo_res
end
if track[i].pos_arc ~= track[i].pos_hi_res then
track[i].pos_arc = track[i].pos_hi_res
end
if track[i].pos_rel ~= pp then
track[i].pos_rel = pp -- relative position within clip
end
if track[i].pos_clip ~= pc then
track[i].pos_clip = pc -- relative position within allocated buffer space
end
-- display position
grid_page(vLFO)
grid_page(vTAPE)
if (grido_view < vLFO or grido_view == vTAPE) then
dirtygrid = true
end
if (gridz_view < vLFO or gridz_view == vTAPE) then
dirtygrid = true
end
page_redraw(vTAPE)
end
-- oneshot play_mode
if track[i].play_mode == 2 and track[i].loop == 0 and track[i].play == 1 then
if track[i].rev == 0 then
if track[i].pos_hi_res == 64 then
stop_track(i)
end
else
if track[i].pos_hi_res == 1 then
stop_track(i)
end
end
end
-- randomize at cycle
if autorand_at_cycle and track[i].sel == 1 and not oneshot_rec then
if track[i].play == 1 then
track[i].step_count = track[i].step_count + 1
if track[i].step_count > rnd_stepcount * 4 then
randomize(i)
end
end
end
-- track 2 trigger
if track[i].play == 1 then
-- rec @step
if trig[i].rec_step > 0 then
if track[i].rev == 0 then
if track[i].pos_hi_res == trig[i].rec_step * 4 - 3 then
toggle_rec(i)
end
else
if track[i].pos_hi_res == trig[i].rec_step * 4 then
toggle_rec(i)
end
end
end
-- trig @step mode
if trig[i].step > 0 then
if track[i].rev == 0 then
if track[i].pos_hi_res == trig[i].step * 4 - 3 then
send_trig(i)
end
else
if track[i].pos_hi_res == trig[i].step * 4 then
send_trig(i)
end
end
end
-- trig @count mode
if trig[i].count > 0 then
trig[i].tick = trig[i].tick + 1 -- count steps
if trig[i].tick >= trig[i].count * 4 then
send_trig(i)
trig[i].tick = 0
end
end
end
end
function update_rate(i)
local n = math.pow(2, track[i].speed + track[i].transpose + track[i].detune)
if track[i].rev == 1 then n = -n end
if track[i].tempo_map == 2 then
local bpmmod = current_tempo / clip[i].bpm
n = n * bpmmod
end
softcut.rate(i, n)
end
--------------------- WAVEFORM VIZ -----------------------
waveform_samples = {}
wave_gain = {}
for i = 1, 6 do
waveform_samples[i] = {}
wave_gain[i] = {}
end
view_buffer = false
function wave_render(ch, start, i, s)
waveform_samples[track_focus] = {}
waveform_samples[track_focus] = s
waveviz_reel = false
wave_gain[track_focus] = wave_getmax(waveform_samples[track_focus])
dirtyscreen = true
end
function wave_getmax(t)
local max = 0
for _,v in pairs(t) do
if math.abs(v) > max then
max = math.abs(v)
end
end
return util.clamp(max, 0.4, 1)
end
function render_splice()
if view == vTAPE and not (view_splice_info or view_presets) then
if view_buffer then
local start = tp[track_focus].s
local length = tp[track_focus].e - tp[track_focus].s
local buffer = tp[track_focus].side
softcut.render_buffer(buffer, start, length, 128)
else
local n = track[track_focus].splice_focus
local start = tp[track_focus].splice[n].s
local length = tp[track_focus].splice[n].e - tp[track_focus].splice[n].s
local buffer = tp[track_focus].side
softcut.render_buffer(buffer, start, length, 128)
end
end
end
--------------------- SCALE AND TRANSPOSITION -----------------------
function set_scale(option) -- set scale id, thanks zebra