2 * Copyright 2012, Gerasim Troeglazov (3dEyes**), 3dEyes@gmail.com.
4 * Distributed under the terms of the MIT License.
11 #include "Equalizer.h"
13 Equalizer::Equalizer()
19 Equalizer::~Equalizer()
24 Equalizer::SetFormat(int channels
, float framerate
)
29 memset(fWDataVector
[0][0], 0, sizeof(fWDataVector
));
31 fActiveBands
= BandCount();
32 while(fFrequency
[fActiveBands
-1] * 2.0 > fRate
) {
36 for(int i
= 0; i
< fActiveBands
; i
++) {
37 BandPassFilterCalcs(fAWeights
[i
], fBWeights
[i
],
38 fFrequency
[i
] / (float)fRate
);
43 Equalizer::SetPreAmp(double value
)
50 Equalizer::PreAmp(void)
56 Equalizer::SetBand(int band
, double value
)
58 if (band
< 0 || band
>= BandCount())
66 Equalizer::Band(int band
)
68 if (band
< 0 || band
>= BandCount())
75 Equalizer::BandCount(void)
81 Equalizer::BandFrequency(int band
)
83 if (band
< 0 || band
>= BandCount())
86 return fFrequency
[band
];
90 Equalizer::ProcessBuffer(float* buffer
, int samples
)
92 if (!fActivated
|| samples
<= 0)
95 for(int i
= 0; i
< fChannels
; i
++) {
96 float *g
= fGainVector
[i
];
97 float *end
= buffer
+ samples
;
98 for(float *fptr
= buffer
+ i
; fptr
< end
; fptr
+= fChannels
) {
100 for (int k
= 0; k
< fActiveBands
; k
++) {
101 float *Wq
= fWDataVector
[i
][k
];
102 float w
= yt
* fBWeights
[k
][0] +
103 Wq
[0] * fAWeights
[k
][0] +
104 Wq
[1] * fAWeights
[k
][1];
105 yt
+= (w
+ Wq
[1] * fBWeights
[k
][1]) * g
[k
];
117 float freq
[EQ_BANDS
] = {31.25, 62.5, 125, 250, 500, 1000, 2000, 4000, 8000, 16000};
119 for(int i
= 0; i
< BandCount(); i
++) {
120 fFrequency
[i
] = freq
[i
];
129 Equalizer::RecalcGains(void)
131 float adjust
[EQ_BANDS
];
133 for(int i
= 0; i
< BandCount(); i
++)
134 adjust
[i
] = fPreAmp
+ fBands
[i
];
136 for(int c
= 0; c
< MAX_CHANNELS
; c
++)
137 for(int i
= 0; i
<BandCount(); i
++)
138 fGainVector
[c
][i
] = pow(10, adjust
[i
] / 20) - 1;
144 Equalizer::BandPassFilterCalcs(float *a
, float *b
, float f
)
147 float theta
= 2.0 * M_PI
* f
;
148 float c_value
= (1 - tanf(theta
* q
/ 2.0)) / (1 + tanf(theta
* q
/ 2.0));
150 a
[0] = (1 + c_value
) * cosf(theta
);
152 b
[0] = (1 - c_value
) / 2.0;