Deluge Firmware 1.3.0
Build date: 2025.04.16
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 "dsp/stereo_sample.h"
21#include <cmath>
22#include <span>
23
24class AbsValueFollower {
25public:
26 AbsValueFollower() = default;
27 void setup(q31_t attack, q31_t release);
28
30 void reset() {
31 meanL = 0;
32 lastMeanL = 0;
33 meanR = 0;
34 lastMeanR = 0;
35 }
36
37 // attack/release in range 0 to 2^31
38 inline q31_t getAttack() { return attackKnobPos; }
39 inline int32_t getAttackMS() { return attackMS; }
40 int32_t setAttack(q31_t attack) {
41 // this exp will be between 1 and 7ish, half the knob range is about 2.5
42 attackMS = 0.5 + (std::exp(2 * float(attack) / ONE_Q31f) - 1) * 10;
43 a_ = (-1000.0 / kSampleRate) / attackMS;
44 attackKnobPos = attack;
45 return attackMS;
46 };
47
48 inline q31_t getRelease() { return releaseKnobPos; }
49 inline int32_t getReleaseMS() { return releaseMS; }
50 int32_t setRelease(q31_t release) {
51 // this exp will be between 1 and 7ish, half the knob range is about 2.5
52 releaseMS = 50 + (std::exp(2 * float(release) / ONE_Q31f) - 1) * 50;
53 r_ = (-1000.0 / kSampleRate) / releaseMS;
54 releaseKnobPos = release;
55 return releaseMS;
56 };
57
58 StereoFloatSample calcApproxRMS(std::span<StereoSample> buffer);
59
60private:
61 float runEnvelope(float current, float desired, float numSamples);
62 // for display
63 float attackMS{1};
64 float releaseMS{10};
65 // parameters in use
66 float a_ = (-1000.0f / kSampleRate) / attackMS;
67
68 float r_ = (-1000.0f / kSampleRate) / releaseMS;
69
70 // state
71 float state{0};
72 float rms{0};
73 float meanL{0};
74 float lastMeanL{0};
75 float meanR{0};
76 float lastMeanR{0};
77
78 // raw knob positions
79 q31_t attackKnobPos{0};
80 q31_t releaseKnobPos{0};
81};
void reset()
Reset the state of the envelope follower.
Definition absolute_value.h:30