chenlh
2026-03-20 f761b6198e2e98f02adb84c83dbf59529e51b2ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "EnvelopeDetector.h"
 
template <typename SampleType>
EnvelopeDetector<SampleType>::EnvelopeDetector()
{
}
 
template <typename SampleType>
void EnvelopeDetector<SampleType>::prepare(SampleType sampleRate)
{
    setSampleRate(sampleRate);
    previousValue = 0.0;
}
 
template <typename SampleType>
SampleType EnvelopeDetector<SampleType>::processSample(SampleType input)
{
    SampleType xn = std::fabs(input);
    SampleType currentValue = 0.0; //Envelope value for the current sample
 
    if(detectionMode == DetectionModes::MS || detectionMode == DetectionModes::RMS)
        xn = xn*xn;
 
    if(xn > previousValue)
        currentValue = attackTimeCoeff * (previousValue - xn) + xn;
    else
        currentValue = releaseTimeCoeff * (previousValue  - xn) + xn;
 
    if (currentValue > 0.0 && currentValue < 1.175494351e-38)
    {
        currentValue = 0;
        
    }
    else if (currentValue < 0.0 && currentValue > -1.175494351e-38)
    {
        currentValue = 0;
        
    }
 
    currentValue = std::fmax(currentValue, 0.0);
    previousValue = currentValue;
 
    //Check if RMS Mode is selected
    if(getDetectionMode() == DetectionModes::RMS)
        currentValue = std::pow(currentValue, 0.5);
 
    //Check if output is linear
    if(!output_In_dB)
        return currentValue;
 
    if (currentValue <= 0)
        return -96.0;
 
    //If output is in dB...
    return 20*(std::log10(currentValue));
}
 
template <typename SampleType>
void EnvelopeDetector<SampleType>::setAttackTime(SampleType newValue)
{
    attackTimeCoeff = std::exp(rc_atc / (newValue * sampleRate * 0.001));
}
 
template <typename SampleType>
SampleType EnvelopeDetector<SampleType>::getAttackTime()
{
    return attackTime;
}
 
template <typename SampleType>
void EnvelopeDetector<SampleType>::setReleaseTime(SampleType newValue)
{
    releaseTimeCoeff = std::exp(rc_atc / (newValue * sampleRate * 0.001));
}
 
template <typename SampleType>
SampleType EnvelopeDetector<SampleType>::getReleaseTime()
{
    return releaseTimeCoeff;
}
 
template <typename SampleType>
void EnvelopeDetector<SampleType>::setDetectionMode(int index)
{
    switch(index)
    {
        case DetectionModes::Peak:
            detectionMode = DetectionModes::Peak;
            break;
        case DetectionModes::MS:
            detectionMode = DetectionModes::MS;
            break;
        case DetectionModes::RMS:
            detectionMode = DetectionModes::RMS;
            break;
        default:
            detectionMode = DetectionModes::Peak;
    }
}
 
template <typename SampleType>
int EnvelopeDetector<SampleType>::getDetectionMode()
{
    return detectionMode;
}
 
template <typename SampleType>
void EnvelopeDetector<SampleType>::setOutputIndB(bool outputIndB)
{
    output_In_dB = outputIndB;
}
 
template <typename SampleType>
bool EnvelopeDetector<SampleType>::isOutputIndB()
{
    return output_In_dB;
}
 
template <typename SampleType>
void EnvelopeDetector<SampleType>::setSampleRate(SampleType sampleRate)
{
    this->sampleRate = sampleRate;
    setAttackTime(attackTime);
    setReleaseTime(releaseTime);
}
 
template class EnvelopeDetector<float>;
template class EnvelopeDetector<double>;