-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl-looper.lua
1747 lines (1473 loc) · 43.3 KB
/
repl-looper.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
-- repl-looper v0.5.1
-- Record/play code loops!
--
-- llllllll.co/t/repl-looper
--
-- Use with keyboard+grid
--
-- ███████████
-- █ █
-- ███ █
-- █ █ █
-- █ ███
-- █ █
-- ███████████
--
-- Add repl-looper lib dir in to load .so files like cjson.so
if not string.find(package.cpath, "/home/we/dust/code/repl-looper/lib/", 1, true) then
package.cpath = package.cpath .. ";/home/we/dust/code/repl-looper/lib/?.so"
package.path = package.path .. ";/home/we/dust/code/repl-looper/lib/?.lua"
end
JSON = require("cjson")
Deque = require("container.deque")
UI = require("ui")
comp = require("completion")
-- Locally augmented libraries
Lattice = require("repl-looper/lib/lattice") -- system one is broken
musicutil = include("repl-looper/lib/musicutil_extended")
sequins = include("repl-looper/lib/sequins_extended")
-- Local helpers
local helper = include("repl-looper/lib/helper")
ls = helper.ls
eval = helper.eval
keys = helper.tabkeys
-- ALL helper
-- Use like all{a,b,c}:stop()
-- Which is equivalent to a:stop();b:stop();c:stop()
all = include("repl-looper/lib/all")
-- Enable/Disable global debugging
function bug(...)
if true then
print(...)
end
end
---------------------------------------------------------------------
-- Grid Wrapper -----------------------------------------------------
---------------------------------------------------------------------
local Grid = {}
Grid.__index = Grid
function Grid.new(key_handler)
local self = {
device = grid.connect(),
data = {}
}
self.device.key = key_handler
-- Initialize data based on grid cols/rows
for x = 1, 16 do
self.data[x] = {}
for y = 1, 8 do
self.data[x][y] = 0
end
end
setmetatable(self, Grid)
return self
end
function Grid:refresh()
self.device:refresh()
redraw()
end
function Grid:led(x, y, brightness)
self.device:led(x, y, brightness)
self.data[x] = self.data[x] or {}
self.data[x][y] = brightness
end
function Grid:all(brightness)
self.device:all(brightness)
self.data = {}
for x = 1, 16 do
self.data[x] = {}
for y = 1, 8 do
self.data[x][y] = 0
end
end
end
---------------------------------------------------------------------
-- Editor -----------------------------------------------------------
---------------------------------------------------------------------
local Editor = {}
Editor.__index = Editor
function Editor.new()
local self = {
content = "",
draw_at_x = 0,
draw_at_y = 62,
cursor = 1
}
setmetatable(self, Editor)
return self
end
function Editor:redraw()
-- This allows us to have a bottom-aligned editor
local height = self:draw_wrapped_content(0, 0, false)
self:draw_wrapped_content(0, self.draw_at_y - height, true)
return height
end
function Editor:draw_wrapped_content(start_x, start_y, do_draw)
-- Draw characters one at a time based on width
-- And then reverse the currect character if it is where the cursor is
-- screen.text draws from the lower-left corner for some weird reason, that's
-- weird. I guess we'll call this method twice, first to get the height and the second to draw the content
local content = self.content .. " " -- Add a space so we can see the cursor
local x = start_x
local y = start_y
for i = 1, #content do
local char = content:sub(i, i)
local char_width = screen.text_extents(char)
if char_width == 0 then
char_width = 4
end
if x + char_width > 127 then
x = 0
y = y + 8
end
if do_draw then
screen.move(x, y)
if i == self.cursor then
screen.level(15)
screen.text(char)
self:invert_rect(x, y-7, char_width, 9)
else
screen.level(15)
screen.text(char)
end
end
x = x + char_width + 1
end
return y
end
function Editor:invert_rect(x, y, width, height)
local rect = screen.peek(x, y, width, height)
local out = {}
for i = 1, #rect do
out[i] = string.char(15 - string.byte(rect, i))
end
screen.poke(x, y, width, height, table.concat(out))
end
function Editor:wrap_text(text, max_len)
local lines = {}
local remaining_text = text
local current_line = ""
local character_number = 1
while #remaining_text > 0 do
local next_character = remaining_text:sub(1, 1)
local current_line_size = screen.text_extents(current_line .. next_character)
if current_line_size > max_len then
table.insert(lines, current_line)
current_line = ""
end
current_line = current_line .. next_character
remaining_text = remaining_text:sub(2)
character_number = character_number + 1
end
table.insert(lines, current_line)
return lines
end
function Editor:insert(char)
self.content = self.content:sub(1, self.cursor - 1) .. char .. self.content:sub(self.cursor)
self.cursor = self.cursor + 1
end
function Editor:backspace()
if self.cursor > 1 then
self.content = self.content:sub(1, self.cursor - 2) .. self.content:sub(self.cursor)
self.cursor = self.cursor - 1
end
end
function Editor:delete()
if self.cursor <= #self.content then
self.content = self.content:sub(1, self.cursor - 1) .. self.content:sub(self.cursor + 1)
end
end
function Editor:move_cursor_left()
self.cursor = math.max(1, self.cursor - 1)
end
function Editor:move_cursor_right()
self.cursor = math.min(#self.content + 1, self.cursor + 1)
end
function Editor:move_cursor_to_start()
self.cursor = 1
end
function Editor:move_cursor_to_end()
self.cursor = #self.content + 1
end
function Editor:clear()
self.content = ""
self.cursor = 1
end
function Editor:set_content(new_content)
self.content = new_content
self.cursor = #new_content + 1
end
-- Single global editor for all to use
editor = Editor.new()
---------------------------------------------------------------------
-- Loop-related objects ---------------------------------------------
---------------------------------------------------------------------
local Event = {}
Event.__index = Event
Event.last_id = 0
function Event.new(init)
local self = init or {
command = "",
relative_time = 0,
pulse_offset = 0,
step = 0
}
setmetatable(self, Event)
Event.last_id = Event.last_id + 1
self.id = Event.last_id
return self
end
function Event:lua()
return {
-- pulse = self.pulse, -- The absolute time
-- pulse_offset = self.pulse_offset, -- loop relative time
-- phase = self.sprocket and self.sprocket.phase, -- current countdown in pulses
step = self.step,
command = self.command
}
end
current_context_loop_id = 0
function Event:eval(context_loop_id, from_playing_loop)
current_context_loop_id = context_loop_id
-- print("Set context loop id to", current_context_loop_id)
local result = live_event(self.command, from_playing_loop)
-- print("Reset context loop id to 0")
current_context_loop_id = 0
return result
end
function Event:destroy()
self.sprocket:destroy()
end
function Event:clone()
return Event.new({
pulse = self.pulse,
command = self.command
})
end
---------------------------------------
Loop = {}
Loop.__index = Loop
Loop.last_id = 0
function Loop.new(init)
local self = init or {
events = {},
off_events = {},
loop_length_qn = 16,
current_step = 1,
current_substep = 1.0,
duration = 10212,
lattice = Lattice:new{},
record_feedback = false,
auto_quantize = false,
send_feedback = false,
stop_next = false,
mods = {
amp = 1,
ampLag = 0,
pan = 0
},
selected = false,
mode = "stop"
}
setmetatable(self, Loop)
Loop.last_id = Loop.last_id + 1
self.id = Loop.last_id
-- Register with global list of loops
loops[self.id] = self
-- Kinda evil shortcut!
-- for loops 1..8 make global var 'a' .. 'h'
if self.id < 9 then
self.loop_letter = string.char(string.byte("a") + self.id - 1)
_G[self.loop_letter] = self
end
-- Basically a quarter-note metronome
-- This is used to update the grid and client
self.status_sprocket = self.status_sprocket or self.lattice:new_sprocket {
action = function(t)
self:send_status(t)
end,
division = 1/4
}
return self
end
function Loop:get_current_step(t)
t = t or self.lattice.transport
return (math.floor(t / self.lattice.ppqn) % self.loop_length_qn) + 1
end
function Loop:get_current_substep(t)
t = t or self.lattice.transport
return ((t / self.lattice.ppqn) % self.loop_length_qn) + 1
end
function Loop:select()
self.selected = true
self:draw_grid_row()
end
function Loop:deselect()
self.selected = false
self:draw_grid_row()
end
function Loop:send_status(t)
t = t or 0
clock.sleep(0.001) -- Allow other things to run
self.current_step = self:get_current_step(t)
self:draw_grid_row()
messageFromServer {
action = "playback_step",
step = self.current_step,
stepCount = self.loop_length_qn,
loop_id = self.id,
command = self.recent_command,
mode = self.mode,
loop_letter = self.loop_letter
}
end
function Loop:qn_per_ms()
return clock.get_tempo() / 60 / 1000
end
function Loop:pulse_per_ms()
return self:qn_per_ms() * self.lattice.ppqn
end
function Loop:pulse_per_measure()
return self.lattice.ppqn * 4
end
function Loop:loop_length_measure()
return self.loop_length_qn / 4
end
function Loop:loop_length_pulse()
return self.loop_length_qn * self.lattice.ppqn
end
function Loop:setLength(qn)
self.loop_length_qn = qn
self:update_lattice()
end
function Loop:update_event(event, off_event)
event.pulse_offset = event.pulse % self:loop_length_pulse()
event.step = event.pulse_offset / self.lattice.ppqn + 1
if off_event then
-- Don't actually schedule grid-key-off events
return
end
if self.auto_quantize then
event.step = math.floor(event.step + 0.5) -- nearest whole step
event.pulse_offset = (event.step - 1) * self.lattice.ppqn
end
local action = function(t)
self.recent_command = event.command
-- Update current step (fractional) and see if we should stop
local substep = self:get_current_substep(t)
if self.stop_next and self.current_substep > substep then
self:stop()
self:setStep(1) -- Reset back to the top of the loop (we might already be there a little)
else
-- We don't need to stop, so go ahead and execute the command!
event:eval(self.id, not self.send_feedback) -- `true` to indicate we are a playback event
end
self.current_substep = substep
end
event.sprocket = event.sprocket or self.lattice:new_sprocket{}
event.sprocket:set_action(action)
event.sprocket:set_division(self:loop_length_measure()) -- division is in measures
-- Forcing the phase is what sets the actual offset for this sprocket based on
-- the current transport (play position). The phase is a count-down until the
-- event is fired
local loop_phase = self.lattice.transport % self:loop_length_pulse()
local phase_distance = (event.pulse_offset - loop_phase) % self:loop_length_pulse()
event.sprocket.phase = self:loop_length_pulse() - phase_distance
end
-- Update all events; this is handy for re-working the loop length
function Loop:update_lattice()
for _, event in ipairs(self.events) do
self:update_event(event)
end
end
function Loop:setStep(step)
step = step or 1
step = step - 1
step = step % self.loop_length_qn
self.lattice.transport = step * self.lattice.ppqn
self.current_step = self:get_current_step()
self:update_lattice()
self:draw_grid_row()
end
function Loop:remove_event(event_to_remove)
-- Go backwards so that when we remove nothing weird happens
for i = #self.events, 1, -1 do
local event = self.events[i]
if event == event_to_remove then
event.sprocket:destroy()
event.sprocket = nil
table.remove(self.events, i)
end
end
end
function Loop:add_event(event)
self:update_event(event)
table.insert(self.events, event)
return event
end
-- Let's get some GRID!!
function Loop:draw_grid_row()
clear_grid_row(self.id)
local row = self:to_grid_row()
for n = 1, 16 do
-- Highlight currently selected row
local val = row[n] or 0
if self.selected then
val = util.clamp(val + 2, 0, 15)
end
-- Mod-16 so we can show long sequences overlaid; maybe confusing
grid_device:led((((n-1) % 16)+1), self.id, val)
end
grid_device:refresh()
end
function Loop:lua()
local output = {}
output.current_step = self.current_step
output.loop_length_qn = self.loop_length_qn
output.transport = self.lattice.transport
output.stop_next = self.stop_next
output.mods = self.mods
output.events = {}
for _, event in ipairs(self.events) do
table.insert(output.events, event:lua())
end
output.off_events = {}
for _, event in ipairs(self.off_events) do
table.insert(output.off_events, event:lua())
end
return output
end
function Loop:show()
return self:lua()
end
function Loop:to_grid_row()
local row = {}
-- Basically zoom-out for longer loops, but still in increments of 16
-- This is might be weird for non-powers-of-2
local div = math.floor((self.loop_length_qn - 1) / 16) + 1
-- Highlight the current step even if we're not
-- on an event; all the rest are dark by default
for n = 1, self.loop_length_qn do
if math.ceil(n/div) == math.ceil(self.current_step/div) then
row[math.ceil(n/div)] = 10
else
row[math.ceil(n/div)] = 0
end
end
-- Entries with events glow. Event+current glow a lot
for _, event in ipairs(self.events) do
local visual_step = math.ceil(math.floor(event.step)/div)
if visual_step == math.ceil(self.current_step/div) then
row[visual_step] = 15
else
row[visual_step] = 5
end
end
return row
end
function Loop:play_events_at_step(step)
for _, event in ipairs(self.events) do
local event_step = math.floor(event.step)
if event_step == step then
event:eval(self.id)
end
end
end
function Loop:play_off_events_at_step(step)
for _, event in ipairs(self.off_events) do
local event_step = math.floor(event.step)
if event_step == step then
event:eval(self.id)
end
end
end
function Loop:commands_at_step(step)
local commands = {}
for _, event in ipairs(self.events) do
local event_step = math.floor(event.step)
if event_step == step then
table.insert(commands, event.command)
end
end
return commands
end
function Loop:toggle_commands_at_step(step, commands)
print("toggle_commands_at_step: ", step)
local found_commands = false
for i = #self.events, 1, -1 do
local event = self.events[i]
local event_step = math.floor(event.step)
if event_step == step then
if self.events[i].sprocket then
self.events[i].sprocket:destroy()
end
table.remove(self.events, i)
found_commands = true
end
end
if not found_commands then
for _, command in ipairs(commands) do
local new_event = Event.new({
command = command,
pulse = step * self.lattice.ppqn
})
table.insert(self.events, new_event)
self:update_event(new_event)
end
end
end
function Loop:play(startStep)
self:yesLoop()
if startStep then
self:setStep(startStep)
end
self.mode = "play"
self.lattice:start()
end
function Loop:stop()
if self.mode == "recording" then
self.mode = "stop_recording"
self:draw_grid_row()
elseif self.mode == "sample_recording" then
self.mode = "stop_recording"
stop_record_sample()
self:draw_grid_row()
else
self.mode = "stop"
self.lattice:stop()
end
end
function Loop:noLoop()
self.stop_next = true
end
function Loop:once()
self:noLoop()
self:play(1)
end
function Loop:yesLoop()
self.stop_next = false
end
function Loop:rec()
self.mode = "start_recording"
self.lattice:start()
end
-- Record to one step and then go to the next
-- This is good for thoughtful recording and macro recording
function Loop:stepRec()
self.mode = "start_recording_step"
end
function Loop:nextStep()
self:setStep(self:get_current_step() + 1)
end
function Loop:prevStep()
self:setStep(self:get_current_step() - 1)
end
function Loop:sampleRec()
self.mode = "sample_recording"
select_nth_loop(self.id)
start_record_sample()
end
function Loop:add_event_command(cmd)
local event = Event.new({
pulse = self.lattice.transport,
command = cmd
})
self:update_event(event)
table.insert(self.events, event)
return event
end
-- Loop Manipulation
--------------------
-- Do a one-time permanent quantize
function Loop:quantize()
for _, event in ipairs(self.events) do
event.step = math.floor(event.step + 0.5) -- nearest whole step
-- Update the absolute-time pulse
event.pulse = (event.step - 1) * self.lattice.ppqn
end
self:update_lattice()
end
function Loop:align(other_loop)
local substep_diff = self:get_current_substep() - other_loop:get_current_substep()
local transport_diff = substep_diff * self.lattice.ppqn
self:shift(-1 * transport_diff)
end
function Loop:shift(transport_diff)
self.lattice.transport = self.lattice.transport + transport_diff
for _, event in ipairs(self.events) do
event.pulse = event.pulse - transport_diff
end
self.current_step = self:get_current_step()
self:update_lattice()
self:draw_grid_row()
end
-- Copy all of the events to another loop
function Loop:clone(other_loop)
other_loop:clear()
for _, event in ipairs(self.events) do
other_loop:add_event(event:clone())
end
self:draw_grid_row()
end
-- a:gen("CH") puts the "CH" function on every step
-- a:gen("CH", 1/2) puts the "CH" on every half step
-- a:gen("CH", 2, 2) puts the "CH" on every other step starting with step 2
-- a:gen("CH", { 1, 3, 4.5 }) puts the "CH" on the given steps (even fractional)
-- a:gen({"BD","SD","CP"}) puts each one on each step
-- a:gen("molly:note(`50+m`)", "molly:offNote(`50+m`)") off notes during grid-play
function Loop:gen(code_string, modification, offset)
if type(code_string) == "table" then
for n, cmd in ipairs(code_string) do
local event = Event.new({
pulse = (n - 1) * self.lattice.ppqn,
command = cmd
})
self:update_event(event)
table.insert(self.events, event)
end
elseif type(modification) == "table" then
for _, n in ipairs(modification) do
local expanded_code_string =
string.gsub(
code_string,
"`([^`]+)`",
function (snippet)
local injected_snippet = "local n = dynamic('n'); local m = n - 1; return " .. snippet
return eval(injected_snippet)
end
)
local event = Event.new({
pulse = (n - 1) * self.lattice.ppqn,
command = expanded_code_string
})
self:update_event(event)
table.insert(self.events, event)
end
elseif type(modification) == "number" then
offset = offset or 1
for step = 1, (self.loop_length_qn / modification) do
local n = (step - 1) * modification + offset
local expanded_code_string =
string.gsub(
code_string,
"`([^`]+)`",
function (snippet)
local injected_snippet = "local step = dynamic('step'); local n = dynamic('n'); local m = n - 1; return " .. snippet
return eval(injected_snippet)
end
)
local event = Event.new({
pulse = (n - 1) * self.lattice.ppqn,
command = expanded_code_string
})
self:update_event(event)
table.insert(self.events, event)
end
else
for n = 1, self.loop_length_qn do
local expanded_code_string =
string.gsub(
code_string,
"`([^`]+)`",
function (snippet)
local injected_snippet = "local n = dynamic('n'); local m = n - 1; return " .. snippet
return eval(injected_snippet)
end
)
local event = Event.new({
pulse = (n - 1) * self.lattice.ppqn,
command = expanded_code_string
})
self:update_event(event)
table.insert(self.events, event)
-- Look for key-off modification
if modification then
local expanded_code_string =
string.gsub(
modification,
"`([^`]+)`",
function (snippet)
local injected_snippet = "local n = dynamic('n'); local m = n - 1; return " .. snippet
return eval(injected_snippet)
end
)
local event = Event.new({
pulse = (n - 1) * self.lattice.ppqn,
command = expanded_code_string
})
self:update_event(event, true)
table.insert(self.off_events, event)
end
end
end
self:draw_grid_row()
end
-- Shorthand to put some code at a specific step
function Loop:put(step, code_string)
self:gen(code_string, { step })
end
function Loop:slice(sample, step_offset, step_count, reverse)
step_offset = step_offset or 1
bug("Current tempo", clock.get_tempo())
bug("Current qn_per_ms", self:qn_per_ms())
width = math.ceil(48000 / (self:qn_per_ms() * 1000)) -- 29090
bug("Slice width", width)
-- We'll name this like ls1 ls2 ls3 etc
-- like "loop_sample_1"
-- Then we can still use it by-name
local sample_name = "ls" .. self.id
_G[sample_name] = sample
local frame_offset = (step_offset - 1) * width
local slice_start = width .. "* m + " .. frame_offset
-- local slice_end = width .. "* n + " .. (frame_offset + 10)
local slice_end = width .. "* n + " .. (frame_offset + 1000)
if reverse then
local num_frames = sample:info().num_frames
slice_start = num_frames .. " - (" .. slice_start .. ")"
slice_end = num_frames .. " - (" .. slice_end .. ")"
end
-- If this is less than a whole loop, cut the loop length
step_count = step_count or math.ceil((sample:info().num_frames - frame_offset) / width)
self:setLength(step_count)
bug("slice step_count", step_count)
self:gen(sample_name .. ":play(`" .. slice_start .. "`, `" .. slice_end .. "`)")
bug("slice gen", sample_name .. ":play(`" .. slice_start .. "`, `" .. slice_end .. "`)")
-- self:gen(
-- sample_name .. ":startFrame(`" .. slice_start .. "`);"
-- .. sample_name .. ":loopStartFrame(`" .. slice_start .. "`);"
-- .. sample_name .. ":loopEndFrame(`" .. slice_end .. "`);"
-- .. sample_name .. ":endFrame(`" .. slice_end .. "`);"
-- .. sample_name .. ":play()"
-- )
end
function Loop:fill(sample, step_count)
local step_count = step_count or 16 -- quarter notes
local frame_count = sample:info().num_frames
bug("Fill frame_count", frame_count)
local length_seconds = frame_count / 48000 -- 48k sample rate
local bpm = step_count / length_seconds * 60
bug("Fill set BPM", bpm)
params:set("clock_tempo", bpm)
-- Run other callbacks
clock.sleep(0.1)
self:slice(sample)
end
function Loop:clear()
for _, event in ipairs(self.events) do
event:destroy()
end
self.events = {}
self.off_events = {}
self:draw_grid_row()
end
function Loop:merge(other_loop)
print "Aligning loops"
other_loop:align(self)
-- clock.sleep(0.001)
for _, event in ipairs(other_loop.events) do
self:add_event(event:clone())
end
other_loop:clear()
other_loop:stop()
self:draw_grid_row()
end
function Loop:split(other_loop, base_command)
if #self.events < 2 then
return
end
clock.run(function()
print "Gathering all events"
local events = {}
for _, event in ipairs(self.events) do
table.insert(events, event:clone())
clock.sleep(0.001)
end
-- Do we need to stop? Eh
-- print "Stopping both loops"
-- self:stop()
-- other_loop:stop()
print "Clearing both loops"
self:clear()
other_loop:clear()
print "Copying settings"
other_loop.loop_length_qn = self.loop_length_qn
other_loop.lattice.transport = self.lattice.transport
other_loop.current_step = self.current_step
print "Calculating event distances"
base_command = base_command or events[1].command
local distances = {}
local total_dist = 0
for _, event in ipairs(events) do
local dist = helper.leven(base_command, event.command)
total_dist = total_dist + dist
-- for debugging and to minimize sort calcs
distances[event.command] = dist
end
print "Sorting by distance"
table.sort(events, function(a, b)
local a_dist = distances[a.command]
local b_dist = distances[b.command]
return a_dist < b_dist
end)
local mean_dist = total_dist / #events
print "Adding events back"
for _, event in ipairs(events) do
local event_dist = distances[event.command]
if event_dist <= mean_dist then
self:add_event(event)
else
other_loop:add_event(event)
end
clock.sleep(0.001)
end
print "Redrawing grid"
self:draw_grid_row()
other_loop:draw_grid_row()
if self.mode == "play" then
print "We are playing so split should play too"
other_loop:play()
end
print "DONE!"
-- return {
-- mean_dist = mean_dist,
-- distances = distances
-- }
end)
end
-- Loops as tracks
function Loop:updateTrack()
engine.trackMod(
self.id,
self.mods.amp,
self.mods.ampLag,
self.mods.pan
)
end
function Loop:amp(amp, ampLag)
self.mods.ampLag = ampLag or 0
self.mods.amp = amp
self:updateTrack()