BogaudioModules

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

Ranalyzer.hpp (4049B)


      1 #pragma once
      2 
      3 #include "bogaudio.hpp"
      4 #include "analyzer_base.hpp"
      5 #include "dsp/oscillator.hpp"
      6 #include "dsp/signal.hpp"
      7 
      8 extern Model* modelRanalyzer;
      9 
     10 using namespace bogaudio::dsp;
     11 
     12 namespace bogaudio {
     13 
     14 struct ChannelDisplayListener {
     15 	virtual void displayChannels(bool c0, bool c1, bool c2) = 0;
     16 };
     17 
     18 struct Ranalyzer : AnalyzerBase {
     19 	enum ParamsIds {
     20 		FREQUENCY1_PARAM,
     21 		FREQUENCY2_PARAM,
     22 		TRIGGER_PARAM,
     23 		EXPONENTIAL_PARAM,
     24 		LOOP_PARAM,
     25 		DELAY_PARAM,
     26 		NUM_PARAMS
     27 	};
     28 
     29 	enum InputsIds {
     30 		TRIGGER_INPUT,
     31 		RETURN_INPUT,
     32 		TEST_INPUT,
     33 		NUM_INPUTS
     34 	};
     35 
     36 	enum OutputsIds {
     37 		TRIGGER_OUTPUT,
     38 		EOC_OUTPUT,
     39 		SEND_OUTPUT,
     40 		NUM_OUTPUTS
     41 	};
     42 
     43 	enum Traces {
     44 		ALL_TRACES,
     45 		TEST_RETURN_TRACES,
     46 		ANALYSIS_TRACES
     47 	};
     48 
     49 	enum WindowType {
     50 		NONE_WINDOW_TYPE,
     51 		TAPER_WINDOW_TYPE,
     52 		HAMMING_WINDOW_TYPE,
     53 		KAISER_WINDOW_TYPE
     54 	};
     55 
     56 	static constexpr float minFrequency = 1.0f;
     57 	static constexpr float maxFrequencyNyquistRatio = 0.49f;
     58 	static constexpr int maxResponseDelay = 66;
     59 	static constexpr float initialDelaySeconds = 0.01f;
     60 
     61 	struct FrequencyParamQuantity : ParamQuantity {
     62 		float getDisplayValue() override {
     63 			float v = getValue();
     64 			if (!module) {
     65 				return v;
     66 			}
     67 
     68 			float vv = v * v;
     69 			vv *= roundf(APP->engine->getSampleRate() * Ranalyzer::maxFrequencyNyquistRatio) - Ranalyzer::minFrequency;
     70 			vv += Ranalyzer::minFrequency;
     71 			return vv;
     72 		}
     73 
     74 		void setDisplayValue(float displayValue) override {
     75 			if (!module) {
     76 				return;
     77 			}
     78 			displayValue -= Ranalyzer::minFrequency;
     79 			displayValue = std::max(0.0f, displayValue);
     80 			float v = displayValue / (roundf(APP->engine->getSampleRate() * Ranalyzer::maxFrequencyNyquistRatio) - Ranalyzer::minFrequency);
     81 			v = powf(v, 0.5f);
     82 			setValue(v);
     83 		}
     84 	};
     85 
     86 	PureChirpOscillator _chirp;
     87 	Trigger _trigger;
     88 	rack::dsp::PulseGenerator _triggerPulseGen;
     89 	rack::dsp::PulseGenerator _eocPulseGen;
     90 	float _sampleRate = 0.0f;
     91 	float _sampleTime = 0.0f;
     92 	float _maxFrequency = 0.0f;
     93 	bool _exponential = true;
     94 	bool _loop = false;
     95 	float _frequency1 = 0.0f;
     96 	float _frequency2 = 0.0f;
     97 	bool _run = false;
     98 	bool _flush = false;
     99 	int _returnSampleDelay = 2;
    100 	int _currentReturnSampleDelay = 0;
    101 	int _outBufferCount = 0;
    102 	int _analysisBufferCount = 0;
    103 	HistoryBuffer<float> _inputBuffer;
    104 	int _cycleI = 0;
    105 	int _cycleN = 0;
    106 	bool _useTestInput = false;
    107 	Traces _displayTraces = ALL_TRACES;
    108 	ChannelDisplayListener* _channelDisplayListener = NULL;
    109 	bool _triggerOnLoad = true;
    110 	Timer* _initialDelay = NULL;
    111 	WindowType _windowType = TAPER_WINDOW_TYPE;
    112 	bogaudio::dsp::Window* _window = NULL;
    113 
    114 	Ranalyzer()
    115 	: AnalyzerBase(3, NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, 0, SpectrumAnalyzer::OVERLAP_1)
    116 	, _inputBuffer(maxResponseDelay, 0.0f)
    117 	{
    118 		configParam<FrequencyParamQuantity>(FREQUENCY1_PARAM, 0.0f, 1.0f, 0.0f, "Frequency 1", " Hz");
    119 		configParam<FrequencyParamQuantity>(FREQUENCY2_PARAM, 0.0f, 1.0f, 1.0f, "Frequency 2", " Hz");
    120 		configButton(TRIGGER_PARAM, "Trigger");
    121 		configSwitch(EXPONENTIAL_PARAM, 0.0f, 1.0f, 1.0f, "Sweep", {"Linear", "Exponential"});
    122 		configSwitch(LOOP_PARAM, 0.0f, 1.0f, 0.0f, "Loop", {"Disabled", "Enabled"});
    123 		configParam(DELAY_PARAM, 2.0f, (float)maxResponseDelay, 2.0f, "Return sample delay");
    124 		paramQuantities[DELAY_PARAM]->snapEnabled = true;
    125 
    126 		configInput(TRIGGER_INPUT, "Trigger");
    127 		configInput(RETURN_INPUT, "Return signal");
    128 		configInput(TEST_INPUT, "Test signal");
    129 
    130 		configOutput(TRIGGER_OUTPUT, "Trigger");
    131 		configOutput(EOC_OUTPUT, "End-of-cycle trigger");
    132 		configOutput(SEND_OUTPUT, "Send signal");
    133 
    134 		_skinnable = false;
    135 	}
    136 	virtual ~Ranalyzer() {
    137 		if (_initialDelay) {
    138 			delete _initialDelay;
    139 		}
    140 		if (_window) {
    141 			delete _window;
    142 		}
    143 	}
    144 
    145 	void reset() override;
    146 	void sampleRateChange() override;
    147 	json_t* saveToJson(json_t* root) override;
    148 	void loadFromJson(json_t* root) override;
    149 	void modulate() override;
    150 	void processAll(const ProcessArgs& args) override;
    151 	void setDisplayTraces(Traces traces);
    152 	void setChannelDisplayListener(ChannelDisplayListener* listener);
    153 	void setWindow(WindowType wt);
    154 };
    155 
    156 } // namespace bogaudio