9 #define PI2 2*3.14159265358
16 void setup_lowpass(lowpass
* lp
, float f0
, float srate
){
18 float RC
= 1/(PI2
*f0
);
20 lp
->alpha
= T
/ (RC
+ T
);
24 void apply_lowpass(lowpass
* lp
, float buf
[], int count
){
26 for(i
=0; i
<count
; i
++){
27 buf
[i
] = lp
->y
+ lp
->alpha
*(buf
[i
] - lp
->y
);
33 void setup_highpass(highpass
* hp
, float f0
, float srate
){
35 float RC
= 1/(PI2
*f0
);
37 hp
->alpha
= RC
/ (RC
+ T
);
42 void apply_highpass(highpass
* hp
, float buf
[], int count
){
44 for(i
=0; i
<count
; i
++){
46 buf
[i
] = hp
->alpha
* (buf
[i
] + hp
->y
- hp
->x
);
53 void setup_lp2(biquad
* bq
, float f0
, float Q
, float srate
){
54 float w0
= PI2
*f0
/ srate
;
57 float alpha
= s
/ (2*Q
);
68 void setup_hp2(biquad
* bq
, float f0
, float Q
, float srate
){
69 float w0
= PI2
*f0
/ srate
;
72 float alpha
= s
/ (2*Q
);
83 void setup_bp2(biquad
* bq
, float f0
, float BW
, float srate
){
84 float w0
= PI2
*f0
/ srate
;
87 float alpha
= s
*sinh( log(2)/2 * BW
* w0
/s
);
98 void apply_biquad(biquad
* bq
, float buf
[], int count
){
100 for(i
=0; i
<count
; i
++){
103 buf
[i
] = bq
->a0r
*(bq
->b0
*buf
[i
] + bq
->b1
*bq
->x
[0] + bq
->b2
*bq
->x
[1]
104 - bq
->a1
*bq
->y
[0] - bq
->a2
*bq
->y
[1]);
116 x
= (((x
& 0xaaaaaaaa) >> 1) | ((x
& 0x55555555) << 1));
117 x
= (((x
& 0xcccccccc) >> 2) | ((x
& 0x33333333) << 2));
118 x
= (((x
& 0xf0f0f0f0) >> 4) | ((x
& 0x0f0f0f0f) << 4));
119 x
= (((x
& 0xff00ff00) >> 8) | ((x
& 0x00ff00ff) << 8));
120 return((x
>> 16) | (x
<< 16));
123 void ifft(float in
[][2], float out
[], const int N
){
130 for(n
=0; n
<N
; n
++){/* computes top level by reordering input */
131 int n1
= reverse(m
++);
132 int n2
= reverse(m
++);
134 T
[D
-1][n
][0] = in
[n1
][0] + in
[n2
][0];
135 T
[D
-1][n
][1] = in
[n1
][1] + in
[n2
][1];
138 T
[D
-1][n
][0] = in
[n1
][0] - in
[n2
][0];
139 T
[D
-1][n
][1] = in
[n1
][1] - in
[n2
][1];
143 for(L
=D
-2; L
>=0; L
--){/* compute lower levels in terms of one above */
149 float* T1
= T
[A
][j
+1];
150 float angle
= PI2
*((n
>>A
)<<A
)/N
;
151 float C
[2] = {cos(angle
), sin(angle
)};
153 //T[L][n] = T0 + T1*C; /* mini fourier transform */
154 T
[L
][n
][0] = T1
[0]*C
[0] - T1
[1]*C
[1];
155 T
[L
][n
][1] = T1
[0]*C
[1] + T1
[1]*C
[0];
164 for(n
=0; n
<N
; n
++){/* compute output from bottom level */
167 float angle
= PI2
*n
/N
;
168 float C
[2] = {cos(angle
), sin(angle
)};
170 //out[n] = T0 + T1*circle[n];
171 out
[n
] = T1
[0]*C
[0] - T1
[1]*C
[1];
181 /* delay line routines for use with string synth, reverb chorus echo effects*/
191 /* write count samples to the beginning of the delay line */
192 void delay_write(delayline
* d
, float in
[], int count
){
194 if(d
->ptr_in
+ count
< d
->size
){
195 for(i
=0; i
<count
; i
++){
196 d
->buf
[d
->ptr_in
++] = in
[i
];
200 int N
= d
->size
- d
->ptr_in
;
202 d
->buf
[d
->ptr_in
++] = in
[i
];
205 for(i
=N
; i
< count
; i
++){
206 d
->buf
[d
->ptr_in
++] = in
[i
];
211 /* read count samples from the end of the delay line */
212 void delay_read(delayline
* d
, float out
[], int count
){
214 if(d
->ptr_out
+ count
< d
->size
){
215 for(i
=0; i
<count
; i
++){
216 out
[i
] = d
->buf
[d
->ptr_out
++];
220 int N
= d
->size
- d
->ptr_out
;
222 out
[i
] = d
->buf
[d
->ptr_out
++];
225 for(i
=N
; i
< count
; i
++){
226 out
[i
] = d
->buf
[d
->ptr_out
++];
231 /* same as delay_read but does not actually read anything */
233 void delay_drop(delayline* d, int count){
234 if(d->ptr_out + count < d->size){
235 d->ptr_count += count;
238 d->ptr_count = count - (d->size - d->ptr_out);
242 /* read count interpolated samples from t samples in the past */
244 void delay_extract(delayline* d, float t, float out[], int count){
245 float x = d->ptr_in - t;
246 if(x < 0) x += d->size;
248 if(x + count < d->size){
251 float y0 = d->buf[x0];
252 float y1 = d->buf[x1];
253 for(int i=0; i<count; i++){
254 float m = (y1-y0)/(x1-x0);
255 out[i] = m*(x-x0) + y0;
264 int N = d->size - d->ptr_out;
267 float y0 = d->buf[x0];
268 float y1 = d->buf[x1];
269 for(int i=0; i<N; i++){
270 float m = (y1-y0)/(x1-x0);
271 out[i] = m*(x-x0) + y0;
283 float m = (y1-y0)/(x1-x0);
284 out[N] = m*(x-x0) + y0;
292 for(int i=N+1; i < count; i++){
293 float m = (y1-y0)/(x1-x0);
294 out[i] = m*(x-x0) + y0;
304 /* read samples using a variable delay signal */
305 void delay_variable(delayline
* d
, float t
[], float out
[], int count
){
307 for(i
=0; i
<count
; i
++){
308 //delay_extract(d, t[i]+i, out+i, count);
315 /* synthesizes a band limited square/saw wave using 16x oversampling */
317 float dy
; /* frequency */
318 float y
; /* phase counter */
319 float x
; /* low pass state */
322 void set_pulse_freq(pulse
* s
, float f
){
323 s
->dy
= 2*f
/(sample_rate
*16);
326 void generate_pulse(pulse
* s
, float step
[], float out
[], int count
, int type
){
331 const float T
= (1.0f
/(sample_rate
*16));
332 const float A
= T
/(1.0f
/(PI2
*10000) + T
);
339 while(s
->y
> 1){s
->y
-= 2;}
340 buf
[i
] = s
->y
< 0 ? 1 : -1;
348 while(s
->y
> 1){s
->y
-= 2;}
354 buf
[i
] = s
->x
+ A
*(buf
[i
] - s
->x
);
360 for(i
=0; i
<count
; i
++){
369 /* PAD synth algorithm */
370 void pad_synth(float amp
[], float out
[], int size
){
372 float (*in
)[][2] = malloc(size
*sizeof(float[2]));
373 for(i
=0; i
<size
; i
++){
375 float p
= (rand()*PI2
)/RAND_MAX
;
376 (*in
)[i
][0] = A
*cos(p
);
377 (*in
)[i
][1] = A
*sin(p
);
379 ifft(*in
, out
, size
);