-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfretware.lua
More file actions
1713 lines (1602 loc) · 43 KB
/
fretware.lua
File metadata and controls
1713 lines (1602 loc) · 43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
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
-- hi
engine.name = 'Cule'
musicutil = require 'musicutil'
Lattice = require 'lattice'
SliderMapping = include 'lib/slidermapping'
n_voices = 3
grid_controls = {}
Keyboard = include 'lib/keyboard'
k = Keyboard.new(1, 1, 16, 8)
table.insert(grid_controls, 1, k.stepper)
Menu = include 'lib/menu'
arp_menu = Menu.new(4, 5, 10, 2, {
_, 1, 2, 3, 4, 5, 6, 7, 8, 9,
_, _, 14, _, 10, 11, 12 -- value number 11 can be set to 13 when direction is set to 3
})
arp_menu.is_toggle = true
arp_menu.on_select = function(source, old_source)
-- enable arp when a source is selected, disable when toggled off
if source and not k.arping then
k.arping = true
k.gliding = false
elseif not source then
k:arp(false)
k.arping = false
handle_synced_voice_loops(true) -- just in case we had a loop start/end cued up
end
k.arp_plectrum = (source == 13)
end
arp_menu.get_key_level = function(value, selected)
local level = 0
if value <= #arp_divs then
if arp_gates[value] then
level = 2
end
elseif voice_states[k.selected_voice][lfo_gate_names[value - #arp_divs]] then
level = 2
end
return level + (selected and 11 or 4)
end
table.insert(grid_controls, 1, arp_menu)
arp_direction_menu = Menu.new(4, 3, 5, 1, {
3, _, 2, _, 1
})
arp_direction_menu.on_select = function(value)
k.arp_direction = value
if value == 3 then -- plectrum selected
arp_menu.values[11] = 13
else
if arp_menu.value == 13 then
arp_menu:select(false)
end
arp_menu.values[11] = nil
end
end
arp_direction_menu:select_value(1)
table.insert(grid_controls, 1, arp_direction_menu)
source_menu = Menu.new(5, 1, 11, 2, {
-- map of source numbers (in editor.source_names) to keys
-- TODO: add trackball dx, dy?
2, _, 3, _, 5, _, 7, 8, 9, _, 1,
_, _, 4, _, 6, _, _, 10
})
source_menu.is_multi = true
source_menu:select_value(1)
source_menu.get_key_level = function(value, selected, held)
local level = 0
-- darken LFO + amp sources when low
if value == 1 then
if voice_states[k.selected_voice].amp < 0.5 then
level = -1
end
elseif value == 2 then
if tip - palm < 0 then
level = -1
end
elseif value == 3 then
-- TODO: indicate velocity
elseif value == 4 then
-- TODO: indicate sampled velocity
elseif value == 5 then
-- TODO: indicate env state
-- maybe just use gate!
elseif value == 6 then
-- TODO: indicate env state
-- maybe use a fixed-length flash when gate goes high
elseif value >= 7 and value <= 9 then
if not voice_states[k.selected_voice][lfo_gate_names[value - 6]] then
level = -1
end
elseif value == 10 then
-- TODO: indicate S+H state?
end
return (held and 11 or (selected and 6 or 3)) + level
end
table.insert(grid_controls, 1, source_menu)
link_peers = 0
Echo = include 'lib/echo'
echo = Echo.new()
redraw_metro = nil
trackball_values = {
x = 0,
y = 0,
last_x = 0,
last_y = 0,
}
editor = {
source_names = {
'amp',
'hand',
'vel',
'svel',
'eg',
'eg2',
'lfoA',
'lfoB',
'lfoC',
'sh'
},
dests = {
{
name = 'ratioA',
label = 'ratio A',
mode_param = 'opHardA',
modes = { 'soft', 'hard' },
default = -0.4167 -- 1/1 (4th out of 12 harmonics)
},
{
name = 'detuneA',
label = 'detune A',
neutral_value = 0.5,
default = 0
},
{
name = 'indexA',
label = 'index A',
mode_param = 'opTypeA',
modes = { 'fm', 'fb', 'sp', 'wv' },
default = -1,
source_defaults = {
amp = 0.2
}
},
{
name = 'opMix',
label = 'mix A:B',
neutral_value = 0.5,
default = -1
},
{
name = 'ratioB',
label = 'ratio B',
mode_param = 'opHardB',
modes = { 'soft', 'hard' },
default = -0.25 -- 2/1 (5th out of 12 harmonics)
},
{
name = 'detuneB',
label = 'detune B',
neutral_value = 0.5,
default = 0
},
{
name = 'indexB',
label = 'index B',
mode_param = 'opTypeB',
modes = { 'fm', 'fb', 'sp', 'wv' },
default = -1,
source_defaults = {
amp = 0.2
},
has_divider = true
},
{
name = 'fxA',
label = 'fx A',
mode_param = 'fxTypeA',
modes = { 'squiz', 'tanh' },
default = -1
},
{
name = 'fxB',
label = 'fx B',
mode_param = 'fxTypeB',
modes = { 'loss', 'chorus' },
default = -1
},
{
name = 'hpCutoff',
label = 'hp cutoff',
mode_param = 'hpQ',
modes = { 'lo', 'mid', 'hi' },
mode_values = { 1, 1.414, 5 },
default = -1
},
{
name = 'lpCutoff',
label = 'lp cutoff',
neutral_value = 1,
mode_param = 'lpQ',
modes = { 'lo', 'mid', 'hi' },
mode_values = { 1, 1.414, 5 },
default = 0.8,
source_defaults = {
amp = 0.2
},
has_divider = true
},
{
name = 'attack',
label = 'attack',
mode_param = 'amp_mode',
modes = { 'mod', '*', 'amp' },
default = 0
},
{
name = 'release',
label = 'release',
mode_param = 'eg_type',
modes = { 'gate', 'trig' },
default = 0,
has_divider = true
},
{
name = 'lfoAFreq',
label = 'lfo a',
mode_param = 'lfoTypeA',
modes = { 'tri', 'sy', 'rx', 'd', '/' },
default = 0
},
{
name = 'lfoBFreq',
label = 'lfo b',
mode_param = 'lfoTypeB',
modes = { 'tri', 'sy', 'rx', 'd', '/' },
default = -0.2
},
{
name = 'lfoCFreq',
label = 'lfo c',
mode_param = 'lfoTypeC',
modes = { 'tri', 'sy', 'rx', 'd', '/' },
default = -0.4,
has_divider = true
},
{
name = 'loopRate',
label = 'loop rate',
neutral_value = 0.5,
voice_param = 'loopRate',
voice_dest = true
},
{
name = 'loopPosition',
label = 'loop position',
neutral_value = 0.5,
voice_param = 'loopPosition',
voice_dest = true
},
{
name = 'pan',
label = 'pan',
neutral_value = 0.5,
voice_param = 'pan'
},
{
name = 'amp',
label = 'amp',
neutral_value = 0.5,
voice_param = 'outLevel'
}
},
selected_dest = 1,
autoselect_time = 0,
encoder_autoselect_deltas = {
[2] = 0,
[3] = 0,
[4] = 0,
[5] = 0
}
}
patch_param_mappings = {
-- [dest number] = slider mapping
}
voice_param_mappings = {
-- [dest number][voice index] = slider mapping
}
patch_mod_mappings = {
-- [dest number][source number] = slider mapping
}
voice_mod_mappings = {
-- [dest number][source number][voice index] = slider mapping
}
xvi_params = {
'ratioA',
'detuneA',
'indexA',
'opMix',
'ratioB',
'detuneB',
'indexB',
'fxA',
'fxB',
'hpCutoff',
'lpCutoff',
'attack',
'release',
'lfoAFreq',
'lfoBFreq',
'lfoCFreq'
}
xvi_state = {}
for s = 1, #xvi_params do
xvi_state[s] = { value = nil, delta = 0 }
end
held_keys = { false, false, false }
voice_states = {}
for v = 1, n_voices do
voice_states[v] = {
pitch = 0,
amp = 0,
mix_level = 1,
shift = 0,
loop_playing = false,
loop_length = 0,
loop_play_next = false,
loop_record_started = false,
loop_record_next = false,
lfoA_gate = false,
lfoB_gate = false,
lfoC_gate = false,
timbre_lock = false,
polls = {},
}
end
lfo_gate_names = {
'lfoA_gate',
'lfoB_gate',
'lfoC_gate'
}
tip = 0
palm = 0
gate_in = false
arp_divs = { 1/2, 3/8, 1/4, 3/16, 1/8, 1/12, 1/16, 1/24, 1/32 }
arp_gates = {}
arp_gates_inverted = {}
arp_lattice = Lattice.new {
ppqn = 24 -- this is all the resolution we need for the divisions above
}
for d = 1, #arp_divs do
local rate = arp_divs[d] / 2
arp_gates[d] = false
arp_gates_inverted[d] = false
local sprocket = arp_lattice:new_sprocket {
division = rate,
}
sprocket.action = function()
if arp_gates_inverted[d] then
arp_gates[d] = sprocket.downbeat
else
arp_gates[d] = not sprocket.downbeat
end
if arp_menu.value == d then
if arp_gates[d] then
handle_synced_voice_loops()
end
k:arp(arp_gates[d])
end
end
end
-- update peer count and sync SC clock every quarter note
arp_lattice:new_sprocket {
division = 1/4,
action = function()
engine.downbeat()
link_peers = clock.link.get_number_of_peers()
end
}
-- actions to take when specific devices (identified by name) are plugged in
device_callbacks = {
['monome 128 m1000265'] = function(device)
g = device
g.key = function(x, y, z)
local handled = false
-- special-case keys
-- TODO: incorporate these into new or existing grid controls
if z == 1 then
if x == 6 and y == 8 then
k.stepper:toggle()
arp_menu:close()
arp_direction_menu:close()
source_menu:close()
handled = true
elseif x == 7 and y == 8 then
arp_menu:toggle()
if arp_menu.is_open then
arp_direction_menu:open()
else
arp_direction_menu:close()
end
k.stepper:close()
source_menu:close()
handled = true
elseif x == 9 and y == 8 then
source_menu:toggle()
arp_menu:close()
arp_direction_menu:close()
k.stepper:close()
handled = true
elseif arp_menu.is_open and y == 3 and x >= 12 then
if x == 12 then
-- nudge back: pause for one pulse worth of time
clock.run(function()
arp_lattice:stop()
clock.sleep(clock.get_beat_sec() / arp_lattice.ppqn)
arp_lattice:start()
end)
elseif x == 13 then
-- nudge forward: skip forward by one pulse, instantaneously
arp_lattice:pulse()
elseif x == 15 then
-- nudge tempo down
params:set('clock_tempo', params:get('clock_tempo') / 1.04)
elseif x == 16 then
-- nudge tempo up
params:set('clock_tempo', params:get('clock_tempo') * 1.04)
end
handled = true
elseif x == 1 and y == 1 and source_menu.is_open and (source_menu.n_held > 0 or held_keys[1]) then
-- mod reset key
-- TODO: move this into a :delete_key() handler
if source_menu.n_held > 0 then
-- if there are any held sources, reset all routes involving them
for source = 1, #editor.source_names do
if source_menu.held_values[source] then
local source_name = editor.source_names[source]
for d = 1, #editor.dests do
local dest = editor.dests[d]
local dest_name = dest.name
if dest.voice_dest then
dest_name = dest_name .. '_' .. k.selected_voice
end
local defaults = dest.source_defaults
local param = params:lookup_param(source_name .. '_' .. dest_name)
if defaults and defaults[source_name] then
param:set(defaults[source_name])
else
param:set_default()
end
end
end
end
handled = true
end
if held_keys[1] then
-- if K1 is held, reset all routes involving the selected dest
local dest = editor.dests[editor.selected_dest]
local dest_name = dest.name
if dest.voice_dest then
dest_name = dest_name .. '_' .. k.selected_voice
end
local defaults = dest.source_defaults
for source = 1, #editor.source_names do
local source_name = editor.source_names[source]
local param_name = source_name .. '_' .. dest_name
local param = params:lookup_param(param_name)
if defaults and defaults[source_name] then
param:set(defaults[source_name])
else
param:set_default()
end
end
handled = true
end
end
end
-- if any menus or other controls are open, see if those will handle the key
local c = 1
while not handled and c <= #grid_controls do
handled = grid_controls[c]:key(x, y, z, k.held_keys.shift)
c = c + 1
end
-- last stop: keyboard handles all other key events
if not handled then
k:key(x, y, z)
end
grid_redraw()
screen.ping()
end
end,
['TOUCHE 1'] = function(device)
touche = device
touche.event = function(data)
local message = midi.to_msg(data)
if message.ch == 1 and message.type == 'cc' then
-- back = 16, front = 17, left = 18, right = 19
if message.cc == 17 then
tip = message.val / 126
engine.tip(tip)
elseif message.cc == 16 then
palm = message.val / 126
engine.palm(palm)
elseif message.cc == 18 then
k:bend(-math.min(1, message.val / 126))
send_pitch()
elseif message.cc == 19 then
k:bend(math.min(1, message.val / 126))
send_pitch()
end
end
end
end,
['Faderfox UC4'] = function(device)
uc4 = device
uc4.event = function(data)
local message = midi.to_msg(data)
if message.ch == 1 then
if message.type == 'note_on' then
if message.note >= 12 and message.note < 18 then
if message.note == params:get('echo_jump_trigger') + 10 then
params:set('echo_jump_trigger', 1) -- none
uc4:note_off(message.note, 127)
else
params:set('echo_jump_trigger', message.note - 10)
end
elseif message.note == 18 or message.note == 19 then
-- manual jump trigger
params:set('echo_jump_trigger', 1)
echo:jump()
end
elseif message.type == 'note_off' then
if message.note >= 12 and message.note < 18 then
if message.note == params:get('echo_jump_trigger') + 10 then
-- turn UC4 note light back on
uc4:note_on(message.note, 127)
end
end
end
end
end
end,
['MiSW XVI-M'] = function(device)
device.event = function(data)
local message = midi.to_msg(data)
if message.type == 'pitchbend' then
local fader = message.ch
-- scale to [0, 1].
-- max 14-bit pitchbend value is 16383, but xvi only returns up to 16380
-- 12 bits of resolution is plenty
local new_value = message.val / 16380
local state = xvi_state[fader]
local old_value = state.value or new_value
local changed_source = false
for source = 1, #editor.source_names do
if source_menu.held_values[source] then
patch_mod_mappings[fader][source]:delta(new_value - old_value)
changed_source = true
end
end
if not changed_source then
patch_param_mappings[fader]:move(old_value, new_value)
end
state.delta = state.delta + math.abs(new_value - old_value)
state.value = new_value
local now = util.time()
if editor.selected_dest == fader then
-- as long as the currently selected fader is being moved, don't change selection
editor.autoselect_time = now
state.delta = 0
else
-- we'll scale accumulated changes by time, so that a big move over a short time is as
-- likely to change selection as a small move after a long pause, but that small move
-- wouldn't change selection if the selected fader was moved recently
local t = math.min(0.3, now - editor.autoselect_time) / 0.3
if state.delta * t > 0.05 then
state.delta = 0
editor.selected_dest = fader
editor.autoselect_time = now
end
end
screen.ping()
elseif message.type == 'cc' then
local button = message.ch
local dest = editor.dests[button]
if dest.mode_param then
local new_mode = dest.mode % #dest.modes + 1
if dest.mode_values then
params:set(dest.mode_param, dest.mode_values[new_mode])
else
params:set(dest.mode_param, new_mode)
end
end
screen.ping()
end
end
end,
['eDrumIn BLACK'] = function(device)
device.event = function(data)
if arp_menu.value == 14 then
local message = midi.to_msg(data)
if message.type == 'note_on' then
k:arp(true)
elseif message.type == 'note_off' then
k:arp(false)
end
end
end
end,
['enCoReII Mouse RDK'] = function(device)
device.event = function(type, code, value)
if type == 2 then
if code == 0 then
trackball_values.x = trackball_values.x - value
k:move_plectrum(value / -16, 0)
elseif code == 1 then
trackball_values.y = trackball_values.y - value
k:move_plectrum(0, value / -16)
end
end
end
end
}
-- dummy devices
-- these will be replaced on init or when the correct device is plugged in
g = {
all = function() end,
led = function() end,
refresh = function() end
}
uc4 = {
note_on = function() end,
note_off = function() end
}
clock.transport.start = function()
arp_lattice:hard_restart()
end
function voice_loop_clear(v)
local voice = voice_states[v]
if not voice.loop_playing then
return
end
engine.clearLoop(v)
params:lookup_param('loopRate_' .. v):set_default()
params:lookup_param('loopPosition_' .. v):set_default()
for s = 1, #editor.source_names do
params:lookup_param(editor.source_names[s] .. '_loopRate_' .. v):set_default()
params:lookup_param(editor.source_names[s] .. '_loopPosition_' .. v):set_default()
end
voice.loop_playing = false
voice.loop_length = 0
-- clear pitch shift, because it only confuses things when loop isn't engaged
voice.shift = 0
engine.shift(v, 0)
end
function voice_loop_record(v)
-- start recording (set loop start time here)
local voice = voice_states[v]
if k.arping then
voice.loop_record_next = true
else
voice.loop_record_started = util.time()
end
end
function voice_loop_play(v)
local voice = voice_states[v]
-- TODO: limit loop length on Lua side by auto-stopping recording
local length = (util.time() - voice.loop_record_started) / clock.get_beat_sec()
local div = 0
-- round to appropriate beat division
local div_index = arp_menu.value
-- TODO NEXT (QoL): do something special to handle clocking by synced LFOs:
-- send the computed tempo multiple from LFO synth to a poll?
if div_index and div_index <= #arp_divs then
-- arp_divs are in measures, we need beats
div = arp_divs[div_index] * 4
end
engine.playLoop(v, length, div)
params:lookup_param('loopRate_' .. v):set_default()
params:lookup_param('loopPosition_' .. v):set_default()
voice.loop_playing = true
voice.loop_record_started = false
voice.loop_length = length
end
function voice_loop_set_end(v)
-- stop recording, start looping
local voice = voice_states[v]
if k.arping then
voice.loop_play_next = true
else
voice_loop_play(v, false)
end
end
function send_pitch()
engine.pitch(k.active_pitch, k.bent_pitch - k.active_pitch)
end
-- TODO: debounce here
function grid_redraw()
g:all(0)
k:draw()
if arp_menu.is_open or arp_direction_menu.is_open or source_menu.is_open then
for x = 3, 16 do
for y = 1, 7 do
g:led(x, y, 0)
end
end
end
if k.stepper.is_open then
g:led(6, 8, 15)
else
local level = 2
if arp_menu.value then
level = 5
if k.stack[k.arp_index] and k.stack[k.arp_index].gate then
level = level + 2
end
end
g:led(6, 8, level)
end
k.stepper:draw()
if arp_menu.is_open then
g:led(7, 8, 15)
elseif arp_menu.value then
-- an arp clock source is selected; blink
local v = arp_menu.value
local level = 5
if v <= #arp_divs then
if arp_gates[v] then
level = level + 2
end
elseif voice_states[k.selected_voice][lfo_gate_names[v - #arp_divs]] then
level = level + 2
end
g:led(7, 8, level)
else
-- no source selected; go dark
g:led(7, 8, 2)
end
arp_menu:draw()
arp_direction_menu:draw()
if arp_menu.is_open then
-- lattice phase nudge keys
g:led(12, 3, 5)
g:led(13, 3, 5)
-- tempo nudge keys
g:led(15, 3, 4)
g:led(16, 3, 4)
end
g:led(9, 8, source_menu.is_open and 7 or 2)
source_menu:draw()
if source_menu.is_open and (source_menu.n_held > 0 or held_keys[1]) then
g:led(1, 1, 7)
end
local blink = arp_gates[5] -- 1/8 notes
for v = 1, n_voices do
local voice = voice_states[v]
local level = voice.amp
if voice.loop_record_next then
level = level * 0.5 + (blink and 0.2 or 0)
elseif voice.loop_play_next then
level = level * 0.5 + (blink and 0.35 or 0.25)
elseif voice.loop_record_started then
level = level * 0.5 + (blink and 0.5 or 0)
elseif voice.loop_playing then
level = level * 0.75 + 0.25
end
level = math.floor(level * 15)
g:led(1, 8 - v, level)
end
g:refresh()
end
function handle_synced_voice_loops(immediate)
-- if we're playing a sequence straight (not randomized order),
-- wait until the first step to either start or stop
if not immediate and k.arp_direction == 1 and k.arp_index > 1 then
-- TODO NEXT (QoL): in this situation, switching the selected voice should ALSO be delayed!
-- or something else needs to happen to ensure that we're still
-- sending user input to the voice that's actually recording
return
end
for v = 1, n_voices do
local voice = voice_states[v]
if voice.loop_record_next then
-- get ready to loop (set loop start time here)
voice.loop_record_started = util.time()
voice.loop_record_next = false
elseif voice.loop_play_next then
-- start looping
voice_loop_play(v)
voice.loop_playing = true
voice.loop_play_next = false
voice.loop_record_started = false
-- stop arpeggiating!
arp_menu:select(false)
k.held_keys.latch = false
k:maybe_clear_stack()
end
end
end
function init()
norns.enc.accel(1, false)
norns.enc.sens(1, 8)
k.on_select_voice = function(v)
-- if any other voice is recording, stop recording & start looping it
for ov = 1, n_voices do
if ov ~= v and voice_states[ov].loop_record_started then
voice_loop_set_end(ov)
end
end
engine.select_voice(v)
send_pitch()
end
k.on_voice_shift = function(v, d)
local voice = voice_states[v]
if d == 0 then
voice.shift = 0
else
voice.shift = voice.shift + d
end
engine.shift(v, voice.shift)
end
k.on_stack_change = function()
engine.heldKeys(#k.stack)
end
k.on_pitch = function()
send_pitch()
grid_redraw()
end
k.on_gate = function(gate)
engine.gate(gate and 1 or 0)
end
-- set up softcut echo
softcut.reset()
echo:init()
-- set up polls
for v = 1, n_voices do
local voice = voice_states[v]
-- one poll to respond to voice amplitude info
voice.polls.amp = poll.set('amp_' .. v, function(value)
voice.amp = value
end)
voice.polls.amp:start()
-- a second to respond to pitch AND refresh grid; this helps a lot when voices are arpeggiating or looping
voice.polls.instant_pitch = poll.set('instant_pitch_' .. v, function(value)
voice.pitch = value
grid_redraw()
end)
voice.polls.instant_pitch:start()
-- and another poll for "routine" pitch updates, to show glide, vibrato, etc.
voice.polls.pitch = poll.set('pitch_' .. v, function(value)
voice.pitch = value
end)
voice.polls.pitch:start()
-- and polls for LFO updates, which will only fire when a voice is selected and an LFO is used as an arp clock
for g, name in ipairs(lfo_gate_names) do
local echo_jump_trigger = 1 + g
local echo_uc4_note = 11 + g
local arp_source = #arp_divs + g
voice.polls[name] = poll.set(name .. '_' .. v, function(gate)
gate = gate > 0
voice[name] = gate
if v == k.selected_voice then
if echo.jump_trigger == echo_jump_trigger then
if gate then
if uc4 then
uc4:note_off(echo_uc4_note)
clock.run(function()
clock.sleep(0.05)
-- double check trigger setting just in case it's changed in the last 20ms
if echo.jump_trigger == echo_jump_trigger then
uc4:note_on(echo_uc4_note, 127)
end
end)
end
echo:jump()
else
if uc4 then uc4:note_on(echo_uc4_note, 127) end
end
end
if arp_menu.value == arp_source then
if gate then
handle_synced_voice_loops()
end
k:arp(gate)
end
end
end)
voice.polls[name]:start()
end
end
params:add_group('tuning', 5)
params:add {
name = 'base frequency (C)',
id = 'base_freq',
type = 'control',
controlspec = controlspec.new(musicutil.note_num_to_freq(48), musicutil.note_num_to_freq(72), 'exp', 0, musicutil.note_num_to_freq(60), 'Hz'),
action = function(value)
engine.baseFreq(value)
end
}
params:add {
name = 'base freq reset',
id = 'base_freq_reset',
type = 'binary',
behavior = 'trigger',
action = function()
params:set('base_freq', musicutil.note_num_to_freq(60))
end
}
params:add {
type = 'file',
id = 'tuning_file',
name = 'tuning_file',
path = '/home/we/dust/data/fretwork/scales/12tet.scl',
action = function(value)
k.scale:read_scala_file(value)
end
}
params:add {
name = 'bend range',
id = 'bend_range',
type = 'number',
min = -7,
max = 24,
default = -2,
formatter = function()
local value = k.bend_range * 12
if value < 1 then
return string.format('%.2f', value)
end
return string.format('%d', value)
end,
action = function(value)
if value < 1 then
value = math.pow(0.75, 1 - value)
end
k.bend_range = value / 12
k:set_bend_targets()
k:bend(k.bend_amount)
send_pitch()
end
}
params:add {
name = 'pitch slew',
id = 'pitchSlew',
type = 'control',
controlspec = controlspec.new(0, 0.1, 'lin', 0, 0.01, 's'),
action = function(value)
engine.pitchSlew(value)
end