-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloom.lua
1241 lines (1004 loc) · 32.1 KB
/
loom.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
-- Loom
-- 1.1.0 @markeats
-- llllllll.co/t/loom
--
-- Pattern weaver for grids.
--
-- Hold a grid key and press
-- another on the same row/col
-- to add a trigger or note.
-- Three keys clear a row/col.
--
-- E1 : Change page
-- K2 : Play/Pause
--
-- PAGE 1:
-- E2 : BPM
-- E3 : Add/Remove
-- K3 : New sound
-- PAGE 2:
-- E2 : Root note/Select
-- E3 : Scale type/Note edit
-- K3 : Scale/Custom
-- PAGE 3:
-- Load/Save/Delete
--
-- Concept Jay Gilligan
--
local MusicUtil = require "musicutil"
local UI = require "ui"
local BeatClock = require "beatclock"
local MollyThePoly = require "molly_the_poly/lib/molly_the_poly_engine"
engine.name = "MollyThePoly"
local options = {}
options.OUTPUT = {"Audio", "MIDI", "Audio + MIDI", "Crow Out 1+2", "Crow ii JF"}
options.STEP_LENGTH_NAMES = {"1 bar", "1/2", "1/3", "1/4", "1/6", "1/8", "1/12", "1/16", "1/24", "1/32", "1/48", "1/64"}
options.STEP_LENGTH_DIVIDERS = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64}
local DATA_FILE_PATH = _path.data .. "loom/loom.data"
local SCREEN_FRAMERATE = 15
local screen_dirty = true
local GRID_FRAMERATE = 30
local grid_dirty = true
local grid_leds = {}
local grid_w, grid_h = 16, 8
local beat_clock
local grid_device
local midi_in_device
local midi_out_device
local midi_out_channel
local notes = {}
local triggers = {}
local root_note = 36
local scale_type = 1
local custom_scale = false
local scale_edit_id = 1
local scale_notes = {}
local scale_note_names = {}
local SCALES_LEN = #MusicUtil.SCALES
local active_notes = {}
local down_marks = {}
local down_keys = {}
local trails = {}
local remove_animations = {}
local DOWN_ANI_LENGTH = 0.2
local REMOVE_ANI_LENGTH = 0.4
local TRAIL_ANI_LENGTH = 6.0
local DOWN_BRIGHTNESS = 3
local TRAIL_BRIGHTNESS = 3
local OUTSIDE_BRIGHTNESS = 3
local INACTIVE_BRIGHTNESS = 10
local ACTIVE_BRIGHTNESS = 15
local pages
local playback_icon
local add_remove_animations = {}
local ADD_REMOVE_ANI_LENGTH = 0.2
local notes_changed_timeout = 0
local triggers_changed_timeout = 0
local NOTES_TRIGGERS_TIMEOUT_LENGTH = 0.2
local save_data = {version = 1, patterns = {}}
local save_menu_items = {"Load", "Save", "Delete"}
local save_slot_list
local save_menu_list
local last_edited_slot = 0
local confirm_message
local confirm_function
local function copy_object(object)
if type(object) ~= 'table' then return object end
local result = {}
for k, v in pairs(object) do result[copy_object(k)] = copy_object(v) end
return result
end
local function update_save_slot_list()
local entries = {}
for i = 1, math.min(#save_data.patterns + 1, 999) do
local entry
if i <= #save_data.patterns then
entry = save_data.patterns[i].name
else
entry = "-"
end
if i == last_edited_slot then entry = entry .. "*" end
entries[i] = i .. ". " .. entry
end
save_slot_list.entries = entries
end
local function read_data()
local disk_data = tab.load(DATA_FILE_PATH)
if disk_data then
if disk_data.version then
if disk_data.version == 1 then
save_data = disk_data
else
print("Unrecognized data, version " .. disk_data.version)
end
end
end
update_save_slot_list()
end
local function write_data()
tab.save(save_data, DATA_FILE_PATH)
end
local function load_pattern(index)
if index > #save_data.patterns then return end
local pattern = copy_object(save_data.patterns[index])
params:set("bpm", pattern.bpm)
params:set("step_length", pattern.step_length)
params:set("pattern_width", pattern.pattern_width)
params:set("pattern_height", pattern.pattern_height)
params:set("min_velocity", pattern.min_velocity)
params:set("max_velocity", pattern.max_velocity)
notes = pattern.notes
triggers = pattern.triggers
root_note = pattern.root_note
scale_type = pattern.scale_type
custom_scale = pattern.custom_scale
scale_notes = pattern.scale_notes
scale_note_names = MusicUtil.note_nums_to_names(scale_notes, true)
last_edited_slot = index
update_save_slot_list()
grid_dirty = true
end
local function save_pattern(index)
local pattern = {
name = os.date("%b %d %H:%M"),
bpm = params:get("bpm"),
step_length = params:get("step_length"),
pattern_width = params:get("pattern_width"),
pattern_height = params:get("pattern_height"),
min_velocity = params:get("min_velocity"),
max_velocity = params:get("max_velocity"),
notes = notes,
triggers = triggers,
root_note = root_note,
scale_type = scale_type,
custom_scale = custom_scale,
scale_notes = scale_notes
}
save_data.patterns[index] = copy_object(pattern)
last_edited_slot = index
update_save_slot_list()
write_data()
end
local function delete_pattern(index)
if index > 0 and index <= #save_data.patterns then
table.remove(save_data.patterns, index)
if index == last_edited_slot then
last_edited_slot = 0
elseif index < last_edited_slot then
last_edited_slot = last_edited_slot - 1
end
end
update_save_slot_list()
write_data()
end
local function note_on(note_num)
local min_vel, max_vel = params:get("min_velocity"), params:get("max_velocity")
if min_vel > max_vel then
max_vel = min_vel
end
local note_midi_vel = math.random(min_vel, max_vel)
-- print("note_on", note_num, note_midi_vel)
-- Audio engine out
if params:get("output") == 1 or params:get("output") == 3 then
engine.noteOn(note_num, MusicUtil.note_num_to_freq(note_num), note_midi_vel / 127)
end
-- MIDI out
if (params:get("output") == 2 or params:get("output") == 3) then
midi_out_device:note_on(note_num, note_midi_vel, midi_out_channel)
end
-- Crow 1+2 out
if params:get("output") == 4 then
crow.output[1].volts = (note_num - 60) / 12
crow.output[2].execute()
-- Crow ii JF out
elseif params:get("output") == 5 then
crow.ii.jf.play_note((note_num - 60) / 12, 5)
end
end
local function note_off(note_num)
-- print("note_off", note_num)
-- Audio engine out
if params:get("output") == 1 or params:get("output") == 3 then
engine.noteOff(note_num)
end
-- MIDI out
if (params:get("output") == 2 or params:get("output") == 3) then
midi_out_device:note_off(note_num, nil, midi_out_channel)
end
end
local function all_notes_kill()
-- Audio engine out
engine.noteKillAll()
-- MIDI out
if (params:get("output") == 2 or params:get("output") == 3) then
for _, a in pairs(active_notes) do
midi_out_device:note_off(a, 96, midi_out_channel)
end
end
active_notes = {}
end
local function start_remove_animation(orientation, position)
local ani = {orientation = orientation, position = position, time_remaining = REMOVE_ANI_LENGTH }
table.insert(remove_animations, ani)
end
local function start_add_remove_animation(icon)
if #add_remove_animations < 16 then
local ani = {icon = icon, x = math.random(118) + 5, y = math.random(54) + 5, time_remaining = ADD_REMOVE_ANI_LENGTH}
table.insert(add_remove_animations, ani)
screen_dirty = true
end
end
local function add_note(position, head, length, direction, show_screen_animation)
length = length or 1
direction = direction or 1
local note = {position = position, head = head, length = length, advance_countdown = length, direction = direction, active = false}
table.insert(notes, note)
if show_screen_animation then
start_add_remove_animation("note")
else
notes_changed_timeout = NOTES_TRIGGERS_TIMEOUT_LENGTH
end
if pages.index == 1 or show_screen_animation then screen_dirty = true end
grid_dirty = true
end
local function add_trigger(position, head, length, direction, show_screen_animation)
length = length or 1
direction = direction or 1
local trigger = {position = position, head = head, length = length, advance_countdown = length, direction = direction, active = false}
table.insert(triggers, trigger)
if show_screen_animation then
start_add_remove_animation("trigger")
else
triggers_changed_timeout = NOTES_TRIGGERS_TIMEOUT_LENGTH
end
if pages.index == 1 or show_screen_animation then screen_dirty = true end
grid_dirty = true
end
local function remove_note(position, show_screen_animation, silent)
local note
if position then
for k, v in pairs(notes) do
if v.position == position then
note = table.remove(notes, k)
break
end
end
else
note = table.remove(notes)
end
if note and not silent then
if show_screen_animation then
start_add_remove_animation("remove")
else
notes_changed_timeout = NOTES_TRIGGERS_TIMEOUT_LENGTH
end
start_remove_animation("col", note.position)
if pages.index == 1 or show_screen_animation then screen_dirty = true end
grid_dirty = true
end
end
local function remove_trigger(position, show_screen_animation, silent)
local trigger
if position then
for k, v in pairs(triggers) do
if v.position == position then
trigger = table.remove(triggers, k)
break
end
end
else
trigger = table.remove(triggers)
end
if trigger and not silent then
if show_screen_animation then
start_add_remove_animation("remove")
else
triggers_changed_timeout = NOTES_TRIGGERS_TIMEOUT_LENGTH
end
start_remove_animation("row", trigger.position)
if pages.index == 1 or show_screen_animation then screen_dirty = true end
grid_dirty = true
end
end
local function add_random()
if #notes >= grid_w and #triggers >= grid_h then return end
-- Note
if math.random() >= 0.5 then
local available_positions = {}
for i = 1, grid_w do
local available = true
for _, vn in pairs(notes) do
if vn.position == i then
available = false
break
end
end
if available then
table.insert(available_positions, i)
end
end
if #available_positions > 0 then
local length = util.round(math.pow(math.random(), 4) * (grid_h - 2) + 1)
add_note(available_positions[math.random(#available_positions)], math.random(grid_h), length, (math.random() >= 0.5 and 1 or -1), true)
end
-- Trigger
else
local available_positions = {}
for i = 1, grid_h do
local available = true
for _, vt in pairs(triggers) do
if vt.position == i then
available = false
break
end
end
if available then
table.insert(available_positions, i)
end
end
if #available_positions > 0 then
local length = util.round(math.pow(math.random(), 4) * (grid_w - 2) + 1)
add_trigger(available_positions[math.random(#available_positions)], math.random(grid_w), length, (math.random() >= 0.5 and 1 or -1), true)
end
end
end
local function remove_last()
if #notes == 0 and #triggers == 0 then return end
if math.random() >= 0.5 then
remove_note(nil, true)
else
remove_trigger(nil, true)
end
end
local function advance_step()
if grid_device then
grid_w = grid_device.cols
grid_h = grid_device.rows
if grid_w ~= 8 and grid_w ~= 16 then grid_w = 16 end
if grid_h ~= 8 and grid_h ~= 16 then grid_h = 8 end
end
local active_notes_this_step = {}
for _, t in pairs(triggers) do
-- Move triggers
t.advance_countdown = t.advance_countdown - 1
if t.advance_countdown == 0 then
t.advance_countdown = t.length
if t.direction > 0 then t.head = t.head % params:get("pattern_width") + 1
else t.head = (t.head + params:get("pattern_width") - 2) % params:get("pattern_width") + 1 end
end
t.active = false
-- Generate trigger trails
local tx
for ti = 0, t.length - 1 do
tx = t.head + (ti * t.direction * -1)
tx = (tx - 1) % params:get("pattern_width") + 1
if tx <= grid_w then trails[tx][t.position] = TRAIL_ANI_LENGTH end
end
grid_dirty = true
end
for _, n in pairs(notes) do
-- Move notes
n.advance_countdown = n.advance_countdown - 1
if n.advance_countdown == 0 then
n.advance_countdown = n.length
if n.direction > 0 then n.head = n.head % params:get("pattern_height") + 1
else n.head = (n.head + params:get("pattern_height") - 2) % params:get("pattern_height") + 1 end
end
n.active = false
-- Generate note trails
local ny
for ni = 0, n.length - 1 do
ny = n.head + (ni * n.direction * -1)
ny = (ny - 1) % params:get("pattern_height") + 1
if ny <= grid_h then trails[n.position][ny] = TRAIL_ANI_LENGTH end
end
-- Check for intersections
local n_top, n_bottom
if n.direction > 0 then
n_top = n.head - n.length + 1
n_bottom = n.head
else
n_top = n.head
n_bottom = n.head + n.length - 1
end
n_top = (n_top - 1) % params:get("pattern_height") + 1
n_bottom = (n_bottom - 1) % params:get("pattern_height") + 1
for _, t in pairs(triggers) do
-- Is the note on a trigger row?
if (n_top <= n_bottom and (t.position >= n_top and t.position <= n_bottom))
or (n_top > n_bottom and (t.position >= n_top or t.position <= n_bottom)) then
local t_left, t_right
if t.direction > 0 then
t_left = t.head - t.length + 1
t_right = t.head
else
t_left = t.head
t_right = t.head + t.length - 1
end
t_left = (t_left - 1) % params:get("pattern_width") + 1
t_right = (t_right - 1) % params:get("pattern_width") + 1
-- Is the trigger on the note column?
if (t_left <= t_right and (n.position >= t_left and n.position <= t_right))
or (t_left > t_right and (n.position >= t_left or n.position <= t_right)) then
table.insert(active_notes_this_step, scale_notes[n.position])
n.active = true
t.active = true
break
end
end
end
grid_dirty = true
end
-- Work out which need noteOffs
for i = #active_notes, 1, -1 do
local still_active = false
for sk, sa in pairs(active_notes_this_step) do
if sa == active_notes[i] then
still_active = true
table.remove(active_notes_this_step, sk)
break
end
end
if not still_active then
note_off(active_notes[i])
table.remove(active_notes, i)
end
end
-- Add remaining, the new notes
for _, sa in pairs(active_notes_this_step) do
note_on(sa)
table.insert(active_notes, sa)
end
screen_dirty = true
end
local function stop()
all_notes_kill()
end
local function reset_step()
beat_clock:reset()
end
local function grid_update()
if #down_marks > 0 or #remove_animations > 0 then grid_dirty = true end
local time_increment = 1 / GRID_FRAMERATE
-- Trails
for x = 1, grid_w do
for y = 1, grid_h do
trails[x][y] = util.clamp(trails[x][y] - time_increment, 0, TRAIL_ANI_LENGTH)
if trails[x][y] > 0 then grid_dirty = true end
end
end
-- Down marks
for i = #down_marks, 1, -1 do
if not down_marks[i].active then
down_marks[i].time_remaining = down_marks[i].time_remaining - time_increment
if down_marks[i].time_remaining <= 0 then
table.remove(down_marks, i)
end
end
end
-- Remove animations
for i = #remove_animations, 1, -1 do
remove_animations[i].time_remaining = remove_animations[i].time_remaining - time_increment
if remove_animations[i].time_remaining <= 0 then
table.remove(remove_animations, i)
end
end
end
local function screen_update()
if pages.index == 1 and #add_remove_animations > 0 then screen_dirty = true end
local time_increment = 1 / SCREEN_FRAMERATE
-- Add/remove animations
for i = #add_remove_animations, 1, -1 do
add_remove_animations[i].time_remaining = add_remove_animations[i].time_remaining - time_increment
if add_remove_animations[i].time_remaining <= 0 then
table.remove(add_remove_animations, i)
end
end
notes_changed_timeout = util.clamp(notes_changed_timeout - time_increment, 0, NOTES_TRIGGERS_TIMEOUT_LENGTH)
triggers_changed_timeout = util.clamp(triggers_changed_timeout - time_increment, 0, NOTES_TRIGGERS_TIMEOUT_LENGTH)
end
local function init_scale()
scale_notes = MusicUtil.generate_scale_of_length(root_note, scale_type, 16)
while #scale_notes < 16 do
table.insert(scale_notes, scale_notes[#scale_notes])
end
scale_note_names = MusicUtil.note_nums_to_names(scale_notes, true)
end
-- Encoder input
function enc(n, delta)
-- Global
if n == 1 then
pages:set_index_delta(util.clamp(delta, -1, 1), false)
save_menu_list:set_index(1)
else
-- Time
if pages.index == 1 then
if n == 2 and beat_clock.external == false then
params:delta("bpm", delta)
elseif n == 3 then
if delta > 0 then
add_random()
else
remove_last()
end
end
-- Pitch
elseif pages.index == 2 then
if custom_scale then
if n == 2 then
scale_edit_id = util.clamp(scale_edit_id + util.clamp(delta, -1, 1), 1, grid_w)
elseif n == 3 then
scale_notes[scale_edit_id] = util.clamp(scale_notes[scale_edit_id] + delta, 0, 127)
scale_note_names = MusicUtil.note_nums_to_names(scale_notes, true)
end
else
if n == 2 then
root_note = util.clamp(root_note + util.clamp(delta, -1, 1), 0, 127)
init_scale()
elseif n == 3 then
scale_type = util.clamp(scale_type + util.clamp(delta, -1, 1), 1, SCALES_LEN)
init_scale()
end
end
-- Load/Save
elseif pages.index == 3 then
if n == 2 then
save_slot_list:set_index_delta(util.clamp(delta, -1, 1))
elseif n == 3 then
save_menu_list:set_index_delta(util.clamp(delta, -1, 1))
end
end
end
screen_dirty = true
end
-- Key input
function key(n, z)
if z == 1 then
-- Key 2
if n == 2 then
if confirm_message then
confirm_message = nil
confirm_function = nil
else
if not beat_clock.external then
if beat_clock.playing then
beat_clock:stop()
else
beat_clock:start()
end
end
end
-- Key 3
elseif n == 3 then
if confirm_message then
confirm_function()
confirm_message = nil
confirm_function = nil
else
-- Time
if pages.index == 1 then
MollyThePoly.randomize_params("lead")
-- Pitch
elseif pages.index == 2 then
custom_scale = not custom_scale
if not custom_scale then
init_scale()
else
scale_edit_id = 1
end
-- Load/Save
elseif pages.index == 3 then
-- Load
if save_menu_list.index == 1 then
load_pattern(save_slot_list.index)
-- Save
elseif save_menu_list.index == 2 then
if save_slot_list.index < #save_slot_list.entries then
confirm_message = UI.Message.new({"Replace saved pattern?"})
confirm_function = function() save_pattern(save_slot_list.index) end
else
save_pattern(save_slot_list.index)
end
-- Delete
elseif save_menu_list.index == 3 then
if save_slot_list.index < #save_slot_list.entries then
confirm_message = UI.Message.new({"Delete saved pattern?"})
confirm_function = function() delete_pattern(save_slot_list.index) end
end
end
end
end
end
screen_dirty = true
end
end
-- Grid event
local function grid_key(x, y, z)
if z == 1 then
-- Is there a relevant down mark?
local relevant_down_mark = nil
for k, v in pairs(down_marks) do
-- Re-activate fading down mark
if v.x == x and v.y == y then
v.active = true
v.time_remaining = DOWN_ANI_LENGTH
relevant_down_mark = v
break
-- Note
elseif v.x == x and v.active then
local three_keys = false
for _, kv in pairs(down_keys) do
if kv.x == x then
three_keys = true
break
end
end
if three_keys then
remove_note(x, true, false)
else
remove_note(x, false, true)
add_note(x, v.y, math.abs(v.y - y), (y < v.y and 1 or -1), false)
end
relevant_down_mark = v
-- Trigger
elseif v.y == y and v.active then
local three_keys = false
for _, kv in pairs(down_keys) do
if kv.y == y then
three_keys = true
break
end
end
if three_keys then
remove_trigger(y, true, false)
else
remove_trigger(y, false, true)
add_trigger(y, v.x, math.abs(v.x - x), (x < v.x and 1 or -1), false)
end
relevant_down_mark = v
end
end
-- Make it the down mark
if relevant_down_mark then
if relevant_down_mark.x ~= x or relevant_down_mark.y ~= y then
table.insert(down_keys, {x = x, y = y})
end
else
table.insert(down_marks, {active = true, x = x, y = y, time_remaining = DOWN_ANI_LENGTH})
end
else
for _, v in pairs(down_marks) do
if v.x == x and v.y == y then
v.active = false
break
end
end
for k, v in pairs(down_keys) do
if v.x == x and v.y == y then
table.remove(down_keys, k)
break
end
end
end
grid_dirty = true
end
-- MIDI event
local function midi_clock_event(data)
beat_clock:process_midi(data)
if not beat_clock.playing and playback_icon.status == 1 then
screen_dirty = true
end
end
function init()
for x = 1, 16 do
grid_leds[x] = {}
trails[x] = {}
for y = 1, 16 do
grid_leds[x][y] = 0
trails[x][y] = 0
end
end
for k, v in pairs(MusicUtil.SCALES) do
if v.name == "Major Pentatonic" then
scale_type = k
break
end
end
init_scale()
grid_device = grid.connect(1)
grid_device.key = grid_key
beat_clock = BeatClock.new()
beat_clock.on_step = advance_step
beat_clock.on_stop = stop
beat_clock.on_select_internal = function()
beat_clock:start()
screen_dirty = true
end
beat_clock.on_select_external = function()
reset_step()
screen_dirty = true
end
midi_in_device = midi.connect(1)
midi_in_device.event = midi_clock_event
midi_out_device = midi.connect(1)
local screen_refresh_metro = metro.init()
screen_refresh_metro.event = function()
screen_update()
if screen_dirty then
screen_dirty = false
redraw()
end
end
local grid_redraw_metro = metro.init()
grid_redraw_metro.event = function()
grid_update()
if grid_dirty and grid_device.device then
grid_dirty = false
grid_redraw()
end
end
-- Add params
params:add_separator("In/Out")
params:add{type = "number", id = "grid_device", name = "Grid Device", min = 1, max = 4, default = 1,
action = function(value)
grid_device:all(0)
grid_device:refresh()
grid_device.key = nil
grid_device = grid.connect(value)
grid_device.key = grid_key
grid_dirty = true
end}
params:add{type = "option", id = "output", name = "Output", options = options.OUTPUT,
action = function(value)
all_notes_kill()
-- Crow 1+2 out
if value == 4 then
crow.output[2].action = "{to(5,0),to(0,0.25)}"
-- Crow ii JF out
elseif value == 5 then
crow.ii.pullup(true)
crow.ii.jf.mode(1)
end
end}
params:add{type = "number", id = "midi_out_device", name = "MIDI Out Device", min = 1, max = 4, default = 1,
action = function(value)
midi_out_device = midi.connect(value)
end}
params:add{type = "number", id = "midi_out_channel", name = "MIDI Out Channel", min = 1, max = 16, default = 1,
action = function(value)
all_notes_kill()
midi_out_channel = value
end}
params:add{type = "option", id = "clock", name = "Clock", options = {"Internal", "External"}, default = beat_clock.external or 2 and 1,
action = function(value)
beat_clock:clock_source_change(value)
end}
params:add{type = "number", id = "midi_clock_in_device", name = "Clock MIDI In Device", min = 1, max = 4, default = 1,
action = function(value)
midi_in_device.event = nil
midi_in_device = midi.connect(value)
midi_in_device.event = midi_clock_event
end}
params:add{type = "option", id = "clock_out", name = "Clock Out", options = {"Off", "On"}, default = beat_clock.send or 2 and 1,
action = function(value)
if value == 1 then beat_clock.send = false
else beat_clock.send = true end
end}
params:add_separator("Fabric")
params:add{type = "number", id = "bpm", name = "BPM", min = 1, max = 240, default = beat_clock.bpm,
action = function(value)
beat_clock:bpm_change(value)
screen_dirty = true
end}
params:add{type = "option", id = "step_length", name = "Step Length", options = options.STEP_LENGTH_NAMES, default = 10,
action = function(value)
beat_clock.ticks_per_step = 96 / options.STEP_LENGTH_DIVIDERS[value]
beat_clock.steps_per_beat = options.STEP_LENGTH_DIVIDERS[value] / 4
beat_clock:bpm_change(beat_clock.bpm)
end}
params:add{type = "number", id = "pattern_width", name = "Pattern Width", min = 4, max = 64, default = 16,
action = function()
grid_dirty = true
end}
params:add{type = "number", id = "pattern_height", name = "Pattern Height", min = 4, max = 64, default = 16,
action = function()
grid_dirty = true
end}
params:add{type = "number", id = "min_velocity", name = "Min Velocity", min = 1, max = 127, default = 80}
params:add{type = "number", id = "max_velocity", name = "Max Velocity", min = 1, max = 127, default = 100}
params:add_separator()
midi_out_channel = params:get("midi_out_channel")
-- Engine params