BogaudioModules

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

Chirp.hpp (2810B)


      1 #pragma once
      2 
      3 #include "bogaudio.hpp"
      4 #include "dsp/oscillator.hpp"
      5 #include "dsp/signal.hpp"
      6 
      7 extern Model* modelChirp;
      8 
      9 using namespace bogaudio::dsp;
     10 
     11 namespace bogaudio {
     12 
     13 struct Chirp : BGModule {
     14 	enum ParamsIds {
     15 		TIME_PARAM,
     16 		FREQUENCY1_PARAM,
     17 		FREQUENCY2_PARAM,
     18 		TRIGGER_PARAM,
     19 		EXPONENTIAL_PARAM,
     20 		LOOP_PARAM,
     21 		NUM_PARAMS
     22 	};
     23 
     24 	enum InputsIds {
     25 		FREQUENCY1_INPUT,
     26 		FREQUENCY2_INPUT,
     27 		TIME_INPUT,
     28 		TRIGGER_INPUT,
     29 		NUM_INPUTS
     30 	};
     31 
     32 	enum OutputsIds {
     33 		EOC_OUTPUT,
     34 		OUT_OUTPUT,
     35 		NUM_OUTPUTS
     36 	};
     37 
     38 	static constexpr float minTimeSeconds = 0.05f;
     39 	static constexpr float maxTimeSeconds = 10.0f;
     40 	static constexpr float minFrequency = 1.0f;
     41 	static constexpr float maxFrequencyNyquistRatio = 0.49f;
     42 	static constexpr float cycleSlewSeconds = 0.01f;
     43 
     44 	struct TimeParamQuantity : ParamQuantity {
     45 		float getDisplayValue() override {
     46 			float v = getValue();
     47 			if (!module) {
     48 				return v;
     49 			}
     50 
     51 			float vv = v * v;
     52 			vv *= Chirp::maxTimeSeconds - Chirp::minTimeSeconds;
     53 			vv += Chirp::minTimeSeconds;
     54 			return vv;
     55 		}
     56 
     57 		void setDisplayValue(float displayValue) override {
     58 			if (!module) {
     59 				return;
     60 			}
     61 			displayValue -= Chirp::minTimeSeconds;
     62 			displayValue = std::max(0.0f, displayValue);
     63 			float v = displayValue / (Chirp::maxTimeSeconds - Chirp::minTimeSeconds);
     64 			v = powf(v, 0.5f);
     65 			setValue(v);
     66 		}
     67 	};
     68 
     69 	struct Engine {
     70 		ChirpOscillator chirp;
     71 		Trigger trigger;
     72 		rack::dsp::PulseGenerator eocPulseGen;
     73 		Amplifier amp;
     74 		bogaudio::dsp::SlewLimiter ampSL;
     75 		float targetAmplitude = 0.0f;
     76 
     77 		void reset();
     78 		void sampleRateChange(float sr);
     79 	};
     80 
     81 	Engine* _engines[maxChannels] {};
     82 	float _sampleTime;
     83 	bool _run = false;
     84 	bool _exponential = false;
     85 	bool _loop = false;
     86 
     87 	Chirp() {
     88 		config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS);
     89 		configParam<TimeParamQuantity>(TIME_PARAM, 0.0f, 1.0f, 0.30899415, "Time", "s");
     90 		configParam<FrequencyParamQuantity>(FREQUENCY1_PARAM, -3.0f, 6.0f, -3.0f, "Frequency 1", " Hz");
     91 		configParam<FrequencyParamQuantity>(FREQUENCY2_PARAM, -3.0f, 6.0f, 6.0f, "Frequency 2", " Hz");
     92 		configButton(TRIGGER_PARAM, "Trigger");
     93 		configButton(EXPONENTIAL_PARAM, "Exponential");
     94 		configButton(LOOP_PARAM, "Loop");
     95 
     96 		configInput(FREQUENCY1_INPUT, "Frequency 1 (1V/octave)");
     97 		configInput(FREQUENCY2_INPUT, "Frequency 2 (1V/octave)");
     98 		configInput(TIME_INPUT, "Time CV");
     99 		configInput(TRIGGER_INPUT, "Trigger");
    100 
    101 		configOutput(EOC_OUTPUT, "End-of-cycle trigger");
    102 		configOutput(OUT_OUTPUT, "Signal");
    103 	}
    104 
    105 	void reset() override;
    106 	void sampleRateChange() override;
    107 	bool active() override;
    108 	int channels() override;
    109 	void addChannel(int c) override;
    110 	void removeChannel(int c) override;
    111 	void modulate() override;
    112 	void modulateChannel(int c) override;
    113 	void processChannel(const ProcessArgs& args, int c) override;
    114 };
    115 
    116 } // namespace bogaudio