summaryrefslogtreecommitdiff
path: root/media/libeffects/dynamicsproc/dsp/DPBase.h
blob: e74f91d05f41bb9b88335ad69cd236fcd88313d7 (plain)
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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef DPBASE_H_
#define DPBASE_H_


#include <stdint.h>
#include <cmath>
#include <vector>
#include <android/log.h>

namespace dp_fx {

#define DP_DEFAULT_BAND_ENABLED false
#define DP_DEFAULT_BAND_CUTOFF_FREQUENCY_HZ 1000
#define DP_DEFAULT_ATTACK_TIME_MS 50
#define DP_DEFAULT_RELEASE_TIME_MS 120
#define DP_DEFAULT_RATIO 2
#define DP_DEFAULT_THRESHOLD_DB -30
#define DP_DEFAULT_KNEE_WIDTH_DB 0
#define DP_DEFAULT_NOISE_GATE_THRESHOLD_DB -90
#define DP_DEFAULT_EXPANDER_RATIO 1
#define DP_DEFAULT_GAIN_DB 0
#define DP_DEFAULT_STAGE_INUSE false
#define DP_DEFAULT_STAGE_ENABLED false
#define DP_DEFAULT_LINK_GROUP 0



class DPStage {
public:
    DPStage();
    ~DPStage() = default;
    void init(bool inUse, bool enabled);
    bool isInUse() const {
        return mInUse;
    }
    bool isEnabled() const {
        return mEnabled;
    }
    void setEnabled(bool enabled) {
        mEnabled = enabled;
    }
private:
    bool mInUse;
    bool mEnabled;
};

class DPBandStage : public DPStage {
public:
    DPBandStage();
    ~DPBandStage() = default;
    void init(bool inUse, bool enabled, int bandCount);
    uint32_t getBandCount() const {
        return mBandCount;
    }
    void setBandCount(uint32_t bandCount) {
        mBandCount = bandCount;
    }
private:
    uint32_t mBandCount;
};

class DPBandBase {
public:
    DPBandBase();
    ~DPBandBase() = default;
    void init(bool enabled, float cutoffFrequency);
    bool isEnabled() const {
        return mEnabled;
    }
    void setEnabled(bool enabled) {
        mEnabled = enabled;
    }
    float getCutoffFrequency() const {
        return mCutoofFrequencyHz;
    }
    void setCutoffFrequency(float cutoffFrequency) {
        mCutoofFrequencyHz = cutoffFrequency;
    }
private:
    bool mEnabled;
    float mCutoofFrequencyHz;
};

class DPEqBand : public DPBandBase {
public:
    DPEqBand();
    ~DPEqBand() = default;
    void init(bool enabled, float cutoffFrequency, float gain);
    float getGain() const;
    void setGain(float gain);
private:
    float mGainDb;
};

class DPMbcBand : public DPBandBase {
public:
    DPMbcBand();
    ~DPMbcBand() = default;
    void init(bool enabled, float cutoffFrequency, float attackTime, float releaseTime,
            float ratio, float threshold, float kneeWidth, float noiseGateThreshold,
            float expanderRatio, float preGain, float postGain);
    float getAttackTime() const {
        return mAttackTimeMs;
    }
    void setAttackTime(float attackTime) {
        mAttackTimeMs = attackTime;
    }
    float getReleaseTime() const {
        return mReleaseTimeMs;
    }
    void setReleaseTime(float releaseTime) {
        mReleaseTimeMs = releaseTime;
    }
    float getRatio() const {
        return mRatio;
    }
    void setRatio(float ratio) {
        mRatio = ratio;
    }
    float getThreshold() const {
        return mThresholdDb;
    }
    void setThreshold(float threshold) {
        mThresholdDb = threshold;
    }
    float getKneeWidth() const {
        return mKneeWidthDb;
    }
    void setKneeWidth(float kneeWidth) {
        mKneeWidthDb = kneeWidth;
    }
    float getNoiseGateThreshold() const {
        return mNoiseGateThresholdDb;
    }
    void setNoiseGateThreshold(float noiseGateThreshold) {
        mNoiseGateThresholdDb = noiseGateThreshold;
    }
    float getExpanderRatio() const {
        return mExpanderRatio;
    }
    void setExpanderRatio(float expanderRatio) {
        mExpanderRatio = expanderRatio;
    }
    float getPreGain() const {
        return mPreGainDb;
    }
    void setPreGain(float preGain) {
        mPreGainDb = preGain;
    }
    float getPostGain() const {
        return mPostGainDb;
    }
    void setPostGain(float postGain) {
        mPostGainDb = postGain;
    }
private:
    float mAttackTimeMs;
    float mReleaseTimeMs;
    float mRatio;
    float mThresholdDb;
    float mKneeWidthDb;
    float mNoiseGateThresholdDb;
    float mExpanderRatio;
    float mPreGainDb;
    float mPostGainDb;
};

class DPEq : public DPBandStage {
public:
    DPEq();
    ~DPEq() = default;
    void init(bool inUse, bool enabled, uint32_t bandCount);
    DPEqBand * getBand(uint32_t band);
    void setBand(uint32_t band, DPEqBand &src);
private:
    std::vector<DPEqBand> mBands;
};

class DPMbc : public DPBandStage {
public:
    DPMbc();
    ~DPMbc() = default;
    void init(bool inUse, bool enabled, uint32_t bandCount);
    DPMbcBand * getBand(uint32_t band);
    void setBand(uint32_t band, DPMbcBand &src);
private:
    std::vector<DPMbcBand> mBands;
};

class DPLimiter : public DPStage {
public:
    DPLimiter();
    ~DPLimiter() = default;
    void init(bool inUse, bool enabled, uint32_t linkGroup, float attackTime, float releaseTime,
            float ratio, float threshold, float postGain);
    uint32_t getLinkGroup() const {
        return mLinkGroup;
    }
    void setLinkGroup(uint32_t linkGroup) {
        mLinkGroup = linkGroup;
    }
    float getAttackTime() const {
        return mAttackTimeMs;
    }
    void setAttackTime(float attackTime) {
        mAttackTimeMs = attackTime;
    }
    float getReleaseTime() const {
        return mReleaseTimeMs;
    }
    void setReleaseTime(float releaseTime) {
        mReleaseTimeMs = releaseTime;
    }
    float getRatio() const {
        return mRatio;
    }
    void setRatio(float ratio) {
        mRatio = ratio;
    }
    float getThreshold() const {
        return mThresholdDb;
    }
    void setThreshold(float threshold) {
        mThresholdDb = threshold;
    }
    float getPostGain() const {
        return mPostGainDb;
    }
    void setPostGain(float postGain) {
        mPostGainDb = postGain;
    }
private:
    uint32_t mLinkGroup;
    float mAttackTimeMs;
    float mReleaseTimeMs;
    float mRatio;
    float mThresholdDb;
    float mPostGainDb;
};

class DPChannel {
public:
    DPChannel();
    ~DPChannel() = default;
    void init(float inputGain, bool preEqInUse, uint32_t preEqBandCount,
            bool mbcInUse, uint32_t mbcBandCount, bool postEqInUse, uint32_t postEqBandCount,
            bool limiterInUse);

