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
#pragma once
#include <cmath>
 
/**
 * Envelope Detector Class
 */
template <typename SampleType>
class EnvelopeDetector
{
public:
 
    /**
     * @brief Constructor
     * 
     */
    EnvelopeDetector();
    
    /**
     * @brief Prepares object for processing
     * 
     * @param sampleRate Current sampling rate
     */
    void prepare(SampleType sampleRate);
 
    /**
     * @brief Processes a single sample
     * 
     * @param input Input sample
     */
    SampleType processSample(SampleType input);
 
    /**
     * @brief Sets the attack time of the Envelope Detector object
     * 
     * @param newValue New attack time in milliseconds
     */
    void setAttackTime(SampleType newValue);
 
    /**
     * @brief Returns the attack time parameter value of the Envelope Detector object
     */
    SampleType getAttackTime();
 
    /**
     * @brief Sets the release time of the Envelope Detector object
     * 
     * @param newValue New Release time in milliseconds
     */
    void setReleaseTime(SampleType newValue);
 
    /**
     * @brief Returns the release time parameter value of the Envelope Detector object
     * 
     * @return SampleType 
     */
    SampleType getReleaseTime();
    
    /**
     * @brief Sets the detection mode of the Envelope Detector object
     * 
     * @param index Detection mode index
     */
    void setDetectionMode(int index);
 
    /**
     * @brief Returns the detection mode index of the Envelope Detector object
     */
    int getDetectionMode();
 
    /**
     * @brief Sets the envelope detector in dB or linear output mode
     * 
     * @param outputIndB Should output be in dB boolean. 
     * True = db Output, False = Linear Output
     */
    void setOutputIndB(bool outputIndB);
 
    /**
     * @brief Returns Envelope Detector detection mode
     * 
     * @return true dB output
     * @return false Linear output
     */
    bool isOutputIndB();
 
    enum DetectionModes
    {
        Peak = 0,
        MS,        //Mean Square
        RMS        //Root Mean Square
    };
 
 
private:    
 
    SampleType sampleRate{ 44100.0 };
 
    //User Parameters
    SampleType attackTime{ 0.0 };   //In ms
    SampleType releaseTime{ 0.0 };  //In ms
    int detectionMode{DetectionModes::Peak};
 
 
    SampleType attackTimeCoeff{ 0.0 };
    SampleType releaseTimeCoeff{ 0.0 };
    bool output_In_dB{false};
 
    const double rc_atc = -0.99967234081320612357829304641019; //RC filter analog time constant
 
    SampleType previousValue{0.0}; //Envelope value for the previous sample
 
    void setSampleRate(SampleType sampleRate);
};