Deluge Firmware
1.3.0
Build date: 2026.11.18
Toggle main menu visibility
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
24
class
AbsValueFollower {
25
public
:
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);
61
StereoFloatSample
calcApproxRMSFromMeans
(
float
meanL,
float
meanR,
size_t
numSamples);
62
63
private
:
64
float
runEnvelope(
float
current,
float
desired,
float
numSamples);
65
// for display
66
float
attackMS{1};
67
float
releaseMS{10};
68
// parameters in use
69
float
a_ = (-1000.0f / kSampleRate) / attackMS;
70
71
float
r_ = (-1000.0f / kSampleRate) / releaseMS;
72
73
// state
74
float
state{0};
75
float
rms{0};
76
float
meanL{0};
77
float
lastMeanL{0};
78
float
meanR{0};
79
float
lastMeanR{0};
80
81
// raw knob positions
82
q31_t attackKnobPos{0};
83
q31_t releaseKnobPos{0};
84
};
AbsValueFollower::calcApproxRMSFromMeans
StereoFloatSample calcApproxRMSFromMeans(float meanL, float meanR, size_t numSamples)
Definition
absolute_value.cpp:59
AbsValueFollower::reset
void reset()
Reset the state of the envelope follower.
Definition
absolute_value.h:30