chenlh
2026-03-26 36e42207da4c088b5bfd96f2cfc8944f890440d7
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once
 
#include <DynamicsProcessor.h>
#include <GainUtilities.h>
 
/**
 * Basic Compressor Effect
 */
template <typename SampleType>
class Compressor : public DynamicsProcessor<SampleType>
{
public:
 
    /**
     * @brief Constructor
     */
    Compressor();
 
    /**
     * @brief Destructor
     */
    ~Compressor();
 
    /**
     * @brief Prepares object for playback
     * 
     * @param sampleRate Current sampling rate
     */
    void prepare(SampleType sampleRate) override;
 
    /**
     * @brief Processes a single sample
     * 
     * @param input Input sample
     */
    SampleType processSample(SampleType input) override;
 
    /**
     * @brief Processes a memory block that holds audio samples
     * 
     * @param data Memory block start pointer 
     * @param startSample Sample index to start processing from
     * @param endSample Number of samples to process
     */
    void process(SampleType* data, int startSample, int endSample) override;
 
    /**
     * @brief Sets the compressor threshold
     * 
     * @param newThreshold New threshold value in decibels
     */
    void setThreshold(SampleType newThreshold) override;
 
    /**
     * @brief Sets the compression ratio
     * 
     * @param newRatio New ratio value
     */
    void setRatio(SampleType newRatio);
 
    /**
     * @brief Sets the compressor attack time
     * 
     * @param newAttack New attack time value in milliseconds
     */
    void setAttack(SampleType newAttack) override;
 
    /**
     * @brief Sets the compressor release time
     * 
     * @param newRelease New release time value in milliseconds
     */
    void setRelease(SampleType newRelease) override;
 
    /**
     * @brief Sets the compressor make-up gain
     * 
     * @param newGain New gain value in decibels
     */
    void setGain(SampleType newGain) override;
 
    /**
     * @brief Sets one of the compressor's parameters
     * 
     * @param index Parameter index
     * @param newValue New parameter value
     */
    void setParameter(int index, SampleType newValue);
 
    /**
     * @brief Returns one of the compressor's parameters
     * 
     * @param index Parameter index
     */
    SampleType getParameter(int index);
 
    /**
     * @brief Sets the compressor knee type
     * 
     * @param index Knee type index
     */
    void setKneeType(int index);
 
    /**
     * @brief Returns the compressor knee type index
     */
    int getKneeType();
 
    enum Parameters
    {
        Threshold = 0,    //-80 | 0 dB
        Ratio,            //
        Attack,            //1ms | 100ms
        Release,        //10ms | 1000ms
        MakeupGain
    };
        
    enum KneeTypes
    {
        Soft = 0,
        Hard
    };
 
private:
 
    SampleType sampleRate;
    
    // User Parameters
    SampleType threshold{ -10.0 }; //in dB
    SampleType ratio{ 50.0 };
    SampleType makeupGain{ 0.0 }; //in dB
    SampleType attack{ 10.0 }; //in ms
    SampleType release{ 100.0 }; //in ms
    int kneeType{ KneeTypes::Soft };
 
    //=============================================
 
    SampleType kneeWidth{ 30.0 }; //in dB
 
    //============================================
 
    SampleType calaculateGain(SampleType input);
};