Deluge Firmware 1.3.0
Build date: 2025.09.14
Loading...
Searching...
No Matches
absolute_value.h
1/*
2 * Copyright © 2024 Mark Adams
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "definitions_cxx.hpp"
21#include "dsp_ng/core/types.hpp"
22#include <cmath>
23#include <span>
24
25namespace deluge::dsp {
26class AbsValueFollower {
27public:
28 AbsValueFollower() = default;
29 void setup(q31_t attack, q31_t release);
30
32 void reset() {
33 meanL = 0;
34 lastMeanL = 0;
35 meanR = 0;
36 lastMeanR = 0;
37 }
38
39 // attack/release in range 0 to 2^31
40 inline q31_t getAttack() { return attackKnobPos; }
41 inline int32_t getAttackMS() { return attackMS; }
42 int32_t setAttack(q31_t attack) {
43 // this exp will be between 1 and 7ish, half the knob range is about 2.5
44 attackMS = 0.5 + (std::exp(2 * float(attack) / ONE_Q31f) - 1) * 10;
45 a_ = (-1000.0 / kSampleRate) / attackMS;
46 attackKnobPos = attack;
47 return attackMS;
48 };
49
50 inline q31_t getRelease() { return releaseKnobPos; }
51 inline int32_t getReleaseMS() { return releaseMS; }
52 int32_t setRelease(q31_t release) {
53 // this exp will be between 1 and 7ish, half the knob range is about 2.5
54 releaseMS = 50 + (std::exp(2 * float(release) / ONE_Q31f) - 1) * 50;
55 r_ = (-1000.0 / kSampleRate) / releaseMS;
56 releaseKnobPos = release;
57 return releaseMS;
58 };
59
60 StereoSample<float> calcApproxRMS(StereoBuffer<q31_t> buffer);
61
62private:
63 float runEnvelope(float current, float desired, float numSamples);
64 // for display
65 float attackMS{1};
66 float releaseMS{10};
67 // parameters in use
68 float a_ = (-1000.0f / kSampleRate) / attackMS;
69
70 float r_ = (-1000.0f / kSampleRate) / releaseMS;
71
72 // state
73 float state{0};
74 float rms{0};
75 float meanL{0};
76 float lastMeanL{0};
77 float meanR{0};
78 float lastMeanR{0};
79
80 // raw knob positions
81 q31_t attackKnobPos{0};
82 q31_t releaseKnobPos{0};
83};
84} // namespace deluge::dsp
void reset()
Reset the state of the envelope follower.
Definition absolute_value.h:32