Add a wrapper for the EQ filter

This commit is contained in:
Jean Pierre Cimalando 2020-02-05 22:41:02 +01:00
parent e6978b71c2
commit 0015703130
3 changed files with 147 additions and 0 deletions

View file

@ -13,6 +13,9 @@
namespace sfz {
//------------------------------------------------------------------------------
// SFZ v2 multi-mode filter
template <unsigned NCh>
struct Filter<NCh>::Impl {
FilterType fType = kFilterNone;
@ -246,4 +249,84 @@ void Filter<NCh>::setType(FilterType type)
template class Filter<1>;
template class Filter<2>;
//------------------------------------------------------------------------------
// SFZ v1 equalizer filter
template <unsigned NCh>
struct FilterEq<NCh>::Impl {
sfzEq<NCh> fDsp;
};
template <unsigned NCh>
FilterEq<NCh>::FilterEq()
: P{new Impl}
{
}
template <unsigned NCh>
FilterEq<NCh>::~FilterEq()
{
}
template <unsigned NCh>
void FilterEq<NCh>::init(double sampleRate)
{
sfzEq<NCh> &filter = P->fDsp;
filter.init(sampleRate);
}
template <unsigned NCh>
void FilterEq<NCh>::clear()
{
sfzEq<NCh> &filter = P->fDsp;
filter.instanceClear();
}
template <unsigned NCh>
void FilterEq<NCh>::process(const float *const in[NCh], float *const out[NCh], float cutoff, float bw, float pksh, unsigned nframes)
{
sfzEq<NCh> &filter = P->fDsp;
filter.setCutoff(cutoff);
filter.setBandwidth(bw);
filter.setPkShGain(pksh);
filter.compute(nframes, const_cast<float **>(in), const_cast<float **>(out));
}
template <unsigned NCh>
void FilterEq<NCh>::processModulated(const float *const in[NCh], float *const out[NCh], const float *cutoff, const float *bw, const float *pksh, unsigned nframes)
{
sfzEq<NCh> &filter = P->fDsp;
unsigned frame = 0;
while (frame < nframes) {
unsigned current = nframes - frame;
if (current > config::filterControlInterval)
current = config::filterControlInterval;
const float *current_in[NCh];
float *current_out[NCh];
for (unsigned c = 0; c < NCh; ++c) {
current_in[c] = in[c] + frame;
current_out[c] = out[c] + frame;
}
filter.setCutoff(cutoff[frame]);
filter.setBandwidth(bw[frame]);
filter.setPkShGain(pksh[frame]);
filter.compute(current, const_cast<float **>(current_in), const_cast<float **>(current_out));
frame += current;
}
}
template class FilterEq<1>;
template class FilterEq<2>;
} // namespace sfz

View file

@ -97,4 +97,53 @@ enum FilterType : int {
kFilterPeq,
};
/**
Equalizer filter for SFZ v1
Available for mono and stereo. (NCh=1, NCh=2)
Parameters:
`cutoff`: it's the opcode `egN_freq` (Hz)
`bw`: it's the opcode `eqN_bw` (octave)
`pksh`: it's the opcode `eqN_gain` (dB)
*/
template <unsigned NCh>
class FilterEq {
public:
FilterEq();
~FilterEq();
/**
Set up the filter constants.
Run it exactly once after instantiating.
*/
void init(double sampleRate);
/**
Reinitialize the filter memory to zeros.
*/
void clear();
/**
Process one cycle of the filter without modulating cutoff or bandwidth.
`cutoff` is a frequency expressed in Hz.
`bw` is a bandwidth expressed in octaves.
`pksh` is a peak/shelf gain expressed in dB.
`in[i]` and `out[i]` may refer to identical buffers, for in-place processing
*/
void process(const float *const in[NCh], float *const out[NCh], float cutoff, float bw, float pksh, unsigned nframes);
/**
Process one cycle of the filter with cutoff and bandwidth values varying over time.
`cutoff` is a frequency expressed in Hz.
`bw` is a bandwidth expressed in octaves.
`pksh` is a peak/shelf gain expressed in dB.
`in[i]` and `out[i]` may refer to identical buffers, for in-place processing
*/
void processModulated(const float *const in[NCh], float *const out[NCh], const float *cutoff, const float *bw, const float *pksh, unsigned nframes);
private:
struct Impl;
std::unique_ptr<Impl> P;
};
} // namespace sfz

View file

@ -36,6 +36,7 @@ struct UI {};
#include "gen/filters/sfzLsh.cxx"
#include "gen/filters/sfzHsh.cxx"
#include "gen/filters/sfzPeq.cxx"
#include "gen/filters/sfzEq.cxx"
#include "gen/filters/sfz2chApf1p.cxx"
#include "gen/filters/sfz2chBpf1p.cxx"
@ -60,6 +61,7 @@ struct UI {};
#include "gen/filters/sfz2chLsh.cxx"
#include "gen/filters/sfz2chHsh.cxx"
#include "gen/filters/sfz2chPeq.cxx"
#include "gen/filters/sfz2chEq.cxx"
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
@ -105,6 +107,16 @@ template <class F> struct sfzFilterPkSh : public F {
void setPkShGain(float v) { F::fPkShGain = v; }
};
/**
Wrapper of equalizer filters with a bandwidth control
Parameterized by cutoff, bandwidth, peak/shelf gain
*/
template <class F> struct sfzFilterEq : public F {
void setCutoff(float v) { F::fCutoff = v; }
void setBandwidth(float v) { F::fBandwidth = v; }
void setPkShGain(float v) { F::fPkShGain = v; }
};
template <unsigned NCh> struct sfzLpf1p;
template <unsigned NCh> struct sfzLpf2p;
template <unsigned NCh> struct sfzLpf4p;
@ -128,6 +140,7 @@ template <unsigned NCh> struct sfzBrf2pSv;
template <unsigned NCh> struct sfzLsh;
template <unsigned NCh> struct sfzHsh;
template <unsigned NCh> struct sfzPeq;
template <unsigned NCh> struct sfzEq;
template<> struct sfzLpf1p<1> : public sfzFilterNoQ<faustLpf1p> {};
template<> struct sfzLpf2p<1> : public sfzFilter<faustLpf2p> {};
@ -152,6 +165,7 @@ template<> struct sfzBrf2pSv<1> : public sfzFilter<faustBrf2pSv> {};
template<> struct sfzLsh<1> : public sfzFilterPkSh<faustLsh> {};
template<> struct sfzHsh<1> : public sfzFilterPkSh<faustHsh> {};
template<> struct sfzPeq<1> : public sfzFilterPkSh<faustPeq> {};
template<> struct sfzEq<1> : public sfzFilterEq<faustEq> {};
template<> struct sfzLpf1p<2> : public sfzFilterNoQ<faust2chLpf1p> {};
template<> struct sfzLpf2p<2> : public sfzFilter<faust2chLpf2p> {};
@ -176,3 +190,4 @@ template<> struct sfzBrf2pSv<2> : public sfzFilter<faust2chBrf2pSv> {};
template<> struct sfzLsh<2> : public sfzFilterPkSh<faust2chLsh> {};
template<> struct sfzHsh<2> : public sfzFilterPkSh<faust2chHsh> {};
template<> struct sfzPeq<2> : public sfzFilterPkSh<faust2chPeq> {};
template<> struct sfzEq<2> : public sfzFilterEq<faust2chEq> {};