#include <math.h>
#include "resonator.h"
#ifndef PI
#define PI 3.1415926535897932384626433832795f
#endif
void resonator::initResonator(
int aFrequency,
int aBandwidth,
int aSamplerate)
{
float arg = (-PI / aSamplerate) * aBandwidth;
float r = (float)exp(arg);
mC = -(r * r);
arg = (-2.0f * PI / aSamplerate) * aFrequency;
mB = r * (float)cos(arg) * 2.0f;
mA = 1.0f - mB - mC;
}
void resonator::initAntiresonator(
int aFrequency,
int aBandwidth,
int aSamplerate)
{
initResonator(aFrequency, aBandwidth, aSamplerate);
mA = 1.0f / mA;
mB *= -mA;
mC *= -mA;
}
float resonator::resonate(float input)
{
float x = mA * input + mB * mP1 + mC * mP2;
mP2 = mP1;
mP1 = x;
return x;
}
float resonator::antiresonate(float input)
{
float x = mA * input + mB * mP1 + mC * mP2;
mP2 = mP1;
mP1 = input;
return x;
}
resonator::resonator()
{
mA = mB = mC = mP1 = mP2 = 0;
}
resonator::~resonator()
{
}
void resonator::setGain(float aG)
{
mA *= aG;
}