BogaudioModules

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

Additator.hpp (4469B)


      1 #pragma once
      2 
      3 #include "bogaudio.hpp"
      4 #include "dsp/oscillator.hpp"
      5 #include "dsp/pitch.hpp"
      6 #include "dsp/signal.hpp"
      7 
      8 using namespace bogaudio::dsp;
      9 
     10 extern Model* modelAdditator;
     11 
     12 namespace bogaudio {
     13 
     14 struct Additator : BGModule {
     15 	enum ParamsIds {
     16 		FREQUENCY_PARAM,
     17 		PARTIALS_PARAM,
     18 		WIDTH_PARAM,
     19 		ODD_SKEW_PARAM,
     20 		EVEN_SKEW_PARAM,
     21 		GAIN_PARAM,
     22 		DECAY_PARAM,
     23 		BALANCE_PARAM,
     24 		FILTER_PARAM,
     25 		PHASE_PARAM,
     26 		FINE_PARAM,
     27 		NUM_PARAMS
     28 	};
     29 
     30 	enum InputsIds {
     31 		PITCH_INPUT,
     32 		SYNC_INPUT,
     33 		PARTIALS_INPUT,
     34 		WIDTH_INPUT,
     35 		ODD_SKEW_INPUT,
     36 		EVEN_SKEW_INPUT,
     37 		GAIN_INPUT,
     38 		DECAY_INPUT,
     39 		BALANCE_INPUT,
     40 		FILTER_INPUT,
     41 		NUM_INPUTS
     42 	};
     43 
     44 	enum OutputsIds {
     45 		AUDIO_OUTPUT,
     46 		NUM_OUTPUTS
     47 	};
     48 
     49 	enum LightsIds {
     50 		SINE_LIGHT,
     51 		COSINE_LIGHT,
     52 		NUM_LIGHTS
     53 	};
     54 
     55 	enum Phase {
     56 		PHASE_RESET,
     57 		PHASE_SINE,
     58 		PHASE_COSINE
     59 	};
     60 
     61 	static constexpr int maxPartials = 100;
     62 	static constexpr float maxWidth = 2.0f;
     63 	static constexpr float maxSkew = 0.99f;
     64 	static constexpr float minAmplitudeNormalization = 1.0f;
     65 	static constexpr float maxAmplitudeNormalization = 5.0f;
     66 	static constexpr float minDecay = -1.0f;
     67 	static constexpr float maxDecay = 3.0f;
     68 	static constexpr float minFilter = 0.1;
     69 	static constexpr float maxFilter = 1.9;
     70 	static constexpr float slewLimitTime = 1.0f;
     71 
     72 	struct Engine {
     73 		int partials = 0;
     74 		float width = 0.0f;
     75 		float oddSkew = 0.0f;
     76 		float evenSkew = 0.0f;
     77 		float amplitudeNormalization = 0.0f;
     78 		float decay = 0.0f;
     79 		float balance = 0.0f;
     80 		float filter = 0.0f;
     81 		Phase phase = PHASE_RESET;
     82 		float maxFrequency = 0.0f;
     83 		int activePartials = 1;
     84 		SineBankOscillator oscillator;
     85 		PositiveZeroCrossing syncTrigger;
     86 		bogaudio::dsp::SlewLimiter widthSL;
     87 		bogaudio::dsp::SlewLimiter oddSkewSL;
     88 		bogaudio::dsp::SlewLimiter evenSkewSL;
     89 		bogaudio::dsp::SlewLimiter amplitudeNormalizationSL;
     90 		bogaudio::dsp::SlewLimiter decaySL;
     91 		bogaudio::dsp::SlewLimiter balanceSL;
     92 		bogaudio::dsp::SlewLimiter filterSL;
     93 
     94 		Engine() : oscillator(1000.0f, 100.0f, maxPartials) {}
     95 
     96 		void reset();
     97 		void sampleRateChange();
     98 	};
     99 
    100 	Engine* _engines[maxChannels] {};
    101 
    102 	Additator()	{
    103 		config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
    104 		configParam<FrequencyParamQuantity>(FREQUENCY_PARAM, -3.0f, 6.0f, 0.0f, "Frequency", " Hz");
    105 		configParam(PARTIALS_PARAM, 1.0f, Additator::maxPartials, Additator::maxPartials / 5.0f, "Partials");
    106 		paramQuantities[PARTIALS_PARAM]->snapEnabled = true;
    107 		configParam(FINE_PARAM, -1.0f, 1.0f, 0.0f, "Fine tune", " cents", 0.0f, 100.0f);
    108 		configParam(WIDTH_PARAM, 0.0f, maxWidth, maxWidth / 2.0f, "Width", "%", 0.0f, 2.0f * (1.0f / maxWidth) * 100.0f, -100.0f);
    109 		configParam(ODD_SKEW_PARAM, -maxSkew, maxSkew, 0.0f, "Odd skew", "%", 0.0f, (1.0f / maxSkew) * 100.0f);
    110 		configParam(EVEN_SKEW_PARAM, -maxSkew, maxSkew, 0.0f, "Even skew", "%", 0.0f, (1.0f / maxSkew) * 100.0f);
    111 		configParam(GAIN_PARAM, minAmplitudeNormalization, maxAmplitudeNormalization, (maxAmplitudeNormalization - minAmplitudeNormalization) / 2.0 + minAmplitudeNormalization, "Gain");
    112 		configParam(DECAY_PARAM, minDecay, maxDecay, (maxDecay - minDecay) / 2.0 + minDecay, "Decay");
    113 		configParam(BALANCE_PARAM, -1.0f, 1.0f, 0.0f, "Balance", "%", 0.0f, 100.0f);
    114 		configParam(FILTER_PARAM, minFilter, maxFilter, (maxFilter - minFilter) / 2.0 + minFilter, "Filter");
    115 		configSwitch(PHASE_PARAM, 1.0f, 2.0f, 1.0f, "Phase", {"Sine", "Cosine"});
    116 
    117 		configInput(PITCH_INPUT, "Pitch (1V/octave)");
    118 		configInput(SYNC_INPUT, "Sync");
    119 		configInput(PARTIALS_INPUT, "Partials CV");
    120 		configInput(WIDTH_INPUT, "Width CV");
    121 		configInput(ODD_SKEW_INPUT, "Odd skew CV");
    122 		configInput(EVEN_SKEW_INPUT, "Even skew CV");
    123 		configInput(GAIN_INPUT, "Gain CV");
    124 		configInput(DECAY_INPUT, "Decay CV");
    125 		configInput(BALANCE_INPUT, "Balance CV");
    126 		configInput(FILTER_INPUT, "Filter CV");
    127 
    128 		configOutput(AUDIO_OUTPUT, "Signal");
    129 	}
    130 
    131 	void reset() override;
    132 	void sampleRateChange() override;
    133 	bool active() override;
    134 	int channels() override;
    135 	void addChannel(int c) override;
    136 	void removeChannel(int c) override;
    137 	float widthParam(int c);
    138 	float oddSkewParam(int c);
    139 	float evenSkewParam(int c);
    140 	float amplitudeNormalizationParam(int c);
    141 	float decayParam(int c);
    142 	float balanceParam(int c);
    143 	float filterParam(int c);
    144 	void modulateChannel(int c) override;
    145 	void processAlways(const ProcessArgs& args) override;
    146 	void processChannel(const ProcessArgs& args, int c) override;
    147 	float cvValue(int c, Input& cv, bool dc = false);
    148 };
    149 
    150 } // namespace bogaudio