Repository navigation
Expand file tree
/
Copy pathgridstep.lua
More file actions
executable file
·4748 lines (3931 loc) · 150 KB
/
Copy pathgridstep.lua
File metadata and controls
executable file
·4748 lines (3931 loc) · 150 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
-- gridstep
-- v1.3.4 @Quixotic7
-- a polyphonic, isomorphic
-- grid keyboard sequencer
-- for timber & midi
-- llllllll.co/t/38559
local version_number = "1.3.5"
local _MOLLY_ENGINE = false
local _TIMBER_ENGINE = true
local MollyThePoly = nil
if _MOLLY_ENGINE then
MollyThePoly = require "molly_the_poly/lib/molly_the_poly_engine"
engine.name = "MollyThePoly"
end
local Timber = nil
local NUM_SAMPLES = 16
local KIT_SAMPLES = 128
local KIT_SAMPLES_START = NUM_SAMPLES
local KIT_SAMPLES_END = KIT_SAMPLES + NUM_SAMPLES
local TimberUI = {}
TimberUI.sample_setup_view = {}
TimberUI.waveform_view = {}
TimberUI.filter_amp_view = {}
TimberUI.amp_mod_env_view = {}
-- TimberUI.amp_env_view = {}
-- TimberUI.mod_env_view = {}
TimberUI.lfos_view = {}
TimberUI.mod_matrix_view = {}
TimberUI.key_matrix_view = {}
TimberUI.page_start = 7
TimberUI.pages = {}
TIMBER_QUANTIZATION = {"None", "1/32", "1/24", "1/16", "1/12", "1/8", "1/6", "1/4", "1/3", "1/2", "1 bar"}
if _TIMBER_ENGINE then
Timber = include("gridstep/lib/timber_gridstep")
engine.name = "Timber_Gridstep"
end
-- local TimberUI = include("gridstep/lib/TimberUI")
local music = require 'musicutil'
local beatclock = require 'beatclock'
_GRID_CAPTURE = false
local gcap = nil
if _GRID_CAPTURE then
gcap = require 'GridCapture/GridCapture'
end
-- local hsdelay = require 'gridstep/lib/halfsecond'
-- local Q7Util = require 'gridstep/lib/Q7Util'
-- local UI = require 'gridstep/lib/Q7UI'
-- local Q7GridKeys = require 'gridstep/lib/Q7GridKeys'
-- local Q7GridSeq = require 'gridstep/lib/Q7GridSeq'
-- local ParamListUtil = require 'gridstep/lib/Q7ParamListUtil'
-- local GraphicPageOptions = require 'gridstep/lib/Q7GraphicPageOptions'
local hsdelay = include("gridstep/lib/halfsecond")
local Q7Util = include("gridstep/lib/Q7Util")
local UI = include("gridstep/lib/Q7UI")
local Q7GridKeys = include("gridstep/lib/Q7GridKeys")
local Q7GridSeq = include("gridstep/lib/Q7GridSeq")
local ParamListUtil = include("gridstep/lib/Q7ParamListUtil")
local GraphicPageOptions = include("gridstep/lib/Q7GraphicPageOptions")
local fileselect = require 'fileselect'
local textentry = require 'textentry'
local g = grid.connect()
local gridType_none = 0
local gridType_128 = 1
local gridType_64 = 2
local gridType = 1
local gridKeys = {}
local gridSeq = {}
local midi_1 = midi.connect(1)
local midi_2 = midi.connect(2)
local midi_3 = midi.connect(3)
local midi_4 = midi.connect(4)
local midi_devices = {midi_1, midi_2, midi_3, midi_4}
local active_midi_notes = {}
local active_internal_notes = {}
local SoundModes = {"Internal", "External", "Kit"}
local all_gridKeys = {}
local all_gridSeqs = {}
local config = {
page_index = 1,
grid_page_index = 1,
root_note = 1,
scale_mode = 1,
note_velocity = 100,
header_img = 1,
ui_anim_step = 0,
header_notification_y = -10,
sample_id = 0
}
-- for changing values with encoders nicely
local enc_d = {
page_index = 1,
root_note = 1,
scale_mode = 1
}
local data_path = _path.data.."gridstep/"
local params_path = _path.data.."gridstep/params/"
local screenshot_path = _path.data.."gridstep/screenshots/"
local img_path = _path.code.."gridstep/img/"
local project_name = ""
local fileselect_active = false
local textentry_active = false
local PageTest = {}
local PageScale = {}
local PageSound = {}
local PageKit = {}
local PageTimber = {}
local PageTrack = {}
local PagePattern = {}
local PageClock = {}
local PageSaveLoad = {}
local PageQ7 = {}
local PageTrig = {} -- shown when step key is held
local PageMicroTiming = {}
local showTrigPage = false
local current_page = {}
local pages = {PageSaveLoad, PageScale, PageClock, PageTrig, PageKit, PagePattern, PageTimber, PageTimber, PageTimber, PageTimber, PageTimber, PageTimber, PageTimber, PageSound, PageTrack, PageMicroTiming, PageQ7}
-- local page_titles = {"Scale", "Sample", "Waveform", "Filter Amp", "Env", "LFOS", "Mod Matrix", "Key Matrix",
-- "Sound", "Kit", "Clock", "Track", "Trig", "Step Time", "Save / Load", "Credits"}
local header_height = 12
local GridPlay = {}
local GridSeq = {}
local GridSeqVel = {}
local GridSeqNoteLengths = {}
local GridPatLaunch = {}
local current_grid_page = {}
local grid_pages = {GridPlay, GridPatLaunch, GridSeq, GridSeqVel, GridSeqNoteLengths}
local grid_page_names = {"GridPlay", "GridPatLaunch", "GridSeq", "GridSeqVel", "GridSeqNoteLengths"}
local gridSeqConfig = {
step_counter = {}
}
local patLaunchConfig = {
edit_mode = 1,
clipboard_pattern = nil,
clipboard_gridSeq = nil,
clipboard_gridKeys = nil,
track_paste_mode, -- determines whether to paste gridSeq or gridKeys
y_offset = 0
}
local whiteKeys = {1,0,1,0,1,1,0,1,0,1,0,1}
local pattern_num_to_letter = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"}
local held_step = 0
local grid_dirty = false
GridPatLaunch.edit_mode = seqmode_select
GridPatLaunch.clipboard_pattern = nil
local seq_noteLengths = {1, 2, 4, 8, 16, 32}
-- local seq_active = false
-- local seq_delete = false
local seq_cut = false
local seq_copy = false
local seq_paste = false
local mode_off_brightness = 6
local mode_on_brightness = 12
local shift_brightness = 7
local shift_down = false
local track = 1
local is_playing = false
local edit_steps_9_16 = false -- for 64x64 grids
local blink_on = false -- used to blink keys
local blink_fade = 1
local blink_fade_t1 = 1
local blink_fade_t2 = 0
local blink_fade_d = 0.25
local blink_fade_p = 0.25
local blink_counter = 0
local confirm_delete = false
local gridImage_x = {
{1,1},{2,2},{3,3},{4,4},{5,5},
{1,5},{2,4},{4,2},{5,1},
}
local gridImage_cut = {
{8,3},{9,3},
}
local gridImage_copy = {
{4,2},{5,2},{6,2},{11,2},{12,2},{13,2},
{4,3},{8,3},{9,3},{13,3},
{4,4},{5,4},{6,4},{11,4},{12,4},{13,4}
}
local gridImage_paste = {
{4,2},{5,2},{6,2},{7,2},{8,2},{9,2},{10,2},{11,2},{12,2},{13,2},
{4,3},{13,3},
{4,4},{5,4},{6,4},{7,4},{8,4},{9,4},{10,4},{11,4},{12,4},{13,4}
}
local notification_clockId = 0
local display_notification = false
local temp_notification = false
local notification_text = "none"
-- init
function init()
if g.cols == 16 and g.rows == 8 then
print("grid 128 detected")
gridType = gridType_128
elseif g.cols == 8 and g.rows == 8 then
print("grid 64 detected")
gridType = gridType_64
else
gridType = gridType_none
end
-- gridType = gridType_64 -- uncomment to fake 8x8 grid
current_page = pages[config.page_index]
current_grid_page = grid_pages[config.grid_page_index]
hsdelay.init()
if _MOLLY_ENGINE then
MollyThePoly.add_params()
MollyThePoly.randomize_params("lead")
end
if _TIMBER_ENGINE then
Timber.sample_changed_callback = on_sample_loaded
-- function(id)
-- if Timber.samples_meta[id].manual_load then
-- -- Set our own loop point defaults
-- params:set("loop_start_frame_" .. id, util.round(Timber.samples_meta[id].num_frames * 0.2))
-- params:set("loop_end_frame_" .. id, util.round(Timber.samples_meta[id].num_frames * 0.4))
-- -- Set env defaults
-- params:set("amp_env_attack_" .. id, 0.01)
-- params:set("amp_env_sustain_" .. id, 0.8)
-- params:set("amp_env_release_" .. id, 0.4)
-- end
-- -- callback_set_screen_dirty(id)
-- end
Timber.set_bpm(clock.get_tempo())
Timber.add_params()
params:add_separator()
for i = 0, NUM_SAMPLES - 1 do
Timber.add_sample_params(i)
Timber.load_sample(i, _path.code .. "/gridstep/audio/piano-c.wav")
end
for i = KIT_SAMPLES_START, KIT_SAMPLES_END do
-- local extra_params = {
-- {type = "option", id = "launch_mode_" .. i, name = "Launch Mode", options = {"Gate", "Toggle"}, default = 1, action = function(value)
-- Timber.setup_params_dirty = true
-- end},
-- {type = "option", id = "quantization_" .. i, name = "Quantization", options = {"None", "1/32", "1/24", "1/16", "1/12", "1/8", "1/6", "1/4", "1/3", "1/2", "1 bar"}, default = 1, action = function(value)
-- if value == 1 then
-- for n = #note_queue, 1, -1 do
-- if note_queue[n].sample_id == i then
-- table.remove(note_queue, n)
-- if Timber.samples_meta[i].playing then
-- -- sample_status[i] = STATUS.PLAYING
-- else
-- -- sample_status[i] = STATUS.STOPPED
-- end
-- -- grid_dirty = true
-- end
-- end
-- end
-- Timber.setup_params_dirty = true
-- end}
-- }
Timber.add_sample_params(i)
-- Timber.add_sample_params(i, true, extra_params)
-- Timber.add_sample_params(i, false, extra_params)
end
end
-- params:set_action("clock_tempo", on_tempo_changed) -- this was a bad idea
-- param_list_util = ParamListUtil.new()
-- param_list_util.redraw_func = redraw
gridKeys = {}
gridSeq = {}
for i = 1,16 do
all_gridKeys[i] = Q7GridKeys.new(16,8)
all_gridKeys[i].id = i
all_gridKeys[i].midi_device = 1
all_gridKeys[i].midi_channel = i
all_gridKeys[i].sound_mode = 1
all_gridKeys[i].note_on = grid_note_on
all_gridKeys[i].note_off = grid_note_off
all_gridKeys[i].key_pushed = grid_key_pushed
all_gridSeqs[i] = Q7GridSeq.new(all_gridKeys[i])
all_gridKeys[i].gridSeq = all_gridSeqs[i]
all_gridSeqs[i].on_pat_changed = gridSeq_pat_changed
-- all_gridSeqs[i].on_new_step = seq_newStep
end
gridKeys = all_gridKeys[track]
gridSeq = all_gridSeqs[track]
for d = 1,4 do
active_midi_notes[d] = {}
for i = 1, 16 do
active_midi_notes[d][i] = {}
end
end
-- gridKeys = Q7GridKeys.new(16,8)
-- gridKeys.note_on = grid_note_on
-- gridKeys.note_off = grid_note_off
-- for i = 1,16 do
-- gridSeqs[i] = Q7GridSeq.new(gridKeys)
-- end
-- gridSeq = Q7GridSeq.new(gridKeys)
-- gridKeys.key_on = function(e) grid_key_on(e) end
-- gridKeys.key_off = function(e) grid_key_off(e) end
-- gridKeys:resize_grid(1,1,16,7)
-- gridKeys:change_scale(config.root_note, config.scale_mode)
-- gridKeys.highlight_selected_notes = false
-- gridKeys.layout_mode = 2
for i,gk in pairs(all_gridKeys) do
gk:resize_grid(1,1,16,7)
gk:change_scale(config.root_note, config.scale_mode)
gk.highlight_selected_notes = false
gk.layout_mode = 2
end
-- for i = 1,#midi_devices do
-- midi_devices[i].event = function (data)
-- end
-- end
-- local note = gridKeys:grid_to_note(10,4)
-- print("note = "..note)
-- print("gridKeys.layout_mode = "..gridKeys.layout_mode)
-- print("gridKeys2.layout_mode = "..gridKeys2.layout_mode)
for i,p in pairs(pages) do
if p.init ~= nil then p.init() end
end
PageTrig.init()
change_grid_page("GridPlay")
-- if current_page.init ~= nil then current_page.init() end
grid_redraw()
if _GRID_CAPTURE then
gcap:set_grid(g)
-- gcap:set_theme("bw")
end
clock.run(screen_redraw_clock)
clock.run(grid_redraw_clock) -- start the grid redraw clock
end
function create_new_project()
if is_playing then
clock.transport.stop()
end
all_midi_notes_off()
all_engine_notes_off()
config = {
page_index = 1,
grid_page_index = 1,
root_note = 1,
scale_mode = 1,
note_velocity = 100,
header_img = 1,
ui_anim_step = 0,
header_notification_y = -10,
}
gridSeqConfig = {
step_counter = {}
}
patLaunchConfig = {
edit_mode = 1,
clipboard_pattern = nil,
clipboard_gridSeq = nil,
clipboard_gridKeys = nil,
track_paste_mode, -- determines whether to paste gridSeq or gridKeys
y_offset = 0
}
held_step = 0
GridPatLaunch.edit_mode = seqmode_select
GridPatLaunch.clipboard_pattern = nil
seq_cut = false
seq_copy = false
seq_paste = false
shift_down = false
track = 1
is_playing = false
confirm_delete = false
current_page = pages[config.page_index]
current_grid_page = grid_pages[config.grid_page_index]
if _MOLLY_ENGINE then
MollyThePoly.randomize_params("lead")
end
gridKeys = {}
gridSeq = {}
for i = 1,16 do
all_gridKeys[i] = Q7GridKeys.new(16,8)
all_gridKeys[i].id = i
all_gridKeys[i].midi_device = 1
all_gridKeys[i].midi_channel = i
all_gridKeys[i].sound_mode = 1
all_gridKeys[i].note_on = grid_note_on
all_gridKeys[i].note_off = grid_note_off
all_gridSeqs[i] = Q7GridSeq.new(all_gridKeys[i])
all_gridKeys[i].gridSeq = all_gridSeqs[i]
all_gridSeqs[i].on_pat_changed = gridSeq_pat_changed
-- all_gridSeqs[i].on_new_step = seq_newStep
end
gridKeys = all_gridKeys[track]
gridSeq = all_gridSeqs[track]
for d = 1,4 do
active_midi_notes[d] = {}
for i = 1, 16 do
active_midi_notes[d][i] = {}
end
end
for i,gk in pairs(all_gridKeys) do
gk:resize_grid(1,1,16,7)
gk:change_scale(config.root_note, config.scale_mode)
gk.highlight_selected_notes = false
gk.layout_mode = 2
end
for i,p in pairs(pages) do
if p.init ~= nil then p.init() end
end
PageTrig.init()
change_grid_page("GridPlay")
grid_redraw()
show_temporary_notification("New Project Created")
-- clock.run(screen_redraw_clock)
-- clock.run(grid_redraw_clock) -- start the grid redraw clock
end
function update_gridKeys_samples()
for sampleId = KIT_SAMPLES_START, KIT_SAMPLES_END do
for i, gKeys in pairs(all_gridKeys) do
gKeys.kit_has_sample[sampleId-KIT_SAMPLES_START+1] = Timber.does_sample_exist(sampleId) and 1 or 0
end
end
end
function on_sample_loaded(sampleId)
if Timber.samples_meta[sampleId].manual_load then
if sampleId >= KIT_SAMPLES_START then
-- if Timber.samples_meta[sampleId].streaming == 0 and Timber.samples_meta[sampleId].num_frames / Timber.samples_meta[sampleId].sample_rate < 1 and string.find(string.lower(params:get("sample_" .. sampleId)), "loop") == nil then
-- params:set("play_mode_" .. sampleId, 4) -- One shot
-- end
if string.find(string.lower(params:get("sample_" .. sampleId)), "loop") == nil then
params:set("play_mode_" .. sampleId, 4) -- One shot
end
-- for i, gKeys in pairs(all_gridKeys) do
-- gKeys.kit_has_sample[sampleId-KIT_SAMPLES_START+1] = 1
-- end
else
-- Set our own loop point defaults
params:set("loop_start_frame_" .. sampleId, util.round(Timber.samples_meta[sampleId].num_frames * 0.2))
params:set("loop_end_frame_" .. sampleId, util.round(Timber.samples_meta[sampleId].num_frames * 0.4))
-- Set env defaults
params:set("amp_env_attack_" .. sampleId, 0.01)
params:set("amp_env_sustain_" .. sampleId, 0.8)
params:set("amp_env_release_" .. sampleId, 0.4)
end
-- update_gridKeys_samples()
end
if sampleId >= KIT_SAMPLES_START then
for i, gKeys in pairs(all_gridKeys) do
gKeys.kit_has_sample[sampleId-KIT_SAMPLES_START+1] = Timber.does_sample_exist(sampleId) and 1 or 0
end
grid_dirty = true
end
-- callback_set_screen_dirty(id)
end
function on_tempo_changed(newTempo)
newTempo = newTempo or clock.get_tempo()
-- print("tempo changed to "..newTempo)
hsdelay.tempo_changed(newTempo)
end
function kill_all_notes()
if is_playing then
clock.transport.stop()
end
all_midi_notes_off()
if _TIMBER_ENGINE then
engine.noteKillAll()
end
if _MOLLY_ENGINE then
all_engine_notes_off()
end
end
function stop()
print("stop")
end
function screen_redraw_clock()
while true do
clock.sleep(1/15)
redraw()
end
end
function grid_redraw_clock() -- our grid redraw clock
while true do -- while it's running...
clock.sync(1/8)
-- clock.sleep(1/15) -- refresh at 30fps.
blink_counter = (blink_counter + 1) % 2
if blink_counter == 0 then
blink_on = not blink_on
end
-- triangle wave fade
blink_fade_p = blink_fade_p + blink_fade_d
if blink_fade_p > 1 then
blink_fade_p = 0
if blink_fade_t1 == 1 then
blink_fade_t1 = 0
blink_fade_t2 = 1
else
blink_fade_t1 = 1
blink_fade_t2 = 0
end
end
blink_fade = util.linlin(0,1, blink_fade_t1, blink_fade_t2, blink_fade_p)
-- blink_fade = util.linlin(-1,1,0,1, math.sin(clock.get_beats() * 1.57 * 2 ))
-- print("Blink_fade: "..blink_fade)
if gridKeys:animate() or is_playing or gridSeq.record then
grid_redraw()
end
config.ui_anim_step = config.ui_anim_step + 1
if config.ui_anim_step > 2 then
config.ui_anim_step = 1
if is_playing then
config.header_img = (config.header_img % 4) + 1
end
end
-- redraw()
end
end
function toggle_playback()
if is_playing then
clock.transport.stop()
all_midi_notes_off()
show_temporary_notification("Stop")
for i = 1, #midi_devices do
midi_devices[i]:stop()
end
else
clock.transport.start()
show_temporary_notification("Play")
for i = 1, #midi_devices do
midi_devices[i]:start()
end
end
end
function clock.transport.start()
-- print("we begin")
-- position = 16
-- id = clock.run(count)
active_internal_notes = {}
on_tempo_changed(clock.get_tempo())
-- hsdelay.tempo_changed(clock.get_tempo())
for i,seq in pairs(all_gridSeqs) do
seq:play_start()
end
if not is_playing then
is_playing = true
play_clock_id = clock.run(play_sequence)
end
end
function clock.transport.stop()
-- clock.cancel(id)
for i,seq in pairs(all_gridSeqs) do
seq:play_stop()
end
-- gridSeq:play_stop()
if is_playing then
clock.cancel(play_clock_id)
is_playing = false
all_engine_notes_off()
end
end
function play_sequence()
local prev_bpm = clock.get_tempo()
-- local beats = clock.get_beats()
-- local beat_int = math.floor(beats)
-- local beat_frac = beats % beat_int
-- local next_16th = beat_int
-- if beat_frac < 0.25 then
-- next_16th = beat_int + 0.25
-- elseif beat_frac < 0.5 then
-- next_16th = beat_int + 0.5
-- elseif beat_frac < 0.75 then
-- next_16th = beat_int + 0.75
-- else
-- next_16th = beat_int + 1
-- end
-- clock.sync(next_16th - beats) -- sleep until clock is on next 16th for perfect sync
-- might want to do this every step, we'll see
while is_playing do
clock.sync(1/48) -- 6 substeps
-- print("beats: " .. clock.get_beats())
-- self:clock_step16()
if clock.get_tempo() ~= prev_bpm then
prev_bpm = clock.get_tempo()
on_tempo_changed(prev_bpm)
end
for i,seq in pairs(all_gridSeqs) do
seq:clock_step16()
end
active_internal_notes = {} -- prevents notes from playing on top of each other in a step
-- clock.sync(1/48) -- 12 substeps
-- grid_redraw()
-- clock.sync(1/16)
end
end
-- function grid_key_on(e)
-- if e ~= nil then
-- print("e not nil")
-- else
-- print("e is nil")
-- end
-- print("key_on id: "..e.id.." yID: " ..e.y)
-- gridSeq:key_on(e)
-- end
-- function grid_key_off(noteNum)
-- gridSeq:key_off(e)
-- end
local function change_sample_id(id)
config.sample_id = id
-- while current_sample_id >= NUM_SAMPLES do current_sample_id = current_sample_id - NUM_SAMPLES end
-- while current_sample_id < 0 do current_sample_id = current_sample_id + NUM_SAMPLES end
TimberUI.sample_setup_view:set_sample_id(config.sample_id)
TimberUI.waveform_view:set_sample_id(config.sample_id)
TimberUI.filter_amp_view:set_sample_id(config.sample_id)
TimberUI.amp_mod_env_view:set_sample_id(config.sample_id)
-- TimberUI.amp_env_view:set_sample_id(config.sample_id)
-- TimberUI.mod_env_view:set_sample_id(config.sample_id)
TimberUI.lfos_view:set_sample_id(config.sample_id)
TimberUI.mod_matrix_view:set_sample_id(config.sample_id)
TimberUI.key_matrix_view:set_sample_id(config.sample_id)
end
function change_track(newTrack)
if newTrack == track or newTrack < 1 or newTrack > 16 then
return false
end
track = newTrack
local prevGridKeys = gridKeys
local prevGridSeq = gridSeq
gridKeys = all_gridKeys[track]
gridSeq = all_gridSeqs[track]
gridSeq.step_edit = prevGridSeq.step_edit
gridKeys:resize_grid(prevGridKeys.grid_x, prevGridKeys.grid_y, prevGridKeys.grid_width, prevGridKeys.grid_height)
gridSeq:select_step(prevGridSeq.current_step)
gridKeys.enable_key_playback = prevGridKeys.enable_key_playback
gridSeq.edit_mode = prevGridSeq.edit_mode
-- PageTrack.init()
if gridKeys.sound_mode == 1 then
change_sample_id(gridKeys.midi_channel - 1)
elseif gridKeys.sound_mode == 3 then
change_sample_id(KIT_SAMPLES_START)
end
return true
end
function change_gridKey_layout(new_layout_mode, show_notification)
-- gridKeys.layout_mode = (gridKeys.layout_mode == 1) and 2 or 1
new_layout_mode = new_layout_mode or ((gridKeys.layout_mode % 3) + 1)
show_notification = show_notification == nil and true or show_notification
gridKeys.layout_mode = new_layout_mode
if gridKeys.layout_mode == 3 then
gridKeys:zero_vertical_offset()
end
if gridKeys.sound_mode == 1 then
all_engine_notes_off()
elseif gridKeys.sound_mode == 2 then
all_midi_notes_off(gridKeys.midi_device)
elseif gridKeys.sound_mode == 3 then
all_engine_notes_off()
end
if show_notification then
show_temporary_notification("Grid Layout = "..Q7GridKeys.layout_names[gridKeys.layout_mode])
end
end
function change_grid_page(newMode)
showTrigPage = false -- hide trig page
held_step = 0
-- GridPlay, GridSeq, GridSeqVel, GridSeqNoteLengths
if newMode == "GridPlay" then
config.grid_page_index = 1
current_grid_page = grid_pages[config.grid_page_index]
gridSeq.step_edit = false
if gridType == gridType_64 then
gridKeys:resize_grid(1,1,8,7)
else
gridKeys:resize_grid(1,1,16,7)
end
gridSeq:clear_step_edit()
gridSeq.edit_mode = seqmode_select
gridKeys.enable_key_playback = true
show_temporary_notification("Grid Play")
elseif newMode == "GridPatLaunch" then
config.grid_page_index = 2
current_grid_page = grid_pages[config.grid_page_index]
gridSeq.step_edit = false
-- gridKeys:resize_grid(1,2,14,6)
gridSeq:clear_step_edit()
gridSeq.edit_mode = seqmode_select
gridKeys.enable_key_playback = false
gridSeq:select_step(gridSeq.current_step)
show_temporary_notification("Pattern Launch")
elseif newMode == "GridSeq" then
config.grid_page_index = 3
current_grid_page = grid_pages[config.grid_page_index]
gridSeq.step_edit = true
if gridType == gridType_64 then
gridKeys:resize_grid(1,2,8,6)
else
gridKeys:resize_grid(1,2,14,6)
end
gridSeq:select_step(gridSeq.current_step)
if gridSeq.is_playing then
gridKeys.enable_key_playback = false
end
show_temporary_notification("Edit Seq")
elseif newMode == "GridSeqVel" then
config.grid_page_index = 4
current_grid_page = grid_pages[config.grid_page_index]
gridSeq.step_edit = true
gridSeq:clear_step_edit()
gridSeq.edit_mode = seqmode_select
gridKeys.enable_key_playback = false
show_temporary_notification("Edit Vel")
elseif newMode == "GridSeqNoteLengths" then
config.grid_page_index = 5
current_grid_page = grid_pages[config.grid_page_index]
gridSeq.step_edit = true
gridSeq:clear_step_edit()
gridSeq.edit_mode = seqmode_select
gridKeys.enable_key_playback = false
show_temporary_notification("Edit Note Length")
end
end
function screenshot()
if not util.file_exists(screenshot_path) then
util.make_dir(screenshot_path)
end
local screenNumber = 1
local screenPath = screenshot_path.."screenshot_"..screenNumber..".png"
while util.file_exists(screenPath) do
screenNumber = screenNumber + 1
screenPath = screenshot_path.."screenshot_"..screenNumber..".png"
end
_norns.screen_export_png(screenPath)
show_temporary_notification("Screenshot saved")
end
function grid_screenshot()
if _GRID_CAPTURE then
if not util.file_exists(screenshot_path) then
util.make_dir(screenshot_path)
end
local screenNumber = 1
local screenPath = screenshot_path.."grid_screenshot_"..screenNumber..".png"
while util.file_exists(screenPath) do
screenNumber = screenNumber + 1
screenPath = screenshot_path.."grid_screenshot_"..screenNumber..".png"
end
gcap:screenshot(screenPath)
show_temporary_notification("Screenshot saved")
end
end
function grid_record()
if _GRID_CAPTURE then
if not util.file_exists(screenshot_path) then
util.make_dir(screenshot_path)
end
local screenNumber = 1
local screenPath = screenshot_path.."grid_record_"..screenNumber..".gif"
while util.file_exists(screenPath) do
screenNumber = screenNumber + 1
screenPath = screenshot_path.."grid_record_"..screenNumber..".gif"
end
gcap:record(24, 5, screenPath)
show_temporary_notification("Gif saved")
end
end
function grid_key_pushed(gKeys, noteNum, vel)
if gKeys.sound_mode == 3 then -- internal kit
if _TIMBER_ENGINE then
local sample_id = KIT_SAMPLES_START + noteNum
change_sample_id(sample_id)
end
end
end
function gridSeq_pat_changed(gSeq, gKeys, newPatern)