1 /*
2     Copyright (C) 2010 Fons Adriaensen <fons@kokkinizita.net>
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 
20 #include <math.h>
21 #include "allpass.h"
22 
23 
prepare(float f1,float b1,float f2,float b2)24 void Allpass22::prepare (float f1, float b1, float f2, float b2)
25 {
26     f1 *= float (M_PI);
27     b1 *= f1;
28     _c1 = -cosf (2 * f1);
29     _c2 = (1 - b1) / (1 + b1);
30     f2 *= float (M_PI);
31     b2 *= f2;
32     _c3 = -cosf (2 * f2);
33     _c4 = (1 - b2) / (1 + b2);
34 }
35 
36 
process(int n,float * inp,float * out)37 void Allpass22::process (int n, float *inp, float *out)
38 {
39     float x, y, z1, z2, z3, z4;
40 
41     z1 = _z1;
42     z2 = _z2;
43     z3 = _z3;
44     z4 = _z4;
45     while (n--)
46     {
47 	x = *inp++;
48 	y = x - _c2 * z2;
49 	x = z2 + _c2 * y;
50 	y -= _c1 * z1;
51 	z2 = z1 + _c1 * y;
52 	z1 = y + 1e-20f;
53 	y = x - _c4 * z4;
54 	x = z4 + _c4 * y;
55 	y -= _c3 * z3;
56 	z4 = z3 + _c3 * y;
57 	z3 = y + 1e-20f;
58 	*out++ = x;
59     }
60     _z1 = z1;
61     _z2 = z2;
62     _z3 = z3;
63     _z4 = z4;
64 }
65 
66 
67 float Allpass22::quad44real [4] = {  41.0f, 4.00f,  5.10e3f, 3.35f };
68 float Allpass22::quad44imag [4] = { 153.0f, 3.50f, 13.80e3f, 2.00f };
69 float Allpass22::quad48real [4] = {  41.0f, 4.00f,  5.05e3f, 3.40f };
70 float Allpass22::quad48imag [4] = { 153.0f, 3.50f, 14.20e3f, 2.20f };
71 float Allpass22::quad96real [4] = {  41.0f, 4.00f,  5.50e3f, 3.40f };
72 float Allpass22::quad96imag [4] = { 155.0f, 3.65f, 18.00e3f, 3.00f };
73 
74 
initquad(Allpass22 * A,Allpass22 * B,float fsam)75 int Allpass22::initquad (Allpass22 *A, Allpass22 *B, float fsam)
76 {
77     float *a = 0;
78     float *b = 0;
79 
80     if ((fsam >= 43.1e3f) && (fsam < 45.1e3f))
81     {
82 	a = quad44real;
83 	b = quad44imag;
84     }
85     else if ((fsam >= 47e3f) && (fsam < 49e3f))
86     {
87 	a = quad48real;
88 	b = quad48imag;
89     }
90     else if ((fsam >= 86e3f) && (fsam < 98e3f))
91     {
92 	a = quad96real;
93 	b = quad96imag;
94     }
95 
96     if (!a) return 1;
97     if (A) A->prepare (a [0] / fsam, a [1], a [2] / fsam, a [3]);
98     if (B) B->prepare (b [0] / fsam, b [1], b [2] / fsam, b [3]);
99     return 0;
100 }
101 
102