File: DynamicProcessor.h

package info (click to toggle)
lsp-plugins 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 91,856 kB
  • sloc: cpp: 427,831; xml: 57,779; makefile: 9,961; php: 1,005; sh: 18
file content (381 lines) | stat: -rw-r--r-- 13,532 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
 *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
 *
 * This file is part of lsp-dsp-units
 * Created on: 19 окт. 2016 г.
 *
 * lsp-dsp-units is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * lsp-dsp-units is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with lsp-dsp-units. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LSP_PLUG_IN_DSP_UNITS_DYNAMICS_DYNAMICPROCESSOR_H_
#define LSP_PLUG_IN_DSP_UNITS_DYNAMICS_DYNAMICPROCESSOR_H_

#include <lsp-plug.in/dsp-units/version.h>
#include <lsp-plug.in/dsp-units/iface/IStateDumper.h>

#define DYNAMIC_PROCESSOR_DOTS      4
#define DYNAMIC_PROCESSOR_RANGES    (DYNAMIC_PROCESSOR_DOTS + 1)

namespace lsp
{
    namespace dspu
    {
        typedef struct dyndot_t
        {
            float   fInput;         // Negative value means off
            float   fOutput;        // Negative value means off
            float   fKnee;          // Negative value means off
        } dyndot_t;

        class LSP_DSP_UNITS_PUBLIC DynamicProcessor
        {
            private:
                DynamicProcessor & operator = (const DynamicProcessor &);
                DynamicProcessor(const DynamicProcessor &);

            protected:
                typedef struct spline_t
                {
                    float       fPreRatio;      // Pre-knee ratio
                    float       fPostRatio;     // Post-knee ratio
                    float       fKneeStart;     // Start knee threshold
                    float       fKneeStop;      // Stop knee threshold
                    float       fThresh;        // Logarithmic threshold
                    float       fMakeup;        // Makeup gain of the knee
                    float       vHermite[4];    // Hermite interpolation
                } spline_t;

                typedef struct reaction_t
                {
                    float       fLevel;
                    float       fTau;
                } reaction_t;

                enum counters_t
                {
                    CT_SPLINES,
                    CT_ATTACK,
                    CT_RELEASE,

                    CT_TOTAL
                };

            protected:
                // Input parameters
                dyndot_t    vDots[DYNAMIC_PROCESSOR_DOTS];
                float       vAttackLvl[DYNAMIC_PROCESSOR_DOTS];
                float       vReleaseLvl[DYNAMIC_PROCESSOR_DOTS];
                float       vAttackTime[DYNAMIC_PROCESSOR_RANGES];
                float       vReleaseTime[DYNAMIC_PROCESSOR_RANGES];
                float       fInRatio;   // Input ratio
                float       fOutRatio;  // Output ratio

                // Processing parameters
                spline_t    vSplines[DYNAMIC_PROCESSOR_DOTS];
                reaction_t  vAttack[DYNAMIC_PROCESSOR_RANGES];
                reaction_t  vRelease[DYNAMIC_PROCESSOR_RANGES];
                uint8_t     fCount[CT_TOTAL];  // Number of elements for AttackLvl, ReleaseLvl, ... etc

                // Dynamic patameters
                float       fEnvelope;

                // Additional parameters
                size_t      nSampleRate;
                bool        bUpdate;

            protected:
                static inline float     spline_amp(const spline_t *s, float x);
                static inline float     spline_model(const spline_t *s, float x);
                void                    sort_reactions(reaction_t *s, size_t count);
                void                    sort_splines(spline_t *s, size_t count);
                static inline float     solve_reaction(const reaction_t *s, float x, size_t count);

            public:
                explicit DynamicProcessor();
                ~DynamicProcessor();

                /**
                 * Construct object
                 */
                void    construct();

                /**
                 * Destroyp object
                 */
                void    destroy();

            public:
                /** Check that some of processor's parameters have been modified
                 * and we need to call update_settings();
                 *
                 * @return true if some of processor's parameters have been modified
                 */
                inline bool modified() const
                {
                    return bUpdate;
                }

                /** Update processor's settings
                 *
                 */
                void update_settings();

                /** Set sample rate
                 *
                 * @param sr sample rate
                 */
                inline void set_sample_rate(size_t sr)
                {
                    if (sr == nSampleRate)
                        return;
                    nSampleRate = sr;
                    bUpdate     = true;
                }

                /** Get input ratio
                 *
                 * @return input ratio
                 */
                inline float get_in_ratio() const
                {
                    return fInRatio;
                }

                /** Set input ratio
                 *
                 * @param ratio input ratio
                 */
                inline void set_in_ratio(float ratio)
                {
                    if (fInRatio == ratio)
                        return;
                    fInRatio = ratio;
                    bUpdate = true;
                }

                /** Get output ratio
                 *
                 * @return output ratio
                 */
                inline float get_out_ratio() const
                {
                    return fOutRatio;
                }

                /** Set output ratio
                 *
                 * @param ratio output ratio
                 */
                inline void set_out_ratio(float ratio)
                {
                    if (fOutRatio == ratio)
                        return;
                    fOutRatio = ratio;
                    bUpdate = true;
                }

