1 #include <SC_PlugIn.h>
2 #include <SC_InterfaceTable.h>
3 #include <math.h>
4 #include <stdio.h>
5 
6 #include "sinctab.h"
7 #include "Oversamp.h"
8 
9 // number of poles and zeros for the variable slope filter
10 #define npoles 4
11 #define nzeros 4
12 
13 #ifndef M_PI
14 #define M_PI           3.14159265358979323846
15 #endif
16 
bilinear(float p)17 static inline float bilinear(float p)
18 {
19    return ((2.f+p)/(2.f-p));
20 }
21 
tanapprox(float x)22 static inline float tanapprox (float x)
23 {
24   return x*(x*(x*(0.96369f*x-0.865157f)+0.53576f)+0.93f);
25 }
26 
27 
28 static InterfaceTable *ft;
29 
30 struct PeakEQ4 : public Unit
31 {
32 	double m_a[8];
33 	double m_b[10];
34 	double m_mem[10];
35 	float freq, width, gain;
36 };
37 
38 struct PeakEQ2 : public Unit
39 {
40 	double m_mem[4];
41 	double m_a[4];
42 	double m_b[5];
43 	float freq, width, gain;
44 };
45 
46 /* ************************************************************************ */
47 /* ***** Josep Comajuncosas' 18dB/oct resonant 3-pole LPF with tanh dist ** */
48 /* ***** Coded in C by John ffitch, 2000 Dec 17 *************************** */
49 /* ************************************************************************ */
50 /* ported to SuperCollider by Bjoern Erlach. 2008 */
51 
52 struct LPF18 : public Unit
53 {
54 	float ay1, ay2, lastin, aout;
55 	float kp, kres, last_fco, m_value;
56 };
57 
58 /* LPFVS.cpp
59  Copyright (C) 2007 by Bjoern Erlach. */
60 
61 /* Contains a variable slope lowpass filter with slope adjustable from 0-6db and
62  a simple one pole lowpass (LPF1). */
63 
64 struct LPF1 : public Unit
65 {
66 	float m_y1;
67 	float p;
68 	float last_freq;
69 };
70 
71 struct LPFVS6 : public Unit
72 {
73 	float mem[npoles];     // memory for the delay lines
74 	float a[npoles];       // coefficients for the feedforward part
75 	float b[npoles+1];     // coefficients for the feedforward part
76 	float norm;            // gain normalization
77 	float last_slope;      // slope
78 	float last_freq;       // cutoff frequency
79 };
80 
81 /* BLBufRd.cpp
82  Copyright (C) 2007 by Bjoern Erlach. */
83 
84 struct BLBufRd : public Unit
85 {
86 	SndBuf *m_buf;
87 	float m_fbufnum;
88 };
89 
90 /* OSNonlinear.cpp
91  Copyright (C) 2006 by Bjoern Erlach. */
92 
93 struct OSWrap4 : public Oversamp4 {};
94 struct OSWrap8 : public Oversamp8 {};
95 struct OSFold4 : public Oversamp4 {};
96 struct OSFold8 : public Oversamp8 {};
97 struct OSTrunc4 : public Oversamp4 {};
98 struct OSTrunc8 : public Oversamp8 {};
99 struct Clipper8 : public Oversamp8 {};
100 struct Clipper4 : public Oversamp4 {};
101 struct SoftClipper8 : public Oversamp8 {};
102 struct SoftClipper4 : public Oversamp4 {};
103 struct SoftClipAmp8 : public Oversamp8 {};
104 struct SoftClipAmp4 : public Oversamp4 {};
105 struct DriveNoise : public Unit {};
106 
107 
108 
109 extern "C"  {
110 
111 void PeakEQ4_Ctor(PeakEQ4 *unit);
112 void PeakEQ4_next(PeakEQ4 *unit, int inNumSamples);
113 
114 void PeakEQ2_Ctor(PeakEQ2 *unit);
115 void PeakEQ2_next(PeakEQ2 *unit, int inNumSamples);
116 
117 void LPF18_Ctor(LPF18 *unit);
118 void LPF18_next(LPF18 *unit, int inNumSamples);
119 
120 void LPF1_Ctor(LPF1 *unit);
121 void LPF1_next(LPF1 *unit, int inNumSamples);
122 void LPF1_next_a(LPF1 *unit, int inNumSamples);
123 
124 void LPFVS6_Ctor(LPFVS6 *unit);
125 void LPFVS6_next(LPFVS6 *unit, int inNumSamples);
126 
127 void LPF1_Ctor(LPF1 *unit);
128 void LPF1_next(LPF1 *unit, int inNumSamples);
129 void LPF1_next_a(LPF1 *unit, int inNumSamples);
130 
131 void BLBufRd_Ctor(BLBufRd *unit);
132 void BLBufRd_next(BLBufRd *unit, int inNumSamples);
133 
134 
135 void OSWrap8_Ctor(OSWrap8 *unit);
136 void OSWrap8_Dtor(OSWrap8 *unit);
137 void OSWrap8_next(OSWrap8 *unit, int inNumSamples);
138 
139 void OSWrap4_Ctor(OSWrap4 *unit);
140 void OSWrap4_Dtor(OSWrap4 *unit);
141 void OSWrap4_next(OSWrap4 *unit, int inNumSamples);
142 
143 void OSFold4_Ctor(OSFold4 *unit);
144 void OSFold4_Dtor(OSFold4 *unit);
145 void OSFold4_next(OSFold4 *unit, int inNumSamples);
146 
147 void OSFold8_Ctor(OSFold8 *unit);
148 void OSFold8_Dtor(OSFold8 *unit);
149 void OSFold8_next(OSFold8 *unit, int inNumSamples);
150 
151 void OSTrunc4_Ctor(OSTrunc4 *unit);
152 void OSTrunc4_Dtor(OSTrunc4 *unit);
153 void OSTrunc4_next(OSTrunc4 *unit, int inNumSamples);
154 
155 void OSTrunc8_Ctor(OSTrunc8 *unit);
156 void OSTrunc8_Dtor(OSTrunc8 *unit);
157 void OSTrunc8_next(OSTrunc8 *unit, int inNumSamples);
158 
159 void Clipper8_Ctor(Clipper8 *unit);
160 void Clipper8_Dtor(Clipper8 *unit);
161 void Clipper8_next(Clipper8 *unit, int inNumSamples);
162 
163 void Clipper4_Ctor(Clipper4 *unit);
164 void Clipper4_Dtor(Clipper4 *unit);
165 void Clipper4_next(Clipper4 *unit, int inNumSamples);
166 
167 void SoftClipper8_Ctor(SoftClipper8 *unit);
168 void SoftClipper8_Dtor(SoftClipper8 *unit);
169 void SoftClipper8_next(SoftClipper8 *unit, int inNumSamples);
170 
171 void SoftClipper4_Ctor(SoftClipper4 *unit);
172 void SoftClipper4_Dtor(SoftClipper4 *unit);
173 void SoftClipper4_next(SoftClipper4 *unit, int inNumSamples);
174 
175 void SoftClipAmp8_Ctor(SoftClipAmp8 *unit);
176 void SoftClipAmp8_Dtor(SoftClipAmp8 *unit);
177 void SoftClipAmp8_next(SoftClipAmp8 *unit, int inNumSamples);
178 
179 void SoftClipAmp4_Ctor(SoftClipAmp4 *unit);
180 void SoftClipAmp4_Dtor(SoftClipAmp4 *unit);
181 void SoftClipAmp4_next(SoftClipAmp4 *unit, int inNumSamples);
182 
183 void DriveNoise_Ctor(DriveNoise *unit);
184 void DriveNoise_next(DriveNoise *unit, int inNumSamples);
185 
186 }
187 
188 
189 
190 // macros to put rgen state in registers
191 #define RGET \
192 	RGen& rgen = *unit->mParent->mRGen; \
193 	uint32 s1 = rgen.s1; \
194 	uint32 s2 = rgen.s2; \
195 	uint32 s3 = rgen.s3;
196 
197 #define RPUT \
198 	rgen.s1 = s1; \
199 	rgen.s2 = s2; \
200 	rgen.s3 = s3;
201 
202 
203 #define G0 1.f
204 #define exp10f(x) expf((x)*2.30258509299404590109361f)
205 #define exp10(x) exp((x)*2.30258509299404590109361)
206 
calc_coeffs4(double * bcoeff,double * acoeff,double w0,double Dw,double gain)207 static inline void calc_coeffs4 (double *bcoeff, double *acoeff, double w0, double Dw, double gain)
208 {
209   double G = exp10f(gain/20.f);
210   // bandwidth gain is set to half gain
211   double GB = exp10f(gain/40.f);
212   double c0 = cosf(w0);
213   double WB = tanf(Dw/2);
214   double e = sqrtf((G*G-GB*GB)/(GB*GB-1.f));
215   double g = powf(G, 0.25f);
216   double a = powf(e, 0.25f);
217 #define  si1  0.382683432365089781779f;
218 #define  si2  0.923879532511286738483f;
219   //Ba(1+i,:) = [g^2*WB^2*v, 2*g*b*si*WB, b^2*v]
220   //Aa(1+i,:) = [WB^2*v, 2*a*si*WB, a^2*v]
221   double Aa0 = WB*WB;
222   double Aatmp = 2*a*WB;
223   double Aa10 = Aatmp * si1;
224   double Aa11 = Aatmp * si2;
225   double as = a*a;
226   double Ba0 = Aa0 * g*g;
227   double Batmp = 2*g * a * WB;
228   double Ba10 = Batmp * si1;
229   double Ba11 = Batmp * si2;
230   //  fprintf(stdout, "%f %f %f %f %f %f %f\n", Aa0, Aa10, Aa11, Ba0, Ba10, Ba11, as);
231   //  double Bhat0 = Ba0/Aa0;
232   //  D = A0(i)+A1(i)+A2(i)
233   double D1 = Aa0+Aa11+as;
234   double D0 = Aa0+Aa10+as;
235   //Bhat(i,1) = (B0(i)+B1(i)+B2(i))./D
236   //(B0(i)+B1(i)+B2(i))./D
237   double tmp = Ba0+as;
238   double x;
239   double Bhat00 = (Ba10+tmp)/D0;
240   double Bhat01 = (Ba11+tmp)/D1;
241   x=2.f*(Ba0-as);
242   double Bhat10 = x/D0;
243   double Bhat11 = x/D1;
244   double Bhat20 = (tmp-Ba10)/D0;
245   double Bhat21 = (tmp-Ba11)/D1;
246   //Ahat(i,1) = 1
247   //Ahat(i,2) = 2*(A0(i)-A2(i))./D
248   //Ahat(i,3) = (A0(i)-A1(i)+A2(i))./D
249   x = 2.f*(Aa0-as);
250   double Ahat10 = x/D0;
251   double Ahat11 = x/D1;
252   double Ahat20 = (Aa0-Aa10+as)/D0;
253   double Ahat21 = (Aa0-Aa11+as)/D1;
254 
255   //  fprintf(stdout, "%f %f %f %f %f\n %f %f %f %f\n\n",Bhat00, Bhat01, Bhat10, Bhat11, Bhat21, Ahat11, Ahat10, Ahat20, Ahat21);
256 
257 
258   // B(i,2) = c0*(Bhat(i,2)-2*Bhat(i,1))
259   bcoeff[0] = Bhat00;
260   bcoeff[5] = Bhat01;
261   bcoeff[1] = c0*(Bhat10-2.f*Bhat00);
262   bcoeff[6] = c0*(Bhat11-2.f*Bhat01);
263   bcoeff[2] = (Bhat00-Bhat10+Bhat20)*c0*c0- Bhat10;
264   bcoeff[7] = (Bhat01-Bhat11+Bhat21)*c0*c0 - Bhat11;
265   bcoeff[3] = c0*(Bhat10-2.f*Bhat20);
266   bcoeff[8] = c0*(Bhat11-2.f*Bhat21);
267   bcoeff[4] = Bhat20;
268   bcoeff[9] = Bhat21;
269 
270   //  fprintf(stdout, "%f %f %f %f %f\n %f %f %f %f %f\n", Bres00, Bres10, Bres20, Bres30, Bres40, Bres01, Bres11, Bres21, Bres31, Bres41);
271 
272   acoeff[0]= c0*(Ahat10-2.f);
273   acoeff[4]= c0*(Ahat11-2.f);
274   acoeff[1]= (1.f-Ahat10+Ahat20)*c0*c0 - Ahat10;
275   acoeff[5]= (1.f-Ahat11+Ahat21)*c0*c0- Ahat11;
276   acoeff[2]= c0*(Ahat10-2.f*Ahat20);
277   acoeff[6]= c0*(Ahat11-2.f*Ahat21);
278   acoeff[3]= Ahat20;
279   acoeff[7]= Ahat21;
280 
281   //  fprintf(stdout, "%f %f %f %f %f\n %f %f %f %f %f\n", Ares00, Ares10, Ares20, Ares30, Ares40, Ares01, Ares11, Ares21, Ares31, Ares41);
282 }
283 
284 
PeakEQ4_Ctor(PeakEQ4 * unit)285 void PeakEQ4_Ctor(PeakEQ4* unit)
286 {
287   // force coeff calculation
288   unit->freq = -1.1;
289   memset(unit->m_mem, 0, sizeof(double)*10);
290   memset(unit->m_b, 0, sizeof(double)*10);
291   memset(unit->m_a, 0, sizeof(double)*8);
292 
293   SETCALC(PeakEQ4_next);
294 }
295 
296 
PeakEQ4_next(PeakEQ4 * unit,int inNumSamples)297 void PeakEQ4_next(PeakEQ4* unit, int inNumSamples)
298 {
299   float *out = ZOUT(0);
300   float *input = ZIN(0);
301   float freq = ZIN0(1);
302   float width = ZIN0(2);
303   float gain = ZIN0(3);
304   double *bc = unit->m_b;
305   double *ac = unit->m_a;
306   double *buf = unit->m_mem;
307 
308 
309   if ((unit->freq!=freq)||(unit->gain!=gain)||(unit->width!=width)) {
310 	  double w0 = freq * 2*M_PI / SAMPLERATE;
311 	  double Dw = w0 * width;
312 	  calc_coeffs4(bc,ac,w0, Dw, gain);
313   }
314 
315   LOOP(inNumSamples,
316        double in = (double) ZXP(input);
317        double iir;
318        double iir2;
319        double fir;
320        iir= in-
321          ac[0]*buf[3]-
322          ac[1]*buf[2]-
323          ac[2]*buf[1]-
324          ac[3]*buf[0];
325        fir= bc[0]*iir+
326          bc[1]*buf[3]+
327          bc[2]*buf[2]+
328          bc[3]*buf[1]+
329          bc[4]*buf[0];
330        iir2= fir-
331          ac[4]*buf[7]-
332          ac[5]*buf[6]-
333          ac[6]*buf[5]-
334          ac[7]*buf[4];
335        fir= bc[5]*iir2+
336          bc[6]*buf[7]+
337          bc[7]*buf[6]+
338          bc[8]*buf[5]+
339          bc[9]*buf[4];
340        memmove(buf, buf+1, 7*sizeof(double));
341        buf[3]= iir;
342        buf[7]= iir2;
343        ZXP(out) = (float)fir;
344        )
345 }
346 
347 
348 
349 
350 ////////////////////////////////////////////////////////////////////////////
351 
352 
calc_coeffs2(double * bcoeff,double * acoeff,double w0,double Dw,double gain)353 static inline void calc_coeffs2 (double *bcoeff, double *acoeff, double w0, double Dw, double gain)
354 {
355   double G = exp10(((double)gain)/20);
356   // bandwidth gain is set to half gain
357   double GB = exp10(((double)gain)/40);
358   double c0 = cos(w0);
359   double WB = tan(((double)Dw)/2);
360   double e = sqrt((G*G-GB*GB)/(GB*GB-1.0));
361   double g = pow(G, 0.5);
362   double a = pow(e, 0.5);
363 #define  si  0.70710678118654748
364   //Ba(1+i,:) = [g^2*WB^2*v, 2*g*b*si*WB, b^2*v]
365   //Aa(1+i,:) = [WB^2*v, 2*a*si*WB, a^2*v]
366   double Aa0 = WB*WB;
367   double Aatmp = 2*a*WB;
368   double Aa10 = Aatmp * si;
369   double as = a*a;
370   double Ba0 = Aa0 * g*g;
371   double Batmp = 2*g * a * WB;
372   double Ba10 = Batmp * si;
373   //  fprintf(stdout, "%f %f %f %f %f %f %f\n", Aa0, Aa10, Aa11, Ba0, Ba10, Ba11, as);
374   //  double Bhat0 = Ba0/Aa0;
375   //  D = A0(i)+A1(i)+A2(i)
376   double D0 = Aa0+Aa10+as;
377   //Bhat(i,1) = (B0(i)+B1(i)+B2(i))./D
378   //(B0(i)+B1(i)+B2(i))./D
379   double tmp = Ba0+as;
380   double x;
381   double Bhat00 = (Ba10+tmp)/D0;
382   x=2.f*(Ba0-as);
383   double Bhat10 = x/D0;
384   double Bhat20 = (tmp-Ba10)/D0;
385   //Ahat(i,1) = 1
386   //Ahat(i,2) = 2*(A0(i)-A2(i))./D
387   //Ahat(i,3) = (A0(i)-A1(i)+A2(i))./D
388   x = 2.f*(Aa0-as);
389   double Ahat10 = x/D0;
390   double Ahat20 = (Aa0-Aa10+as)/D0;
391 
392   //  fprintf(stdout, "%f %f %f %f %f\n %f %f %f %f\n\n",Bhat00, Bhat01, Bhat10, Bhat11, Bhat21, Ahat11, Ahat10, Ahat20, Ahat21);
393 
394 
395   // B(i,2) = c0*(Bhat(i,2)-2*Bhat(i,1))
396   bcoeff[0] = (double) Bhat00;
397   bcoeff[1] = (double) (c0*(Bhat10-2.f*Bhat00));
398   bcoeff[2] = (double) ((Bhat00-Bhat10+Bhat20)*c0*c0- Bhat10);
399   bcoeff[3] = (double) (c0*(Bhat10-2.f*Bhat20));
400   bcoeff[4] = (double) Bhat20;
401 
402   //  fprintf(stdout, "%f %f %f %f %f\n %f %f %f %f %f\n", Bres00, Bres10, Bres20, Bres30, Bres40, Bres01, Bres11, Bres21, Bres31, Bres41);
403 
404   acoeff[0]= (double) (c0*(Ahat10-2.f));
405   acoeff[1]= (double) ((1.f-Ahat10+Ahat20)*c0*c0 - Ahat10);
406   acoeff[2]= (double) (c0*(Ahat10-2.f*Ahat20));
407   acoeff[3]= (double) Ahat20;
408 
409   //  fprintf(stdout, "%f %f %f %f %f\n %f %f %f %f %f\n", Ares00, Ares10, Ares20, Ares30, Ares40, Ares01, Ares11, Ares21, Ares31, Ares41);
410 }
411 
412 
413 
PeakEQ2_Ctor(PeakEQ2 * unit)414 void PeakEQ2_Ctor(PeakEQ2* unit)
415 {
416   // force coeff calculation
417   unit->freq = -1.1;
418   memset(unit->m_mem, 0, sizeof(double)*4);
419   memset(unit->m_b, 0, sizeof(double)*5);
420   memset(unit->m_a, 0, sizeof(double)*4);
421 
422   SETCALC(PeakEQ2_next);
423 }
424 
425 
PeakEQ2_next(PeakEQ2 * unit,int inNumSamples)426 void PeakEQ2_next(PeakEQ2* unit, int inNumSamples)
427 {
428   float *out = ZOUT(0);
429   float *input = ZIN(0);
430   float freq = ZIN0(1);
431   float width = ZIN0(2);
432   float gain = ZIN0(3);
433   double *bc = unit->m_b;
434   double *ac = unit->m_a;
435   double *buf = unit->m_mem;
436 
437 
438   if ((unit->freq!=freq)||(unit->gain!=gain)||(unit->width!=width)) {
439 	  double w0 = freq * 2*M_PI / SAMPLERATE;
440 	  double Dw = w0 * width;
441 	  calc_coeffs2(bc,ac,w0, Dw, gain);
442   }
443 
444   LOOP(inNumSamples,
445        double in = (double) ZXP(input);
446        double iir;
447        double fir;
448        iir= in-
449          ac[0]*buf[3]-
450          ac[1]*buf[2]-
451          ac[2]*buf[1]-
452          ac[3]*buf[0];
453        fir= bc[0]*iir+
454          bc[1]*buf[3]+
455          bc[2]*buf[2]+
456          bc[3]*buf[1]+
457          bc[4]*buf[0];
458        memmove(buf, buf+1, 3*sizeof(double));
459        buf[3]= iir;
460        ZXP(out) = (float)fir;
461        )
462 }
463 
464 
465 #define  SIGN(x) (x>=0.f?1.f:-1.f)
466 
tanhapprox(float x)467 static inline float tanhapprox (float x) {
468   if (fabsf(x) > 1.691976516634051019849f) {
469 	  return SIGN(x) * 0.811728001858655012768f + x * 0.068523721733649733956f;
470   } else {
471 	  return (x * (x * (x * (x * (0.942810948279731997346f * x)) + -0.191839831351772377444f))
472 	   + 0.942810948279731997346f);
473   }
474 }
475 
476 //#define TANH tanhapprox
477 #define TANH tanhf
478 
479 /* This code is transcribed from a Csound macro, so no real comments */
480 
LPF18_Ctor(LPF18 * unit)481 void LPF18_Ctor(LPF18 *unit)
482 {
483   float fco = ZIN0(1);
484   float res = ZIN0(2);
485   float dist = ZIN0(3);
486   float kfcn = 2.0f * fco * SAMPLEDUR;
487   float kp = ((-2.7528f*kfcn + 3.0429f)*kfcn +
488 			   1.718f)*kfcn - 0.9984f;
489   float kp1 = kp+1.0f;
490   float kres = res * (((-2.7079f*kp1 + 10.963f)*kp1
491 		       - 14.934f)*kp1 + 8.4974f);
492   float value = 1.0f+(dist*(1.5f+2.0f*kres*(1.0f-kfcn)));
493   unit->kp = kp;
494   unit->m_value = value;
495   unit->kres = kres;
496   unit->last_fco = fco;
497 
498   /* Initialise delay lines */
499   unit->ay1 = 0.0f;
500   unit->ay2 = 0.0f;
501   unit->aout = 0.0f;
502   unit->lastin = 0.0f;
503   SETCALC(LPF18_next);
504 }
505 
LPF18_next(LPF18 * unit,int nsmps)506 void LPF18_next(LPF18 *unit, int nsmps)
507 {
508   float *in = ZIN(0);
509   float *out = ZOUT(0);
510   float fco = ZIN0(1);
511   float res = ZIN0(2);
512   float dist = ZIN0(3);
513 
514   float aout = unit->aout;
515   float ay1 = unit->ay1;
516   float ay2 = unit->ay2;
517   float lastin = unit->lastin;
518   float kfcn, kp = unit->kp;
519   float kp1, kp1h;
520 
521 
522   if (fco != unit->last_fco) {
523 	  kfcn = 2.0f * unit->last_fco * SAMPLEDUR;
524 	  kp1 = kp+1.0f;
525 	  kp1h = 0.5f*kp1;
526 
527 	  float newkfcn = 2.0f * fco * SAMPLEDUR;
528 	  float newkp = ((-2.7528f*newkfcn + 3.0429f)*newkfcn +
529 			   1.718f)*newkfcn - 0.9984f;
530 	  float newkp1 = kp+1.0f;
531 	  float newkp1h = 0.5f*kp1;
532 	  float kp1hinc = (newkp1h-kp1h)/nsmps;
533 	  float kpinc = (newkp-kp)/nsmps;
534 	  float kres = unit->kres;
535 	  float newkres = res * (((-2.7079f*newkp1 + 10.963f)*newkp1
536 				  - 14.934f)*newkp1 + 8.4974f);
537 	  float kresinc = (newkres-kres)/nsmps;
538 	  float value = unit->m_value;
539 	  float newvalue = 1.0f+(dist*(1.5f+2.0f*newkres*(1.0f-newkfcn)));
540 	  float valueinc = (newvalue-value)/nsmps;
541 	  unit->kp = newkp;
542 	  unit->m_value = newvalue;
543 	  unit->kres = newkres;
544 	  unit->last_fco = fco;
545 	  LOOP(nsmps,
546 	       float ax1   = lastin;
547 	       float ay11  = ay1;
548 	       float ay31  = ay2;
549 	       lastin  =  ZXP(in) - TANH(kres*aout);
550 	       ay1      = kp1h * (lastin + ax1) - kp*ay1;
551 	       ay2      = kp1h * (ay1 + ay11) - kp*ay2;
552 	       aout     = kp1h * (ay2 + ay31) - kp*aout;
553 	       ZXP(out) = TANH(aout*value);
554 	       kp1h += kp1hinc;
555 	       kp += kpinc;
556 	       kres += kresinc;
557 	       value += valueinc;
558 	       );
559 
560   } else {
561 	  kfcn = 2.0f * fco * SAMPLEDUR;
562 	  kp1 = kp+1.0f;
563 	  kp1h = 0.5f*kp1;
564 	  /* Version using log */
565 	  /* float kres = *unit->res * (2.2173f - 1.6519f*log(kp+1.0f)); */
566 	  float kres = res * (((-2.7079f*kp1 + 10.963f)*kp1
567 			       - 14.934f)*kp1 + 8.4974f);
568 	  float value = 1.0f+(dist*(1.5f+2.0f*kres*(1.0f-kfcn)));
569 	  unit->m_value = value;
570 	  unit->kres = kres;
571 	  LOOP(nsmps,
572 	       float ax1   = lastin;
573 	       float ay11  = ay1;
574 	       float ay31  = ay2;
575 	       lastin  =  ZXP(in) - TANH(kres*aout);
576 	       ay1      = kp1h * (lastin + ax1) - kp*ay1;
577 	       ay2      = kp1h * (ay1 + ay11) - kp*ay2;
578 	       aout     = kp1h * (ay2 + ay31) - kp*aout;
579 	       ZXP(out) = TANH(aout*value);
580 	       );
581   }
582 
583 
584 
585   unit->ay1 = ay1;
586   unit->ay2 = ay2;
587   unit->aout = aout;
588   unit->lastin = lastin;
589 }
590 
591 
592 
593 void
LPF1_Ctor(LPF1 * unit)594 LPF1_Ctor (LPF1 *unit)
595 {
596   unit->m_y1 = 0.f;
597   if (INRATE(1) == calc_FullRate) {
598 	  SETCALC(LPF1_next_a);
599   } else {
600 	  unit->last_freq = ZIN0(1);
601 	  unit->p = (1.f-2.f*tanf((unit->last_freq/SAMPLERATE)));
602 	  SETCALC(LPF1_next);
603   }
604 }
605 
606 void
LPF1_next(LPF1 * unit,int inNumSamples)607 LPF1_next (LPF1 *unit, int inNumSamples)
608 {
609   float *out = ZOUT(0);
610   float *in = ZIN(0);
611   float freq = ZIN0(1);
612   float p = unit->p;
613   float y1 = unit->m_y1;
614 
615   if (unit->last_freq != freq) {
616 	  float newp = (1.f-2.f*tanf((freq/SAMPLERATE)));
617 	  float pinc = (newp-p)/inNumSamples;
618 	  unit->p=newp;
619 	  unit->last_freq=freq;
620 	  LOOP(inNumSamples,
621 	       ZXP(out) = y1 = (1.f-p)*ZXP(in)+ p*y1;
622 	       p += pinc;
623 	       );
624   } else {
625 	  LOOP(inNumSamples,
626 	       ZXP(out) = y1 = (1.f-p)*ZXP(in)+ p*y1;
627 	       );
628   }
629 
630    unit->m_y1 = y1;
631 }
632 
633 
634 void
LPF1_next_a(LPF1 * unit,int inNumSamples)635 LPF1_next_a (LPF1 *unit, int inNumSamples)
636 {
637   float *out = ZOUT(0);
638   float *in = ZIN(0);
639   float *freq = ZIN(1);
640   float y1 = unit->m_y1;
641   float srate = SAMPLERATE;
642 
643   LOOP(inNumSamples,
644        float p = (1.f-2.f*tanapprox((ZXP(freq)/srate)));
645        ZXP(out) = y1 = (1.f-p)*ZXP(in)+ p*y1;
646        );
647 
648    unit->m_y1 = y1;
649 }
650 
651 
652 #define PI34 ((3.f/4.f)*((float)M_PI))
653 
654 #define COSPI34  (-0.707106781186547461715)
655 
656 
657 static float proto[] = { -1.0, -6.3246, -40, -252.9824, -1600, -10119.2896, -64000 };
658 
659 
660 
661 void
LPFVS6_Ctor(LPFVS6 * unit)662 LPFVS6_Ctor (LPFVS6 *unit)
663 {
664   // force calculation of the poles and zeros
665   unit->last_freq = ZIN0(1);
666   unit->last_slope = ZIN0(2);
667   float *b = unit->b;
668   float *a = unit->a;
669   float g = 1.f;
670 
671   unit->mem[0] = 0.f;  unit->mem[1] = 0.f;
672   unit->mem[2] = 0.f;  unit->mem[3] = 0.f;
673 
674   float w = tanf((unit->last_freq*M_PI/(2.f*SAMPLERATE)));
675 
676   //  fprintf(stderr, "w: %f\n", w);
677 
678   b[0] = bilinear(proto[0] * w);
679   //fprintf(stderr, "pole: %f, ", b[0]);
680 
681   for (int i=1; i<5; i++) {
682 	  b[i] = bilinear(proto[i] * w);
683 	  //  fprintf(stderr, "pole: %f, ", b[i]);
684 	  if (b[i] < 0.f) b[i] = 0.f;
685 	  a[i-1] = (b[i-1] + (b[i] - b[i-1]) * unit->last_slope);
686 	  if (a[i-1] < 0.f) a[i-1] = 0.f;
687 	  // normalization
688 	  g *= (1.f-b[i-1])/(1.f-a[i-1]);
689 	  //fprintf(stderr, "zero: %f, ", a[i-1]);
690   }
691   //fprintf(stderr, "g: %f\n", g);
692   unit->norm = g;
693   SETCALC(LPFVS6_next);
694 }
695 
696 
697 
698 void
LPFVS6_next(LPFVS6 * unit,int inNumSamples)699 LPFVS6_next (LPFVS6 *unit, int inNumSamples)
700 {
701   float *out   = ZOUT(0);
702   float *in    = ZIN(0);
703   float freq   = ZIN0(1);
704   float slope  = ZIN0(2);
705   float g = unit->norm;
706   float *b = unit->b;
707   float *a = unit->a;
708   float *mem = unit->mem;
709   float binc[5];
710   float ainc[5];
711   float ginc;
712 
713 #if 0
714   for (int i=0; i<4; i++) {
715 	  fprintf(stderr, "%f ", b[i]);
716   }
717   fprintf(stderr, "\n");
718   for (int i=0; i<4; i++) {
719 	  fprintf(stderr, "%f ", a[i]);
720   }
721   fprintf(stderr, "\n");
722   fprintf(stderr, "%f\n", g);
723 #endif
724 
725   // calculate poles for new cutoff frequency
726   if (freq != unit->last_freq) {
727 	  float w = tanf((freq*M_PI/(2.f*SAMPLERATE)));
728 	  float nb[5];
729 	  float ng = 1.f;
730 	  nb[0] = bilinear(proto[0] * w);
731 	  binc[0] = (nb[0]-b[0])/inNumSamples;
732 	  for (int i=1; i<5; i++) {
733 		  float na;
734 		  nb[i] = bilinear(proto[i] * w);
735 		  //fprintf(stdout, "pole: %f\n", p);
736 		  if (nb[i] < 0.f) nb[i] = 0.f;
737 		  binc[i] = (nb[i]-b[i])/inNumSamples;
738 		  na = (nb[i-1] + (nb[i] - nb[i-1]) * slope);
739 		  if (na < 0.f) na = 0.f;
740 		  ainc[i-1] = (na - a[i-1])/inNumSamples;
741 		  // normalization
742 		  ng *= (1.f-nb[i-1])/(1.f-na);
743 	  }
744 	  ginc = (ng-g)/inNumSamples;
745 	  unit->last_freq = freq;
746 	  unit->last_slope = slope;
747   } else if (slope != unit->last_slope) {
748 	  // caculate for new slope
749 	  float ng = 1.f;
750 	  for (int i=0; i<4; i++) {
751 		  float na;
752 		  na = b[i] + (b[i+1] - b[i]) * slope;
753 		  if (na < 0.f) na = 0.f;
754 		  ng *= (1.f-b[i])/(1.f-na);
755 		  ainc[i] = (na - a[i])/inNumSamples;
756 	  }
757 	  ginc = (ng-g)/inNumSamples;
758 	  binc[0] = 0.f; binc[1] = 0.f;binc[2] = 0.f;binc[3] = 0.f;
759 	  // calculate normalization
760 	  unit->last_slope = slope;
761   } else {
762 	  ginc = 0.f;
763 	  binc[0] = 0.f; binc[1] = 0.f;binc[2] = 0.f;binc[3] = 0.f;
764 	  ainc[0] = 0.f; ainc[1] = 0.f;ainc[2] = 0.f;ainc[3] = 0.f;
765   }
766 
767 
768 
769   LOOP(inNumSamples,
770        float x;
771        float y;
772        x = ZXP(in);
773        //for (k=0; k<4; k++) {
774        //	       y = x + b[k]*mem[k]; // first order section (direct form II)
775        //       x = y + a[k]*mem[k];
776        //	       mem[k] = y;
777        //}
778        y = x + b[0]*mem[0]; // first order section (direct form II)
779        x = y - a[0]*mem[0];
780        mem[0] = y;
781        b[0] += binc[0];
782        a[0] += ainc[0];
783        y = x + b[1]*mem[1]; // first order section (direct form II)
784        x = y - a[1]*mem[1];
785        mem[1] = y;
786        b[1] += binc[1];
787        a[1] += ainc[1];
788        y = x + b[2]*mem[2]; // first order section (direct form II)
789        x = y - a[2]*mem[2];
790        mem[2] = y;
791        b[2] += binc[2];
792        a[2] += ainc[2];
793        y = x + b[3]*mem[3]; // first order section (direct form II)
794        x = y - a[3]*mem[3];
795        mem[3] = y;
796        b[3] += binc[3];
797        a[3] += ainc[3];
798        ZXP(out) = x*g;
799        g += ginc;
800        );
801 
802   unit->norm = g;
803   mem[0] = zapgremlins(mem[0]);
804   mem[1] = zapgremlins(mem[1]);
805   mem[2] = zapgremlins(mem[2]);
806   mem[3] = zapgremlins(mem[3]);
807 }
808 
809 
810 
811 
812 ////////////////////////////////////////////////////////////////////////////
813 
814 
815 #define BL_GET_BUF \
816 	float fbufnum  = ZIN0(0); \
817 	if (fbufnum != unit->m_fbufnum) { \
818 		uint32 bufnum = (int)fbufnum; \
819 		World *world = unit->mWorld; \
820 		if (bufnum >= world->mNumSndBufs) bufnum = 0; \
821 		unit->m_fbufnum = fbufnum; \
822 		unit->m_buf = world->mSndBufs + bufnum; \
823 	} \
824 	SndBuf *buf = unit->m_buf; \
825 
826 #define BL_CHECK_BUF \
827 	if (!bufData) { \
828                 unit->mDone = true; \
829 		ClearUnitOutputs(unit, inNumSamples); \
830 		return; \
831 	}
832 
833 #define SINC(iphs, fphs) ((1.f-fphs) * sinctab[iphs] + fphs * sinctab[iphs+1])
834 #define SINCN(iphs) (sinctab[iphs])
835 
836 void
BLBufRd_Ctor(BLBufRd * unit)837 BLBufRd_Ctor (BLBufRd *unit)
838 {
839     unit->m_fbufnum = -1.f;
840     SETCALC(BLBufRd_next);
841 }
842 
843 void
BLBufRd_next(BLBufRd * unit,int inNumSamples)844 BLBufRd_next (BLBufRd *unit, int inNumSamples)
845 {
846   float *out = ZOUT(0);
847   float *phsin = ZIN(1);
848   float ratio = ZIN0(2);
849 
850 
851   BL_GET_BUF;
852 
853 
854   float *bufData = buf->data;
855   int bufFrames = buf->frames;
856 
857   BL_CHECK_BUF;
858 
859   LOOP(inNumSamples,
860        float sum = 0.f;
861        float phs = ZXP(phsin);
862        float ratio256 = 256.f*(1.f/ratio);
863        int pos = (int) phs;
864        int nsamps = (int) (ratio * 2.f);
865        float frac = phs - (float) pos;
866        int n;
867 
868        n = sc_min(nsamps, pos);
869        for (int k=0; k<n; k++) {
870 	       float distance = frac+k;
871 	       float fphs = ratio256*distance;
872 	       //int iphs = (int) fphs;
873 	       //fphs = fphs - (float) iphs;
874 	       //sum += bufData[pos - k] * SINCN(iphs, fphs);
875 	       int iphs = lrintf(fphs);
876 	       sum += bufData[pos - k] * SINCN(iphs);
877        }
878 
879        frac = 1.f - frac;
880        pos ++;
881 
882        n = sc_min(nsamps, bufFrames-pos);
883        for (int k=0; k<n; k++) {
884 	       float distance = frac+k;
885 	       float fphs = ratio256*distance;
886 	       //int iphs = (int) fphs;
887 	       //fphs = fphs - (float) iphs;
888 	       //sum += bufData[pos - k] * SINCN(iphs, fphs);
889 	       int iphs = lrintf(fphs);
890 	       sum += bufData[pos + k] * SINCN(iphs);
891        }
892 
893        ZXP(out) = sum/ratio;
894        );
895 
896 }
897 
898 
899 void
Clipper8_Ctor(Clipper8 * unit)900 Clipper8_Ctor (Clipper8 *unit)
901 {
902   OVERSAMPLE8_INIT;
903   SETCALC(Clipper8_next);
904 }
905 
906 void
Clipper8_Dtor(Clipper8 * unit)907 Clipper8_Dtor (Clipper8 *unit)
908 {
909   OVERSAMPLE_DTOR;
910 }
911 
912 void
Clipper8_next(Clipper8 * unit,int inNumSamples)913 Clipper8_next (Clipper8 *unit, int inNumSamples)
914 {
915   float *out = ZOUT(0);
916   float *in = ZIN(0);
917   float min = ZIN0(1);
918   float max = ZIN0(2);
919 
920   UPSAMPLE8;
921 
922   for (int j=0; j<(inNumSamples*8); j++) {
923 	  domemoff[j] = sc_clip(domemoff[j], min, max);
924   }
925 
926   DOWNSAMPLE8;
927 }
928 
929 ////////////////////////////////////////////////////////////////
930 
931 
932 void
Clipper4_Ctor(Clipper4 * unit)933 Clipper4_Ctor (Clipper4 *unit)
934 {
935   OVERSAMPLE4_INIT;
936   SETCALC(Clipper4_next);
937 }
938 
939 void
Clipper4_Dtor(Clipper4 * unit)940 Clipper4_Dtor (Clipper4 *unit)
941 {
942   OVERSAMPLE_DTOR;
943 }
944 
945 void
Clipper4_next(Clipper4 * unit,int inNumSamples)946 Clipper4_next (Clipper4 *unit, int inNumSamples)
947 {
948   float *out = ZOUT(0);
949   float *in = ZIN(0);
950   float min = ZIN0(1);
951   float max = ZIN0(2);
952 
953   UPSAMPLE4;
954 
955   for (int j=0; j<(inNumSamples*4); j++) {
956 	  domemoff[j] = sc_clip(domemoff[j], min, max);
957 	  //	  domemoff[j] = domemoff[j] * 0.25;
958 
959   }
960 
961   //DOWNSAMPLE4;
962   DOWNSAMPLE4;
963 }
964 
965 ////////////////////////////////////////////////////////////////////////////
966 
967 
968 
969 
970 void
SoftClipper8_Ctor(SoftClipper8 * unit)971 SoftClipper8_Ctor (SoftClipper8 *unit)
972 {
973   OVERSAMPLE8_INIT;
974   SETCALC(SoftClipper8_next);
975 }
976 
977 void
SoftClipper8_Dtor(SoftClipper8 * unit)978 SoftClipper8_Dtor (SoftClipper8 *unit)
979 {
980   OVERSAMPLE_DTOR;
981 }
982 
983 void
SoftClipper8_next(SoftClipper8 * unit,int inNumSamples)984 SoftClipper8_next (SoftClipper8 *unit, int inNumSamples)
985 {
986   float *out = ZOUT(0);
987   float *in = ZIN(0);
988 
989   UPSAMPLE8;
990 
991   for (int j=0; j<(inNumSamples*8); j++) {
992 	  domemoff[j] = sc_softclip(domemoff[j]);
993 
994   }
995 
996   DOWNSAMPLE8;
997 }
998 
999 ////////////////////////////////////////////////////////////////
1000 
1001 
1002 void
SoftClipper4_Ctor(SoftClipper4 * unit)1003 SoftClipper4_Ctor (SoftClipper4 *unit)
1004 {
1005   OVERSAMPLE4_INIT;
1006   SETCALC(SoftClipper4_next);
1007 }
1008 
1009 void
SoftClipper4_Dtor(SoftClipper4 * unit)1010 SoftClipper4_Dtor (SoftClipper4 *unit)
1011 {
1012   OVERSAMPLE_DTOR;
1013 }
1014 
1015 void
SoftClipper4_next(SoftClipper4 * unit,int inNumSamples)1016 SoftClipper4_next (SoftClipper4 *unit, int inNumSamples)
1017 {
1018   float *out = ZOUT(0);
1019   float *in = ZIN(0);
1020 
1021   UPSAMPLE4;
1022 
1023   for (int j=0; j<(inNumSamples*4); j++) {
1024 	  domemoff[j] = sc_softclip(domemoff[j]);
1025   }
1026 
1027   DOWNSAMPLE4;
1028 }
1029 
1030 //////////////////////////////////////////////////////////////////////////
1031 
1032 void
DriveNoise_Ctor(DriveNoise * unit)1033 DriveNoise_Ctor (DriveNoise *unit)
1034 {
1035   SETCALC(DriveNoise_next);
1036 }
1037 
1038 void
DriveNoise_next(DriveNoise * unit,int inNumSamples)1039 DriveNoise_next (DriveNoise *unit, int inNumSamples)
1040 {
1041   float *out = ZOUT(0);
1042   float *in = ZIN(0);
1043   float amount = ZIN0(1);
1044   int multi = (int) ZIN0(2);
1045 
1046   RGET
1047 
1048   LOOP(inNumSamples,
1049        float noise;
1050        float x;
1051        float m;
1052        m = x = ZXP(in);
1053        noise = frand2(s1, s2, s3);
1054        for (int i=0; i<multi; i++) m *= m;
1055        ZXP(out) = x + noise * m * amount;
1056   );
1057 
1058   RPUT
1059 }
1060 
1061 ////////////////////////////////////////////////////////////////////////////
1062 
1063 
1064 void
SoftClipAmp8_Ctor(SoftClipAmp8 * unit)1065 SoftClipAmp8_Ctor (SoftClipAmp8 *unit)
1066 {
1067   OVERSAMPLE8_INIT;
1068   SETCALC(SoftClipAmp8_next);
1069 }
1070 
1071 void
SoftClipAmp8_Dtor(SoftClipAmp8 * unit)1072 SoftClipAmp8_Dtor (SoftClipAmp8 *unit)
1073 {
1074   OVERSAMPLE_DTOR;
1075 }
1076 
1077 void
SoftClipAmp8_next(SoftClipAmp8 * unit,int inNumSamples)1078 SoftClipAmp8_next (SoftClipAmp8 *unit, int inNumSamples)
1079 {
1080   float *out = ZOUT(0);
1081   float *in = ZIN(0);
1082   float pregain = ZIN0(1);
1083 
1084   UPSAMPLE8;
1085 
1086   for (int j=0; j<(inNumSamples*8); j++) {
1087 	  domemoff[j] = sc_softclip(domemoff[j]*pregain);
1088   }
1089 
1090   DOWNSAMPLE8;
1091 }
1092 
1093 ////////////////////////////////////////////////////////////////
1094 
1095 
1096 void
SoftClipAmp4_Ctor(SoftClipAmp4 * unit)1097 SoftClipAmp4_Ctor (SoftClipAmp4 *unit)
1098 {
1099   OVERSAMPLE4_INIT;
1100   SETCALC(SoftClipAmp4_next);
1101 }
1102 
1103 void
SoftClipAmp4_Dtor(SoftClipAmp4 * unit)1104 SoftClipAmp4_Dtor (SoftClipAmp4 *unit)
1105 {
1106   OVERSAMPLE_DTOR;
1107 }
1108 
1109 void
SoftClipAmp4_next(SoftClipAmp4 * unit,int inNumSamples)1110 SoftClipAmp4_next (SoftClipAmp4 *unit, int inNumSamples)
1111 {
1112   float *out = ZOUT(0);
1113   float *in = ZIN(0);
1114   float pregain = ZIN0(1);
1115 
1116   UPSAMPLE4;
1117 
1118   for (int j=0; j<(inNumSamples*4); j++) {
1119 	  domemoff[j] = sc_softclip(domemoff[j]*pregain);
1120   }
1121 
1122   DOWNSAMPLE4;
1123 }
1124 
1125 
1126 void
OSWrap4_Ctor(OSWrap4 * unit)1127 OSWrap4_Ctor (OSWrap4 *unit)
1128 {
1129   OVERSAMPLE4_INIT;
1130   SETCALC(OSWrap4_next);
1131 }
1132 
1133 void
OSWrap4_Dtor(OSWrap4 * unit)1134 OSWrap4_Dtor (OSWrap4 *unit)
1135 {
1136   OVERSAMPLE_DTOR;
1137 }
1138 
1139 void
OSWrap4_next(OSWrap4 * unit,int inNumSamples)1140 OSWrap4_next (OSWrap4 *unit, int inNumSamples)
1141 {
1142   float *out = ZOUT(0);
1143   float *in = ZIN(0);
1144   float min = ZIN0(1);
1145   float max = ZIN0(2);
1146 
1147   UPSAMPLE4;
1148 
1149   for (int j=0; j<(inNumSamples*4); j++) {
1150 	  domemoff[j] = sc_wrap(domemoff[j], min, max);
1151   }
1152 
1153   DOWNSAMPLE4;
1154 }
1155 
1156 
1157 void
OSWrap8_Ctor(OSWrap8 * unit)1158 OSWrap8_Ctor (OSWrap8 *unit)
1159 {
1160   OVERSAMPLE8_INIT;
1161   SETCALC(OSWrap8_next);
1162 }
1163 
1164 void
OSWrap8_Dtor(OSWrap8 * unit)1165 OSWrap8_Dtor (OSWrap8 *unit)
1166 {
1167   OVERSAMPLE_DTOR;
1168 }
1169 
1170 void
OSWrap8_next(OSWrap8 * unit,int inNumSamples)1171 OSWrap8_next (OSWrap8 *unit, int inNumSamples)
1172 {
1173   float *out = ZOUT(0);
1174   float *in = ZIN(0);
1175   float min = ZIN0(1);
1176   float max = ZIN0(2);
1177 
1178   UPSAMPLE8;
1179 
1180   for (int j=0; j<(inNumSamples*8); j++) {
1181 	  domemoff[j] = sc_wrap(domemoff[j], min, max);
1182   }
1183 
1184   DOWNSAMPLE8;
1185 }
1186 
1187 
1188 void
OSFold4_Ctor(OSFold4 * unit)1189 OSFold4_Ctor (OSFold4 *unit)
1190 {
1191   OVERSAMPLE4_INIT;
1192   SETCALC(OSFold4_next);
1193 }
1194 
1195 void
OSFold4_Dtor(OSFold4 * unit)1196 OSFold4_Dtor (OSFold4 *unit)
1197 {
1198   OVERSAMPLE_DTOR;
1199 }
1200 
1201 void
OSFold4_next(OSFold4 * unit,int inNumSamples)1202 OSFold4_next (OSFold4 *unit, int inNumSamples)
1203 {
1204   float *out = ZOUT(0);
1205   float *in = ZIN(0);
1206   float min = ZIN0(1);
1207   float max = ZIN0(2);
1208 
1209   UPSAMPLE4;
1210 
1211   for (int j=0; j<(inNumSamples*4); j++) {
1212 	  domemoff[j] = sc_fold(domemoff[j], min, max);
1213   }
1214 
1215   DOWNSAMPLE4;
1216 }
1217 
1218 
1219 void
OSFold8_Ctor(OSFold8 * unit)1220 OSFold8_Ctor (OSFold8 *unit)
1221 {
1222   OVERSAMPLE8_INIT;
1223   SETCALC(OSFold8_next);
1224 }
1225 
1226 void
OSFold8_Dtor(OSFold8 * unit)1227 OSFold8_Dtor (OSFold8 *unit)
1228 {
1229   OVERSAMPLE_DTOR;
1230 }
1231 
1232 void
OSFold8_next(OSFold8 * unit,int inNumSamples)1233 OSFold8_next (OSFold8 *unit, int inNumSamples)
1234 {
1235   float *out = ZOUT(0);
1236   float *in = ZIN(0);
1237   float min = ZIN0(1);
1238   float max = ZIN0(2);
1239 
1240   UPSAMPLE8;
1241 
1242   for (int j=0; j<(inNumSamples*8); j++) {
1243 	  domemoff[j] = sc_fold(domemoff[j], min, max);
1244   }
1245 
1246   DOWNSAMPLE8;
1247 }
1248 
1249 
1250 
1251 void
OSTrunc4_Ctor(OSTrunc4 * unit)1252 OSTrunc4_Ctor (OSTrunc4 *unit)
1253 {
1254   OVERSAMPLE4_INIT;
1255   SETCALC(OSTrunc4_next);
1256 }
1257 
1258 void
OSTrunc4_Dtor(OSTrunc4 * unit)1259 OSTrunc4_Dtor (OSTrunc4 *unit)
1260 {
1261   OVERSAMPLE_DTOR;
1262 }
1263 
1264 void
OSTrunc4_next(OSTrunc4 * unit,int inNumSamples)1265 OSTrunc4_next (OSTrunc4 *unit, int inNumSamples)
1266 {
1267   float *out = ZOUT(0);
1268   float *in = ZIN(0);
1269   float quant = ZIN0(1);
1270 
1271   UPSAMPLE4;
1272 
1273   for (int j=0; j<(inNumSamples*4); j++) {
1274 	  domemoff[j] = sc_trunc(domemoff[j], quant);
1275   }
1276 
1277   DOWNSAMPLE4;
1278 }
1279 
1280 
1281 void
OSTrunc8_Ctor(OSTrunc8 * unit)1282 OSTrunc8_Ctor (OSTrunc8 *unit)
1283 {
1284   OVERSAMPLE8_INIT;
1285   SETCALC(OSTrunc8_next);
1286 }
1287 
1288 void
OSTrunc8_Dtor(OSTrunc8 * unit)1289 OSTrunc8_Dtor (OSTrunc8 *unit)
1290 {
1291   OVERSAMPLE_DTOR;
1292 }
1293 
1294 void
OSTrunc8_next(OSTrunc8 * unit,int inNumSamples)1295 OSTrunc8_next (OSTrunc8 *unit, int inNumSamples)
1296 {
1297   float *out = ZOUT(0);
1298   float *in = ZIN(0);
1299   float quant = ZIN0(1);
1300 
1301 
1302   UPSAMPLE8;
1303 
1304   for (int j=0; j<(inNumSamples*8); j++) {
1305 	  domemoff[j] = sc_trunc(domemoff[j], quant);
1306   }
1307 
1308   DOWNSAMPLE8;
1309 }
1310 
1311 
1312 ////////////////////////////////////////////////////////////////////////////////////
1313 
PluginLoad(Berlach)1314 PluginLoad(Berlach)
1315 {
1316   ft = inTable;
1317   DefineSimpleUnit(PeakEQ4);
1318   DefineSimpleUnit(PeakEQ2);
1319   DefineSimpleUnit(LPF18);
1320   DefineSimpleUnit(LPF1);
1321   DefineSimpleUnit(LPFVS6);
1322   DefineSimpleUnit(LPF1);
1323   DefineSimpleUnit(BLBufRd);
1324   DefineDtorUnit(OSWrap4);
1325   DefineDtorUnit(OSWrap8);
1326   DefineDtorUnit(OSTrunc4);
1327   DefineDtorUnit(OSTrunc8);
1328   DefineDtorUnit(OSFold4);
1329   DefineDtorUnit(OSFold8);
1330   DefineDtorUnit(Clipper8);
1331   DefineDtorUnit(Clipper4);
1332   DefineDtorUnit(SoftClipper8);
1333   DefineDtorUnit(SoftClipper4);
1334   DefineDtorUnit(SoftClipAmp8);
1335   DefineDtorUnit(SoftClipAmp4);
1336   DefineSimpleUnit(DriveNoise);
1337 
1338 
1339 }
1340