File onepole.h¶
File List > DaisySP > Source > Filters > onepole.h
Go to the documentation of this file
Source Code¶
/*
Copyright (c) 2020 Electrosmith, Corp, Emilie Gillet
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
#ifndef DSY_ONEPOLE_H
#define DSY_ONEPOLE_H
#include "Utility/dsp.h"
#include <cmath>
#include <algorithm>
namespace daisysp
{
class OnePole
{
public:
OnePole() {}
~OnePole() {}
enum FilterMode
{
FILTER_MODE_LOW_PASS,
FILTER_MODE_HIGH_PASS
};
void Init()
{
Reset();
mode_ = FILTER_MODE_LOW_PASS;
}
inline void Reset() { state_ = 0.0f; }
inline void SetFrequency(float freq)
{
// Clip coefficient to about 100.
freq = freq < 0.497f ? freq : 0.497f;
g_ = tanf(PI_F * freq);
gi_ = 1.f / (1.f + g_);
}
inline void SetFilterMode(FilterMode mode) { mode_ = mode; }
inline float Process(float in)
{
float lp;
lp = (g_ * in + state_) * gi_;
state_ = g_ * (in - lp) + lp;
switch(mode_)
{
case FILTER_MODE_LOW_PASS: return lp;
case FILTER_MODE_HIGH_PASS: return in - lp;
}
return 0.0f;
}
inline void ProcessBlock(float* in_out, size_t size)
{
while(size--)
{
*in_out = Process(*in_out);
++in_out;
}
}
private:
float g_;
float gi_;
float state_;
FilterMode mode_;
};
} // namespace daisysp
#endif // DSY_ONEPOLE_H