-
Notifications
You must be signed in to change notification settings - Fork 0
/
impact.lua
1682 lines (1531 loc) · 50.4 KB
/
impact.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
-- impact
-- v1.1.0 @Mendrzec
--
-- 8 track drum machine
local ui = require "ui"
local music = require "musicutil"
engine.name = "Impact"
local impact_params = include("lib/impact_params")
local ui_utils = include("lib/ui_utils")
local table_utils = include("lib/table_utils")
--local grid = include("midigrid/lib/mg_128")
local g = grid.connect()
local MIDI_Clock = require "beatclock"
clk = MIDI_Clock.new() -- global for impact_params how to fix that?
local clk_midi = midi.connect()
-- TODO clean up globals
-- globals
local flash_slow = false
local flash_fast = false
local flash_function = function ()
while true do
flash_fast = not flash_fast
if flash_fast then
flash_slow = not flash_slow
end
grid_dirty = true
clock.sleep(0.125)
end
end
local flash_timer = clock.run(flash_function)
function cleanup()
clock.cancel(flash_timer)
end
local utility_mode = {
NONE = 1,
ERASE = 2,
COPY = 3,
SAVE = 4,
current = 1,
is_none = function (self)
return self.current == self.NONE
end,
set_none = function (self)
self.current = self.NONE
end,
is_erase = function (self)
return self.current == self.ERASE
end,
set_erase = function (self)
self.current = self.ERASE
end,
is_copy = function (self)
return self.current == self.COPY
end,
set_copy = function (self)
self.current = self.COPY
end
}
local clipboard = {
track = nil,
pattern = nil,
store_track = function (self, track)
self.track = track
self.pattern = nil
end,
has_track = function (self, track) -- track is optional
if track then
return self.track == track
else
return self.track ~= nil
end
end,
store_pattern = function (self, pattern)
self.pattern = pattern
self.track = nil
end,
has_pattern = function (self, pattern) -- pattern is optional
if pattern then
return self.pattern == pattern
else
return self.pattern ~= nil
end
end,
clear = function (self)
self.pattern = nil
self.track = nil
end,
empty = function (self)
return self.pattern == nil and self.track == nil
end
}
local playback_mode = {
STOPPED = 4,
PLAYING = 1,
PAUSED = 3
}
local recording_mode = {
NOT_RECORDING = 1,
RECORDING = 2,
RECORDING_UNQUANTIZED = 3
}
local color = {
DISABLED = 0,
DEFAULT = 1,
PRESSED = 15,
}
-- merge those vars as done in see. utility_mode
local current_playback_mode = playback_mode.STOPPED
local current_recording_mode = recording_mode.NOT_RECORDING
local tracks_buttons_grid_map = {}
local tracks_data = {}
local currently_focused_track = nil
local shift_pressed = false
local mode = {
PATTERN_SELECTION = 1,
STEP_EDIT = 2
}
-- merge those vars as done in see. utility_mode
local current_mode = mode.STEP_EDIT
local pattern_selector = {
selected_to_play_next_color = 3,
currently_playing_color = 10,
current_pattern = 1, -- currently playing end edited pattern
selected_pattern = 1, -- to be played next
total_patterns = 16,
longest_track_in_pattern = {},
grid_rows = 2,
grid_row_length = 8,
grid_start_x = 1,
grid_start_y = 4,
pattern_to_grid_xy_map = {},
grid_xy_to_pattern_map = {},
init = function (self)
for i=1,self.total_patterns do
self.longest_track_in_pattern[i] = 1
end
-- init grid_xy_to_pattern_map
local ptrn_index2 = 1
local grid_y_end = (self.grid_start_y + self.grid_rows - 1)
local grid_x_end = (self.grid_start_x + self.grid_row_length - 1)
for y=self.grid_start_y,grid_y_end do
for x=self.grid_start_x,grid_x_end do
if self.grid_xy_to_pattern_map[x] == nil then
self.grid_xy_to_pattern_map[x] = {}
end
self.grid_xy_to_pattern_map[x][y] = ptrn_index2
ptrn_index2 = ptrn_index2 + 1
end
end
-- init pattern_to_grid_xy_map
local x = 0
local y = 0
for ptrn_index=1,self.total_patterns do
self.pattern_to_grid_xy_map[ptrn_index] = {x + self.grid_start_x, y + self.grid_start_y}
x = x + 1
if ptrn_index % self.grid_row_length == 0 then
x = 0
y = y + 1
end
end
end,
evaluate_longest_track = function (self)
local highest_last_step = 1
for _, track in pairs(tracks_data) do
local track_last_step = track.patterns[self.current_pattern].last_step
if track_last_step > highest_last_step then
self.longest_track_in_pattern[self.current_pattern] = track.index
end
end
end,
is_longest_track_in_current_pattern = function (self, track_index)
return self.longest_track_in_pattern[self.current_pattern] == track_index
end,
index_to_grid_xy = function (self, index)
local grid_xy = self.pattern_to_grid_xy_map[index]
return grid_xy[1], grid_xy[2]
end,
grid_xy_valid = function (self, x, y)
return self.grid_start_x <= x and x < (self.grid_start_x + self.grid_row_length)
and self.grid_start_y <= y and y < (self.grid_start_y + self.grid_rows)
end,
grid_xy_to_index = function (self, x, y)
return self.grid_xy_to_pattern_map[x][y]
end
}
local pages_buttons_grid_map = {}
step_editor = {
edited_params_color = 6,
active_color = 9,
currently_playing_color = 13,
total_steps = 64,
steps_per_page = 16,
page_count = 4,
current_page = 1,
current_track = 1,
currently_pressed_step = nil,
pattern_follow = false,
pattern_follow_tick = function (self)
if self.pattern_follow then
local current_step = tracks_data[self.current_track].patterns[pattern_selector.current_pattern].current_step
-- replace with ifs on perfromance problems
self.current_page = math.floor((current_step - 1)/ step_editor.steps_per_page) + 1
end
end,
grid_rows = 2,
grid_row_length = 8,
grid_start_x = 1,
grid_start_y = 4,
step_to_grid_xy_map = {},
grid_xy_to_step_map = {},
pages_grid_start_x = 5,
pages_grid_start_y = 2,
pages_buttons = {}
}
function step_editor:init()
-- init grid_xy_to_step_map
local step_index2 = 1
local grid_y_end = (self.grid_start_y + self.grid_rows - 1)
local grid_x_end = (self.grid_start_x + self.grid_row_length - 1)
for y=self.grid_start_y,grid_y_end do
for x=self.grid_start_x,grid_x_end do
if self.grid_xy_to_step_map[x] == nil then
self.grid_xy_to_step_map[x] = {}
end
self.grid_xy_to_step_map[x][y] = step_index2
step_index2 = step_index2 + 1
end
end
-- init step_to_grid_xy_map
local x = 0
local y = 0
for step_index=1,self.total_steps do
self.step_to_grid_xy_map[step_index] = {x + self.grid_start_x, y + self.grid_start_y}
x = x + 1
if step_index % self.grid_row_length == 0 then
x = 0
y = y + 1
end
if step_index % self.steps_per_page == 0 then
y = 0
end
end
for page_button_index=1,self.page_count do
self.pages_buttons[page_button_index] = {
grid_x = self.pages_grid_start_x + page_button_index - 1,
grid_y = self.pages_grid_start_y,
pressed = false,
press_timer = nil,
long_press = function (self)
clock.sleep(0.5)
if page_button_index == 1 then
step_editor.pattern_follow = true
end
self.press_timer = nil
end,
on_press = function (self)
self.pressed = true
self.press_timer = clock.run(self.long_press, self)
end,
on_release = function (self)
self.pressed = false
if self.press_timer then
clock.cancel(self.press_timer)
step_editor.current_page = page_button_index
step_editor.pattern_follow = false
end
end,
get_grid_color = function (self)
-- pressed or being viewed page
if self.pressed or step_editor.current_page == page_button_index then
return step_editor.pattern_follow and step_editor.currently_playing_color or color.PRESSED
end
-- currently playing or active page - active means that it is non empty
local current_pattern = tracks_data[step_editor.current_track].patterns[pattern_selector.current_pattern]
if current_playback_mode ~= playback_mode.STOPPED
and (math.floor((current_pattern.current_step - 1)/ step_editor.steps_per_page) + 1) == page_button_index then
return step_editor.currently_playing_color
elseif math.floor((current_pattern.last_step - 1)/ step_editor.steps_per_page) + 1 >= page_button_index then
return step_editor.edited_params_color
end
return color.DEFAULT
end
}
end
end
function step_editor:get_current_view_range()
return 1 + (self.current_page - 1) * self.steps_per_page, self.current_page * self.steps_per_page
end
function step_editor:is_index_in_current_view_range(index)
local low_bound, high_bound = self:get_current_view_range()
return low_bound <= index and index <= high_bound
end
function step_editor:index_to_grid_xy(index)
local grid_xy = self.step_to_grid_xy_map[index]
return grid_xy[1], grid_xy[2]
end
function step_editor:grid_xy_valid(x, y)
return self.grid_start_x <= x and x < (self.grid_start_x + self.grid_row_length)
and self.grid_start_y <= y and y < (self.grid_start_y + self.grid_rows)
end
function step_editor:grid_xy_to_index(x, y)
return self.grid_xy_to_step_map[x][y] + (self.current_page - 1) * self.steps_per_page
end
TrackData = {
partials_per_step = 4, -- used for higher resolution
index = nil,
name = nil,
grid_x = nil,
grid_y = nil,
screen_x = nil,
screen_y = nil,
trigger_callback = nil,
max_params_count = 3,
track_params = nil,
patterns = nil,
trigger_button = nil,
focused = false,
focus_button = nil,
muted = false,
}
function TrackData:trigger()
if not self.muted then
self.trigger_callback(self)
end
end
function TrackData:prepare_step_params()
local pattern = self.patterns[pattern_selector.current_pattern]
local step_params = pattern.sequence[pattern.current_step].step_params
if pattern.reset_to_track_params then
for key,param in ipairs(self.track_params) do
param:reset()
end
pattern.reset_to_track_params = false
end
if next(step_params) ~= nil then
for param_index=1,#self.track_params do
local param = step_params[param_index]
if param ~= nil then
self.track_params[param_index].set(param.value)
end
end
pattern.reset_to_track_params = true
end
end
function TrackData:new(index, name, gx, gy, sx, sy, track_params, callback)
local object = {}
setmetatable(object, self)
self.__index = self
object.index = index;
object.name = name;
object.grid_x = gx
object.grid_y = gy
object.screen_x = sx
object.screen_y = sy
object.trigger_callback = callback
object.track_params = {}
for key,value in ipairs(track_params) do
object.track_params[key] = {
param_id = value.param_id,
draw_note = value.draw_note,
dial_x = object.screen_x,
dial_y = object.screen_y - 6 - (self.max_params_count - key + 1) * 16,
dial = nil,
range = params:get_range(value.param_id),
track_value = params:get(value.param_id),
set = function(v) params:set(value.param_id, v) end,
reset = function(self) self.set(self.track_value, false) end,
get = function() return params:get(value.param_id) end,
track_param_delta = function(self, d)
self.track_value = util.clamp(self.track_value + d, self.range[1], self.range[2])
self.set(self.track_value)
end,
dial_redraw = function(self)
if self.dial == nil then
self.dial = ui.Dial.new(self.dial_x, self.dial_y, 8, self.track_value, self.range[1], self.range[2], 1, self.range[1],
{}, nil, self.draw_note and music.note_num_to_name(self.track_value) or nil)
end
if current_mode == mode.STEP_EDIT and step_editor.currently_pressed_step and step_editor.current_track == object.index then
self.dial.active = true
local step_params = object.patterns[pattern_selector.current_pattern].sequence[step_editor.currently_pressed_step].step_params
local param = step_params[key]
if param ~=nil then
self.dial.title = self.draw_note and music.note_num_to_name(param.value, true) or nil
self.dial:set_value(param.value)
ui_utils.draw_dot(self.dial_x -2, self.dial_y, 15)
else
self.dial.title = self.draw_note and music.note_num_to_name(self.track_value, true) or nil
self.dial:set_value(self.track_value)
end
self.dial:redraw()
return
end
self.dial.active = object.focused
self.dial.title = self.draw_note and music.note_num_to_name(self.track_value, true) or nil
self.dial:set_value(self.track_value)
self.dial:redraw()
end
}
-- TODO fix this hack
-- init engine values hack
object.track_params[key]:track_param_delta(-1)
object.track_params[key]:track_param_delta(1)
end
object.trigger_button = {
grid_x = object.grid_x,
grid_y = object.grid_y,
pressed = false,
on_press = function (self)
self.pressed = true
object:trigger()
if current_recording_mode == recording_mode.RECORDING and current_playback_mode == playback_mode.PLAYING then
local pattern = object.patterns[pattern_selector.current_pattern]
pattern.sequence[pattern.current_step]:set()
end
if current_mode == mode.STEP_EDIT then
step_editor.current_track = object.index
end
end,
on_release = function (self)
self.pressed = false
end,
get_grid_color = function (self)
if object.muted then
return color.DISABLED
end
if current_mode == mode.STEP_EDIT and step_editor.current_track == object.index then
return not self.pressed and color.PRESSED or color.DEFAULT
else
return self.pressed and color.PRESSED or color.DEFAULT
end
end
}
object.focus_button = {
grid_x = object.grid_x,
grid_y = object.grid_y - 1,
pressed = false,
timer = nil,
run_timer = function (self)
self.timer = clock.run(function (self)
clock.sleep(1.5)
self.timer = nil
end, self)
end,
on_press = function (self)
self.pressed = true
if shift_pressed then
object.muted = not object.muted
return
end
if utility_mode:is_erase() then
utility_mode:set_none()
local current_pattern = pattern_selector.current_pattern
object.patterns[current_pattern] = Pattern:new(current_pattern)
self:run_timer()
return
end
if utility_mode:is_copy() then
if clipboard:empty() then
clipboard:store_track(object.patterns[pattern_selector.current_pattern])
elseif clipboard:has_track() then
-- if clipboard track is different than current then copy
if not clipboard:has_track(object.patterns[pattern_selector.current_pattern]) then
object.patterns[pattern_selector.current_pattern] = table_utils.deepcopy(clipboard.track)
object.patterns[pattern_selector.current_pattern].index = pattern_selector.current_pattern
object.patterns[pattern_selector.current_pattern]:reset_step_params()
end
clipboard:clear()
utility_mode:set_none()
self:run_timer()
end
return
end
object.focused = true
currently_focused_track = object.index
if current_mode == mode.STEP_EDIT then
step_editor.current_track = object.index
end
end,
on_release = function (self)
self.pressed = false
object.focused = false
if currently_focused_track == object.index then
currently_focused_track = nil
end
end,
get_grid_color = function(self)
if self.pressed then
return color.PRESSED
elseif self.timer then
return flash_fast and color.PRESSED or color.DEFAULT
elseif clipboard:has_track(object.patterns[pattern_selector.current_pattern]) then
return flash_fast and color.PRESSED or color.DEFAULT
elseif utility_mode:is_erase() then
return flash_slow and color.PRESSED or color.DEFAULT
elseif clipboard:has_track() then
return flash_slow and color.PRESSED or color.DEFAULT
elseif utility_mode:is_copy() and clipboard:empty() then
return flash_slow and color.PRESSED or color.DEFAULT
else
return color.DEFAULT
end
end
}
Step = {
pattern = nil,
index = nil,
partial_steps = nil,
current_fill = 1,
current_offset = 0,
active = false,
press_timer = nil,
pressed = false,
edited_while_pressed = false,
edited_params = false,
edited_fill_or_offset = false,
step_params = nil,
new = function (self, pattern, index)
local o = {}
setmetatable(o, self)
self.__index = self
o.pattern = pattern
o.index = index
o.step_params = {}
o.partial_steps = {0,0,0,0, 0,0,0,0} -- 4 and 4 more to recover from offset
return o
end,
param_delta = function (self, param_index, delta)
if self.step_params[param_index] == nil then
local param = object.track_params[param_index]
if param ~= nil then
local param_range = param.range
self.step_params[param_index] = {
range = param_range,
value = util.clamp(param.track_value + delta, param_range[1], param_range[2])
}
end
else
local step_params = self.step_params[param_index]
local param_range = step_params.range
step_params.value = util.clamp(step_params.value + delta, param_range[1], param_range[2])
end
end,
set_fill = function (self, fill)
if fill == 1 then
self.partial_steps = {1,0,0,0, 0,0,0,0}
elseif fill == 2 then
self.partial_steps = {1,0,1,0, 0,0,0,0}
elseif fill == 4 then
self.partial_steps = {1,1,1,1, 0,0,0,0}
else
print("Unsupported fill value: " .. tostring(fill))
return false
end
self.current_fill = fill
table_utils.shift(self.partial_steps, self.current_offset)
return true
end,
edit_fill = function (self, fill)
if self.current_fill ~= fill then
if self:set_fill(fill) then
self.edited_fill_or_offset = true
self.edited_while_pressed = true
end
end
end,
set_offset = function (self, offset)
if table_utils.shift(self.partial_steps, offset - self.current_offset) then
self.current_offset = offset
end
end,
edit_offset = function (self, offset)
self.edited_fill_or_offset = self:set_offset(offset)
self.edited_while_pressed = self.edited_fill_or_offset
end,
set = function (self)
self.partial_steps = {1,0,0,0, 0,0,0,0}
self.current_offset = 0
self.current_fill = 1
self.edited_while_pressed = false
self.edited_fill_or_offset = false
self.step_params = {}
self.active = true
end,
reset = function (self)
self.partial_steps = {0,0,0,0, 0,0,0,0}
self.current_offset = 0
self.current_fill = 1
self.edited_while_pressed = false
self.edited_fill_or_offset = false
self.step_params = {}
self.active = false
end,
toggle = function(self)
if not self.active then
self:set()
else
self:reset()
end
end,
long_press = function (self)
clock.sleep(0.5)
-- do nothing for now
self.press_timer = nil
end,
short_press = function (self)
self:toggle()
end,
on_press = function (self)
self.pressed = true
if last_step_pressed then
self.pattern.last_step = self.index
self.pattern.end_step = self.index
pattern_selector:evaluate_longest_track()
return
end
step_editor.currently_pressed_step = self.index
self.press_timer = clock.run(self.long_press, self)
end,
on_release = function (self)
self.pressed = false
if last_step_pressed then
return
end
if step_editor.currently_pressed_step == self.index then
step_editor.currently_pressed_step = nil
end
-- if not self.active then
-- self:set()
-- return
-- end
if self.edited_while_pressed then
self.edited_while_pressed = false
return
end
if self.press_timer then
clock.cancel(self.press_timer)
self:short_press()
end
end,
get_grid_color = function (self)
if self.pressed and self.active then
return color.PRESSED
elseif current_playback_mode ~= playback_mode.STOPPED and self.index == self.pattern.current_step then
return step_editor.currently_playing_color
elseif last_step_pressed then
return self.index == self.pattern.last_step and step_editor.active_color or color.DEFAULT
elseif self.active and not self.edited_fill_or_offset and next(self.step_params) == nil then
return step_editor.active_color
elseif self.active and (self.edited_fill_or_offset or next(self.step_params) ~= nil) then
return step_editor.edited_params_color
elseif not self.active then
return color.DEFAULT
end
end
}
Pattern = {
index = nil,
pressed = false,
current_step = 1,
begin_step = 1,
end_step = 16,
last_step = 16,
max_step = 64,
sequence = nil,
reset_to_track_params = true,
-- roll specific
roll_enabled = false,
rolled_steps = 0,
timer = nil,
run_timer = function (self)
self.timer = clock.run(function (self)
clock.sleep(1.5)
self.timer = nil
end, self)
end,
new = function (self, index)
local o = {}
setmetatable(o, self)
self.__index = self
o.index = index
o.sequence = {}
for i=1,o.max_step do
o.sequence[i] = Step:new(o, i)
end
return o
end,
extend = function (self)
local steps_to_copy = self.last_step % 16 == 0 and 16 or self.last_step
for i=1,steps_to_copy do
if self.last_step < self.max_step then
self.last_step = self.last_step + 1
else
break
end
local dont_copy = {}
dont_copy[self] = self
self.sequence[self.last_step] = table_utils.deepcopy(self.sequence[i], dont_copy)
self.sequence[self.last_step].index = self.last_step
end
self.end_step = self.last_step
end,
reset_step_params = function (self)
for _, step in pairs(self.sequence) do
step.step_params = {}
step.edited_params = false
end
end,
reset_pattern_data = function (self)
self.current_step = 1
self.begin_step = 1
self.end_step = 16
self.last_step = 16
self.max_step = 64
self.reset_to_track_params = true
self.roll_enabled = false
self.rolled_steps = 0
self.sequence = {}
for i=1,self.max_step do
self.sequence[i] = Step:new(self, i)
end
end,
set_roll = function (self, roll)
if roll == 4 or roll == 2 then
local beat_step_base = math.floor(self.current_step / roll) * roll
if self.current_step == beat_step_base then
beat_step_base = beat_step_base - roll
end
self.begin_step = beat_step_base + 1
self.end_step = util.clamp(beat_step_base + roll, 1, self.last_step)
elseif roll == 1 then
self.begin_step = self.current_step
self.end_step = self.current_step
else
print("Unsupported roll value: " .. tostring(roll))
return false
end
self.rolled_steps = self.current_step
self.roll_enabled = true
return true
end,
reset_roll = function (self)
if self.roll_enabled then
self.begin_step = 1
self.end_step = self.last_step
self.current_step = (self.rolled_steps - 1) % self.last_step + 1
self.roll_enabled = false
end
end,
on_press = function (self)
self.pressed = true
if utility_mode:is_erase() then
utility_mode:set_none()
for _, track_data in pairs(tracks_data) do
track_data.patterns[self.index]:reset_pattern_data()
end
self:run_timer()
return
end
if utility_mode:is_copy() then
if clipboard:empty() then
clipboard:store_pattern(self.index)
return
elseif clipboard:has_pattern() then
-- if clipboard pattern is different than this then copy
if not clipboard:has_pattern(self.index) then
for _, track_data in pairs(tracks_data) do
track_data.patterns[self.index] = table_utils.deepcopy(track_data.patterns[clipboard.pattern])
track_data.patterns[self.index].index = self.index
track_data.patterns[self.index]:reset_step_params()
end
end
-- after copy, THIS pattern (self) is no longer accessed anywhere therefore
-- run_timer() must be called on fresh copy
tracks_data[step_editor.current_track].patterns[self.index]:run_timer()
clipboard:clear()
utility_mode:set_none()
return
end
end
pattern_selector.selected_pattern = self.index
if current_playback_mode == playback_mode.STOPPED then
pattern_selector.current_pattern = self.index
end
end,
on_release = function (self)
self.pressed = false
end,
get_grid_color = function (self)
if self.pressed then
return color.PRESSED
elseif self.timer then
return flash_fast and color.PRESSED or color.DEFAULT
elseif clipboard:has_pattern(self.index) then
return flash_fast and color.PRESSED or color.DEFAULT
elseif utility_mode:is_erase() then
return flash_slow and color.PRESSED or color.DEFAULT
elseif clipboard:has_pattern() then
return flash_slow and color.PRESSED or color.DEFAULT
elseif utility_mode:is_copy() and clipboard:empty() then
return flash_slow and color.PRESSED or color.DEFAULT
elseif pattern_selector.current_pattern == self.index then
return pattern_selector.currently_playing_color
elseif pattern_selector.selected_pattern == self.index then
return flash_slow and pattern_selector.selected_to_play_next_color or color.DEFAULT
else
return color.DEFAULT
end
end
}
object.patterns = {}
for i=1,pattern_selector.total_patterns do
object.patterns[i] = Pattern:new(i)
end
return object
end
function init_tracks_data()
tracks_data = {
TrackData:new(1,"BD", 1,8, 11, 64,
{
{param_id = "BD level"},
{param_id = "BD tone", draw_note = true},
{param_id = "BD decay"}
},
function(self)
engine.kick_trigger()
end
),
TrackData:new(2,"SD", 2,8, 26, 64,
{
{param_id = "SD level"},
{param_id = "SD snappy"},
-- {param_id = "SD tone", draw_note = true},
{param_id = "SD decay"}
},
function(self)
engine.snare_trigger()
end
),
TrackData:new(3,"MT", 3,8, 41, 64,
{
{param_id = "MT level"},
{param_id = "MT tone", draw_note = true},
{param_id = "MT decay"}
},
function(self)
engine.mt_trigger()
end
),
TrackData:new(4,"CH", 4,8, 56, 64,
{
{param_id = "CH level"},
{param_id = "CH tone", draw_note = true},
{param_id = "CH decay"}
},
function(self)
engine.ch_trigger()
end
),
TrackData:new(5,"OH", 5,8, 71, 64,
{
{param_id = "OH level"},
{param_id = "OH tone", draw_note = true},
{param_id = "OH decay"}
},
function(self)
engine.oh_trigger()
end
),
TrackData:new(6,"CP", 6,8, 86, 64,
{
{param_id = "CP level"}
},
function(self)
engine.clap_trigger()
end
),
TrackData:new(7,"CW", 7,8, 101, 64,
{
{param_id = "CW level"}
},
function(self)
engine.cowbell_trigger()
end
),
TrackData:new(8,"RS", 8,8, 116, 64,
{
{param_id = "RS level"}
},
function(self)
engine.rimshot_trigger()
end
)
}
end
local utility_buttons_grid_map = {}
local utility_buttons = {}
function init_utility_buttons()
-- TODO make each utility an object and then put into a table for easy access by grid
-- TODO this will allow to use play_pause_button.pressed in the code, and skip globals
utility_buttons = {
{
name = "rec",
grid_x = 1,
grid_y = 1,
pressed = false,
on_press = function (self)
self.pressed = true
if current_recording_mode == recording_mode.RECORDING then
current_recording_mode = recording_mode.NOT_RECORDING
else
current_recording_mode = recording_mode.RECORDING
end
end,
on_release = function (self)
self.pressed = false