BogaudioModules

BogaudioModules for VCV Rack
Log | Files | Refs | README | LICENSE

parametric_eq.hpp (4044B)


      1 #pragma once
      2 
      3 #include "bogaudio.hpp"
      4 #include "follower_base.hpp"
      5 #include "dsp/filters/equalizer.hpp"
      6 #include "dsp/filters/multimode.hpp"
      7 #include "dsp/filters/utility.hpp"
      8 #include "dsp/signal.hpp"
      9 
     10 using namespace bogaudio::dsp;
     11 
     12 namespace bogaudio {
     13 
     14 struct PEQChannel {
     15 	static const float maxDecibels;
     16 	static const float minDecibels;
     17 	static constexpr float maxFrequency = 20000.0f;
     18 	static constexpr float minFrequency = MultimodeFilter::minFrequency;
     19 	static const float maxFrequencySemitone;
     20 	static const float minFrequencySemitone;
     21 
     22 	float _sampleRate;
     23 	Amplifier _amplifier;
     24 	bogaudio::dsp::SlewLimiter _levelSL;
     25 	MultimodeFilter* _filter = NULL;
     26 	bogaudio::dsp::SlewLimiter _frequencySL;
     27 	bogaudio::dsp::SlewLimiter _bandwidthSL;
     28 	RootMeanSquare _rms;
     29 
     30 	int _c;
     31 	MultimodeFilter::Mode _mode;
     32 	bool _fullFrequencyMode = true;
     33 	int _poles;
     34 	Param& _levelParam;
     35 	Param& _frequencyParam;
     36 	Param& _frequencyCv1Param;
     37 	Param* _frequencyCv2Param;
     38 	Param& _bandwidthParam;
     39 	Input& _levelInput;
     40 	Input& _frequency1Input;
     41 	Input& _frequency2Input;
     42 	Input* _bandwidthInput;
     43 
     44 	float out = 0.0f;
     45 	float rms = 0.0f;
     46 	float frequency = 0.0f;
     47 	float bandwidth = 0.0f;
     48 
     49 	PEQChannel(
     50 		MultimodeFilter* filter,
     51 		int polyChannel,
     52 		Param& levelParam,
     53 		Param& frequencyParam,
     54 		Param& frequencyCv1Param,
     55 		Param* frequencyCv2Param,
     56 		Param& bandwidthParam,
     57 		Input& levelCvInput,
     58 		Input& frequencyCv1Input,
     59 		Input& frequencyCv2Input,
     60 		Input* bandwidthCvInput,
     61 		float sampleRate = 1000.0f
     62 	)
     63 	: _filter(filter)
     64 	, _c(polyChannel)
     65 	, _levelParam(levelParam)
     66 	, _frequencyParam(frequencyParam)
     67 	, _frequencyCv1Param(frequencyCv1Param)
     68 	, _frequencyCv2Param(frequencyCv2Param)
     69 	, _bandwidthParam(bandwidthParam)
     70 	, _levelInput(levelCvInput)
     71 	, _frequency1Input(frequencyCv1Input)
     72 	, _frequency2Input(frequencyCv2Input)
     73 	, _bandwidthInput(bandwidthCvInput)
     74 	{
     75 		setSampleRate(sampleRate);
     76 		setFilterMode(MultimodeFilter::BANDPASS_MODE);
     77 		_rms.setSensitivity(0.05f);
     78 	}
     79 	virtual ~PEQChannel() {
     80 		delete _filter;
     81 	}
     82 
     83 	void setSampleRate(float sampleRate);
     84 	void setFilterMode(MultimodeFilter::Mode mode);
     85 	inline void setFrequencyMode(bool full) { _fullFrequencyMode = full; }
     86 	void modulate();
     87 	void next(float sample); // outputs on members out, rms.
     88 };
     89 
     90 struct PEQEngine {
     91 	int _n;
     92 	PEQChannel** _channels;
     93 	Saturator _saturator;
     94 
     95 	float* outs = NULL;
     96 	float* frequencies = NULL;
     97 	float bandwidth = 0.0f;
     98 
     99 	PEQEngine(int channels) : _n(channels) {
    100 		_channels = new PEQChannel*[_n] {};
    101 		outs = new float[_n] {};
    102 		frequencies = new float[_n] {};
    103 	}
    104 	~PEQEngine() {
    105 		for (int i = 0; i < _n; ++i) {
    106 			delete _channels[i];
    107 		}
    108 		delete[] _channels;
    109 		delete[] outs;
    110 		delete[] frequencies;
    111 	}
    112 
    113 	inline void configChannel(
    114 		int i,
    115 		int polyChannel,
    116 		Param& levelParam,
    117 		Param& frequencyParam,
    118 		Param& frequencyCv1Param,
    119 		Param* frequencyCv2Param,
    120 		Param& bandwidthParam,
    121 		Input& levelCvInput,
    122 		Input& frequencyCv1Input,
    123 		Input& frequencyCv2Input,
    124 		Input* bandwidthCvInput
    125 	) {
    126 		_channels[i] = new PEQChannel(
    127 			i == 0 || i == _n - 1 ? (MultimodeFilter*)(new MultimodeFilter8()) : (MultimodeFilter*)(new MultimodeFilter4()),
    128 			polyChannel,
    129 			levelParam,
    130 			frequencyParam,
    131 			frequencyCv1Param,
    132 			frequencyCv2Param,
    133 			bandwidthParam,
    134 			levelCvInput,
    135 			frequencyCv1Input,
    136 			frequencyCv2Input,
    137 			bandwidthCvInput
    138 		);
    139 	}
    140 	inline void setLowFilterMode(MultimodeFilter::Mode mode) { _channels[0]->setFilterMode(mode); }
    141 	inline void setHighFilterMode(MultimodeFilter::Mode mode) { _channels[_n - 1]->setFilterMode(mode); }
    142 	void setFrequencyMode(bool full);
    143 	void setSampleRate(float sr);
    144 	void modulate();
    145 	float next(float sample, float* rmsSums);
    146 };
    147 
    148 struct PEQXFBase : FollowerBase {
    149 	float scaleEF(float ef, float frequency, float bandwidth);
    150 };
    151 
    152 struct BandExcludeModule : BGModule {
    153 	bool _bandExclude = false;
    154 
    155 	json_t* saveToJson(json_t* root) override;
    156 	void loadFromJson(json_t* root) override;
    157 };
    158 
    159 struct BandExcludeModuleWidget : BGModuleWidget {
    160 	void contextMenu(Menu* menu) override;
    161 };
    162 
    163 } // namespace bogaudio