-
Notifications
You must be signed in to change notification settings - Fork 2
/
abacus.lua
1107 lines (1028 loc) · 28.4 KB
/
abacus.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
-- abacus v0.2.0
-- sequence rows of beats
-- with samples.
--
-- llllllll.co/t/abacus
--
--
-- ▼ instructions below ▼
-- K1+E1 changes mode
-- K1+K3 starts/stops chain
--
-- sample mode
-- E1 changes sample
-- E2/E3 change splice position
-- K1+K3 starts/stops chain
-- K2 zooms
-- K3 plays sample (can hold)
--
-- pattern mode
-- E1 changes pattern
-- E2 selects sample
-- E3 positions sample
-- K2 patterns
-- K3 plays sample
-- K1+K2 erases position
-- K1+K3 plays pattern
--
-- chain mode
-- E2 positions
-- E3 selects pattern
-- K2/K3 does effects
json=include("lib/json")
local ControlSpec=require 'controlspec'
local Formatters=require 'formatters'
--
-- globals
--
-- user state
us={
mode=0,-- 0=sampler,1=pattern,2==chain
shift=false,
update_ui=false,
zoomed=false,
message='',
available_files={},
waveform_samples={},
waveform_view={0,0},
interval=0,
scale=0,
sample_cur=1,
pattern_cur=1,
chain_cur=1,
pattern_temp={start=1,length=1},
playing=false,-- is playing or not
playing_sample={0,0},-- width of sample being played
playing_beat=0,-- current
playing_chain=1,
playing_pattern=0,-- current pattern
playing_once=0,
playing_pattern_segment=0,-- current sample pattern (sample id + random int decimal)
playing_loop_end=0,
playing_position=0,
playing_sampleid=0,
samples_usable={},
samples_usable_id=1,
effect_on=false,
effect_stutter=false,
effect_reverse=false,
one_shot=false,
crow_sample_cur=0,
crow_position=0,
crow_gate=0,
}
-- user parameters
-- put things that can be saved
-- don't put things here that can be put into global parameters
up={
filename_save='1.json',
filename='',
length=0,
rate=1,
samples={},
patterns={},
chain={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
}
-- user constants
uc={
update_timer_interval=0.05,
audio_dir=_path.audio..'abacus/',
tape_dir=_path.audio..'tape/',
code_dir=_path.code..'abacus/',
data_dir=_path.data..'abacus/',
}
--
-- initialization
--
function init()
-- determine which files are available
-- us.available_files={'amenbreak.wav'}
-- us.available_saves={''}
-- create data directory if it doesn't exist
-- and move the code audio to the data directory
if not util.file_exists(uc.data_dir..'Amen-break.wav') then
print("making data directory")
util.make_dir(uc.data_dir)
local f=io.popen('cp '..uc.code_dir..'sounds/* '..uc.data_dir)
end
-- load files from tape/data direcotry
local files={}
local files_fullpath={}
local previous_files={}
local previous_files_fullpath={}
local f=io.popen('cd '..uc.tape_dir..'; ls -d *')
for name in f:lines() do
if string.match(name,".wav") then
table.insert(files,name:match("^(.+).wav$"))
table.insert(files_fullpath,uc.tape_dir..name)
end
end
f=io.popen('cd '..uc.data_dir..'; ls -d *')
for name in f:lines() do
if string.match(name,".wav") and not string.match(name,".json")then
table.insert(files,name:match("^(.+).wav$"))
table.insert(files_fullpath,uc.data_dir..name)
end
if string.match(name,".wav.json") then
table.insert(previous_files,name:match("^(.+).wav.json$"))
table.insert(previous_files_fullpath,uc.data_dir..name)
end
end
table.sort(files)
table.sort(previous_files)
print(files[1])
local chosen_file=''
for i,f in ipairs(files_fullpath) do
-- https://stackoverflow.com/questions/48402876/getting-current-file-name-in-lua/48403164
if get_file_name(f)==files[1]..".wav" then
chosen_file=f
print("chosen_file "..chosen_file)
break
end
end
local previous_chosen_file=''
for i,f in ipairs(previous_files_fullpath) do
-- https://stackoverflow.com/questions/48402876/getting-current-file-name-in-lua/48403164
if get_file_name(f)==previous_files[1]..".wav.json" then
previous_chosen_file=f
print("previous_chosen_file "..previous_chosen_file)
break
end
end
print("previous_files: ")
print(previous_files[1])
local specs={}
specs.AMP=ControlSpec.new(0,1,'lin',0,1,'')
specs.FILTER_FREQ=ControlSpec.new(20,20000,'exp',0,20000,'Hz')
specs.TIME=ControlSpec.new(0,5,'lin',0,1,'s')
specs.FILTER_RESONANCE=ControlSpec.new(0.05,1,'lin',0,0.25,'')
specs.PERCENTAGEADD=ControlSpec.new(-1,1,'lin',0.01,0,'%')
specs.PERCENTAGE=ControlSpec.new(0,1,'lin',0.01,0,'%')
params:add_separator("abacus")
params:add_group("save/load",4)
params:add {
type='option',
id='load_sample',
name='choose sample',
options=files,
action=function(value)
for i,f in ipairs(files_fullpath) do
-- https://stackoverflow.com/questions/48402876/getting-current-file-name-in-lua/48403164
if get_file_name(f)==files[value]..".wav" then
chosen_file=f
break
end
end
end
}
params:add {
type='trigger',
id='load_loops',
name='load sample',
action=function(value)
initialize_samples()
load_sample(chosen_file)
end
}
params:add {
type='option',
id='load_previous',
name='choose previous',
options=previous_files,
action=function(value)
for i,f in ipairs(previous_files_fullpath) do
-- https://stackoverflow.com/questions/48402876/getting-current-file-name-in-lua/48403164
if get_file_name(f)==previous_files[value]..".wav.json" then
previous_chosen_file=f
break
end
end
end
}
params:add {
type='trigger',
id='open_previous',
name='load previous',
action=function(value)
initialize_samples()
parameters_load(previous_chosen_file)
end
}
params:add_group("effects",6)
params:add{
type='control',
id='global_rate',
name='global rate',
controlspec=specs.PERCENTAGEADD,
formatter=Formatters.percentage,
action=function(x)
for i=1,4 do
softcut.rate(i,up.rate+x)
end
end
}
params:add{
type='control',
id='effect_stutter',
name='effect stutter',
controlspec=specs.PERCENTAGE,
formatter=Formatters.percentage,
}
params:add{
type='control',
id='effect_reverse',
name='effect reverse',
controlspec=specs.PERCENTAGE,
formatter=Formatters.percentage,
}
params:add{
type='control',
id='effect_slow',
name='effect slow',
controlspec=specs.PERCENTAGE,
formatter=Formatters.percentage,
}
params:add {
type='control',
id='filter_frequency',
name='filter cutoff',
controlspec=specs.FILTER_FREQ,
formatter=Formatters.format_freq,
action=function(value)
for i=1,4 do
softcut.post_filter_fc(i,value)
end
end
}
params:add {
type='control',
id='filter_reso',
name='filter resonance',
controlspec=specs.FILTER_RESONANCE,
action=function(value)
for i=1,4 do
softcut.post_filter_rq(i,value)
end
end
}
-- TODO: add individual parameters for pitching up/down specific samples
params:add_group("crow",4)
params:add {
type='option',
id='crow_mode',
name='crow mode',
options={"free","samples"},
action=function(value)
if value == 1 then
softcut.loop(4,1)
softcut.loop_start(4,0)
softcut.loop_end(4,up.length)
end
end
}
params:add {
type='control',
id='gate_voltage',
name='gate threshold',
controlspec=ControlSpec.new(-5,10,'lin',0.01,2,'volts'),
action=function(value)
crow.input[1].mode("change",value,0.25,"both")
end
}
params:add {
type='option',
id='crow_gating',
name='gating',
options={"once","continuous"},
action=function(value)
if value == 1 then
softcut.loop(4,0)
else
softcut.loop(4,1)
end
end
}
params:add {
type='control',
id='crow_slew1',
name='crow slew',
controlspec=specs.TIME,
action=function(value)
softcut.rate_slew_time(4,value)
softcut.level_slew_time(4,value)
end
}
initialize_samples()
-- initialize softcut
for i=1,4 do
softcut.enable(i,1)
softcut.level(i,1)
softcut.pan(i,0)
softcut.rate(i,1)
softcut.loop(i,0)
softcut.rec(i,0)
softcut.buffer(i,1)
softcut.position(i,0)
softcut.level_slew_time(i,clock.get_beat_sec()/4)
softcut.rate_slew_time(i,clock.get_beat_sec()/4)
softcut.post_filter_dry(i,0.0)
softcut.post_filter_lp(i,1.0)
softcut.post_filter_rq(i,0.3)
softcut.post_filter_fc(i,44100)
end
softcut.level(3,0)
softcut.play(3,1)
softcut.loop(4,1)
softcut.phase_quant(1,0.025)
softcut.event_render(update_render)
-- position poll
softcut.event_phase(update_positions)
softcut.poll_start_phase()
-- update clocks
clock.run(update_beat)
-- initialize timer for updating screen
timer=metro.init()
timer.time=uc.update_timer_interval
timer.count=-1
timer.event=update_timer
timer:start()
-- initialize crow
crow.input[1].change = process_change
crow.input[1].mode("change",2.0,0.25,"both")
crow.input[2].stream = process_stream
crow.input[2].mode("stream",0.1)
parameters_load(uc.data_dir.."play.json")
end
function initialize_samples()
up={
filename_save='1.json',
filename='',
length=0,
rate=1,
samples={},
patterns={},
chain={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
}
-- initialize samples
local alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for i=1,26 do
up.samples[i]={}
up.samples[i].start=0
up.samples[i].length=0
up.samples[i].name=alphabet:sub(i,i)
end
for i=1,8 do
up.patterns[i]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
end
end
--
-- updaters
--
function update_positions(i,x)
-- adjust position so it is relative to loop start
if i==1 then
us.playing_position=x
end
end
function update_render(ch,start,i,s)
us.waveform_samples=s
us.interval=i
us.update_ui=true
end
function update_timer()
if us.update_ui then
redraw()
end
end
function update_beat()
local current_voice=1
local p=up.patterns[1]
local current_level=0
local is_slowing = false
while true do
clock.sync(1/4)
if us.playing==false then
if current_level==1 then
current_level=0
softcut.level(1,0)
end
goto continue
end
clock.run(function()
us.playing_beat=us.playing_beat+1
if us.playing_beat>16 then
if us.playing_once==2 then
print("playing once!")
us.playing_once=1
elseif us.playing_once==1 then
us.playing_once=0
us.playing=false
return
else
-- iterate through chain
us.playing_chain=us.playing_chain+1
if us.playing_chain>#up.chain or up.chain[us.playing_chain]==0 then
us.playing_chain=1
end
us.pattern_cur=up.chain[us.playing_chain]
end
p=up.patterns[us.pattern_cur]
us.playing_beat=1
end
-- if silence, continue
local playing_pattern_segment=p[us.playing_beat]
-- get sample id from the pattern segment
local sample_id=math.floor(playing_pattern_segment)
-- do effects
effect_slow=us.effect_slow or math.random()<params:get("effect_slow")
effect_stutter=us.effect_stutter or math.random()<params:get("effect_stutter")
effect_reverse=us.effect_reverse or math.random()<params:get("effect_reverse")
if effect_slow then
clock.run(function()
is_slowing=true
local slow_time = clock.get_beat_sec()*(math.random(2))
softcut.rate_slew_time(1,slow_time)
softcut.rate(1,0.5*up.rate+params:get("global_rate"))
clock.sleep(slow_time)
softcut.rate(1,up.rate+params:get("global_rate"))
is_slowing = false
end)
elseif (effect_stutter or effect_reverse) and us.playing_once==0 and not is_slowing then
us.effect_on=false
us.effect_stutter=false
us.effect_reverse=false
if us.playing_sampleid>0 then
local pos=up.samples[us.playing_sampleid].start
rate=1
softcut.loop(3,1)
if effect_stutter then
print("stutter")
softcut.loop(3,1)
local stutter_amount=math.random(4)
softcut.loop_end(3,pos+clock.get_beat_sec()/(64.0/stutter_amount))
softcut.loop_start(3,pos-clock.get_beat_sec()/(64.0/stutter_amount))
else
softcut.loop_start(3,us.playing_loop_end)
softcut.loop_end(3,us.playing_loop_end)
end
if effect_reverse then
print("reverse")
rate=-1
end
softcut.rate(3,rate*(up.rate+params:get("global_rate")))
softcut.position(3,pos)
if us.effect_reverse then
for i=1,10 do
softcut.level(3,i/10.0)
softcut.level(1,(10-i)/10.0)
clock.sleep(clock.get_beat_sec()/10)
end
else
softcut.level(3,1)
softcut.level(1,0)
end
clock.sleep(clock.get_beat_sec()/4*(2+math.random(8)))
softcut.level(1,1)
softcut.level(3,0)
end
elseif not us.effect_on then
if sample_id==0 then
if current_level==1 then
current_level=0
softcut.level(1,0)
end
us.playing_pattern_segment=0
us.playing_sample={0,0}
us.playing_sampleid=0
us.update_ui=true
return
end
if playing_pattern_segment==us.playing_pattern_segment then
return
end
us.playing_pattern_segment=playing_pattern_segment
-- play sample
local sample_start=up.samples[sample_id].start
if up.samples[sample_id].start+up.samples[sample_id].length~=us.playing_loop_end then
us.playing_loop_end=up.samples[sample_id].start+up.samples[sample_id].length
-- softcut.loop_end(1,us.playing_loop_end)
end
us.playing_sampleid=sample_id
us.playing_sample={up.samples[sample_id].start,us.playing_loop_end}
softcut.position(1,up.samples[sample_id].start)
if current_level==0 then
current_level=1
softcut.level(1,1)
end
us.update_ui=true
end
end)
::continue::
end
end
function update_parameters()
end
function update_waveform_view(pos1,pos2)
us.waveform_view={pos1,pos2}
-- render new waveform
softcut.render_buffer(1,pos1,pos2-pos1,128)
end
--
-- pattern controls
--
function pattern_stamp(sampleid,start,length)
local p=table.clone(up.patterns[us.pattern_cur])
rvalue=math.random()
for i=start,start+length-1 do
p[i]=sampleid+rvalue
end
return p
end
--
-- sample controls
--
function load_sample(filename)
-- load file
up.filename=filename
up.length,up.rate=load_file(filename)
softcut.loop_start(1,0)
softcut.loop_end(1,up.length)
softcut.loop(1,1)
update_waveform_view(0,up.length)
end
function sample_one_shot(z)
if z==0 then
-- stop looping
us.one_shot=false
softcut.loop(2,0)
us.playing_sample={0,0}
else
us.one_shot=true
sample_one_shot_update()
softcut.loop(2,1)
softcut.play(2,1)
softcut.rate(2,up.rate+params:get("global_rate"))
end
us.update_ui=true
end
function sample_one_shot_update()
if not us.one_shot then do return end end
local s=up.samples[us.sample_cur].start
local e=up.samples[us.sample_cur].start+up.samples[us.sample_cur].length
us.playing_sample={s,e}
softcut.position(2,s)
softcut.loop_start(2,s)
softcut.loop_end(2,e)
us.update_ui=true
end
--
-- save/load
--
function parameters_save()
data=json.encode(up)
write_file(uc.data_dir.."play.json",data)
write_file(uc.data_dir..get_file_name(up.filename)..".json",data)
end
function parameters_load(filename)
print("loading "..filename)
if util.file_exists(filename) then
local f=io.open(filename,"rb")
print(f)
local content=f:read("*all")
up=json.decode(content)
f:close()
load_sample(up.filename)
end
end
--
-- input
--
function enc(n,d)
if n==1 and us.shift then
-- toggle sample/pattern/chain mode
us.mode=util.clamp(us.mode+sign(d),0,2)
if us.mode==1 then
-- figure out which samples are usable
us.samples_usable={}
for i=1,#up.samples do
if up.samples[i].length>0 then
table.insert(us.samples_usable,i)
end
end
us.pattern_temp.length=util.round(up.samples[us.sample_cur].length/(clock.get_beat_sec()/4))
end
elseif n==1 and us.mode==0 then
us.sample_cur=util.clamp(us.sample_cur+sign(d),1,26)
elseif n==1 and us.mode==1 then
-- change pattern
us.pattern_cur=util.clamp(us.pattern_cur+sign(d),1,8)
elseif n==2 and us.mode==0 then
local x=d*up.length/1000
up.samples[us.sample_cur].start=util.clamp(up.samples[us.sample_cur].start+x,0,up.length)
if up.samples[us.sample_cur].length==0 then
up.samples[us.sample_cur].length=clock.get_beat_sec()/4
up.samples[us.sample_cur].start=util.clamp(up.samples[us.sample_cur].start,us.waveform_view[1],up.length)
end
local new_end=up.samples[us.sample_cur].start+up.samples[us.sample_cur].length
if up.samples[us.sample_cur].start<us.waveform_view[1] then
update_waveform_view(up.samples[us.sample_cur].start,us.waveform_view[2]+(up.samples[us.sample_cur].start-us.waveform_view[1]))
elseif new_end>us.waveform_view[2] then
update_waveform_view(us.waveform_view[1]+(new_end-us.waveform_view[2]),up.samples[us.sample_cur].start+up.samples[us.sample_cur].length)
end
sample_one_shot_update()
elseif n==3 and us.mode==0 then
-- local x=d*clock.get_beat_sec()/4
local x=d*up.length/1000
up.samples[us.sample_cur].length=util.clamp(up.samples[us.sample_cur].length+x,0,up.length-up.samples[us.sample_cur].start)
if up.samples[us.sample_cur].start+up.samples[us.sample_cur].length>us.waveform_view[2] then
update_waveform_view(up.samples[us.sample_cur].start,up.samples[us.sample_cur].start+up.samples[us.sample_cur].length)
end
us.pattern_temp.length=util.round(up.samples[us.sample_cur].length/(clock.get_beat_sec()/4))
sample_one_shot_update()
elseif n==2 and us.mode==1 then
us.samples_usable_id=util.clamp(us.samples_usable_id+sign(d),1,#us.samples_usable)
us.sample_cur=us.samples_usable[us.samples_usable_id]
us.pattern_temp.length=util.round(up.samples[us.sample_cur].length/(clock.get_beat_sec()/4))
elseif n==3 and us.mode==1 then
-- change start position
us.pattern_temp.start=util.clamp(us.pattern_temp.start+sign(d),1,16)
us.pattern_temp.length=util.round(up.samples[us.sample_cur].length/(clock.get_beat_sec()/4))
elseif n==2 and us.mode==2 then
local last_chain=1
for i=1,#up.chain do
if up.chain[i]==0 then
last_chain=i
break
end
end
us.chain_cur=util.clamp(us.chain_cur+sign(d),1,last_chain)
elseif n==3 and us.mode==2 then
local last_chain=1
for i=1,#up.chain do
if up.chain[i]==0 then
last_chain=i
break
end
end
min_chain=1
if us.chain_cur>=last_chain-1 then
min_chain=0
end
up.chain[us.chain_cur]=util.clamp(up.chain[us.chain_cur]+sign(d),min_chain,9)
end
us.update_ui=true
end
function key(n,z)
if n==1 then
us.shift=(z==1)
elseif n>=2 and z==1 and us.mode==2 and not us.shift then
-- effects in chain mode
us.effect_stutter=n==2
us.effect_reverse=not us.effect_stutter
elseif n==3 and z==1 and us.shift then
-- toggle playback
parameters_save()
if not us.playing then
softcut.rate(1,up.rate+params:get("global_rate"))
softcut.level(1,1)
softcut.play(1,1)
else
softcut.level(1,0)
softcut.play(1,0)
end
us.playing_chain=0
us.playing_loop_end=0
us.playing_sample={0,0}
us.playing_beat=17
us.playing_pattern=1
if us.mode==1 then
-- toggle playback of this chain only
us.playing_once=2
end
us.playing=not us.playing
elseif n==2 and z==1 and us.mode==0 then
if up.samples[us.sample_cur].start==us.waveform_view[1] and up.samples[us.sample_cur].start+up.samples[us.sample_cur].length==us.waveform_view[2] then
update_waveform_view(0,up.length)
else
print("zooming to "..up.samples[us.sample_cur].start..","..up.samples[us.sample_cur].start+up.samples[us.sample_cur].length)
update_waveform_view(up.samples[us.sample_cur].start,up.samples[us.sample_cur].start+up.samples[us.sample_cur].length)
end
elseif n==2 and z==1 and us.mode==1 and us.shift then
-- make new pattern
up.patterns[us.pattern_cur][us.pattern_temp.start]=0
elseif n==2 and z==1 and us.mode==1 then
-- make new pattern
up.patterns[us.pattern_cur]=pattern_stamp(us.sample_cur,us.pattern_temp.start,us.pattern_temp.length)
elseif n==3 then
-- play a sample at curent position
sample_one_shot(z)
end
us.update_ui=true
end
--
-- ui
--
function redraw()
us.update_ui=false
screen.clear()
-- check shift
local shift_amount=0
if us.shift then
shift_amount=4
end
-- show sample info
if us.mode==0 then
screen.level(15)
else
screen.level(4)
end
screen.rect(1+shift_amount,1+shift_amount,7,8)
screen.stroke()
screen.move(2+shift_amount,7+shift_amount)
screen.text(up.samples[us.sample_cur].name)
-- show pattern info
if us.mode==1 then
screen.level(15)
else
screen.level(4)
end
screen.rect(10+shift_amount,1+shift_amount,7,8)
screen.stroke()
isone=0
if us.pattern_cur==1 then
isone=1
end
screen.move(11+isone+shift_amount,7+shift_amount)
screen.text(us.pattern_cur)
-- show chain info
local last_position=0
for i=1,#up.chain do
if i==us.chain_cur and us.mode==2 then
screen.level(15)
else
screen.level(4)
end
if i==us.playing_chain and us.playing then
screen.level(15)
end
if up.chain[i]>0 or us.chain_cur==i then
isone=0
if up.chain[i]==1 then
isone=1
end
last_position=i
screen.move(21+(i-1)*7+isone+shift_amount,7+shift_amount)
if up.chain[i]>0 then
screen.text(up.chain[i])
else
screen.text(" ")
end
end
end
if us.mode==2 then
screen.level(15)
end
screen.rect(19+shift_amount,1+shift_amount,21+(last_position-1)*7-13,8)
screen.stroke()
-- show pattern
local p=table.clone(up.patterns[us.pattern_cur])
if us.mode==1 then
-- fill in temp pattern
p=pattern_stamp(us.sample_cur,us.pattern_temp.start,us.pattern_temp.length)
end
local start=us.pattern_temp.start
local finish=us.pattern_temp.start+us.pattern_temp.length
if us.shift then
for i=start+1,finish do
p[i]=up.patterns[us.pattern_cur][i]
end
finish=start+1
end
for i=1,16 do
screen.level(4)
local isactive=false
if i>=start and i<finish and us.mode==1 then
screen.level(15)
isactive=true
end
if p[i]==us.playing_pattern_segment and p[i]>0 and us.playing then
screen.level(15)
end
if p[i]==0 and us.playing and i==us.playing_beat then
screen.level(15)
end
if p[i]~=0 then
if i>1 and p[i-1]==p[i] then
if i<16 and p[i+1]==p[i] then
screen.rect(1+(i-1)*8,13,8,5)
else
screen.rect(1+(i-1)*8,13,7,5)
end
else
screen.move(1+(i-1)*8,18)
screen.text(up.samples[math.floor(p[i])].name)
if i<16 and p[i+1]==p[i] then
screen.rect(6+(i-1)*8,13,3,5)
else
screen.rect(6+(i-1)*8,13,2,5)
end
end
else
screen.rect(1+(i-1)*8,13,7,5)
end
if us.shift and isactive then
screen.rect(1+(i-1)*8,13,7,5)
end
screen.fill()
end
-- plot waveform
-- https://github.com/monome/softcut-studies/blob/master/8-copy.lua
if #us.waveform_samples>0 then
screen.level(4)
local x_pos=0
local scale=19
for i,s in ipairs(us.waveform_samples) do
local height=util.round(math.abs(s)*scale)
local current_time=util.linlin(0,128,us.waveform_view[1],us.waveform_view[2],x_pos)
if current_time>us.playing_sample[1] and current_time<us.playing_sample[2] then
screen.level(15)
else
screen.level(4)
end
screen.move(i,45-height)
screen.line_rel(0,2*height)
screen.stroke()
x_pos=x_pos+1
end
screen.level(15)
for i,s in ipairs(up.samples) do
if (i==us.sample_cur or i==us.playing_sampleid) and s.length>0 and (s.start>=us.waveform_view[1] and s.start<=us.waveform_view[2]) then
x_pos=util.linlin(us.waveform_view[1],us.waveform_view[2],1,128,s.start)
if us.waveform_view[1]~=s.start then
screen.move(x_pos-3,26)
else
screen.move(x_pos+4,26)
end
screen.text(up.samples[i].name)
screen.move(x_pos,29)
screen.line_rel(0,34)
screen.move(x_pos,62)
screen.line_rel(3,3)
screen.move(x_pos,29)
screen.line_rel(3,-3)
x_pos=util.linlin(us.waveform_view[1],us.waveform_view[2],1,128,s.start+s.length)
screen.move(x_pos,29)
screen.line_rel(0,34)
-- if us.waveform_view[1] == s.start then
-- screen.move(x_pos+1,64)
-- screen.text(up.samples[i].name)
-- end
screen.move(x_pos,62)
screen.line_rel(-3,3)
screen.move(x_pos,29)
screen.line_rel(-3,-3)
end
end
screen.stroke()
end
-- show message if exists
if us.message~="" then
screen.level(0)
x=64
y=28
w=string.len(us.message)*6
screen.rect(x-w/2,y,w,10)
screen.fill()
screen.level(15)
screen.rect(x-w/2,y,w,10)
screen.stroke()
screen.move(x,y+7)
screen.text_center(us.message)
end
screen.update()
end
--
-- crow
--
function process_stream(v)
if params:get("crow_mode") == 2 then
-- sample mode
us.samples_usable={}
for i=1,#up.samples do
if up.samples[i].length>0 then
table.insert(us.samples_usable,i)
end
end
us.crow_sample_cur = us.samples_usable[util.round(util.linlin(-10,10,1,#us.samples_usable,v))]
local s=up.samples[us.crow_sample_cur].start
local e=up.samples[us.crow_sample_cur].start+up.samples[us.crow_sample_cur].length
us.playing_sample={s,e}
softcut.loop_start(4,s)
softcut.loop_end(4,e)
if us.crow_gating == 1 then
process_change(1)
end
else
-- free mode
us.crow_position = util.linlin(-10,10,0,up.length,v)
print("setting new position "..us.crow_position)
softcut.position(4,us.crow_position)
softcut.loop_end(4,up.length)
end
end
function process_change(s)
us.crow_gating=s
if params:get("crow_mode") == 2 then
-- sample mode