    float getInputGain() const {
        if (!mInitialized) {
            return 0;
        }
        return mInputGainDb;
    }
    void setInputGain(float gain) {
        mInputGainDb = gain;
    }

    float getOutputGain() const {
        if (!mInitialized) {
            return 0;
        }
        return mOutputGainDb;
    }
    void setOutputGain(float gain) {
        mOutputGainDb = gain;
    }

    DPEq* getPreEq();
    DPMbc* getMbc();
    DPEq* getPostEq();
    DPLimiter *getLimiter();
    void setLimiter(DPLimiter &limiter);

private:
    bool mInitialized;
    float mInputGainDb;
    float mOutputGainDb;

    DPEq mPreEq;
    DPMbc mMbc;
    DPEq mPostEq;
    DPLimiter mLimiter;

    bool mPreEqInUse;
    bool mMbcInUse;
    bool mPostEqInUse;
    bool mLimiterInUse;
};

class DPBase {
public:
    DPBase();
    virtual ~DPBase() = default;

    void init(uint32_t channelCount, bool preEqInUse, uint32_t preEqBandCount,
            bool mbcInUse, uint32_t mbcBandCount, bool postEqInUse, uint32_t postEqBandCount,
            bool limiterInUse);
    virtual size_t processSamples(const float *in, float *out, size_t samples) = 0;
    virtual void reset() = 0;

    DPChannel* getChannel(uint32_t channelIndex);
    uint32_t getChannelCount() const {
        return mChannelCount;
    }
    uint32_t getPreEqBandCount() const {
        return mPreEqBandCount;
    }
    uint32_t getMbcBandCount() const {
        return mMbcBandCount;
    }
    uint32_t getPostEqBandCount() const {
        return mPostEqBandCount;
    }
    bool isPreEQInUse() const {
        return mPreEqInUse;
    }
    bool isMbcInUse() const {
        return mMbcInUse;
    }
    bool isPostEqInUse() const {
        return mPostEqInUse;
    }
    bool isLimiterInUse() const {
        return mLimiterInUse;
    }

private:
    bool mInitialized;
    //general
    uint32_t mChannelCount;
    bool mPreEqInUse;
    uint32_t mPreEqBandCount;
    bool mMbcInUse;
    uint32_t mMbcBandCount;
    bool mPostEqInUse;
    uint32_t mPostEqBandCount;
    bool mLimiterInUse;

    std::vector<DPChannel> mChannel;
};

} //namespace dp_fx


#endif  // DPBASE_H_