Parallel resonance
This commit is contained in:
parent
7c36a8bf98
commit
54bee81c43
|
@ -5,20 +5,55 @@ void Resonator::clear()
|
||||||
xp = xpp = yp = ypp = 0;
|
xp = xpp = yp = ypp = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Resonator::process(float* output, const float* const input, uint32_t frames)
|
inline float Resonator::process(float x)
|
||||||
{
|
{
|
||||||
for(uint32_t i = 0; i < frames; ++i){
|
// Apply biquad filter.
|
||||||
float x = static_cast<float>(input[i] * scale);
|
float y = scale * x - xpp - app * ypp + ap * yp;
|
||||||
float y = x - xpp - app * ypp + ap * yp;
|
// Update biquad memory.
|
||||||
output[i] = y;
|
|
||||||
xpp = xp;
|
xpp = xp;
|
||||||
xp = x;
|
xp = x;
|
||||||
ypp = yp;
|
ypp = yp;
|
||||||
yp = y;
|
yp = y;
|
||||||
}
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Resonator::set(double _app, double _ap, double _scale)
|
void Resonator::set(double _app, double _ap, double _scale)
|
||||||
{
|
{
|
||||||
app = _app; ap = _ap; scale = _scale;
|
app = _app;
|
||||||
|
ap = _ap;
|
||||||
|
scale = _scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
Filter::Filter(){};
|
||||||
|
|
||||||
|
void Filter::process(float **outputs, const float **inputs, uint32_t frames)
|
||||||
|
{
|
||||||
|
for (uint chn = 0; chn < 2; ++chn)
|
||||||
|
{
|
||||||
|
for (auto &r : res[chn])
|
||||||
|
{
|
||||||
|
for (uint32_t j = 0; j < frames; ++j)
|
||||||
|
{
|
||||||
|
outputs[chn][j] += r.process(inputs[chn][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute filter coefficients from
|
||||||
|
// frequency as a proportion of the sample rate
|
||||||
|
// REMEMBER: THAT'S DOUBLE THE Nyquist RATE, dummy
|
||||||
|
// and resonance in [0, 1]
|
||||||
|
void Filter::set(double x, double y, double p)
|
||||||
|
{
|
||||||
|
double r = p * 0.049 + 0.95; // Filter unstable at r = 1.
|
||||||
|
double app = r * r;
|
||||||
|
double xap = (1.0 + r * r) * cos(M_PI * x);
|
||||||
|
double yap = (1.0 + r * r) * cos(M_PI * y);
|
||||||
|
double scale = sqrt(0.5 - 0.5 * r * r);
|
||||||
|
for (auto &channelFilter : res)
|
||||||
|
{
|
||||||
|
channelFilter[0].set(app, xap, scale);
|
||||||
|
channelFilter[1].set(app, yap, scale);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -8,7 +8,7 @@ public:
|
||||||
Resonator():
|
Resonator():
|
||||||
xp(0), xpp(0), yp(0), ypp(0), app(0), ap(0), scale(1){};
|
xp(0), xpp(0), yp(0), ypp(0), app(0), ap(0), scale(1){};
|
||||||
|
|
||||||
void process(float *output, const float *const input, uint32_t frames);
|
inline float process(float x);
|
||||||
void clear();
|
void clear();
|
||||||
void set(double _app, double _ap, double _scale);
|
void set(double _app, double _ap, double _scale);
|
||||||
|
|
||||||
|
@ -25,46 +25,10 @@ private:
|
||||||
class Filter
|
class Filter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Filter(){};
|
Filter();
|
||||||
|
void process(float **outputs, const float **inputs, uint32_t frames);
|
||||||
void process(float **outputs, const float **inputs, uint32_t frames)
|
void set(double x, double y, double p);
|
||||||
{
|
|
||||||
for (uint chn = 0; chn < 2; ++chn)
|
|
||||||
{
|
|
||||||
for (uint32_t j = 0; j < frames; ++j)
|
|
||||||
{
|
|
||||||
outputs[chn][j] = inputs[chn][j];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& r : res[chn])
|
|
||||||
{
|
|
||||||
r.process(outputs[chn], outputs[chn], frames);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//Compute filter coefficients from
|
|
||||||
//frequency as a proportion of the sample rate
|
|
||||||
//REMEMBER: THAT'S DOUBLE THE Nyquist RATE, dummy
|
|
||||||
//and resonance in [0, 1]
|
|
||||||
void set(double x, double y, double p)
|
|
||||||
{
|
|
||||||
double r = p * 0.99;
|
|
||||||
double app = r * r;
|
|
||||||
double xap = (1.0 + r * r) * cos(M_PI * x);
|
|
||||||
double yap = (1.0 + r * r) * cos(M_PI * y);
|
|
||||||
double scale = sqrt(0.5 - 0.5 * r * r);
|
|
||||||
for (auto& channelFilter : res)
|
|
||||||
{
|
|
||||||
channelFilter[0].set(app, xap, scale);
|
|
||||||
channelFilter[1].set(app, xap, scale);
|
|
||||||
channelFilter[2].set(app, yap, scale);
|
|
||||||
channelFilter[3].set(app, yap, scale);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::array<std::array<Resonator, 4>, 2> res;
|
std::array<std::array<Resonator, 2>, 2> res;
|
||||||
};
|
};
|
Loading…
Reference in New Issue