                /** Get dot configuration
                 *
                 * @param id identifier of dot
                 * @param dst pointer to store data
                 * @return status of operation
                 */
                inline bool get_dot(size_t id, dyndot_t *dst) const
                {
                    if ((id >= DYNAMIC_PROCESSOR_DOTS) || (dst == NULL))
                        return false;
                    *dst    = vDots[id];
                    return true;
                }

                /** Set dot configuration
                 *
                 * @param id identifier of dot
                 * @param src new configuration pointer or NULL
                 * @return status of operation
                 */
                bool set_dot(size_t id, const dyndot_t *src);

                /** Set dot configuration
                 *
                 * @param id identifier of dot
                 * @param in input level
                 * @param out output level
                 * @param knee knee size
                 * @return status of operation
                 */
                bool set_dot(size_t id, float in, float out, float knee);

                /** Get attack level
                 *
                 * @param id split level
                 * @return attack level
                 */
                inline float get_attack_level(size_t id) const
                {
                    return (id >= DYNAMIC_PROCESSOR_DOTS) ? -1.0f : vAttackLvl[id];
                }

                /** Set attack level
                 *
                 * @param id split level
                 * @param value level value
                 */
                inline void set_attack_level(size_t id, float value)
                {
                    if ((id >= DYNAMIC_PROCESSOR_DOTS) || (vAttackLvl[id] == value))
                        return;
                    vAttackLvl[id] = value;
                    bUpdate = true;
                }

                /** Get release level
                 *
                 * @param id split level
                 * @return attack level
                 */
                inline float get_release_level(size_t id) const
                {
                    return (id >= DYNAMIC_PROCESSOR_DOTS) ? -1.0f : vReleaseLvl[id];
                }

                /** Set release level
                 *
                 * @param id split level
                 * @param value level value
                 */
                inline void set_release_level(size_t id, float value)
                {
                    if ((id >= DYNAMIC_PROCESSOR_DOTS) || (vReleaseLvl[id] == value))
                        return;
                    vReleaseLvl[id] = value;
                    bUpdate = true;
                }

                /** Get attack time of the specified range
                 *
                 * @param id identifier of the range
                 * @return attack time
                 */
                inline float get_attack_time(size_t id) const
                {
                    return (id >= DYNAMIC_PROCESSOR_RANGES) ? -1.0f : vAttackTime[id];
                }

                /** Set attack time
                 *
                 * @param id identifier of the range
                 * @param value attack time value
                 */
                inline void set_attack_time(size_t id, float value)
                {
                    if ((id >= DYNAMIC_PROCESSOR_RANGES) || (vAttackTime[id] == value))
                        return;
                    vAttackTime[id] = value;
                    bUpdate = true;
                }

                /** Get release time of the specified range
                 *
                 * @param id identifier of the range
                 * @return release time
                 */
                inline float get_release_time(size_t id) const
                {
                    return (id >= DYNAMIC_PROCESSOR_RANGES) ? -1.0f : vReleaseTime[id];
                }

                /** Set release time
                 *
                 * @param id identifier of the range
                 * @param value attack time value
                 */
                inline void set_release_time(size_t id, float value)
                {
                    if ((id >= DYNAMIC_PROCESSOR_RANGES) || (vReleaseTime[id] == value))
                        return;
                    vReleaseTime[id] = value;
                    bUpdate = true;
                }

                /** Process sidechain signal
                 *
                 * @param out output signal gain to VCA
                 * @param env envelope signal of processor
                 * @param in sidechain signal
                 * @param samples number of samples to process
                 */
                void process(float *out, float *env, const float *in, size_t samples);

                /** Process one sample of sidechain signal
                 *
                 * @param in sidechain signal
                 * @param out envelope signal of processor, may be NULL
                 * @return output signal gain to VCA
                 */
                float process(float *env, float in);

                /** Get dynamic curve
                 *
                 * @param out output compression value
                 * @param in input compression value
                 * @param dots number of input dots
                 */
                void curve(float *out, const float *in, size_t dots);

                /** Get dynamic curve point
                 *
                 * @param in input level
                 */
                float curve(float in);

                /** Get dynamic curve model
                 *
                 * @param out output compression value
                 * @param in input compression value
                 * @param dots number of input dots
                 */
                void model(float *out, const float *in, size_t dots);

                /** Get dynamic curve point
                 *
                 * @param in input level
                 */
                float model(float in);

                /** Get dynamic gain reduction
                 *
                 * @param out output signal
                 * @param in input signal
                 * @param dots number of dots
                 */
                void reduction(float *out, const float *in, size_t dots);

                /** Get dynamic gain reduction
                 *
                 * @param in input level
                 */
                float reduction(float in);
    
                /**
                 * Dump internal state
                 * @param v state dumper
                 */
                void dump(IStateDumper *v) const;
        };
    }
} /* namespace lsp */

#endif /* LSP_PLUG_IN_DSP_UNITS_DYNAMICS_DYNAMICPROCESSOR_H_ */