BogaudioModules

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

LFO.hpp (3297B)


      1 #pragma once
      2 
      3 #include "bogaudio.hpp"
      4 #include "lfo_base.hpp"
      5 
      6 using namespace bogaudio::dsp;
      7 
      8 extern Model* modelLFO;
      9 
     10 namespace bogaudio {
     11 
     12 struct LFO : LFOBase {
     13 	enum ParamsIds {
     14 		FREQUENCY_PARAM,
     15 		SLOW_PARAM,
     16 		SAMPLE_PARAM,
     17 		PW_PARAM,
     18 		OFFSET_PARAM,
     19 		SCALE_PARAM,
     20 		SMOOTH_PARAM,
     21 		NUM_PARAMS
     22 	};
     23 
     24 	enum InputsIds {
     25 		SAMPLE_INPUT,
     26 		PW_INPUT,
     27 		OFFSET_INPUT,
     28 		SCALE_INPUT,
     29 		PITCH_INPUT,
     30 		RESET_INPUT,
     31 		NUM_INPUTS
     32 	};
     33 
     34 	enum OutputsIds {
     35 		RAMP_UP_OUTPUT,
     36 		RAMP_DOWN_OUTPUT,
     37 		SQUARE_OUTPUT,
     38 		TRIANGLE_OUTPUT,
     39 		SINE_OUTPUT,
     40 		STEPPED_OUTPUT,
     41 		NUM_OUTPUTS
     42 	};
     43 
     44 	struct Engine {
     45 		int sampleSteps = 1;
     46 		int sampleStep = 0;
     47 		float offset = 0.0f;
     48 		float scale = 0.0f;
     49 		PositiveZeroCrossing resetTrigger;
     50 
     51 		Phasor phasor;
     52 		SineTableOscillator sine;
     53 		TriangleOscillator triangle;
     54 		SawOscillator ramp;
     55 		SquareOscillator square;
     56 		SteppedRandomOscillator stepped;
     57 
     58 		float sineSample = 0.0f;
     59 		float triangleSample = 0.0f;
     60 		float rampUpSample = 0.0f;
     61 		float rampDownSample = 0.0f;
     62 		float squareSample = 0.0f;
     63 		float steppedSample = 0.0f;
     64 
     65 		bool sineActive = false;
     66 		bool triangleActive = false;
     67 		bool rampUpActive = false;
     68 		bool rampDownActive = false;
     69 		bool squareActive = false;
     70 		bool steppedActive = false;
     71 
     72 		Smoother sineSmoother;
     73 		Smoother triangleSmoother;
     74 		Smoother rampUpSmoother;
     75 		Smoother rampDownSmoother;
     76 		Smoother squareSmoother;
     77 		Smoother steppedSmoother;
     78 
     79 		void reset();
     80 		void sampleRateChange();
     81 	};
     82 
     83 	const float amplitude = 5.0f;
     84 	Engine* _engines[maxChannels] {};
     85 	bool _useOffsetCvForSmooth = false;
     86 
     87 	LFO() : LFOBase(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {
     88 		configParam<LFOFrequencyParamQuantity>(FREQUENCY_PARAM, -5.0f, 8.0f, 0.0f, "Frequency", " Hz");
     89 		configButton(SLOW_PARAM, "Slow");
     90 		configParam(SAMPLE_PARAM, 0.0f, 1.0f, 0.0f, "Output sampling", "%", 0.0f, 100.0f);
     91 		configParam(PW_PARAM, -1.0f, 1.0f, 0.0f, "Pulse width", "%", 0.0f, 100.0f*0.5f*(1.0f - 2.0f * SquareOscillator::minPulseWidth), 50.0f);
     92 		configParam(SMOOTH_PARAM, 0.0f, 1.0f, 0.0f, "Smoothing", "%", 0.0f, 100.0f);
     93 		configParam<OffsetParamQuantity>(OFFSET_PARAM, -1.0f, 1.0f, 0.0f, "Offset", " V", 0.0f, 5.0f);
     94 		configParam(SCALE_PARAM, 0.0f, 1.0f, 1.0f, "Scale", "%", 0.0f, 100.0f);
     95 
     96 		configInput(SAMPLE_INPUT, "Sample CV");
     97 		configInput(PW_INPUT, "Pulse width CV");
     98 		configInput(OFFSET_INPUT, "Offset CV");
     99 		configInput(SCALE_INPUT, "Scale CV");
    100 		configInput(PITCH_INPUT, "Pitch (1V/octave)");
    101 		configInput(RESET_INPUT, "Reset");
    102 
    103 		configOutput(RAMP_UP_OUTPUT, "Ramp up");
    104 		configOutput(RAMP_DOWN_OUTPUT, "Ramp down");
    105 		configOutput(SQUARE_OUTPUT, "Square");
    106 		configOutput(TRIANGLE_OUTPUT, "Triangle");
    107 		configOutput(SINE_OUTPUT, "Sine");
    108 		configOutput(STEPPED_OUTPUT, "Stepped");
    109 	}
    110 
    111 	void reset() override;
    112 	void sampleRateChange() override;
    113 	json_t* saveToJson(json_t* root) override;
    114 	void loadFromJson(json_t* root) override;
    115 	bool active() override;
    116 	int channels() override;
    117 	void addChannel(int c) override;
    118 	void removeChannel(int c) override;
    119 	void modulate() override;
    120 	void modulateChannel(int c) override;
    121 	void processChannel(const ProcessArgs& args, int c) override;
    122 	void updateOutput(int c, Phasor& wave, bool useSample, bool invert, Output& output, float& sample, bool& active, Smoother& smoother);
    123 };
    124 
    125 } // namespace bogaudio