1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 16 сент. 2016 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef PLUGINS_COMPRESSOR_H_
23 #define PLUGINS_COMPRESSOR_H_
24 
25 #include <metadata/plugins.h>
26 
27 #include <core/plugin.h>
28 #include <core/util/Bypass.h>
29 #include <core/util/Sidechain.h>
30 #include <core/util/Delay.h>
31 #include <core/util/MeterGraph.h>
32 #include <core/filters/Equalizer.h>
33 #include <core/dynamics/Compressor.h>
34 
35 namespace lsp
36 {
37     class compressor_base: public plugin_t
38     {
39         protected:
40             enum c_mode_t
41             {
42                 CM_MONO,
43                 CM_STEREO,
44                 CM_LR,
45                 CM_MS
46             };
47 
48             enum sc_source_t
49             {
50                 SCT_FEED_FORWARD,
51                 SCT_FEED_BACK,
52                 SCT_EXTERNAL
53             };
54 
55             enum sc_graph_t
56             {
57                 G_IN,
58                 G_SC,
59                 G_ENV,
60                 G_GAIN,
61                 G_OUT,
62 
63                 G_TOTAL
64             };
65 
66             enum sc_meter_t
67             {
68                 M_IN,
69                 M_SC,
70                 M_ENV,
71                 M_GAIN,
72                 M_CURVE,
73                 M_OUT,
74 
75                 M_TOTAL
76             };
77 
78             enum sync_t
79             {
80                 S_CURVE     = 1 << 0,
81 
82                 S_ALL       = S_CURVE
83             };
84 
85             typedef struct channel_t
86             {
87                 Bypass          sBypass;            // Bypass
88                 Sidechain       sSC;                // Sidechain module
89                 Equalizer       sSCEq;              // Sidechain equalizer
90                 Compressor      sComp;              // Compression module
91                 Delay           sDelay;             // Lookahead delay
92                 Delay           sCompDelay;         // Compensation delay
93                 Delay           sDryDelay;          // Dry delay
94                 MeterGraph      sGraph[G_TOTAL];    // Input meter graph
95 
96                 float          *vIn;                // Input data
97                 float          *vOut;               // Output data
98                 float          *vSc;                // Sidechain data
99                 float          *vEnv;               // Envelope data
100                 float          *vGain;              // Gain reduction data
101                 bool            bScListen;          // Listen sidechain
102                 size_t          nSync;              // Synchronization flags
103                 size_t          nScType;            // Sidechain mode
104                 float           fMakeup;            // Makeup gain
105                 float           fFeedback;          // Feedback
106                 float           fDryGain;           // Dry gain
107                 float           fWetGain;           // Wet gain
108                 float           fDotIn;             // Dot input gain
109                 float           fDotOut;            // Dot output gain
110 
111                 IPort          *pIn;                // Input port
112                 IPort          *pOut;               // Output port
113                 IPort          *pSC;                // Sidechain port
114 
115                 IPort          *pGraph[G_TOTAL];    // History graphs
116                 IPort          *pMeter[M_TOTAL];    // Meters
117 
118                 IPort          *pScType;            // Sidechain location
119                 IPort          *pScMode;            // Sidechain mode
120                 IPort          *pScLookahead;       // Sidechain lookahead
121                 IPort          *pScListen;          // Sidechain listen
122                 IPort          *pScSource;          // Sidechain source
123                 IPort          *pScReactivity;      // Sidechain reactivity
124                 IPort          *pScPreamp;          // Sidechain pre-amplification
125                 IPort          *pScHpfMode;         // Sidechain high-pass filter mode
126                 IPort          *pScHpfFreq;         // Sidechain high-pass filter frequency
127                 IPort          *pScLpfMode;         // Sidechain low-pass filter mode
128                 IPort          *pScLpfFreq;         // Sidechain low-pass filter frequency
129 
130                 IPort          *pMode;              // Mode
131                 IPort          *pAttackLvl;         // Attack level
132                 IPort          *pReleaseLvl;        // Release level
133                 IPort          *pAttackTime;        // Attack time
134                 IPort          *pReleaseTime;       // Release time
135                 IPort          *pRatio;             // Ratio
136                 IPort          *pKnee;              // Knee
137                 IPort          *pBThresh;           // Boost threshold
138                 IPort          *pBoost;             // Boost signal amount
139                 IPort          *pMakeup;            // Makeup
140 
141                 IPort          *pDryGain;           // Dry gain
142                 IPort          *pWetGain;           // Wet gain
143                 IPort          *pCurve;             // Curve graph
144                 IPort          *pReleaseOut;        // Output release level
145             } channel_t;
146 
147         protected:
148             size_t          nMode;          // Compressor mode
149             bool            bSidechain;     // External side chain
150             channel_t      *vChannels;      // Compressor channels
151             float          *vCurve;         // Compressor curve
152             float          *vTime;          // Time points buffer
153             bool            bPause;         // Pause button
154             bool            bClear;         // Clear button
155             bool            bMSListen;      // Mid/Side listen
156             float           fInGain;        // Input gain
157             bool            bUISync;
158             float_buffer_t *pIDisplay;      // Inline display buffer
159 
160             IPort          *pBypass;        // Bypass port
161             IPort          *pInGain;        // Input gain
162             IPort          *pOutGain;       // Output gain
163             IPort          *pPause;         // Pause gain
164             IPort          *pClear;         // Cleanup gain
165             IPort          *pMSListen;      // Mid/Side listen
166 
167             uint8_t        *pData;          // Compressor data
168 
169         protected:
170             float           process_feedback(channel_t *c, size_t i, size_t channels);
171             void            process_non_feedback(channel_t *c, float **in, size_t samples);
172             static compressor_mode_t    decode_mode(int mode);
173 
174         public:
175             explicit compressor_base(const plugin_metadata_t &metadata, bool sc, size_t mode);
176             virtual ~compressor_base();
177 
178         public:
179             virtual void init(IWrapper *wrapper);
180             virtual void destroy();
181 
182             virtual void update_settings();
183             virtual void update_sample_rate(long sr);
184             virtual void ui_activated();
185 
186             virtual void process(size_t samples);
187             virtual bool inline_display(ICanvas *cv, size_t width, size_t height);
188 
189             virtual void dump(IStateDumper *v) const;
190     };
191 
192     class compressor_mono: public compressor_base, public compressor_mono_metadata
193     {
194         public:
195             compressor_mono();
196     };
197 
198     class compressor_stereo: public compressor_base, public compressor_stereo_metadata
199     {
200         public:
201             compressor_stereo();
202     };
203 
204     class compressor_lr: public compressor_base, public compressor_lr_metadata
205     {
206         public:
207             compressor_lr();
208     };
209 
210     class compressor_ms: public compressor_base, public compressor_ms_metadata
211     {
212         public:
213             compressor_ms();
214     };
215 
216     class sc_compressor_mono: public compressor_base, public sc_compressor_mono_metadata
217     {
218         public:
219             sc_compressor_mono();
220     };
221 
222     class sc_compressor_stereo: public compressor_base, public sc_compressor_stereo_metadata
223     {
224         public:
225             sc_compressor_stereo();
226     };
227 
228     class sc_compressor_lr: public compressor_base, public sc_compressor_lr_metadata
229     {
230         public:
231             sc_compressor_lr();
232     };
233 
234     class sc_compressor_ms: public compressor_base, public sc_compressor_ms_metadata
235     {
236         public:
237             sc_compressor_ms();
238     };
239 
240 }
241 
242 #endif /* PLUGINS_COMPRESSOR_H_ */
243