BogaudioModules

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

vco_base.hpp (2559B)


      1 #pragma once
      2 
      3 #include "bogaudio.hpp"
      4 #include "dsp/filters/resample.hpp"
      5 #include "dsp/oscillator.hpp"
      6 #include "dsp/signal.hpp"
      7 #include <math.h>
      8 
      9 using namespace bogaudio::dsp;
     10 
     11 namespace bogaudio {
     12 
     13 struct VCOBase : BGModule {
     14 	struct Engine {
     15 		static constexpr int oversample = 8;
     16 
     17 		float frequency = INFINITY;
     18 		float baseVOct = 0.0f;
     19 		float baseHz = 0.0f;
     20 
     21 		Phasor phasor;
     22 		BandLimitedSquareOscillator square;
     23 		BandLimitedSawOscillator saw;
     24 		TriangleOscillator triangle;
     25 		SineTableOscillator sine;
     26 		CICDecimator squareDecimator;
     27 		CICDecimator sawDecimator;
     28 		CICDecimator triangleDecimator;
     29 		float squareBuffer[oversample];
     30 		float sawBuffer[oversample];
     31 		float triangleBuffer[oversample];
     32 		PositiveZeroCrossing syncTrigger;
     33 		bogaudio::dsp::SlewLimiter squarePulseWidthSL;
     34 		bool squareActive = false;
     35 		bool sawActive = false;
     36 		bool triangleActive = false;
     37 		bool sineActive = false;
     38 		float squareOut = 0.0f;
     39 		float sawOut = 0.0f;
     40 		float triangleOut = 0.0f;
     41 		float sineOut = 0.0f;
     42 		Phasor::phase_delta_t additionalPhaseOffset = 0;
     43 
     44 		Engine() {
     45 			saw.setQuality(12);
     46 			square.setQuality(12);
     47 		}
     48 		void reset();
     49 		void sampleRateChange(float sampleRate);
     50 		void setFrequency(float frequency);
     51 	};
     52 
     53 	const float amplitude = 5.0f;
     54 	const float slowModeOffset = -7.0f;
     55 	Engine* _engines[maxChannels] {};
     56 	float _oversampleThreshold = 0.0f;
     57 	bool _slowMode = false;
     58 	bool _linearMode = false;
     59 	float _fmDepth = 0.0f;
     60 	bool _fmLinearMode = false;
     61 	int _frequencyParamID;
     62 	int _fineFrequencyParamID;
     63 	int _pitchInputID;
     64 	int _syncInputID;
     65 	int _fmInputID;
     66 	int _polyInputID;
     67 	bool _dcCorrection = true;
     68 
     69 	struct VCOFrequencyParamQuantity : FrequencyParamQuantity {
     70 		float offset() override;
     71 		float getDisplayValue() override;
     72 		void setDisplayValue(float v) override;
     73 	};
     74 
     75 	VCOBase(int fpID, int ffpID, int piID, int siID, int fiID)
     76 	: _frequencyParamID(fpID)
     77 	, _fineFrequencyParamID(ffpID)
     78 	, _pitchInputID(piID)
     79 	, _syncInputID(siID)
     80 	, _fmInputID(fiID)
     81 	, _polyInputID(piID)
     82 	{}
     83 
     84 	inline float linearModeVoltsToHertz(float v) { return _slowMode ? v : 1000.0f * v; }
     85 	void reset() override;
     86 	void sampleRateChange() override;
     87 	json_t* saveToJson(json_t* root) override;
     88 	void loadFromJson(json_t* root) override;
     89 	int channels() override;
     90 	void addChannel(int c) override;
     91 	void removeChannel(int c) override;
     92 	void modulateChannel(int c) override;
     93 	void processChannel(const ProcessArgs& args, int c) override;
     94 };
     95 
     96 struct VCOBaseModuleWidget : BGModuleWidget {
     97 	void contextMenu(Menu* menu) override;
     98 };
     99 
    100 } // namespace bogaudio