]> git.bts.cx Git - benzene.git/blob - third_party/soloud_speech/resonator.cpp
Initial version
[benzene.git] / third_party / soloud_speech / resonator.cpp
1 #include <math.h>
2 #include "resonator.h"
3
4 #ifndef PI
5 #define PI 3.1415926535897932384626433832795f
6 #endif
7
8 /* Convert formant freqencies and bandwidth into resonator difference equation coefficents
9 */
10 void resonator::initResonator(
11 int aFrequency, /* Frequency of resonator in Hz */
12 int aBandwidth, /* Bandwidth of resonator in Hz */
13 int aSamplerate)
14 {
15 float arg = (-PI / aSamplerate) * aBandwidth;
16 float r = (float)exp(arg);
17 mC = -(r * r);
18 arg = (-2.0f * PI / aSamplerate) * aFrequency;
19 mB = r * (float)cos(arg) * 2.0f;
20 mA = 1.0f - mB - mC;
21 }
22
23 /* Convert formant freqencies and bandwidth into anti-resonator difference equation constants
24 */
25 void resonator::initAntiresonator(
26 int aFrequency, /* Frequency of resonator in Hz */
27 int aBandwidth, /* Bandwidth of resonator in Hz */
28 int aSamplerate)
29 {
30 initResonator(aFrequency, aBandwidth, aSamplerate); /* First compute ordinary resonator coefficients */
31 /* Now convert to antiresonator coefficients */
32 mA = 1.0f / mA; /* a'= 1/a */
33 mB *= -mA; /* b'= -b/a */
34 mC *= -mA; /* c'= -c/a */
35 }
36
37 /* Generic resonator function */
38 float resonator::resonate(float input)
39 {
40 float x = mA * input + mB * mP1 + mC * mP2;
41 mP2 = mP1;
42 mP1 = x;
43 return x;
44 }
45
46 /* Generic anti-resonator function
47 Same as resonator except that a,b,c need to be set with initAntiresonator()
48 and we save inputs in p1/p2 rather than outputs.
49 There is currently only one of these - "mNasalZero"
50 */
51 /* Output = (mNasalZero.a * input) + (mNasalZero.b * oldin1) + (mNasalZero.c * oldin2) */
52
53 float resonator::antiresonate(float input)
54 {
55 float x = mA * input + mB * mP1 + mC * mP2;
56 mP2 = mP1;
57 mP1 = input;
58 return x;
59 }
60
61 resonator::resonator()
62 {
63 mA = mB = mC = mP1 = mP2 = 0;
64 }
65
66 resonator::~resonator()
67 {
68 }
69
70 void resonator::setGain(float aG)
71 {
72 mA *= aG;
73 }
74