| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (C) 2003-2013 Fons Adriaensen <[email protected]> |
| 4 |
// |
| 5 |
// This program is free software; you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU General Public License as published by |
| 7 |
// the Free Software Foundation; either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// This program is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// ---------------------------------------------------------------------------- |
| 19 |
|
| 20 |
|
| 21 |
#include <math.h> |
| 22 |
#include "acweight.h" |
| 23 |
|
| 24 |
|
| 25 |
#define ACW_F1 20.5990 |
| 26 |
#define ACW_F2 107.652 |
| 27 |
#define ACW_F3 737.862 |
| 28 |
#define ACW_F4 12194.2 |
| 29 |
#define AW_GAIN 1.250 |
| 30 |
#define CW_GAIN 1.007 |
| 31 |
|
| 32 |
|
| 33 |
int ACweight::init (int fsamp) |
| 34 |
{ |
| 35 |
double f, g; |
| 36 |
|
| 37 |
reset (); |
| 38 |
_w1 = _w2 = _w3 = _w4 = _ga = _gc = 0; |
| 39 |
|
| 40 |
switch (fsamp) |
| 41 |
{ |
| 42 |
case 44100: _w4 = 0.846; break; |
| 43 |
case 48000: _w4 = 0.817; break; |
| 44 |
case 88200: _w4 = 0.587; break; |
| 45 |
case 96000: _w4 = 0.555; break; |
| 46 |
default: return 1; |
| 47 |
} |
| 48 |
|
| 49 |
f = ACW_F1 / fsamp; |
| 50 |
_w1 = 2 * M_PI * f; |
| 51 |
g = 4 / ((2 - _w1) * (2 - _w1)); |
| 52 |
_w1 *= 1 - 3 * f; |
| 53 |
_gc = CW_GAIN * g; |
| 54 |
|
| 55 |
f = ACW_F2 / fsamp; |
| 56 |
_w2 = 2 * M_PI * f; |
| 57 |
g *= 2 / (2 - _w2); |
| 58 |
_w2 *= 1 - 3 * f; |
| 59 |
f = ACW_F3 / fsamp; |
| 60 |
_w3 = 2 * M_PI * f; |
| 61 |
g *= 2 / (2 - _w3); |
| 62 |
_w3 *= 1 - 3 * f; |
| 63 |
_ga = AW_GAIN * g; |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
void ACweight::reset (void) |
| 70 |
{ |
| 71 |
// reset filter state |
| 72 |
_z1a = _z1b = _z2 = _z3 = _z4a = _z4b = 0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
void ACweight::process (size_t n, const float *in, float *opA, float *opC) |
| 77 |
{ |
| 78 |
float x, e; |
| 79 |
|
| 80 |
e = ldexpf (1.0f, -20); |
| 81 |
|
| 82 |
while (n--) |
| 83 |
{ |
| 84 |
x = *in++; |
| 85 |
|
| 86 |
// highpass sections, A and C |
| 87 |
_z1a += _w1 * (x - _z1a + e); |
| 88 |
x -= _z1a; |
| 89 |
_z1b += _w1 * (x - _z1b + e); |
| 90 |
x -= _z1b; |
| 91 |
|
| 92 |
// lowpass sections, A, and C |
| 93 |
_z4a += _w4 * (x - _z4a); |
| 94 |
x = 0.25 * _z4b; |
| 95 |
_z4b += _w4 * (_z4a - _z4b); |
| 96 |
x += 0.75 * _z4b; |
| 97 |
|
| 98 |
*opC++ = _gc * x; |
| 99 |
|
| 100 |
// highpass sections, A only |
| 101 |
_z2 += _w2 * (x - _z2 + e); |
| 102 |
x -= _z2; |
| 103 |
_z3 += _w3 * (x - _z3 + e); |
| 104 |
x -= _z3; |
| 105 |
|
| 106 |
*opA++ = _ga * x; |
| 107 |
} |
| 108 |
} |