-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly_blep.jsfx-inc
1325 lines (996 loc) · 28.8 KB
/
poly_blep.jsfx-inc
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
desc:PolyBLEP quasi-bandlimited tone generator
// Copyright (C) 2013-2023 Theo Niessink <[email protected]>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
/* Example
desc:Bandlimited sawtooth oscillator
slider1:440<20,20000,1>Freq (Hz)
import Tale/poly_blep.jsfx-inc
@slider
osc.poly_setf(slider1);
@sample
spl0 = spl1 = 0.25 * osc.poly_saw();
Setting Functions
* poly_setf(freq)
Example: osc.poly_setf(440);
Sets the oscillator frequency (specified in Hz), and returns the
frequency in seconds/sample.
(To convert from Hz to seconds/sample, divide by srate. To convert
from seconds/sample to Hz, multiply with srate.)
Note: Although the maximum frequency supported is srate/4, you can
safely specify higher frequencies, even beyond srate/2.
* poly_setf(note, tuning)
Example: osc.poly_setf(60, 440);
Sets the oscillator frequency to the specified MIDI note and tuning
(in Hz), and returns the frequency in seconds/sample.
* poly_setdt(time)
Example: osc2.poly_setdt(osc1.dt);
Sets the oscillator frequency (specified in seconds/sample), and
returns this value.
* poly_setpw(pw)
Example: osc.poly_setpw(0.3);
Example: osc.pw = 0.3;
Sets the pulse width [0.0..1.0] of the waveform, and returns this
value.
* poly_setf2(freq)
Example: osc.poly_setf2(6.0);
Sets the follower frequency (specified in Hz) of the hard sync
waveform, and returns the follower/leader frequency ratio.
Note: You should always first set the leader oscillator frequency, and
then set the follower frequency. If you change the leader frequency,
then you will probably also want to update the follower frequency.
* poly_setn(num_steps)
Example: osc.poly_setn(3);
Sets the integer number of steps (>=1) for the stepped waveform, and
returns this value.
Waveform Functions
* poly_sin() -- Sine
* poly_cos() -- Cosine
* poly_tri() -- Triangle
* poly_sqr() -- Square
* poly_rect() -- Rectangle (pulse)
* poly_saw() -- Sawtooth
* poly_ramp() -- Ramp
* poly_tri2() -- Modified triangle
* poly_sqr2() -- Modified square
* poly_half() -- Half-wave rectified sine (fixed 0.5 pulse width)
* poly_half2() -- Half-wave rectified sine (variable pulse width)
* poly_full() -- Full-wave rectified sine
* poly_sinp() -- Pulse sine (fixed 0.5 pulse width)
* poly_sawp() -- Saw pulse
* poly_trip() -- Triangular pulse
* poly_hwsaw() -- Half-wave rectified sawtooth
* poly_alt() -- Alternating sine
* poly_camel() -- Camel sine
* poly_camela() -- Alternating camel sine
* poly_camel2() -- Bipolar camel sine
* poly_sin2() -- Bipolar squared sine
* poly_para() -- Parabola
* poly_trap() -- Trapezoid (fixed 0.5 pulse width)
* poly_trap2() -- Trapezoid (variable pulse width)
* poly_hyptri() -- Hyper triangular wave
* poly_lpsqrN() -- LPF square approximation (order N = 2 or 3)
* poly_intlpsqrN() -- Integrated LPF square (order N = 2 or 3)
* poly_bpsqr2() -- BPF square approximation
* poly_intsaw() -- Integrated sawtooth
* poly_cubsaw() -- Cubic sawtooth
* poly_sinsaw() -- Sine sawtooth
* poly_hpsaw6() -- HPF sawtooth approximation
* poly_logit3() -- Logit approximation
* poly_sqrm1() -- Square wave minus fundamental
* poly_sawm1() -- Sawtooth minus fundamental
* poly_sinn() -- Stepped sine
* poly_trin() -- Stepped triangle
* poly_sawn() -- Stepped sawtooth
* poly_hssaw() -- Hard sync sawtooth
* poly_ham() -- Might not be suitable for vegetarians
* poly_stairs() -- Staircase (fixed 0.5 pulse width)
* poly_stairs3() -- Staircase (variable pulse width)
* poly_stairs2() -- Uneven staircase
Example: sample = osc.poly_saw();
Returns a sample of a waveform, and increments its phase.
Note: In v20151024 the phase of poly_full() and poly_trip() has been
corrected. To convert code relying on the old behavior, synchronize
the phase to t-0.25 for poly_full(), and to t-(0.75+0.5*pw) for
poly_trip().
Miscellaneous Functions
* poly_sync(phase)
Example: osc2.poly_sync(osc1.t + 0.5);
Synchronizes the oscillator with the specified phase, and returns the
normalized phase.
Note: You can safely specify out or range (and even negative) values
here.
* poly_inc()
Example: osc.poly_inc();
Increments the oscillator phase, and returns it.
Note: All waveform functions automatically increment the phase.
* poly_resetf()
Example: osc.poly_resetf();
Call this before changing the waveform to poly_stairs() or poly_ham().
* poly_blep(t, dt) -- Band-limited step
* poly_blamp(t, dt) -- Band-limited ramp
* poly_bluh(t, dt) -- Band-limited curve
Example: y = poly_blep(osc.t, osc.dt);
Returns a polynomial around a discontinuity (i.e. when it passes 1.0
and wraps to 0.0), or 0.0 otherwise.
Instance Variables
* t
Example: phase = osc.t;
The current phase [0.0..1.0) of the oscillator.
* dt
Example: freq = osc.dt * srate;
The oscillator frequency, in seconds/sample.
* pw
Example: duty_cycle = osc.pw;
The pulse width [0.0..1.0] of the waveform.
* fc
Example: freq2 = poly.fc * lfo.dt * srate;
The hard sync oscillator frequency ratio.
* a
* a2
* a3
Example: osc2.a = osc1.a;
The frequency dependent gain [0.0..1.0].
*/
@init
function _poly_setdt(dt)
(
dt <= 0.2 ? 1 : dt < 0.25 ? 1 - sqr((dt - 0.2) * 20);
// 0 otherwise
);
function poly_setdt(time)
instance(dt, a)
(
a = _poly_setdt(time);
dt = time;
);
function poly_setf(freq)
// global(srate)
(
this.poly_setdt(freq / srate);
);
function poly_setf(note, tuning)
// global(srate)
(
this.poly_setdt(exp((note - 69) * /* log(2)/12 */ 0.057762265046662109) * tuning / srate);
);
function poly_resetf()
instance(dt2)
(
dt2 = 0;
);
function poly_setpw(pw)
(
this.pw = pw;
);
function poly_setf2(freq)
// global(srate)
instance(fc, dt)
(
dt > 0 ? fc = freq / (dt * srate) : fc = 0;
);
function poly_setn(num_steps)
instance(n)
(
n = num_steps|0;
);
function poly_sync(phase)
instance(t)
(
t = phase;
t >= 0 ? t -= t|0 : t += 1 - (t|0);
);
function poly_inc()
instance(t, dt)
(
t += dt;
t -= t|0;
);
// Adapted from "Phaseshaping Oscillator Algorithms for Musical Sound
// Synthesis" by Jari Kleimola, Victor Lazzarini, Joseph Timoney, and Vesa
// Valimaki.
// http://www.acoustics.hut.fi/publications/papers/smc2010-phaseshaping/
function poly_blep(t, dt)
(
t < dt ? (
// x = t/dt
// y = -(x^2) + 2*x - 1
-sqr(t/dt - 1);
) :
t > 1 - dt ? (
// x = (t - 1) / dt
// y = x^2 + 2*x + 1
sqr((t - 1) / dt + 1);
);
// 0 otherwise
);
// Derived from poly_blep().
function poly_blamp(t, dt)
(
t < dt ? (
// x = t/dt
// y = -1/3*x^3 + x^2 - x + 1/3
t = t/dt - 1;
sqr(t)*t * -1/3;
) :
t > 1 - dt ? (
// x = (t - 1) / dt
// y = 1/3*x^3 + x^2 + x + 1/3
t = (t - 1) / dt + 1;
sqr(t)*t * 1/3;
);
// 0 otherwise
);
function poly_bluh(t, dt)
(
t < dt ? (
// x = t/dt
// y = -4*x^4 + 16*x^3 - 20*x^2 + 8*x
t = sqr(t/dt - 1);
(sqr(t) - t) * -4;
) :
t > 1 - dt ? (
// x = (t - 1) / dt
// y = 4*x^4 + 16*x^3 + 20*x^2 + 8*x
t = sqr((t - 1) / dt + 1);
(sqr(t) - t) * 4;
);
// 0 otherwise
);
// Sine
function poly_sin()
instance(a)
local(t)
(
t = this.t;
this.poly_inc();
sin(t * /* 2*$pi */ 6.2831853071795865) * a;
);
// Cosine
function poly_cos()
instance(a)
local(t)
(
t = this.t;
this.poly_inc();
cos(t * /* 2*$pi */ 6.2831853071795865) * a;
);
// Stepped sine
function poly_sinn()
instance(dt, a, n)
local(t1, t2, t3, dt2, dt3, y1, y2, s0, s1)
(
t1 = this.t;
this.poly_inc();
n >= 2 ? (
y1 = sin(((t1 * n * 2 + 0.5)|0) * $pi / n);
t1 += (dt2 = 0.5 / n) * 0.5;
) : (
t1 < 0.5 ? y1 = /* 1/sqrt(2) */ 0.70710678118654752 : y1 = /* -1/sqrt(2) */ -0.70710678118654752;
dt2 = 0.25;
);
t2 = t1 + 0.5;
t3 = dt3 = dt2 * /* 2*$pi */ 6.2831853071795865;
y2 = s0 = 0;
loop(n,
t1 -= t1|0;
t2 -= t2|0;
s1 = s0;
y2 += (poly_blep(t1, dt) - poly_blep(t2, dt)) * ((s0 = sin(t3)) - s1);
t1 += dt2;
t2 += dt2;
t3 += dt3;
);
((n >= 2 ? 0.5 : /* 1/sqrt(2) */ 0.70710678118654752) * y2 + y1) * a;
);
// Half-wave rectified sine
// Note: Anti-aliasing depends on frequency and pulse width (pw == 1.0 is
// good, pw < 1.0 is increasingly not so good).
function poly_half()
instance(dt, a)
local(t)
(
t = this.t;
this.poly_inc();
((poly_blamp(t, dt)
+ poly_blamp(t + 0.5 - ((t + 0.5)|0), dt)) * /* 2*$pi */ 6.2831853071795865 * dt
+ (t < 0.5 ? sin(t * /* 2*$pi */ 6.2831853071795865) * 2 : 0) - /* 2/$pi */ 0.63661977236758134
) * a;
);
function poly_half2()
instance(dt, a, pw)
local(t1, t2)
(
t2 = (t1 = pw * 0.5 + this.t + 0.75) - pw;
this.poly_inc();
t1 -= t1|0;
((poly_blamp(t2 - (t2|0), dt)
+ poly_blamp(t1, dt)) / pw * $pi * dt
+ (t1 < pw ? sin(t1/pw * $pi) * 2 : 0) - pw * /* 4/$pi */ 1.2732395447351627
) * a;
);
// Full-wave rectified sine
function poly_full()
instance(dt, a)
local(t)
(
t = this.t + 0.25;
this.poly_inc();
t -= t|0;
(poly_blamp(t, dt) * /* 2*$pi */ 6.2831853071795865 * dt
+ sin(t * $pi) * 2 - /* 4/$pi */ 1.2732395447351627
) * a;
);
// Pulse sine
function poly_sinp()
instance(dt, a)
local(t)
(
t = this.t;
this.poly_inc();
(-poly_blep(t + 0.5 - ((t + 0.5)|0), dt)
+ poly_blamp(t, dt) * $pi * dt
+ (t < 0.5 ? sin(t * $pi) * 2 : 0) - /* 2/$pi */ 0.63661977236758134
) * a;
);
// Saw pulse
function poly_sawp()
instance(dt, a)
local(t)
(
t = this.t + 0.5;
this.poly_inc();
t -= t|0;
(-poly_blep(t, dt)
+ (t >= 0.5 ? cos(t * /* 2*$pi */ 6.2831853071795865) + 1 : 0) - 0.5
) * a;
);
// Alternating sine
function poly_alt()
instance(dt, a)
local(t)
(
t = this.t;
this.poly_inc();
((poly_blamp(t + 0.5 - ((t + 0.5)|0), dt)
- poly_blamp(t, dt)) * /* -2*$pi */ -6.2831853071795865 * dt
+ (t < 0.5 ? sin(t * /* 4*$pi */ 12.566370614359173) : 0)
) * a;
);
// Camel sine
// Warning: Anti-aliasing doesn't work well at higher frequencies.
function poly_camel()
instance(dt, a)
local(t1, t2, t3)
(
t3 = (t2 = (t1 = this.t) + 0.5) + 0.25;
this.poly_inc();
((poly_blamp(t3 - (t3|0), dt) * 2
+ poly_blamp(t2 - (t2|0), dt)
+ poly_blamp(t1, dt)) * /* 4*$pi */ 12.566370614359173 * dt
+ (t1 < 0.5 ? abs(sin(t1 * /* 4*$pi */ 12.566370614359173)) * 2 : 0) - /* 2/$pi */ 0.63661977236758134
) * a;
);
// Alternating camel sine
function poly_camela()
instance(dt, a)
local(t1, t2, t3, y)
(
t3 = (t2 = (t1 = this.t) + 0.5) + 0.25;
this.poly_inc();
y = sin(t1 * /* 4*$pi */ 12.566370614359173);
t1 < 0.5 ? y = abs(y);
((poly_blamp(t2 - (t2|0), dt)
+ poly_blamp(t3 - (t3|0), dt)) * /* 4*$pi */ 12.566370614359173 * dt
+ y - /* 1/$pi */ 0.31830988618379067
) * a;
);
// Bipolar camel sine
function poly_camel2()
instance(dt, a)
local(t1, t2)
(
t2 = (t1 = this.t + 0.25) + 0.5;
this.poly_inc();
t1 -= t1|0;
((poly_blamp(t2 - (t2|0), dt)
- poly_blamp(t1, dt)) * /* 4*$pi */ 12.566370614359173 * dt
+ sin(abs(t1 - 0.5) * /* 4*$pi */ 12.566370614359173)
) * a;
);
// Bipolar squared sine
function poly_sin2()
instance(dt, a)
local(t)
(
t = this.t;
this.poly_inc();
((poly_bluh(t + 0.5 - ((t + 0.5)|0), dt)
- poly_bluh(t, dt)) * sqr(dt) * /* -sqrt($pi) */ -1.7724538509055160
+ (t < 0.5 ? 1 : -1) * sqr(sin(t * /* 2*$pi */ 6.2831853071795865))
) * a;
);
// Parabola
function poly_para()
instance(dt, a)
local(t)
(
t = this.t;
this.poly_inc();
((poly_bluh(t + 0.5 - ((t + 0.5)|0), dt)
- poly_bluh(t, dt)) * /* 2/3 */ 0.66666666666666667 * sqr(dt)
+ (t * 16 - 8) * (abs(t - 0.5) - 0.5)
) * a;
);
// Circle (approximation)
function poly_circ()
instance(dt, a, lp)
local(t1, t2, t3)
(
// n = 32;
t3 = (t2 = (t1 = this.t) + /* 1/(n*2) */ 0.015625) + 0.5;
this.poly_inc();
// Mimic linear-phase filter by mixing 1st-order low-pass filtered
// square...
t2 -= t2|0;
((lp += (
poly_blep(t2, dt)
- poly_blep(t3 - (t3|0), dt)
+ (t2 < 0.5) * 2 - 1
- lp) / (dt + /* 1/(2*$pi*sqrt(n)) */ 0.028134884879909565) * dt)
// ... with sine.
+ sin(t1 * /* 2*$pi */ 6.2831853071795865)) * 0.5 * a;
);
// Cycloid
// Warning: Anti-aliasing probably isn't correct!
function poly_arc()
instance(dt, a)
local(t)
(
t = this.t + 0.25;
this.poly_inc();
t -= t|0;
(poly_blamp(t, dt) * sqrt(dt) * /* 2*$pi */ 6.2831853071795865
+ sqrt(t - sqr(t)) * 4 - 1
) * a;
);
// Triangle
function poly_tri()
instance(dt, a)
local(t0, t1, t2, y, p)
(
t2 = (t1 = (t0 = this.t) + 0.25) + 0.5;
this.poly_inc();
y = abs((((t0 + 0.75)|0) - t0) * 4 - 1) - 1;
// y += poly_blamp(t1 - (t1|0), dt) * 4*dt;
(t1 -= t1|0) < dt ? (
p = t1/dt - 1;
y -= sqr(p)*p * 1.3333333333333333 * dt;
) :
t1 > 1 - dt ? (
p = (t1 - 1) / dt + 1;
y += sqr(p)*p * 1.3333333333333333 * dt;
);
// y -= poly_blamp(t2 - (t2|0), dt) * 4*dt;
(t2 -= t2|0) < dt ? (
p = t2/dt - 1;
y += sqr(p)*p * 1.3333333333333333 * dt;
) :
t2 > 1 - dt ? (
p = (t2 - 1) / dt + 1;
y -= sqr(p)*p * 1.3333333333333333 * dt;
);
y * a;
);
// Hyper triangular wave
function poly_hyptri()
instance(dt, a)
local(t1, t2)
(
t2 = (t1 = this.t) + 0.5;
this.poly_inc();
((poly_blamp(t1, dt) * /* 4/$pi */ 1.2732395447351627
- poly_blamp(t2 - (t2|0), dt) * 9.273) * dt
+ exp((t1 < 0.5 ? t1 : 1 - t1) * 4) * /* 2/(exp(2) - 1) */ 0.31303528549933130 - 1
) * a;
);
// Stepped triangle
function poly_trin()
instance(dt, a, n)
local(t1, t2, dt2, y1, y2)
(
t1 = (dt2 = 0.5 / n) * 0.5 + 0.25 + this.t;
this.poly_inc();
y1 = (((t1 -= t1|0) * n * 2)|0) * 2 / n;
t1 < 0.5 ? y1 -= 1 : y1 = 3 - y1;
t2 = t1 + 0.5;
y2 = 0;
loop(n,
// y2 -= poly_blep(t1, dt);
t1 < dt ? y2 += sqr(t1/dt - 1) : t1 > 1 - dt ? y2 -= sqr((t1 - 1) / dt + 1);
t1 += dt2;
t1 -= t1|0;
t2 -= t2|0;
// y2 += poly_blep(t2, dt);
t2 < dt ? y2 -= sqr(t2/dt - 1) : t2 > 1 - dt ? y2 += sqr((t2 - 1) / dt + 1);
t2 += dt2;
);
(y2 / n + y1) * a;
);
// Modified triangle
function poly_tri2()
instance(dt, a)
local(pw, t1, t2)
(
t2 = (t1 = (pw = min(max(0.0001, this.pw), 0.9999)) * 0.5 + this.t) - pw + 1;
this.poly_inc();
t1 -= t1|0;
((poly_blamp(t1, dt)
- poly_blamp(t2 - (t2|0), dt)) / (pw - sqr(pw)) * dt
+ (t1 < pw ? t1 / pw : (t1 - pw) / (pw - 1) + 1) * 2 - 1
) * a;
);
// Triangular pulse
function poly_trip()
instance(dt, a, pw)
local(t1, t2, t3)
(
t2 = (t1 = (t3 = this.t + 0.75) + pw * 0.5) - pw;
this.poly_inc();
t1 -= t1|0;
((poly_blamp(t3 - (t3|0), dt) * -2
+ poly_blamp(t2 - (t2|0), dt)
+ poly_blamp(t1, dt)) / pw * 2*dt
+ (t1 < pw ? 2 - abs(t1 * 4/pw - 2) : 0) - pw
) * a;
);
// Trapezoid
function poly_trap()
instance(dt, a)
local(t0, t1, t2, t3, t4)
(
t4 = (t2 = (t3 = (t1 = (t0 = this.t) + 0.125) + 0.25) + 0.25) + 0.25;
this.poly_inc();
(min(max(-1, (abs((((t0 + 0.75)|0) - t0) * -4 + 1) - 1) * 2), 1)
+ (poly_blamp(t1 - (t1|0), dt)
- poly_blamp(t2 - (t2|0), dt)
+ poly_blamp(t3 - (t3|0), dt)
- poly_blamp(t4 - (t4|0), dt)) * 4*dt
) * a;
);
function poly_trap2()
instance(dt, a)
local(pw, scale, t0, t1, t2, t3, t4)
(
pw = min(0.9999, this.pw);
t4 = (t3 = (t2 = (t1 = (t0 = this.t) + pw * 0.25 + 0.25) + 0.5) - pw * 0.5) + 0.5;
this.poly_inc();
(min(max(-1, (abs((((t0 + 0.75)|0) - t0) * 4 - 1) - 1) * (scale = 1 / (1 - pw))), 1)
+ (poly_blamp(t1 - (t1|0), dt)
- poly_blamp(t2 - (t2|0), dt)
- poly_blamp(t3 - (t3|0), dt)
+ poly_blamp(t4 - (t4|0), dt)) * scale * 2*dt
) * a;
);
// Square
function poly_sqr()
instance(dt, a)
local(t1, t2, y)
(
t2 = (t1 = this.t) + 0.5;
this.poly_inc();
y = (t1 < 0.5) * 2 - 1;
// y += poly_blep(t1, dt);
t1 < dt ? y -= sqr(t1/dt - 1) : t1 > 1 - dt ? y += sqr((t1 - 1) / dt + 1);
// y -= poly_blep(t2 - (t2|0), dt);
(t2 -= t2|0) < dt ? y += sqr(t2/dt - 1) : t2 > 1 - dt ? y -= sqr((t2 - 1) / dt + 1);
y * a;
);
// Low-pass filtered square approximations
function poly_lpsqr2()
instance(dt, a)
local(t1, t2, y, p)
(
t2 = (t1 = this.t) + 0.5;
this.poly_inc();
y = (t1 < 0.5 ? 0.5 - sqr(t1 * 2 - 1) : sqr((1 - t1) * 2) - 0.5) * 2;
// y += poly_blamp(t1, dt) * 4*dt;
t1 < dt ? (
p = t1/dt - 1;
y -= sqr(p)*p * 1.3333333333333333 * dt;
) :
t1 > 1 - dt ? (
p = (t1 - 1) / dt + 1;
y += sqr(p)*p * 1.3333333333333333 * dt;
);
// y -= poly_blamp(t2 - (t2|0), dt) * 4*dt;
(t2 -= t2|0) < dt ? (
p = t2/dt - 1;
y += sqr(p)*p * 1.3333333333333333 * dt;
) :
t2 > 1 - dt ? (
p = (t2 - 1) / dt + 1;
y -= sqr(p)*p * 1.3333333333333333 * dt;
);
y * a;
);
function poly_lpsqr3()
instance(dt, a)
local(t1, t2, y, p)
(
t2 = (t1 = this.t) + 0.5;
this.poly_inc();
t1 < 0.5 ? (
p = t1 * 2 - 1;
y = sqr(p)*p * 2 + 1;
) : (
p = (1 - t1) * 2;
y = sqr(p)*p * 2 - 1;
);
// y += poly_blamp(t1, dt) * 6*dt;
t1 < dt ? (
p = t1/dt - 1;
y -= sqr(p)*p * 2*dt;
) :
t1 > 1 - dt ? (
p = (t1 - 1) / dt + 1;
y += sqr(p)*p * 2*dt;
);
// y -= poly_blamp(t2 - (t2|0), dt) * 6*dt;
(t2 -= t2|0) < dt ? (
p = t2/dt - 1;
y += sqr(p)*p * 2*dt;
) :
t2 > 1 - dt ? (
p = (t2 - 1) / dt + 1;
y -= sqr(p)*p * 2*dt;
);
y * a;
);
// Integrated low-pass filtered square approximations
function poly_intlpsqr2()
instance(dt, a)
local(t0, t1, t2, y)
(
// (32*t^3 - 48*t^2 + 12*t + 1) / (2*sqrt(2) - 1) = 0
t2 = (t1 = (t0 = this.t + 0.41503077834420436) + 0.5) + 0.5;
this.poly_inc();
(t0 -= t0|0) < 0.5 ? y = 0.5 : t0 += (y = -0.5);
((((64*t0 - 96)*t0 + 24)*t0 + 2) * y
+ (poly_bluh(t1 - (t1|0), dt)
- poly_bluh(t2 - (t2|0), dt)) * sqr(dt)
) * /* -1/(2*sqrt(2) - 1) */ -0.54691816067802716 * a;
);
function poly_intlpsqr3()
instance(dt, a)
local(t0, t1, t2, y)
(
// (64*t^4 - 128*t^3 + 96*t^2 - 16*t - 2) / (3*2^(2/3) - 2) = 0
t2 = (t1 = (t0 = this.t + 0.37399153968783991) + 0.5) + 0.5;
this.poly_inc();
(t0 -= t0|0) < 0.5 ? y = -0.5 : t0 -= (y = 0.5);
(((((64*t0 - 128)*t0 + 96)*t0 - 16)*t0 - 2) * y
+ (poly_bluh(t1 - (t1|0), dt)
- poly_bluh(t2 - (t2|0), dt)) * sqr(dt)
) * /* -2/(3*2^(2/3) - 2) */ -0.72405970419833828 * a;
);
// Band-pass filtered square approximation
function poly_bpsqr2()
instance(dt, a)
local(t1, t2)
(
t2 = (t1 = this.t) + 0.5;
this.poly_inc();
t2 -= t2|0;
(((poly_blamp(t1, dt)
- poly_blamp(t2, dt)) * 3.2
+ (poly_bluh(t2, dt)
- poly_bluh(t1, dt)) * /* 8/15 */ 0.53333333333333333 * dt) * dt
+ (t1 < 0.5 ? (0.75 - t1)*t1 - 0.0625 : (t1 - 1.75)*t1 + 0.6875) * 12.8
) * a;
);
// Square wave minus fundamental
function poly_sqrm1()
instance(dt, a)
local(t1, t2)
(
t2 = (t1 = this.t) + 0.5;
this.poly_inc();
// ((t1 < 0.5) * 2 - 1 - sin(2*$pi*t1) * 4/$pi * (1 - 3.28765 /* @ 1 kHz */ * dt^2)
((sin(t1 * /* 2*$pi */ 6.2831853071795865) * (sqr(dt) * /* 6.5753/$pi */ 2.0929829946242788 - /* 2/$pi */ 0.63661977236758134)
+ (t1 < 0.5)) * 2 - 1
+ poly_blep(t1, dt)
- poly_blep(t2 - (t2|0), dt)
) * a;
);
// Modified square
function poly_sqr2()
instance(dt, a, pw)
local(t1, t2, t3, t4)
(
t4 = (t3 = (t2 = (t1 = pw * 0.25 + this.t + 0.75) - 0.5) - pw * 0.5) + 0.5;
this.poly_inc();
t1 -= t1|0;
t3 -= t3|0;
((poly_blep(t1, dt)
- poly_blep(t2 - (t2|0), dt)
+ poly_blep(t3, dt)
- poly_blep(t4 - (t4|0), dt)) * 0.5
+ (t1 < 0.5) + (t3 < 0.5) - 1
) * a;
);
// Rectangle
function poly_rect()
instance(dt, a, pw)
local(t1, t2, y)
(
t2 = (t1 = this.t) - pw + 1;
this.poly_inc();
y = ((t1 < pw) - pw) * 2;
// y += poly_blep(t1, dt);
t1 < dt ? y -= sqr(t1/dt - 1) : t1 > 1 - dt ? y += sqr((t1 - 1) / dt + 1);
// y -= poly_blep(t2 - (t2|0), dt);
(t2 -= t2|0) < dt ? y += sqr(t2/dt - 1) : t2 > 1 - dt ? y -= sqr((t2 - 1) / dt + 1);
y * a;
);
// Sawtooth
function poly_saw()
instance(dt, a)
local(t)
(
t = this.t + 0.5;
this.poly_inc();
t -= t|0;
(-poly_blep(t, dt)
+ t * 2 - 1
) * a;
);
// Ramp
function poly_ramp()
instance(dt, a)