-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcybin.cybin
1341 lines (1341 loc) · 38.1 KB
/
cybin.cybin
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
Utils={}
function Utils.scale(x,a,b,na,nb)
local i=(x-a)/(b-a)
return na*i+(nb*(1-i))
end
--------------------------------------------
UGen={__call=function(self,...) return self:Set(...) end}
UGen.__index=UGen
setmetatable(UGen,{__call=function(cls,...) return cls.new(...) end})
function UGen.new(params)
local o=params or {}
setmetatable(o,UGen)
return o
end
function UGen:Set(params)
for k,v in pairs(params) do
if type(self[k])=='function' then
self[k](self,v)
else
self[k]=v
end
end
end
function UGen:Process()
return 0
end
--------------------------------------------
Sin={__call=function(self,...) return self:Set(...) end}
Sin.__index=Sin
setmetatable(Sin,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Sin.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.phase = o.phase or 0
o.delta = o.delta or 2*math.pi*(o.freq or 440)/o.samplerate
o.amp = o.amp or 1
o.freq=nil
setmetatable(o,Sin)
return o
end
function Sin:freq(frequency)
self.delta = (2*math.pi*frequency)/self.samplerate
end
function Sin:Process(phase)
self.phase=self.phase+self.delta
while self.phase>2*math.pi do self.phase=self.phase-2*math.pi end
return math.sin(self.phase+(phase or 0))*self.amp
end
--------------------------------------------
Pulse={__call=function(self,...) return self:Set(...) end}
Pulse.__index=Pulse
setmetatable(Pulse,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Pulse.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.phase = o.phase or 0
o.delta = o.delta or (o.freq or 440)/o.samplerate
o.freq = nil
o.amp = o.amp or 1
o.width = o.width or 0.5
setmetatable(o,Pulse)
return o
end
function Pulse:freq(frequency)
self.delta = frequency/self.samplerate
end
function Pulse:Process(params)
self.phase=self.phase+self.delta
while self.phase>1 do self.phase=self.phase-1 end
if self.phase > self.width then return 1*self.amp else return -1*self.amp end
end
--------------------------------------------
Saw={__call=function(self,...) return self:Set(...) end}
Saw.__index=Saw
setmetatable(Saw,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Saw.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.phase = o.phase or 0
o.delta = o.delta or (o.freq or 440)/o.samplerate
o.freq=nil
o.amp = o.amp or 1
setmetatable(o,Saw)
return o
end
function Saw:freq(frequency)
self.delta = frequency/self.samplerate
end
function Saw:Process(params)
self.phase=self.phase+self.delta
while self.phase>1 do self.phase=self.phase-1 end
return (self.phase*2-1)*self.amp
end
--------------------------------------------
Tri={__call=function(self,...) return self:Set(...) end}
Tri.__index=Tri
setmetatable(Tri,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Tri.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.phase = o.phase or 0
o.delta = o.delta or (o.freq or 440)/o.samplerate
o.freq=nil
o.amp = o.amp or 1
setmetatable(o,Tri)
return o
end
function Tri:freq(frequency)
self.delta = frequency/self.samplerate
end
function Tri:Process(params)
self.phase=self.phase+self.delta
while self.phase>1 do self.phase=self.phase-1 end
return (math.abs(self.phase-0.5)*4-1)*self.amp
end
--------------------------------------------
Filter={__call=function(self,...) return self:Set(...) end}
Filter.__index=Filter
setmetatable(Filter,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Filter.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.a0=1
o.a1=0
o.a2=0
o.b1=0
o.b2=0
o.Fc=(o.freq or 1000)/o.samplerate
o.Q=o.reso or 0.7
o.peakGain=0
o.z1=0
o.z2=0
o.ft=o.filterType or "lowpass"
o.freq=nil
o.reso=nil
o.filterType=nil
setmetatable(o,Filter)
o:filterType()
return o
end
function Filter:freq(frequency)
self.Fc=frequency/self.samplerate
self:filterType()
end
function Filter:reso(resonance)
self.Q=resonance
self:filterType()
end
function Filter:filterType(filtertype)
if filtertype==nil then
filtertype=self.ft
else
self.ft=filtertype
end
local norm
local V=math.pow(10,math.abs(self.peakGain)/20)
local K=math.tan(math.pi*self.Fc)
if filtertype=="lowpass" then
norm = 1 / (1 + K / self.Q + K * K)
self.a0 = K * K * norm
self.a1 = 2 * self.a0
self.a2 = self.a0
self.b1 = 2 * (K * K - 1) * norm
self.b2 = (1 - K / self.Q + K * K) * norm
end
if filtertype=="highpass" then
norm = 1 / (1 + K / self.Q + K * K);
self.a0 = 1 * norm;
self.a1 = -2 * self.a0;
self.a2 = self.a0;
self.b1 = 2 * (K * K - 1) * norm;
self.b2 = (1 - K / self.Q + K * K) * norm;
end
if filtertype=="bandpass" then
norm = 1 / (1 + K / self.Q + K * K);
self.a0 = K / self.Q * norm;
self.a1 = 0;
self.a2 = -self.a0;
self.b1 = 2 * (K * K - 1) * norm;
self.b2 = (1 - K / self.Q + K * K) * norm;
end
if filtertype=="notch" then
norm = 1 / (1 + K / self.Q + K * K);
self.a0 = (1 + K * K) * norm;
self.a1 = 2 * (K * K - 1) * norm;
self.a2 = self.a0;
self.b1 = self.a1;
self.b2 = (1 - K / self.Q + K * K) * norm;
end
if filtertype=="peak" then
if self.peakGain >= 0 then
norm = 1 / (1 + 1/self.Q * K + K * K);
self.a0 = (1 + V/self.Q * K + K * K) * norm;
self.a1 = 2 * (K * K - 1) * norm;
self.a2 = (1 - V/self.Q * K + K * K) * norm;
self.b1 = self.a1;
self.b2 = (1 - 1/self.Q * K + K * K) * norm;
else
norm = 1 / (1 + V/self.Q * K + K * K);
self.a0 = (1 + 1/self.Q * K + K * K) * norm;
self.a1 = 2 * (K * K - 1) * norm;
self.a2 = (1 - 1/self.Q * K + K * K) * norm;
self.b1 = self.a1;
self.b2 = (1 - V/self.Q * K + K * K) * norm;
end
end
if filtertype=="lowshelf" then
if self.peakGain >= 0 then
norm = 1 / (1 + math.sqrt(2) * K + K * K);
self.a0 = (1 + math.sqrt(2*V) * K + V * K * K) * norm;
self.a1 = 2 * (V * K * K - 1) * norm;
self.a2 = (1 - math.sqrt(2*V) * K + V * K * K) * norm;
self.b1 = 2 * (K * K - 1) * norm;
self.b2 = (1 - math.sqrt(2) * K + K * K) * norm;
else
norm = 1 / (1 + math.sqrt(2*V) * K + V * K * K);
self.a0 = (1 + math.sqrt(2) * K + K * K) * norm;
self.a1 = 2 * (K * K - 1) * norm;
self.a2 = (1 - math.sqrt(2) * K + K * K) * norm;
self.b1 = 2 * (V * K * K - 1) * norm;
self.b2 = (1 - math.sqrt(2*V) * K + V * K * K) * norm;
end
end
if filtertype=="highshelf" then
if self.peakGain >= 0 then
norm = 1 / (1 + math.sqrt(2) * K + K * K);
self.a0 = (V + math.sqrt(2*V) * K + K * K) * norm;
self.a1 = 2 * (K * K - V) * norm;
self.a2 = (V - math.sqrt(2*V) * K + K * K) * norm;
self.b1 = 2 * (K * K - 1) * norm;
self.b2 = (1 - math.sqrt(2) * K + K * K) * norm;
else
norm = 1 / (V + math.sqrt(2*V) * K + K * K);
self.a0 = (1 + math.sqrt(2) * K + K * K) * norm;
self.a1 = 2 * (K * K - 1) * norm;
self.a2 = (1 - math.sqrt(2) * K + K * K) * norm;
self.b1 = 2 * (K * K - V) * norm;
self.b2 = (V - math.sqrt(2*V) * K + K * K) * norm;
end
end
end
function Filter:Process(input)
local output = input * self.a0 + self.z1;
self.z1 = input * self.a1 + self.z2 - self.b1 * output;
self.z2 = input * self.a2 - self.b2 * output;
return output;
end
--------------------------------------------
Metro={__call=function(self,...) return self:Set(...) end}
Metro.__index=Metro
setmetatable(Metro,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Metro.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.events = {}
o.pats = {}
o.time = o.time or 0
o.cps = o.cps or 1
setmetatable(o,Metro)
return o
end
function Metro:Set(...)
arg = { ... }
if type(arg[1])=='string' or type(arg[1])=='function' then
self:Interval(arg[1])
elseif type(arg[1])=='table' and getmetatable(arg[1])==Pat then
self:Pat(unpack(arg))
elseif type(arg[1])=='table' then
UGen.Set(self,arg[1])
end
end
function Metro:Process(params)
local rmindices={}
local now=self.time
for i=1,#self.events do
if self.time>=self.events[i].start then
self.events[i].func(self.events[i].start,self.events[i].stop)
table.insert(rmindices,i)
end
end
for i=1,#rmindices do
table.remove(self.events,rmindices[#rmindices-i+1])
end
self.time=self.time+self.cps/self.samplerate
return now
end
function Metro:Pat(pat,handler,start,stop)
if stop==nil then
if start==nil then
start=math.ceil(self.time)
stop=math.ceil(self.time+1)
else
stop=math.ceil(self.time)+start
start=math.ceil(self.time)
end
end
local function close(value)
return function(start,stop)
handler(value,start,stop)
end
end
local events=pat:QueryEvents(start,stop,1)
for i=1,#events do
events[i].func=close(events[i].value)
end
self:Events(events)
end
function Metro:Event(func,start,stop)
table.insert(self.events,{func=func,start=start,stop=stop})
end
function Metro:Events(e)
for i=1,#e do
table.insert(self.events,e[i])
end
end
function Metro:Interval(func)
local callback=nil
if type(func)=='function' then
callback=function(start,stop)
start = start or math.ceil(self.time)
local interval=func(start,stop)
local nextTime=start+interval
self:Event(callback,nextTime,nextTime+interval)
end
elseif type(func)=='string' and _G[func]~=nil then
callback=function(start,stop)
if _G[func]~=nil then
start = start or math.ceil(self.time)
local interval=_G[func](start,stop) or 1
local nextTime=start+interval
self:Event(callback,nextTime,nextTime+interval)
end
end
end
if callback~=nil then self:Event(callback,math.ceil(self.time),math.ceil(self.time+1)) end
end
function Metro:Stop()
self.events={}
end
function Metro:Cycles2Seconds(cycles)
return cycles/self.cps
end
Metro.c2s=Metro.Cycles2Seconds
--------------------------------------------
Sampler={__call=function(self,...) return self:Set(...) end}
Sampler.__index=Sampler
setmetatable(Sampler,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Sampler.new(params)
local o=UGen.new(params)
if type(o.sample)=='string' then
o.sample=cybin.loadaudiofile(o.sample)
end
o.samplerate=o.samplerate or cybin.samplerate or 48000
o.grainsize=o.grainsize or o.sample.samplerate*30/1000
o.grain1start=0
o.grain1sindex=0
o.grain2start=0
o.grain2sindex=0
o.grainwindex=0
o.sindex=1
o.speed=o.speed or 1
o.pitch=o.pitch or 1
o.loop=o.loop or false
o.grainxfade=1
o.basenote=60
o.amp=o.amp or 1
o.env=o.env or nil
if o.offset~=nil then Sampler.offset(o,o.offset); o.offset=nil end
if o.duration~=nil then Sampler.duration(o,o.duration); o.duration=nil end
if o.grainDuration~=nil then Sampler.grainDuration(o,o.grainDuration); o.grainDuration=nil end
setmetatable(o,Sampler)
return o
end
function Sampler:offset(offset)
self.sindex=self.sample.frames*offset
if self.grain1start==0 and self.grain2start==0 then
self.grain1start=self.sindex
self.grain2start=self.sindex
end
end
function Sampler:duration(dur)
local seconds=self.sample.frames/self.samplerate
self.speed=seconds/dur
end
function Sampler:grainDuration(dur)
self.grainsize=math.floor(dur*self.samplerate)
end
function Sampler:Process()
local out={}
local env=1
if self.env~=nil then
env=self.env:Process()
if self.env.phase==-1 then self.free=true end
end
for i=1,self.sample.channels do out[i]=0 end
if (self.loop==false and (self.sindex>self.sample.frames or self.sindex<0)) or self.free==true then
self.free=true
return unpack(out)
end
if self.grainwindex >= self.grainsize then
self.grainwindex=0
self.grain1sindex=0
self.grain1start=self.grain2start+self.grain2sindex
self.grain2sindex=0
self.grain2start=self.sindex
end
local grain1channels=self:GetChannels(self.grain1sindex+self.grain1start)
local grain2channels=self:GetChannels(self.grain2sindex+self.grain2start)
local nofx=self:GetChannels(self.sindex)
for i=1,self.sample.channels do
local window1=math.cos(math.pi*0.5*self.grainwindex/self.grainsize)
local window2=math.sin(math.pi*0.5*self.grainwindex/self.grainsize)
out[i]=grain1channels[i]*window1+grain2channels[i]*window2
out[i]=(out[i]*self.grainxfade+nofx[i]*(1-self.grainxfade))*env
end
self.grain1sindex=self.grain1sindex+1*self.pitch
self.grain2sindex=self.grain2sindex+1*self.pitch
self.grainwindex=self.grainwindex+1
self.sindex=self.sindex+1*self.speed
return unpack(out)
end
function Sampler:GetChannels(i)
if self.loop==false and i>self.sample.frames then
local out={}
for i=1,self.sample.channels do out[i]=0 end
return out
end
while i>self.sample.frames do i=i-self.sample.frames end
while i<1 do i=i+self.sample.frames end
if i==0 then i=1 end
local out={}
---
local intIndex,fracIndex=math.modf(i)
local intNextIndex=intIndex+1
if self.pitch<0 then intNextIndex=intIndex-1 end
for j=1,self.sample.channels do
local currentSample=self.sample[intIndex*self.sample.channels+j-1] or 0
local nextSample=self.sample[intNextIndex*self.sample.channels+j-1] or currentSample
out[j]=(currentSample*(1-fracIndex)+nextSample*fracIndex)*self.amp
-- simplified version doesn't work even though mathematically equivalent?????
--out[j]=currentSample-(currentSample+nextSample)*fracIndex
end
return out
end
--------------------------------------------
Env={__call=function(self,...) return self:Reset(...) end}
Env.__index=Env
setmetatable(Env,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Env.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.values=o.values or {1,0}
o.durations=o.durations or {1}
o.times={}
local sum=0
for i=1,#o.durations do
o.times[i]=sum
sum=sum+o.durations[i]
end
o.times[#o.durations+1]=sum
o.delta=1/o.samplerate
o.phase=-1
o.out=0
setmetatable(o,Env)
return o
end
function Env:Reset()
self.phase=0
return self
end
function Env:Process(samplerate)
local i=1
if self.phase==-1 then return self.out end
if self.phase>=self.times[#self.times] then
self.phase=-1
self.out=self.values[#self.values]
return self.out
end
local function wrap(t,i) return t[((i-1)%#t)+1] end
for i=1,#self.durations do
if self.phase<=self.times[i+1] then
local interp=(self.phase-self.times[i])/self.durations[i]
self.out=self.values[i]*(1-interp)+self.values[i+1]*interp
self.phase=self.phase+self.delta
return self.out
end
end
return self.out
end
--------------------------------------------
Compressor={__call=function(self,...) return self:Reset(...) end}
Compressor.__index=Compressor
setmetatable(Compressor,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Compressor.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.threshold=o.threshold or -10
o.ratio=o.ratio or 2
o.attack=o.attack or 0.015
o.release=o.release or 0.07
o.makeup=o.makeup or 0
o.lsat=1
setmetatable(o,Compressor)
return o
end
function Compressor:Process(carrier,modulator)
if modulator==nil then modulator=carrier end
local function lin2db(x) return math.log10(x)*20 end
local function db2lin(x) return math.pow(10,x/20) end
local function rectify(x)
if x>=0 then return x else return 0 end
end
local dBaboveThreshold=rectify(lin2db(math.abs(modulator))-self.threshold)
local linAboveThreshold=db2lin(dBaboveThreshold)
local coef=1/(self.release*self.samplerate)
if linAboveThreshold>self.lsat then coef=1/(self.attack*self.samplerate) end
self.lsat=math.abs(linAboveThreshold)*coef+self.lsat*(1-coef)
dBaboveThreshold=lin2db(self.lsat)
local dBin=lin2db(math.abs(carrier))
local signIn=1
if carrier<0 then signIn=-1 end
return db2lin(dBin-dBaboveThreshold+dBaboveThreshold/self.ratio)*db2lin(self.makeup)*signIn
end
--------------------------------------------
Interp={}
setmetatable(Interp,{__call=function(cls,...) return cls.new(...) end})
function Interp.new(t)
local o={}
o.t={}
for i=1,#t do o.t[i]=t[i] end
setmetatable(o,Interp)
return o
end
function Interp:Write(k,v)
if self.lastWriteIndex==nil or self.lastWriteIndex==k then
k=math.floor(k)
while k<=0 do k=k+#self.t end
while k>#self.t do k=k-#self.t end
self.t[k]=v
self.lastWriteIndex=k
else
local fwdDistance=k-self.lastWriteIndex
while fwdDistance<0 do fwdDistance=fwdDistance+#self.t end
while fwdDistance>#self.t do fwdDistance=fwdDistance-#self.t end
local bckDistance=self.lastWriteIndex-k
while bckDistance<0 do bckDistance=bckDistance+#self.t end
while bckDistance>#self.t do bckDistance=bckDistance-#self.t end
local from=self.t[self.lastWriteIndex]
local to=v
if fwdDistance<bckDistance then
for i=0,fwdDistance do
local interp=i/fwdDistance
self.t[self.lastWriteIndex]=from*(1-interp)+to*interp
if i~=fwdDistance then self.lastWriteIndex=self.lastWriteIndex+1 end
if self.lastWriteIndex>#self.t then self.lastWriteIndex=self.lastWriteIndex-#self.t end
end
else
for i=0,bckDistance do
local interp=i/bckDistance
self.t[self.lastWriteIndex]=from*(1-interp)+to*interp
if i~=bckDistance then self.lastWriteIndex=self.lastWriteIndex-1 end
if self.lastWriteIndex<1 then self.lastWriteIndex=self.lastWriteIndex+#self.t end
end
end
end
end
function Interp:Read(k)
local int=math.floor(k)
local frac=math.fmod(k,1)
while int<=0 do int=int+#self.t end
while int>#self.t do int=int-#self.t end
local nextInt=int+1
if nextInt>#self.t then nextInt=nextInt-#self.t end
return self.t[int]*(1-frac)+self.t[nextInt]*frac
end
Interp.__index=function(self,k)
if type(k)=='number' then
return self:Read(k)
else
return Interp[k]
end
end
Interp.__newindex=function(self,k,v)
if type(k)=='number' then
self:Write(k,v)
else
rawset(self,k,v)
end
end
--------------------------------------------
Allpass={__call=function(self,...) return self:Set(...) end}
Allpass.__index=Allpass
setmetatable(Allpass,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Allpass.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.buf={}
o.size=math.floor(o.samplerate*(o.delay or 0.123))
for i=1,o.size do o.buf[i]=0 end
o.buf=Interp(o.buf)
o.coef=o.coef or 0.5
o.index=0
setmetatable(o,Allpass)
return o
end
function Allpass:Process(input,sampleOffset)
local output=input*self.coef+self.buf[self.index+(sampleOffset or 0)]
self.buf[self.index]=input+output*-self.coef
self.index=self.index+1
while self.index>self.size do self.index=self.index-self.size end
return output
end
--------------------------------------------
Comb={__call=function(self,...) return self:Set(...) end}
Comb.__index=Comb
setmetatable(Comb,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Comb.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.buf={}
o.size=math.floor(o.samplerate*(o.delay or 0.123))
for i=1,o.size do o.buf[i]=0 end
o.buf=Interp(o.buf)
o.coef=o.coef or 0.5
o.index=0
setmetatable(o,Comb)
return o
end
function Comb:Process(input,sampleOffset)
self.buf[self.index]=input+self.buf[self.index+(sampleOffset or 0)]*self.coef
local output=self.buf[self.index]
self.index=self.index+1
while self.index>self.size do self.index=self.index-self.size end
return output
end
--------------------------------------------
Delay={__call=function(self,...) return self:Set(...) end}
Delay.__index=Delay
setmetatable(Delay,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Delay.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.buf={}
o.size=math.floor(o.samplerate*(o.delay or 0.123))
for i=1,o.size do o.buf[i]=0 end
o.buf=Interp(o.buf)
o.index=0
setmetatable(o,Delay)
return o
end
function Delay:Process(input,sampleOffset)
local output=self.buf[self.index+(sampleOffset or 0)]
self.buf[self.index]=input
self.index=self.index+1
while self.index>self.size do self.index=self.index-self.size end
return output
end
--------------------------------------------
Reverb={__call=function(self,...) return self:Set(...) end}
Reverb.__index=Reverb
setmetatable(Reverb,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Reverb.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.coef=o.coef or 0.9
o.nodes = o.nodes or 5
o.spacing = o.spacing or 1.123
o.lastSample=0
o.allpasses={}
for i=1,o.nodes do o.allpasses[i]=Allpass{delay=0.0123*math.pow(o.spacing,i),coef=o.coef,samplerate=o.samplerate} end
setmetatable(o,Reverb)
return o
end
function Reverb:Process(input)
local out=input+self.lastSample*self.coef
for i=1,#self.allpasses do
self.allpasses[i].coef=self.coef
out=self.allpasses[i]:Process(out)
end
return out
end
--------------------------------------------
Reverb2={__call=function(self,...) return self:Set(...) end}
Reverb2.__index=Reverb2
setmetatable(Reverb2,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Reverb2.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.coef=o.coef or 0.9
o.damp=o.damp or 0.3
o.lowpass=Filter{filterType='lowpass',freq=o.lowpassFreq or 5000,reso=0.1}
o.delay=Delay{delay=o.echoDuration or 1}
o.nodes = o.nodes or 5
o.spacing = o.spacing or 1.123
o.lastSample=0
o.allpasses={}
for i=1,o.nodes do o.allpasses[i]=Allpass{delay=0.0123*math.pow(o.spacing,i),coef=o.coef,samplerate=o.samplerate} end
setmetatable(o,Reverb2)
return o
end
function Reverb2:Process(input)
local out=input+self.lowpass:Process(self.delay:Process(self.lastSample)*self.damp)
for i=1,#self.allpasses do
self.allpasses[i].coef=self.coef
out=self.allpasses[i]:Process(out)
end
self.lastSample=out
return out
end
--------------------------------------------
Poly={__call=function(self,...) return self:Set(...) end}
Poly.__index=Poly
setmetatable(Poly,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Poly.new(params)
local o=UGen.new(params)
o.samplerate = o.samplerate or cybin.samplerate or 48000
o.voice=o.voice
o.voices={}
o.maxVoices=o.maxVoices or 3
o.voiceIndex=1
setmetatable(o,Poly)
return o
end
function Poly:Set(params)
if params.voice~=nil then self.voice=params.voice end
params.samplerate=self.samplerate
local voice=self.voice(params)
if self.maxVoices<1 then
table.insert(self.voices,voice)
else
self.voices[self.voiceIndex]=voice
self.voiceIndex=self.voiceIndex+1
self.voiceIndex=((self.voiceIndex-1)%self.maxVoices)+1
end
return voice
end
function Poly:Process(samplerate)
local out=0
for i=self.maxVoices,1,-1 do
if self.voices[i]~=nil then
if self.voices[i].free~=nil then
table.remove(self.voices,i)
else
out=out+self.voices[i]:Process(samplerate)
end
end
end
return out
end
--------------------------------------------
FMVoice={__call=function(self,...) return self:Set(...) end}
FMVoice.__index=FMVoice
setmetatable(FMVoice,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function FMVoice.new(params)
local o=UGen.new(params)
o.env=o.env or Env{values=o.values or {0,1,0},durations=o.durations or {0.01,0.3}}
o.env:Reset()
o.modCoef=o.modCoef or 3
o.modAmp=o.modAmp or 300
o.modShape=o.modShape or 1
o.carrierShape=o.carrierShape or 1
o.amp=o.amp or 0.1
o.carrier=Sin{samplerate=o.samplerate}
o.modulator=Sin{samplerate=o.samplerate}
o.freq=o.freq or 440
o.carrier{freq=o.freq}
o.modulator{freq=o.freq*o.modCoef}
setmetatable(o,FMVoice)
return o
end
function FMVoice:Process()
local env=self.env:Process()
if self.env.phase==-1 then self.free=true end
self.modulator{freq=self.freq*self.modCoef}
self.carrier{freq=self.freq+math.atan(self.modulator:Process(sr)*self.modShape)*self.modAmp*env}
return math.atan(self.carrier:Process(sr)*self.carrierShape)*env
end
--------------------------------------------
Pluck={__call=function(self,...) return self:Set(...) end}
Pluck.__index=Pluck
setmetatable(Pluck,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Pluck.new(params)
local o=UGen.new(params)
o.samplerate=o.samplerate or cybin.samplerate or 48000
o.freq=o.freq or 220
o.harmonic=o.harmonic or 3.93
o.coef=o.coef or 0.9
o.cutoff=o.cutoff or o.samplerate*0.25
o.reso=o.reso or 1
o.osc=Saw{freq=o.freq} or o.osc
o.env=o.env or Env{values=o.values or {0,1,0},durations=o.durations or {0.01,0.4}}
o.env{}
o.filter=o.filter or Filter{filterType='lowpass',freq=o.cutoff,reso=o.reso}
o.comb=Comb{delay=1/(o.freq*o.harmonic),coef=o.coef}
setmetatable(o,Pluck)
return o
end
function Pluck:Process()
local env=math.pow(self.env:Process(),10)
if self.env.phase==-1 then self.free=true end
self.filter{freq=env*self.cutoff+100,reso=self.reso}
return self.filter:Process(self.comb:Process(self.osc:Process()))*env
end
--------------------------------------------
Fring={__call=function(self,...) return self:Set(...) end}
Fring.__index=Fring
setmetatable(Fring,{__call=function(cls,...) return cls.new(...) end,__index=UGen})
function Fring.new(params)
local o=UGen.new(params)
o.env=o.env or Env{values=o.values or {0,1,0},durations=o.durations or {0.01,0.5}}
o.env:Reset()
o.modf=o.modf or function(x) return math.random(50,5000) end
o.basef=o.basef or function(x) return math.random(50,5000) end
o.pitch=o.pitch or 1
o.drive=o.drive or 2
o.nodes=o.nodes or 8
o.oscs={}
o.lasts={}
for i=1,o.nodes do
o.lasts[i]=math.random()*2-1
o.oscs[i]=Sin{freq=o.basef(i),phase=math.random()*2*math.pi}
end
setmetatable(o,Fring)
return o
end
function Fring:Process(input)
input=math.atan(input or 0)
local out=0
local env=self.env:Process()
if self.env.phase==-1 then self.free=true end
for i=1,self.nodes do
local lastIndex=i-1
if lastIndex<1 then lastIndex=lastIndex+self.nodes end
self.oscs[i]{freq=(self.lasts[lastIndex]*self.modf(i)+self.basef(i))*self.pitch}
self.lasts[i]=(math.atan(self.oscs[i]:Process()*self.drive)+input)*math.pow(env,2)
out=out+self.lasts[i]
end
self.last=out*math.pow(env,2)/self.nodes
return self.last
end
----------------------------------------------------------------
-- TODO: Make methods functionalish/idempotent with deep copy --
----------------------------------------------------------------
Pat={}
setmetatable(Pat, {__call = function (cls, ...) return cls.new(...) end,})
Pat.__index=function(t,k)
if type(k)=='number' then
return t.t[k]
else
return Pat[k]
end
end
Pat.__newindex=function(t,k,v)
if type(k)=='number' then
t.t[k]=v
else
rawset(t,k,v)
end
end
function Pat.new(t)
local o={}
setmetatable(o,Pat)
o.t={}
o.cycles=t.cycles or 1
o.offset=t.offset or 0
o.parallel=t.parallel or false
for i=1,#(t.t or t) do
if type(t[i])=='table' then
o.t[i]=Pat(t[i])
else
o.t[i]=t[i]
end
end
return o
end
function Pat.GenDup(n,size)
local pat=Pat{}
for i=1,size do pat[i]=n end
return pat
end
function Pat.GenBjork(nPegs,nHoles)
local pegs,holes,lastPegIndex,work,pegs,holes
local function genDup(n,size)
local t={}
for i=1,size do t[i]=n end
return t
end
local flatten=function(arr)
local result={}
local function flatten(arr)
for _, v in ipairs(arr) do
if type(v)=="table" then
flatten(v)
else
table.insert(result,v)
end
end
end
flatten(arr)
return result
end
local function concat(a,b)
if b==nil then return a end
local ret={}
for i=1,#a do ret[#ret+1]=a[i] end
for i=1,#b do ret[#ret+1]=b[i] end
return ret
end
local function selectRange(t,from,to)
local ret={}
for i=from,to,1 do ret[#ret+1]=t[i] end
return ret
end
if nPegs<1 then return Pat.GenDup(0,nHoles) end
nHoles=nHoles-nPegs
pegs=genDup({1},nPegs)
holes=genDup({0},nHoles)
lastPegIndex=0
work=function()
if (lastPegIndex~=0) then
holes=selectRange(pegs,lastPegIndex,#pegs)
pegs=selectRange(pegs,1,lastPegIndex-1)
end
nPegs=#pegs
nHoles=#holes
for inc=0,nHoles-1,1 do
pegs[(inc%nPegs)+1]=concat(pegs[(inc%nPegs)+1],holes[inc+1])
end
lastPegIndex=(nHoles%nPegs)+1
end
work()
while (lastPegIndex~=1) and (lastPegIndex~=(nPegs)) do
work()
end
return Pat.new(flatten(pegs))
end
function Pat.GenBjorkStructure(size,selects)
local currentLevel=Pat.GenDup(1,size)
local ret=Pat.GenDup(0,size)
for i=1,#selects do
currentLevel=currentLevel:BjorkSelect(selects[i])
ret=ret+currentLevel
end
return ret
end
function Pat:BjorkSelect(select)
local selectIndex,selectPattern,outPattern
outPattern=Pat.new(self)
outPattern.t={}
for i=1,#self.t do outPattern[i]=self[i] end
selectIndex=1
selectPattern=Pat.GenBjork(select,Pat.Sum(self:Apply(function(x) if x>0 then return 1 else return 0 end end)))
for i=1,#outPattern.t do
if outPattern[i]>0 then
outPattern[i]=outPattern[i]*selectPattern[selectIndex]
selectIndex=selectIndex+1
end
end
return outPattern
end
function Pat:Select(select)
local ret=Pat(self);ret.t={}
for i=1,#self.t do
if Pat.WrapAt(select,i)>0 then
ret[#ret.t+1]=self[i]
end
end
return ret
end
function Pat:Mask(select)
local ret=Pat(self);ret.t={}
for i=1,#self.t do
if Pat.WrapAt(select,i)>0 then
ret[#ret.t+1]=self[i]
else
ret[#ret.t+1]=Pat{}
end
end
return ret
end
function Pat:SuperMask(select)
local ret=Pat(self);ret.t={}
local selecti=1
for i=1,#self.t do
if Pat.WrapAt(select,selecti)>0 then
ret[#ret.t+1]=self[i]
else
ret[#ret.t+1]=Pat{}
end
if not(type(self[i])=='table' and #self[i].t<1) then selecti=selecti+1 end
end
return ret
end
function Pat:RemoveAt(index)
local ret=Pat(self)
local len=#(ret.t or ret)
while index<1 do index=index+len end
local remIndex=((index-1)%len)+1
table.remove(ret.t,remIndex)
return ret