-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanes.lua
1238 lines (1110 loc) · 33.3 KB
/
lanes.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
-- lanes v0.0.6b @sonocircuit
-- llllllll.co/t/url
--
--
-- multi track midi
-- player &
-- recorder
--
--
-- E1: select lane
-- E2/E3: change parameter
-- K2/K3: navigate pages
-- hold K1: display options/shift
-- shift + K2/K3: select options
--
local fs = require 'fileselect'
local lt = require 'lattice'
local ms = require 'core/mods'
local rf = include 'lib/lanes_reflection'
local md = include 'lib/lanes_midi'
local nb = include 'lib/nb/lib/nb'
local m = midi.connect()
local g = grid.connect()
------------------------------
-- TODO: docs and testing
------------------------------
--------- variables ----------
local load_pset = false
local NUM_LANES = 7
local NUM_PAGES = 7
local NUM_SNAP = 8
local default_path = _path.code .."lanes/midi_files/"
-- UI
local shift = false
local page_num = 1
local lane_focus = 1
local param_focus = {1, 1, 1, 1, 1}
local param_num = {3, 2, 2, 2, 2}
local reset_key = false
local stop_key = false
local rec_key = false
local all_key = false
local dirtyscreen = false
local dirtygrid = false
-- key viz
local bar_val = 4
local pulse_bar = false
local pulse_beat = false
local pulse_key_fast = 8
local pulse_key_mid = 4
local pulse_key_slow = 4
-- midi import
local STEP_RES = 96 -- reflection defaults at 96ppqn
local tick_res = 0
local p = {} -- temp storage for pattern data
p.count = 0
p.step = 0
p.event = {}
p.endpoint = 0
-- midi io
local glb_midi_in = true
local glb_midi_ch = 1
local midi_devices = {}
local mo = {}
for i = 1, NUM_LANES do
mo[i] = midi.connect()
end
-- held grid keys and midi keys
local held = {}
for i = 1, NUM_LANES do
held[i] = {}
held[i].num = 0
held[i].max = 0
held[i].first = 0
held[i].second = 0
held[i].notes = {}
end
-- note off flag. if true then note off msg has been recorded
local off_flag = {}
for i = 1, NUM_LANES do
off_flag[i] = {}
for note = 0, 127 do
off_flag[i][note] = false
end
end
-- options
local quant_values = {1/4, 1, 4}
local note_q_options = {"none", "1/64", "1/48", "3/128", "1/32", "1/24", "3/64", "1/16", "1/12", "3/32", "1/8", "1/6", "3/16", "1/4"}
local note_q_values = {1/96, 1/64, 1/48, 3/128, 1/32, 1/24, 3/64, 1/16, 1/12, 3/32, 1/8, 1/6, 3/16, 1/4}
local meter_options = {"2/4", "3/4", "4/4", "5/4", "6/4", "7/4", "9/4"}
local meter_values = {2/4, 3/4, 4/4, 5/4, 6/4, 7/4, 9/4}
-- snapshots
local snap = {}
for slot = 1, NUM_SNAP do
snap[slot] = {}
snap[slot].has_data = false
for lane = 1, NUM_LANES do
snap[slot][lane] = {}
snap[slot][lane].playing = false
snap[slot][lane].looping = false
snap[slot][lane].min = 1
snap[slot][lane].max = 16
end
end
-- quantization
local launch_q = 1
local loop_q = 1
local cut_q = 1
local key_q = 1
local snap_q = 1
-- pattern events
local quant_event = {}
local rec_focus = 0
--------- pattern playback ----------
function lane_runner(e, i)
if e.msg == "note_on" then
play_notes(e, i)
table.insert(lane[i].active_notes, e.note)
elseif e.msg == "note_off" then
-- if the corresponding note is not held then stop the note.
if not tab.contains(held[e.i].notes, e.note) then
stop_notes(e, i)
table.remove(lane[i].active_notes, tab.key(lane[i].active_notes, e.note))
end
end
end
function play_notes(e, i)
if lane[i].output == 1 then
mo[i]:note_on(e.note, e.vel, lane[i].mo_ch)
else
local player = params:lookup_param("nb_player_"..i):get_player()
local velocity = util.linlin(0, 127, 0, 1, e.vel)
player:note_on(e.note, velocity)
end
end
function stop_notes(e, i)
if lane[i].output == 1 then
mo[i]:note_off(e.note, 64, lane[i].mo_ch)
else
local player = params:lookup_param("nb_player_"..i):get_player()
player:note_off(e.note)
end
end
function event_q_clock()
while true do
clock.sync(key_q)
if #quant_event > 0 then
for _, e in ipairs(quant_event) do
if e.msg == "note_on" then
lane[e.i]:watch(e)
if lane[e.i].midi_thru then
play_notes(e, e.i)
end
if lane[e.i].rec == 1 then
dirtyscreen = true
end
elseif e.msg == "note_off" then
if not off_flag[e.i][e.note] then
lane[e.i]:watch(e)
off_flag[e.i][e.note] = true
end
if lane[e.i].midi_thru then
stop_notes(e, e.i)
end
end
end
quant_event = {}
end
end
end
lane = {}
for i = 1, NUM_LANES do
lane[i] = rf.new(i)
lane[i].process = lane_runner
lane[i].start_callback = function() clear_active_notes(i) end
lane[i].end_of_loop_callback = function() catch_note_off(i) end
lane[i].end_callback = function() clear_active_notes(i) dirtygrid = true end
lane[i].step_callback = function() track_playhead(i) end
lane[i].length = 16
lane[i].meter_id = 3
lane[i].barnum = 4
lane[i].position = 1
lane[i].file_id = "rec lane "..i
lane[i].played_notes = {}
lane[i].active_notes = {}
lane[i].step_min_viz = 0
lane[i].step_max_viz = 0
lane[i].looping = false
lane[i].loop_reset = false
lane[i].snap_reset = false
lane[i].output = 1
lane[i].mo_ch = 1
lane[i].mi_ch = 1
lane[i].midi_thru = true
end
function track_playhead(i)
local size = math.floor(lane[i].endpoint / 16)
if lane[i].step % size == 1 then
local prev_pos = lane[i].position
lane[i].position = math.floor((lane[i].step) / size) + 1
dirtygrid = true
end
end
function clear_active_notes(i)
if #lane[i].active_notes > 0 and lane[i].endpoint > 0 then
for _, note_num in ipairs(lane[i].active_notes) do
if lane[i].output == 1 then
mo[i]:note_off(note_num, 64, lane[i].mo_ch)
else
local player = params:lookup_param("nb_player_"..i):get_player()
player:note_off(note_num)
end
end
lane[i].active_notes = {}
end
end
function catch_note_off(i)
if lane[i].rec == 1 then
local s = lane[i].step_max
for _, note in ipairs(held[i].notes) do
local e = {i = i, msg = "note_off", note = note, vel = 64}
if not lane[i].event[s] then
lane[i].event[s] = {}
end
table.insert(lane[i].event[s], e)
table.remove(held[i].notes, tab.key(held[i].notes, note))
lane[i].count = lane[i].count + 1
end
end
end
function cut_pattern_pos(i, sync, pos)
clock.sync(sync)
lane[i].step = math.floor(lane[i].endpoint / 16) * (pos - 1)
clear_active_notes(i)
end
function set_pattern_loop(i, sync, first, second)
clock.sync(sync)
local segment = math.floor(lane[i].endpoint / 16)
lane[i].step_min = segment * (math.min(first, second) - 1)
lane[i].step_max = segment * math.max(first, second)
lane[i].step = lane[i].step_min
lane[i].looping = true
clear_active_notes(i)
end
function clear_pattern_loop(i, sync)
clock.sync(sync)
lane[i].step = lane[i].loop_reset and 0 or (math.floor(lane[i].endpoint / 16) * lane[i].position)
lane[i].step_min = 0
lane[i].step_max = lane[i].endpoint
lane[i].looping = false
clear_active_notes(i)
end
function reset_lane(i, sync, clear_loop)
clock.sync(sync)
lane[i].step = clear_loop and 0 or lane[i].step_min
if clear_loop then
lane[i].step_min = 0
lane[i].step_max = lane[i].endpoint
lane[i].looping = false
end
clear_active_notes(i)
end
function num_rec_enabled()
local num_enabled = 0
for i = 1, NUM_LANES do
if lane[i].rec_enabled > 0 then
num_enabled = num_enabled + 1
end
end
return num_enabled
end
--------- snapshots ----------
function save_snapshot(slot)
snap[slot].has_data = true
for i = 1, NUM_LANES do
snap[slot][i].playing = lane[i].play == 1 and true or false
snap[slot][i].looping = lane[i].looping
snap[slot][i].min = lane[i].step_min_viz
snap[slot][i].max = lane[i].step_max_viz
end
end
function load_snapshot(slot) --TODO: redo snap logic.
if snap[slot].has_data then
for i = 1, NUM_LANES do
if snap[slot][i].playing then
if lane[i].play == 0 then
lane[i]:start(snap_q)
else
if snap[slot][i].looping then
clock.run(set_pattern_loop, i, snap_q, snap[slot][i].min, snap[slot][i].max)
lane[i].step_min_viz = snap[slot][i].min
lane[i].step_max_viz = snap[slot][i].max
else
if lane[i].looping then
clock.run(clear_pattern_loop, i, snap_q)
lane[i].looping = false
end
if lane[i].snap_reset then
clock.run(reset_lane, i, snap_q)
end
end
end
else
if lane[i].play == 1 then
lane[i]:stop(snap_q)
end
end
end
end
end
function clear_snapshot(slot)
snap[slot].has_data = false
end
-------- midi --------
function build_midi_device_list()
midi_devices = {}
for i = 1, #midi.vports do
local long_name = midi.vports[i].name
local short_name = string.len(long_name) > 12 and util.acronym(long_name) or long_name
table.insert(midi_devices, i..": "..short_name)
end
end
function set_all_channels()
if glb_midi_in then
for i = 1, NUM_LANES do
params:set("midi_in_channel_"..i, glb_midi_ch)
end
end
end
function set_midi_channel(i, val)
if glb_midi_in then
params:set("midi_in_channel_"..i, glb_midi_ch)
else
lane[i].mi_ch = val
end
end
function midi.add()
build_midi_device_list()
end
function midi.remove()
clock.run(function()
clock.sleep(0.2)
build_midi_device_list()
end)
end
function set_midi_callback()
for i = 1, 16 do
midi.vports[i].event = nil
end
m.event = midi_events
end
function midi_events(data)
local msg = midi.to_msg(data)
local i = rec_focus > 0 and rec_focus or lane_focus
if msg.type == "note_on" or msg.type == "note_off" then
if msg.ch == lane[i].mi_ch then
local e = {i = i, msg = msg.type, note = msg.note, vel = msg.vel}
table.insert(quant_event, e)
if msg.type == "note_on" then
-- flag that note off msg needs to occur
off_flag[i][msg.note] = flase
table.insert(held[i].notes, msg.note)
elseif msg.type == "note_off" then
table.remove(held[i].notes, tab.key(held[i].notes, msg.note))
end
end
end
end
function clock.transport.start()
-- nothing yet
end
function clock.transport.stop()
for i = 1, NUM_LANES do
lane[i]:stop()
end
end
-------- clock coroutines --------
function ledpulse_fast()
pulse_key_fast = pulse_key_fast == 8 and 12 or 8
end
function ledpulse_mid()
pulse_key_mid = util.wrap(pulse_key_mid + 1, 4, 12)
end
function ledpulse_slow()
pulse_key_slow = util.wrap(pulse_key_slow + 1, 4, 12)
end
function ledpulse_bar()
while true do
clock.sync(bar_val)
pulse_bar = true
dirtygrid = true
clock.run(function()
clock.sleep(1/30)
pulse_bar = false
dirtygrid = true
end)
end
end
function ledpulse_beat()
while true do
clock.sync(1)
pulse_beat = true
dirtygrid = true
clock.run(function()
clock.sleep(1/30)
pulse_beat = false
dirtygrid = true
end)
end
end
--------- pattern conversion ----------
-- format event
function format_event(msg, note, vel)
local msg = msg == "noteOn" and "note_on" or "note_off" -- transform string
local e = {i = lane_focus, msg = msg, note = note, vel = vel}
return e
end
-- callback function to grab tick resolution from header
function get_ticks(string, format, tracks, division)
tick_res = division
end
-- handler for note on/off messages
function parse_notes(msg, channel, note, velocity)
local vel = math.floor(util.linlin(0, 1, 0, 127, velocity))
local e = format_event(msg, note, vel)
if not p.event[p.step] then
p.event[p.step] = {}
end
table.insert(p.event[p.step], e)
p.count = p.count + 1
end
-- handler for deltatime increments
function get_position(ticks)
p.step = p.step + math.floor((ticks / tick_res) * STEP_RES)
end
-- callback to for conversion
function to_pattern(msg, ...)
if msg == "deltatime" then
get_position(...)
elseif msg == "noteOn" or msg == "noteOff" then
parse_notes(msg, ...)
elseif msg == "endOfTrack" then
copy_to_pattern(p.focus)
end
end
function convert_to_reflection(i, filename)
if filename ~= "cancel" and filename ~= "" and filename ~= default_path then
-- clear temp pattern
p.step = 1
p.count = 0
p.event = {}
p.endpoint = 0
p.focus = i
-- set id
lane[i].file_id = filename:match("[^/]*$")
-- read midi and convert
local file = assert(io.open(filename, "rb"))
md.processHeader(file, get_ticks)
assert(file:seek("set"))
md.processTrack(file, to_pattern, 1)
file:close()
end
end
function get_length(i)
local num_beats = tonumber(lane[i].file_id:match("[^_]*$"):sub(1, -6))
if type(num_beats) ~= "number" then
local b = p.step / STEP_RES
local bl = math.floor(b)
if b >= bl + 0.5 then
num_beats = bl + 1
else
num_beats = bl
end
end
return num_beats
end
function set_length(i, beats)
local beats = beats or meter_values[lane[i].meter_id] * lane[i].barnum * 4
lane[i].length = beats
lane[i]:set_length(beats)
end
function copy_to_pattern(i)
local num_beats = get_length(i)
set_length(i, num_beats)
lane[i].count = p.count
lane[i].event = deep_copy(p.event)
-- get bar and meter values
if ((lane[i].endpoint % STEP_RES == 0) and (lane[i].endpoint >= (STEP_RES * 2))) then
-- calc values
local current_meter = meter_values[lane[i].meter_id]
local bar_count = num_beats / (current_meter * 4)
-- check bar-size
if bar_count % 1 == 0 then
params:set("lane_bar_num_"..i, bar_count)
else
-- get closest fit
local n = lane[i].endpoint > (STEP_RES * 2) and 2 or 1
for c = n, #meter_values do
local new_meter = meter_values[c]
local new_count = num_beats / (new_meter * 4)
if new_count % 1 == 0 then
local b = math.floor(new_count)
params:set("lane_meter_id_"..i, c)
params:set("lane_bar_num_"..i, b)
break
end
end
end
end
print("copied to pattern: "..i, "num beats: "..lane[i].length)
end
function set_midi_file(i, filename)
convert_to_reflection(i, filename)
screenredrawtimer:start()
dirtyscreen = true
end
--------- utility functions ----------
function set_quant_val()
local l = params:get("launch_quant")
launch_q = l == 3 and bar_val or (l == 2 and 1 or 1/4)
local o = params:get("loop_quant")
loop_q = o == 3 and bar_val or (o == 2 and 1 or 1/4)
local c = params:get("cut_quant")
cut_q = c == 3 and bar_val or (c == 2 and 1 or 1/4)
local s = params:get("snap_quant")
snap_q = s == 3 and bar_val or (s == 2 and 1 or 1/4)
end
function deep_copy(tbl)
local ret = {}
if type(tbl) ~= 'table' then return tbl end
for key, value in pairs(tbl) do
ret[key] = deep_copy(value)
end
return ret
end
function print_event_tab()
for k, v in pairs(p.event) do
print("events @ step "..k)
for e, v in pairs(p.event[k]) do
print("event "..e)
tab.print(p.event[k][e])
end
end
end
--------- init function ----------
function init()
-- build midi device list
build_midi_device_list()
-- make directory
if util.file_exists(default_path) == false then
util.make_dir(default_path)
end
-- add params
params:add_separator("global_settings", "global settings", 4)
-- time signiture
params:add_number("time_signature", "time signature", 2, 9, 4, function(param) return param:get().."/4" end)
params:set_action("time_signature", function(val) bar_val = val set_quant_val() end)
params:add_group("quantization_options", "quantization", 5)
-- input quantization
params:add_option("key_quant", "key Q", note_q_options, 1)
params:set_action("key_quant", function(idx) key_q = note_q_values[idx] * 4 end)
-- cut quantization
params:add_option("cut_quant", "cut Q", {"free", "beat", "bar"}, 2)
params:set_action("cut_quant", function() set_quant_val() end)
-- loop quantization
params:add_option("loop_quant", "loop Q", {"free", "beat", "bar"}, 2)
params:set_action("loop_quant", function() set_quant_val() end)
-- launch quantization
params:add_option("launch_quant", "launch Q", {"free", "beat", "bar"}, 3)
params:set_action("launch_quant", function() set_quant_val() end)
-- snapshot quantization
params:add_option("snap_quant", "snaphot Q", {"free", "beat", "bar"}, 2)
params:set_action("snap_quant", function() set_quant_val() end)
params:add_group("input_options", "midi input", 3)
-- glb midi in device
params:add_option("glb_midi_in_device", "device", midi_devices, 1)
params:set_action("glb_midi_in_device", function(val) mi = midi.connect(val) set_midi_callback() end)
-- glb midi in
params:add_option("glb_midi_in_option", "midi in channel", {"lane", "global"}, 1)
params:set_action("glb_midi_in_option", function(mode) glb_midi_in = mode == 2 and true or false set_all_channels() end)
-- glb midi channel
params:add_number("glb_midi_in_channel", "channel", 1, 16, 1)
params:set_action("glb_midi_in_channel", function(val) glb_midi_ch = val set_all_channels() end)
params:add_separator("lanes_params", "lanes")
for i = 1, NUM_LANES do
params:add_group("lane_settings_"..i, "lane "..i, 15)
params:add_separator("input_options_"..i, "midi input")
-- midi thru
params:add_option("midi_in_thru_"..i, "midi monitor", {"off", "thru"}, 2)
params:set_action("midi_in_thru_"..i, function(mode) lane[i].midi_thru = mode == 2 and true or false end)
-- midi in channel
params:add_number("midi_in_channel_"..i, "channel", 1, 16, 1)
params:set_action("midi_in_channel_"..i, function(val) set_midi_channel(i, val) end)
params:add_separator("output_options_"..i, "lane output")
-- output options
params:add_option("lane_output_"..i, "output", {"midi", "nb player"}, 1)
params:set_action("lane_output_"..i, function(mode) lane[i].output = mode build_menu() end)
-- nb voice
nb:add_param("nb_player_"..i, "player")
-- midi out device
params:add_option("midi_out_device_"..i, "device", midi_devices, 1)
params:set_action("midi_out_device_"..i, function(val) mo[i] = midi.connect(val) end)
-- midi out channel
params:add_number("midi_out_channel_"..i, "channel", 1, 16, 1)
params:set_action("midi_out_channel_"..i, function(val) lane[i].mo_ch = val end)
params:add_separator("playback_options_"..i, "playback options")
-- playback mode
params:add_option("playback_mode_"..i, "playback mode", {"oneshot", "loop"}, 2)
params:set_action("playback_mode_"..i, function(mode) lane[i]:set_loop(mode - 1) end)
-- playback quantization
params:add_option("playback_quant_"..i, "note quantization", note_q_options, 1)
params:set_action("playback_quant_"..i, function(idx) lane[i].quantize = note_q_values[idx] * 4 end)
-- loop clear
params:add_option("loop_clear_mode_"..i, "loop clear", {"reset", "continue"}, 1)
params:set_action("loop_clear_mode_"..i, function(mode) lane[i].loop_reset = mode == 1 and true or false end)
-- snap reset
params:add_option("snap_reset_mode_"..i, "snap load", {"reset", "continue"}, 1)
params:set_action("snap_reset_mode_"..i, function(mode) lane[i].snap_reset = mode == 1 and true or false end)
params:add_number("lane_meter_id_"..i, "meter id", 1, #meter_options, 3)
params:set_action("lane_meter_id_"..i, function(idx) lane[i].meter_id = idx end)
params:hide("lane_meter_id_"..i)
params:add_number("lane_bar_num_"..i, "length", 1, 128, 4)
params:set_action("lane_bar_num_"..i, function(val) lane[lane_focus].barnum = val end)
params:hide("lane_bar_num_"..i)
end
-- nb parameters
params:add_separator("nb_params", "nb voices")
nb:add_player_params()
-- fx parameters
if ms.is_loaded("fx") then
params:add_separator("fx_params", "fx")
end
-- set default length
for i = 1, NUM_LANES do
set_length(i)
end
-- bang params
if load_pset then
params:default()
else
params:bang()
end
-- metros
screenredrawtimer = metro.init(function() screen_redraw() end, 1/15, -1)
screenredrawtimer:start()
dirtyscreen = true
hardwareredrawtimer = metro.init(function() hardware_redraw() end, 1/30, -1)
hardwareredrawtimer:start()
dirtygrid = true
-- clocks
clock.run(ledpulse_bar)
clock.run(ledpulse_beat)
clock.run(event_q_clock)
-- lattice
vizclock = lt:new()
fastpulse = vizclock:new_sprocket{
action = function() ledpulse_fast() end,
division = 1/32,
enabled = true
}
midpulse = vizclock:new_sprocket{
action = function() ledpulse_mid() end,
division = 1/24,
enabled = true
}
slowpulse = vizclock:new_sprocket{
action = function() ledpulse_slow() end,
division = 1/12,
enabled = true
}
vizclock:start()
end
--------- norns UI ----------
function key(n, z)
if n == 1 then
shift = z == 1 and true or false
else
if shift then
if page_num == 1 then
if n == 2 and z == 1 then
screenredrawtimer:stop()
fs.enter(default_path, function(filename) set_midi_file(lane_focus, filename) end)
shift = false
elseif n == 3 and z == 1 then
lane[lane_focus]:clear()
end
elseif page_num == 2 then
if z == 1 then
set_length(lane_focus)
end
elseif page_num == 4 then
if z == 1 then
params:set("lane_output_"..lane_focus, n - 1)
end
end
else
if z == 1 then
local inc = n == 3 and 1 or -1
page_num = util.wrap(page_num + inc, 1, NUM_PAGES)
end
end
end
dirtyscreen = true
end
function enc(n, d)
if n == 1 then
lane_focus = util.clamp(lane_focus + d, 1, NUM_LANES)
else
if page_num == 2 then
local param = {"lane_meter_id_", "lane_bar_num_"}
params:delta(param[n - 1]..lane_focus, d)
elseif page_num == 3 then
if shift then
local param = {"glb_midi_in_option", "glb_midi_in_channel"}
params:delta(param[n - 1], d)
else
local param = {"midi_in_thru_", "midi_in_channel_"}
params:delta(param[n - 1]..lane_focus, d)
end
elseif page_num == 4 then
if lane[lane_focus].output == 1 then
local param = {"midi_out_device_", "midi_out_channel_"}
params:delta(param[n - 1]..lane_focus, d)
else
local param = {"nb_player_", "nb_player_"}
params:delta(param[n - 1]..lane_focus, d)
end
elseif page_num == 5 then
local param = {"playback_mode_", "playback_quant_"}
params:delta(param[n - 1]..lane_focus, d)
elseif page_num == 6 then
local param = {"loop_clear_mode_", "snap_reset_mode_"}
params:delta(param[n - 1]..lane_focus, d)
elseif page_num == 7 then
local param = {"time_signature", "key_quant"}
params:delta(param[n - 1], d)
end
end
dirtyscreen = true
end
function redraw()
screen.clear()
screen.font_face(2)
if page_num ~= 7 then
screen.font_size(8)
screen.level(15)
screen.move(4, 12)
screen.text(lane_focus)
end
if page_num == 1 then -- NOTES
screen.font_size(8)
screen.level(2)
screen.move(64, 12)
screen.text_center("NOTES")
screen.font_size(16)
screen.level(shift and 1 or 12)
screen.move(64, 39)
screen.text_center(lane[lane_focus].count > 0 and lane[lane_focus].file_id or "LOAD or REC")
screen.font_size(8)
screen.level(shift and 8 or 0)
screen.move(34, 58)
screen.text_center("load file")
screen.move(94, 58)
screen.text_center("clear lane")
elseif page_num == 2 then -- LENGTH
screen.font_size(8)
screen.level(2)
screen.move(64, 12)
screen.text_center("LENGTH")
local num_beats = meter_values[lane[lane_focus].meter_id] * lane[lane_focus].barnum * 4
local barnum = math.floor(lane[lane_focus].barnum)
screen.font_size(16)
screen.level(lane[lane_focus].length == num_beats and 12 or 2)
screen.move(34, 39)
screen.text_center(meter_options[lane[lane_focus].meter_id])
screen.move(94, 39)
screen.text_center(barnum..(barnum == 1 and " bar" or " bars"))
screen.font_size(8)
screen.level(shift and 8 or 2)
if shift then
screen.move(64, 58)
screen.text_center("> set <")
else
screen.move(34, 58)
screen.text_center("meter")
screen.move(94, 58)
screen.text_center("length")
end
elseif page_num == 3 then -- INPUT
screen.font_size(8)
screen.level(2)
screen.move(64, 12)
screen.text_center("INPUT")
screen.font_size(16)
screen.level(12)
if shift then
screen.move(34, 39)
screen.text_center(params:string("glb_midi_in_option"))
screen.move(94, 39)
screen.text_center(glb_midi_in and params:string("glb_midi_in_channel") or "-")
else
screen.move(34, 39)
screen.text_center(params:string("midi_in_thru_"..lane_focus))
screen.move(94, 39)
screen.text_center(glb_midi_in and "glb" or params:string("midi_in_channel_"..lane_focus))
end
screen.font_size(8)
screen.level(shift and 8 or 2)
if shift then
screen.move(34, 58)
screen.text_center("midi in")
screen.move(94, 58)
screen.text_center("global ch")
else
screen.move(34, 58)
screen.text_center("monitor")
screen.move(94, 58)
screen.text_center("channel")
end
elseif page_num == 4 then -- OUTPUT
screen.font_size(8)
screen.level(2)
screen.move(64, 12)
screen.text_center("OUTPUT")
screen.font_size(16)
screen.level(12)
if lane[lane_focus].output == 1 then
screen.move(34, 39)
screen.text_center(params:string("midi_out_device_"..lane_focus))
screen.move(94, 39)
screen.text_center(params:string("midi_out_channel_"..lane_focus))
else
screen.move(64, 39)
screen.text_center(params:string("nb_player_"..lane_focus))
end
screen.font_size(8)
screen.level(shift and 8 or 2)
if shift then
screen.move(34, 58)
screen.text_center("> midi")
screen.move(94, 58)
screen.text_center("nb <")
else
if lane[lane_focus].output == 1 then
screen.move(34, 58)
screen.text_center("device")
screen.move(94, 58)
screen.text_center("channel")
else
screen.move(64, 58)
screen.text_center("nb player")
end
end
elseif page_num == 5 then -- PLAYBACK
screen.font_size(8)
screen.level(2)
screen.move(64, 12)
screen.text_center("PLAYBACK")
screen.font_size(16)
screen.level(12)
screen.move(34, 39)
screen.text_center(params:string("playback_mode_"..lane_focus))
screen.move(94, 39)
screen.text_center(params:string("playback_quant_"..lane_focus))
screen.font_size(8)
screen.level(2)
screen.move(34, 58)
screen.text_center("mode")
screen.move(94, 58)
screen.text_center("quantization")
elseif page_num == 6 then -- PLAYHEAD
screen.font_size(8)
screen.level(2)
screen.move(64, 12)
screen.text_center("PLAYHEAD")
screen.font_size(16)
screen.level(12)
screen.move(34, 39)
screen.text_center(params:string("loop_clear_mode_"..lane_focus))
screen.move(94, 39)
screen.text_center(params:string("snap_reset_mode_"..lane_focus))