Repository navigation
Expand file tree
/
Copy pathoscillators.lib
More file actions
3239 lines (3016 loc) · 105 KB
/
Copy pathoscillators.lib
File metadata and controls
3239 lines (3016 loc) · 105 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//############################## oscillators.lib ######################################
// Oscillators library. Its official prefix is `os`.
//
// This library provides a wide range of oscillator designs for sound synthesis.
// It includes classic waveforms (sine, sawtooth, square, triangle), band-limited and anti-aliased
// oscillators, phase and frequency modulation units, as well as noise-based and physical-model
// driven oscillators for advanced synthesis techniques in Faust.
//
// The oscillators library is organized into 9 sections:
//
// * [Wave-Table-Based Oscillators](#wave-table-based-oscillators)
// * [Low Frequency Oscillators](#low-frequency-oscillators)
// * [Low Frequency Sawtooths](#low-frequency-sawtooths)
// * [Alias-Suppressed Sawtooth](#alias-suppressed-sawtooth)
// * [Alias-Suppressed Pulse, Square, and Impulse Trains](#alias-suppressed-pulse-square-and-impulse-trains)
// * [Filter-Based Oscillators](#filter-based-oscillators)
// * [Waveguide-Resonator-Based Oscillators](#waveguide-resonator-based-oscillators)
// * [Casio CZ Oscillators](#casio-cz-oscillators)
// * [PolyBLEP-Based Oscillators](#polyblep-based-oscillators)
//
// #### References
//
// * <https://github.com/grame-cncm/faustlibraries/blob/master/oscillators.lib>
//########################################################################################
/************************************************************************
************************************************************************
FAUST library file, GRAME section
Except where noted otherwise, Copyright (C) 2003-2017 by GRAME,
Centre National de Creation Musicale.
----------------------------------------------------------------------
GRAME LICENSE
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
EXCEPTION TO THE LGPL LICENSE : As a special exception, you may create a
larger FAUST program which directly or indirectly imports this library
file and still distribute the compiled code generated by the FAUST
compiler, or a modified version of this compiled code, under your own
copyright and license. This EXCEPTION TO THE LGPL LICENSE explicitly
grants you the right to freely choose the license for the resulting
compiled code. In particular the resulting compiled code has no obligation
to be LGPL or GPL. For example you are free to choose a commercial or
closed source license or any other license if you decide so.
************************************************************************
************************************************************************/
ma = library("maths.lib");
ba = library("basics.lib");
de = library("delays.lib");
fi = library("filters.lib");
si = library("signals.lib");
declare name "Faust Oscillator Library";
declare version "1.9.6";
// This library contains platform specific constants
pl = library("platform.lib");
//=========================Wave-Table-Based Oscillators===================================
// Oscillators using tables. The table size is set by the
// [pl.tablesize](https://github.com/grame-cncm/faustlibraries/blob/master/platform.lib) constant.
//
// The phasors (`phasor`, `hs_phasor`, `hsp_phasor`, and the `lf_sawpos` family, on which
// the table-based, `m_osc` and `lf_` oscillators are built) share the internal `_phasor_imp`.
//
// In single precision (`faust -single`, the default), it keeps the phase in fixed point
// (an integer count of 2^-30 cycle plus a 24-bit fraction of a count), advanced by the
// float increment `freq/ma.SR`. The rounding of that increment is the only frequency
// error, below 1e-7 relative, with no accumulated drift, at any frequency and sign;
// the output is truncated to a multiple of 2^-24 in [0, 1[. Accumulating the phase in
// float instead (as up to version 1.9.1) rounds each sum to the float spacing near
// the phase (6e-8 near 1), a large fraction of a low-frequency increment: the frequency
// was off by 0.1 % at 2 Hz and 192 kHz, by up to 8 % at 0.01 Hz, and the phasor stopped
// below `ma.SR`/2^25 (0.0057 Hz at 192 kHz).
//
// In double precision, the phase is accumulated in floating point, with a relative
// frequency error below 1e-12 from 2 Hz up (3e-10 at 0.01 Hz and 192 kHz). There, an
// incremental step smaller than `ma.EPSILON` (2.2e-16, a frequency below 1e-11 Hz) has
// no effect when summed to 1, but it is enough to make the fractional function wrap
// around when summed to 0, so that the output alternates between 1 and 0. The incremental step can be clipped to guarantee that the phasor will always
// run correctly for its full cycle: this safer version is used when `SAFE=1` is set in
// the environment using
// [explicit substitution](https://faustdoc.grame.fr/manual/syntax/#explicit-substitution) syntax,
// for example `process = os[SAFE=1;].phasor(1.0, -.001);`.
//========================================================================================
//-----------------------`(os.)SAFE`------------------------
// Global parameter selecting the safer version of the internal phasor
// implementation (0: faster version, 1: safer version, protecting
// against the small-increment numerical problem described at the top of
// this section). It affects double (and higher) precision only: the
// single-precision phasor is fixed point and needs no protection. Meant to be overridden through
// [explicit substitution](https://faustdoc.grame.fr/manual/syntax/#explicit-substitution)
// syntax, and usable by other functions as well.
//
// #### Usage
//
// ```
// SAFE : _
// os[SAFE=1;].phasor(1.0, -.001) : _
// ```
//
// #### Test
// ```
// os = library("oscillators.lib");
// SAFE_test = os.SAFE, os[SAFE=1;].phasor(1.0, -.001);
// ```
//------------------------------------------------------------
SAFE = 0; // 0: use the faster version, 1: use the safer version
//-----------------------`(os.)sinwaveform`------------------------
// Sine waveform ready to use with a `rdtable`.
//
// #### Usage
//
// ```
// sinwaveform(tablesize) : _
// ```
//
// Where:
//
// * `tablesize`: the table size
//
// #### Test
// ```
// os = library("oscillators.lib");
// sinwaveform_test = os.sinwaveform(1024);
// ```
//------------------------------------------------------------
sinwaveform(tablesize) =
sin(float(ba.period(tablesize)) * (2.0 * ma.PI) / float(tablesize));
//-----------------------`(os.)coswaveform`------------------------
// Cosine waveform ready to use with a `rdtable`.
//
// #### Usage
//
// ```
// coswaveform(tablesize) : _
// ```
//
// Where:
//
// * `tablesize`: the table size
//
// #### Test
// ```
// os = library("oscillators.lib");
// coswaveform_test = os.coswaveform(1024);
// ```
//------------------------------------------------------------
coswaveform(tablesize) =
cos(float(ba.period(tablesize)) * (2.0 * ma.PI) / float(tablesize));
// Possibly faster version using integer arithmetic
phasor_env(freq, N) = environment {
//------- GLOBAL PARAMS
nbits = 31;
tablesize = 1<<N;
accuracy = int(nbits - N);
mask = (1<<nbits)-1;
inc(step) = int(tablesize * step * (1<<accuracy));
//------- LAMBDA DSP CASE
lambda(inc_op) = (inc_op : &(mask)) ~ _ : >>(accuracy) : /(tablesize);
//------- MINIMAL CASE
hsp(0,0) = lambda(+(inc(freq/ma.SR)'));
//------- GENERAL CASE
hsp(reset,phase) = lambda(select2(hard_reset,+(inc(freq/ma.SR)),inc(phase)))
with {
hard_reset = (1-1')|reset;
};
};
declare phasor_env author "Pierre Mascarade Relano, Maxime Sirbu, Stéphane Letz";
// Generic phasor with `reset` and `phase` parameters to be specialised in concrete use-cases.
// Single precision accumulates the phase in fixed point (`_phasor_fix`); double (and
// quad, fixed-point) precision keeps the float accumulator `_phasor_flt`, unchanged.
singleprecision _phasor_imp = _phasor_fix;
doubleprecision _phasor_imp = _phasor_flt;
quadprecision _phasor_imp = _phasor_flt;
fixedpointprecision _phasor_imp = _phasor_flt;
// Fixed-point phasor: the phase is an integer count of 2^-30 cycle plus an
// integer fraction of a count in 2^-24 steps (54 bits in all), so the phase
// advances by exactly the float increment freq/SR, whose rounding (about 1e-7
// relative) is then the only frequency error, with no accumulated drift. Only
// integer additions run in the recursion, so neither the Faust normalizer nor
// -ffast-math can reorder them. Integer wraparound is exact for both signs of
// freq, every frequency advances, and freq = 0 freezes the phase.
// The output keeps the top 24 bits, so it is exact in float and never reaches 1.
_phasor_fix(freq, reset, phase) = _phasor_fix_counts(freq, reset, phase) : (_,!) : out
with {
out(p) = float(p >> 6) * (1.0/16777216.0);
};
// The state (p, s) of `_phasor_fix`: the phase is (p + s/2^24)/2^30 cycle.
_phasor_fix_counts(freq, reset, phase) = (((freq : counts) : _fixpt.split), hard_reset, (phase : start))
: loop ~ (_,_)
with {
Q = _fixpt.Q;
mask = _fixpt.mask;
// increment in counts, |x| <= 2^29, so that the phase sum never overflows
counts(f) = (inc - rint(inc)) * Q with { inc = f/ma.SR; };
start(ph) = int(ma.decimal(ph) * Q) & mask;
loop(p, s, xi, xs, hr, p0) = _fixpt.add(p, s, xi, xs)
: \(q, t).(select2(hr, q & mask, p0), select2(hr, t & 16777215, 0));
// To correctly start at `phase` at the first sample
hard_reset = (1-1')|reset;
};
// Fixed-point phase arithmetic shared by `_phasor_fix` and `saw2ptr`.
// A phase is an integer count p of 2^-30 cycle (0 <= p < 2^30) plus an integer
// fraction s of a count in 2^-24 steps (0 <= s < 2^24).
_fixpt = environment {
Q = 1073741824.0; // 2^30 counts per cycle
mask = 1073741823; // 2^30 - 1
// split an increment x in counts into its whole and fractional counts
split(x) = int(floor(x)), int((x - floor(x)) * 16777216.0); // exact for |x| >= 1
// add the increment (xi, xs) to the phase (p, s): returns the count sum q,
// before the wrap (q & mask), and the fraction sum t (carry in bit 24)
add(p, s, xi, xs) = p + xi + (t >> 24), t
with {
t = s + xs;
};
};
// Floating-point phasor (double precision and above)
_phasor_flt(freq, reset, phase) = (select2(hard_reset, +(incr(SAFE)), phase) : ma.decimal) ~ _
with {
incr_aux = freq/ma.SR;
// Faster but less accurate version
incr(0) = incr_aux;
// To make sure that the incremental step is greater or equal to EPSILON or
// less than or equal to -EPSILON to avoid numerical problems.
// A frequency of 0Hz can still be used to freeze the phasor.
incr(1)= (freq != 0) * ba.if(freq < 0, min(-1.0 * ma.EPSILON, incr_aux), max(ma.EPSILON, incr_aux));
// To correctly start at `phase` at the first sample
hard_reset = (1-1')|reset;
};
// Possibly faster version using integer arithmetic
// phasor_imp(freq, reset, phase) = phasor_env(freq, 16).hsp(reset, phase);
// Version to be used with tables
_phasor_table(tablesize, freq, reset, phase) = _phasor_imp(freq, reset, phase) : *(float(tablesize));
//-----------------------`(os.)phasor`------------------------
// A simple phasor to be used with a `rdtable`.
// `phasor` is a standard Faust function.
//
// #### Usage
//
// ```
// phasor(tablesize,freq) : _
// ```
//
// Where:
//
// * `tablesize`: the table size
// * `freq`: the frequency in Hz
//
// Note that `tablesize` is just a multiplier for the output of a unit-amp phasor
// so `phasor(1.0, freq)` can be used to generate a phasor output in the range [0, 1[.
//
// #### Test
// ```
// os = library("oscillators.lib");
// ba = library("basics.lib");
// ma = library("maths.lib");
// phasor_test = os.phasor(1024, 440);
// phasor_slider_test = os.phasor(1024, hslider("phasor:freq", 440, -20000, 20000, 1));
// phasor_modulated_test = os.phasor(1024, 2000*(2*tri - 1)) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
// ```
//------------------------------------------------------------
phasor(tablesize, freq) = _phasor_table(tablesize, freq, 0, 0);
//-----------------------`(os.)hs_phasor`------------------------
// Hardsyncing phasor to be used with a `rdtable`.
//
// #### Usage
//
// ```
// hs_phasor(tablesize,freq,reset) : _
// ```
//
// Where:
//
// * `tablesize`: the table size
// * `freq`: the frequency in Hz
// * `reset`: a reset signal, reset phase to 0 when equal to 1
//
// #### Test
// ```
// os = library("oscillators.lib");
// ba = library("basics.lib");
// hs_phasor_test = os.hs_phasor(1024, 330, ba.pulse(32));
// ```
//---------------------------------------------------------
declare hs_phasor author "Mike Olsen, revised by Stéphane Letz";
hs_phasor(tablesize, freq, reset) = _phasor_table(tablesize, freq, reset, 0);
//-----------------------`(os.)hsp_phasor`------------------------
// Hardsyncing phasor with selectable phase to be used with a `rdtable`.
//
// #### Usage
//
// ```
// hsp_phasor(tablesize,freq,reset,phase)
// ```
//
// Where:
//
// * `tablesize`: the table size
// * `freq`: the frequency in Hz
// * `reset`: reset the oscillator to phase when equal to 1
// * `phase`: phase between 0 and 1
//
// #### Test
// ```
// os = library("oscillators.lib");
// hsp_phasor_test = os.hsp_phasor(1024, 330, button("reset"), 0.25);
// ```
//---------------------------------------------------------
declare hsp_phasor author "Christophe Lebreton, revised by Stéphane Letz";
hsp_phasor(tablesize, freq, reset, phase) = _phasor_table(tablesize, freq, reset, phase);
//-----------------------`(os.)oscsin`------------------------
// Sine wave oscillator.
// `oscsin` is a standard Faust function.
//
// #### Usage
//
// ```
// oscsin(freq) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// ba = library("basics.lib");
// ma = library("maths.lib");
// oscsin_test = os.oscsin(440);
// oscsin_slider_test = os.oscsin(hslider("oscsin:freq", 440, -20000, 20000, 1));
// oscsin_modulated_test = os.oscsin(2000*(2*tri - 1)) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
// ```
//------------------------------------------------------------
oscsin(freq) = rdtable(tablesize, sinwaveform(tablesize), int(phasor(tablesize,freq)))
with {
tablesize = pl.tablesize;
};
//-----------------------`(os.)hs_oscsin`------------------------
// Sin lookup table with hardsyncing phase.
//
// #### Usage
//
// ```
// hs_oscsin(freq,reset) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
// * `reset`: reset the oscillator to 0 when equal to 1
//
// #### Test
// ```
// os = library("oscillators.lib");
// ba = library("basics.lib");
// hs_oscsin_test = os.hs_oscsin(440, ba.pulse(32));
// ```
//---------------------------------------------------------
declare hs_oscsin author "Mike Olsen";
hs_oscsin(freq,reset) = rdtable(tablesize, sinwaveform(tablesize), int(hs_phasor(tablesize,freq,reset)))
with {
tablesize = pl.tablesize;
};
//-----------------------`(os.)osccos`------------------------
// Cosine wave oscillator.
//
// #### Usage
//
// ```
// osccos(freq) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// osccos_test = os.osccos(440);
// ```
//------------------------------------------------------------
osccos(freq) = rdtable(tablesize, coswaveform(tablesize), int(phasor(tablesize,freq)))
with {
tablesize = pl.tablesize;
};
//-----------------------`(os.)hs_osccos`------------------------
// Cos lookup table with hardsyncing phase.
//
// #### Usage
//
// ```
// hs_osccos(freq,reset) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
// * `reset`: reset the oscillator to 0 when equal to 1
//
// #### Test
// ```
// os = library("oscillators.lib");
// hs_osccos_test = os.hs_osccos(440, button("reset"));
// ```
//---------------------------------------------------------
declare hs_osccos author "Stéphane Letz";
hs_osccos(freq,reset) = rdtable(tablesize, coswaveform(tablesize), int(hs_phasor(tablesize,freq,reset)))
with {
tablesize = pl.tablesize;
};
//-----------------------`(os.)oscp`------------------------
// A sine wave generator with controllable phase.
//
// #### Usage
//
// ```
// oscp(freq,phase) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
// * `phase`: the phase in radian
//
// #### Test
// ```
// os = library("oscillators.lib");
// ma = library("maths.lib");
// oscp_test = os.oscp(440, ma.PI/3);
// ```
//------------------------------------------------------------
oscp(freq,phase) = oscsin(freq) * cos(phase) + osccos(freq) * sin(phase);
//-----------------------`(os.)osci`------------------------
// Interpolated phase sine wave oscillator.
//
// #### Usage
//
// ```
// osci(freq) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// osci_test = os.osci(440);
// ```
//------------------------------------------------------------
osci(freq) = s1 + d * (s2 - s1)
with {
tablesize = pl.tablesize;
i = int(phasor(tablesize,freq));
d = ma.decimal(phasor(tablesize,freq));
s1 = rdtable(tablesize+1,sinwaveform(tablesize),i);
s2 = rdtable(tablesize+1,sinwaveform(tablesize),i+1);
};
//-----------------------`(os.)osc`------------------------
// Default sine wave oscillator (same as [oscsin](#oscsin)).
// `osc` is a standard Faust function.
//
// #### Usage
//
// ```
// osc(freq) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// ba = library("basics.lib");
// ma = library("maths.lib");
// osc_test = os.osc(440);
// osc_slider_test = os.osc(hslider("osc:freq", 440, -20000, 20000, 1));
// osc_modulated_test = os.osc(2000*(2*tri - 1)) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
// ```
//------------------------------------------------------------
osc = oscsin;
//-----------------------`(os.)m_oscsin`------------------------
// Sine wave oscillator based on the `sin` mathematical function.
//
// #### Usage
//
// ```
// m_oscsin(freq) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// m_oscsin_test = os.m_oscsin(440);
// ```
//------------------------------------------------------------
m_oscsin(freq) = lf_sawpos(freq) : *(2*ma.PI) : sin;
//-----------------------`(os.)m_osccos`------------------------
// Sine wave oscillator based on the `cos` mathematical function.
//
// #### Usage
//
// ```
// m_osccos(freq) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// m_osccos_test = os.m_osccos(440);
// ```
//------------------------------------------------------------
m_osccos(freq) = lf_sawpos(freq) : *(2*ma.PI) : cos;
//-----------------------`(os.)tphase`------------------------
// Integer phase accumulator: the phase counter `m(n) = (m(n-1) + p) mod N`,
// computed in integers, so exactly, without drift.
//
// A phasor (`phasor`, `lf_sawpos`) adds the increment `freq/ma.SR`, rounded
// to the precision of the program, at every sample: its rounding error
// accumulates, and the phase of a single-precision render drifts away from
// that of a double-precision one.
// `tphase` counts in integers instead. Its properties:
//
// * **exact**: `m(n) = ((n+1)·p) mod N`, with no rounding, as long as
// `N + |p|` stays below 2^31 (the range of a Faust `int`);
// * **the same in every precision**: the integer arithmetic does not depend
// on `-single`, `-double` or the compilation options;
// * **periodic**: the period is exactly `N/gcd(N, p)` samples;
// * **in range**: `0 <= m < N` for `p >= 0`, and `-N < m <= 0` for
// `p < 0` (the remainder takes the sign of the counter);
// * **continuous when `p` changes**: a step changed at run time changes the
// speed of the counter, not its current value.
//
// `float(m)/N` is a phase in cycles; it is exact in `float` as long as
// `N <= 2^24`. The counter starts at `p` (the first sample is `p mod N`).
//
// #### Usage
//
// ```
// tphase(N, p) : _
// ```
//
// Where:
//
// * `N`: the modulus, a positive integer (the number of steps in one cycle)
// * `p`: the step, an integer (positive or negative), added at every sample
//
// #### Test
// ```
// os = library("oscillators.lib");
// tphase_test = os.tphase(480000, 4400);
// tphase_slider_test = os.tphase(480000, int(hslider("step", 4400, -48000, 48000, 1)));
// ```
//------------------------------------------------------------
tphase(N, p) = %(N) ~ +(p);
declare tphase license "LicenseRef-LGPL-2.1-or-later-with-Faust-exception";
//-----------------------`(os.)tosc`------------------------
// Sine wave oscillator on an exact integer phase: the same signal in single
// and double precision, at exactly the requested frequency (to 0.1 Hz).
//
// `osc` and `m_oscsin` add the increment `freq/ma.SR`, rounded differently in
// single and double precision: the two renders drift apart in phase (up to
// 1.7e-5 cycle after one second at 440 Hz, 1e-3 after a minute), and only
// their levels can be compared. `tosc` reads the sine of an integer phase
// `m = tphase(N, p)` with `N = 10·SR` and `p = round(10·freq)`. Its
// properties:
//
// * **no drift**: the phase `m/N` is exact at every sample (see `tphase`);
// the single- and double-precision outputs differ only by the rounding of
// `2π·m/N` and of `sin`, a few ulps at every sample, never accumulated;
// * **exact frequency**: the frequency played is `p/10` Hz, that is `freq`
// rounded to the nearest 0.1 Hz, halves away from zero (`round`, not
// `rint`, which rounds halves to even: 0.25 Hz plays 0.3 Hz, and -0.25 Hz
// plays -0.3 Hz). A frequency below 0.05 Hz in magnitude gives `p = 0`, a
// constant output of 0. The step is the same integer in single and double
// precision, unless `10·freq` falls within a rounding error of a half;
// * **independent of the sample rate**: the step `p` depends on `freq`
// only, never on `SR`, so the same `freq` gives the same frequency at
// every rate, without a rounding that depends on `SR`;
// * **valid ranges**: `SR` an integer up to 1677721 Hz (`N <= 2^24`, so that
// `float(m)` is exact), and any `freq` (negative frequencies included;
// above Nyquist, it aliases like any sampled sine);
// * **phase continuous** when `freq` changes at run time.
//
// The first sample is `sin(2π·p/N)`, not 0. `tosc` is meant as a test source
// for numerical checks (comparing renders across precisions, sample rates or
// compilation options); `sin` is computed at every sample, so it costs more
// than the table-based `osc`.
//
// #### Usage
//
// ```
// tosc(freq) : _
// ```
//
// Where:
//
// * `freq`: the frequency in Hz (rounded to the nearest 0.1 Hz, halves away from zero)
//
// #### Test
// ```
// os = library("oscillators.lib");
// ba = library("basics.lib");
// ma = library("maths.lib");
// tosc_test = os.tosc(440);
// tosc_12000_test = os.tosc(12000);
// tosc_lfo_test = os.tosc(0.1);
// tosc_slider_test = os.tosc(hslider("freq", 440, -20000, 20000, 0.1));
// tosc_modulated_test = os.tosc(20*pow(250, tri)) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
// ```
//------------------------------------------------------------
tosc(freq) = sin(2.0 * ma.PI * float(tphase(N, p)) / N)
with {
p = int(round(freq * 10)); // the integer step, in tenths of a hertz, halves away from zero
N = 10 * int(ma.SR); // a tenth of a hertz of resolution
};
declare tosc license "LicenseRef-LGPL-2.1-or-later-with-Faust-exception";
// end GRAME section
//########################################################################################
/************************************************************************
FAUST library file, jos section
Except where noted otherwise, The Faust functions below in this
section are Copyright (C) 2003-2022 by Julius O. Smith III <jos@ccrma.stanford.edu>
([jos](http://ccrma.stanford.edu/~jos/)), and released under the
(MIT-style) [STK-4.3](#stk-4.3-license) license.
The MarkDown comments in this section are Copyright 2016-2017 by Romain
Michon and Julius O. Smith III, and are released under the
[CCA4I](https://creativecommons.org/licenses/by/4.0/) license (TODO: if/when Romain agrees)
************************************************************************/
//===============================Low Frequency Oscillators===============================
// Low Frequency Oscillators (LFOs) have prefix `lf_`
// (no aliasing suppression, since it is inaudible at LF).
// Use `sawN` and its derivatives for audio oscillators with suppressed aliasing.
//==================================================================
//--------`(os.)lf_imptrain`----------
// Unit-amplitude low-frequency impulse train.
// `lf_imptrain` is a standard Faust function.
//
// #### Usage
//
// ```
// lf_imptrain(freq) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_imptrain_test = os.lf_imptrain(3);
// ```
//------------------------------------------------------------
lf_imptrain(freq) = lf_sawpos(freq)<:-(mem)<0; // definition below
//--------`(os.)lf_pulsetrainpos`----------
// Unit-amplitude nonnegative LF pulse train, duty cycle between 0 and 1.
//
//
// #### Usage
//
// ```
// lf_pulsetrainpos(freq, duty) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
// * `duty`: duty cycle between 0 and 1
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_pulsetrainpos_test = os.lf_pulsetrainpos(3, 0.35);
// ```
//------------------------------------------------------------
lf_pulsetrainpos(freq,duty) = float(lf_sawpos(freq) <= duty);
//pulsetrainpos = lf_pulsetrainpos; // for backward compatibility
//--------`(os.)lf_pulsetrain`----------
// Unit-amplitude zero-mean LF pulse train, duty cycle between 0 and 1.
//
// #### Usage
//
// ```
// lf_pulsetrain(freq,duty) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
// * `duty`: duty cycle between 0 and 1
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_pulsetrain_test = os.lf_pulsetrain(3, 0.35);
// ```
//------------------------------------------------------------
lf_pulsetrain(freq,duty) = 2.0*lf_pulsetrainpos(freq,duty) - 1.0;
//--------`(os.)lf_squarewavepos`----------
// Positive LF square wave in [0,1]
//
// #### Usage
//
// ```
// lf_squarewavepos(freq) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_squarewavepos_test = os.lf_squarewavepos(3);
// ```
//------------------------------------------------------------
lf_squarewavepos(freq) = lf_pulsetrainpos(freq,0.5);
// squarewavepos = lf_squarewavepos; // for backward compatibility
//--------`(os.)lf_squarewave`----------
// Zero-mean unit-amplitude LF square wave.
// `lf_squarewave` is a standard Faust function.
//
// #### Usage
//
// ```
// lf_squarewave(freq) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_squarewave_test = os.lf_squarewave(3);
// ```
//------------------------------------------------------------
lf_squarewave(freq) = 2.0*lf_squarewavepos(freq) - 1.0;
// squarewave = lf_squarewave; // for backward compatibility
//--------`(os.)lf_trianglepos`----------
// Positive unit-amplitude LF positive triangle wave.
//
// #### Usage
//
// ```
// lf_trianglepos(freq) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_trianglepos_test = os.lf_trianglepos(3);
// ```
//------------------------------------------------------------
lf_trianglepos(freq) = 1.0-abs(saw1(freq)); // saw1 defined below
//----------`(os.)lf_triangle`----------
// Zero-mean unit-amplitude LF triangle wave.
// `lf_triangle` is a standard Faust function.
//
// #### Usage
//
// ```
// lf_triangle(freq) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_triangle_test = os.lf_triangle(3);
// ```
//------------------------------------------------------------
declare lf_triangle author "Bart Brouns";
declare lf_triangle licence "LicenseRef-STK-4.3";
lf_triangle(freq) = 2.0*lf_trianglepos(freq) - 1.0;
//================== Low Frequency Sawtooths ====================
// Sawtooth waveform oscillators for virtual analog synthesis et al.
// The 'simple' versions (`lf_rawsaw`, `lf_sawpos` and `saw1`), are mere samplings of
// the ideal continuous-time ("analog") waveforms. While simple, the
// aliasing due to sampling is quite audible. The differentiated
// polynomial waveform family (`saw2`, `sawN`, and derived functions)
// do some extra processing to suppress aliasing (not audible for
// very low fundamental frequencies). According to Lehtonen et al.
// (JASA 2012), the aliasing of `saw2` should be inaudible at fundamental
// frequencies below 2 kHz or so, for a 44.1 kHz sampling rate and 60 dB SPL
// presentation level; fundamentals 415 and below required no aliasing
// suppression (i.e., `saw1` is ok).
// `lf_sawpos` and `saw1` share the phasor of the Wave-Table-Based Oscillators
// section, whose single-precision frequency error is below 1e-7 relative.
//=====================================================================
//-----------------`(os.)lf_rawsaw`--------------------
// Simple sawtooth waveform oscillator between 0 and period in samples.
//
// #### Usage
//
// ```
// lf_rawsaw(periodsamps) : _
// ```
//
// Where:
//
// * `periodsamps`: period in samples
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_rawsaw_test = os.lf_rawsaw(128);
// ```
//---------------------------------------------------------
lf_rawsaw(periodsamps) = (_,periodsamps : fmod) ~ +(1.0);
//-----------------`(os.)lf_sawpos`--------------------
// Simple sawtooth waveform oscillator between 0 and 1.
// In single precision, its frequency is exact to the float rounding of `freq/ma.SR`
// (below 1e-7 relative), without drift, down to `freq` = 0 and for negative `freq`
// (see the Wave-Table-Based Oscillators section).
//
// #### Usage
//
// ```
// lf_sawpos(freq) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
//
// #### Test
// ```
// os = library("oscillators.lib");
// ba = library("basics.lib");
// ma = library("maths.lib");
// lf_sawpos_test = os.lf_sawpos(3);
// lf_sawpos_slider_test = os.lf_sawpos(hslider("lf_sawpos:freq", 3, 0.01, 100, 0.01));
// lf_sawpos_modulated_test = os.lf_sawpos(0.01*pow(10000, tri)) with { P = int(ma.SR/10); tri = 1 - abs(2*ba.period(P)/P - 1); };
// lf_sawpos_negfreq_test = os.lf_sawpos(-0.1);
// ```
//---------------------------------------------------------
declare lf_sawpos author "Bart Brouns, revised by Stéphane Letz";
declare lf_sawpos licence "LicenseRef-STK-4.3";
lf_sawpos(freq) = _phasor_imp(freq, 0, 0);
//-----------------`(os.)lf_sawpos_phase`--------------------
// Simple sawtooth waveform oscillator between 0 and 1
// with phase control.
//
// #### Usage
//
// ```
// lf_sawpos_phase(freq, phase) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz
// * `phase`: phase between 0 and 1
//
// #### Test
// ```
// os = library("oscillators.lib");
// lf_sawpos_phase_test = os.lf_sawpos_phase(3, 0.25);
// lf_sawpos_phase_lowfreq_test = os.lf_sawpos_phase(0.1, 0.5);
// ```
//---------------------------------------------------------
declare lf_sawpos_phase author "Bart Brouns, revised by Stéphane Letz";
declare lf_sawpos_phase licence "LicenseRef-STK-4.3";
lf_sawpos_phase(freq,phase) = _phasor_imp(freq, 0, phase);
//-----------------`(os.)lf_sawpos_reset`--------------------
// Simple sawtooth waveform oscillator between 0 and 1
// with reset.
//
// #### Usage
//
// ```
// lf_sawpos_reset(freq,reset) : _
// ```
//
// Where:
//
// * `freq`: frequency in Hz