1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef DPBASE_H_
18#define DPBASE_H_
19
20
21#include <stdint.h>
22#include <cmath>
23#include <vector>
24#include <android/log.h>
25
26namespace dp_fx {
27
28#define DP_DEFAULT_BAND_ENABLED false
29#define DP_DEFAULT_BAND_CUTOFF_FREQUENCY_HZ 1000
30#define DP_DEFAULT_ATTACK_TIME_MS 50
31#define DP_DEFAULT_RELEASE_TIME_MS 120
32#define DP_DEFAULT_RATIO 2
33#define DP_DEFAULT_THRESHOLD_DB -30
34#define DP_DEFAULT_KNEE_WIDTH_DB 0
35#define DP_DEFAULT_NOISE_GATE_THRESHOLD_DB -90
36#define DP_DEFAULT_EXPANDER_RATIO 1
37#define DP_DEFAULT_GAIN_DB 0
38#define DP_DEFAULT_STAGE_INUSE false
39#define DP_DEFAULT_STAGE_ENABLED false
40#define DP_DEFAULT_LINK_GROUP 0
41
42
43
44class DPStage {
45public:
46    DPStage();
47    ~DPStage() = default;
48    void init(bool inUse, bool enabled);
49    bool isInUse() const {
50        return mInUse;
51    }
52    bool isEnabled() const {
53        return mEnabled;
54    }
55    void setEnabled(bool enabled) {
56        mEnabled = enabled;
57    }
58private:
59    bool mInUse;
60    bool mEnabled;
61};
62
63class DPBandStage : public DPStage {
64public:
65    DPBandStage();
66    ~DPBandStage() = default;
67    void init(bool inUse, bool enabled, int bandCount);
68    uint32_t getBandCount() const {
69        return mBandCount;
70    }
71    void setBandCount(uint32_t bandCount) {
72        mBandCount = bandCount;
73    }
74private:
75    uint32_t mBandCount;
76};
77
78class DPBandBase {
79public:
80    DPBandBase();
81    ~DPBandBase() = default;
82    void init(bool enabled, float cutoffFrequency);
83    bool isEnabled() const {
84        return mEnabled;
85    }
86    void setEnabled(bool enabled) {
87        mEnabled = enabled;
88    }
89    float getCutoffFrequency() const {
90        return mCutoofFrequencyHz;
91    }
92    void setCutoffFrequency(float cutoffFrequency) {
93        mCutoofFrequencyHz = cutoffFrequency;
94    }
95private:
96    bool mEnabled;
97    float mCutoofFrequencyHz;
98};
99
100class DPEqBand : public DPBandBase {
101public:
102    DPEqBand();
103    ~DPEqBand() = default;
104    void init(bool enabled, float cutoffFrequency, float gain);
105    float getGain() const;
106    void setGain(float gain);
107private:
108    float mGainDb;
109};
110
111class DPMbcBand : public DPBandBase {
112public:
113    DPMbcBand();
114    ~DPMbcBand() = default;
115    void init(bool enabled, float cutoffFrequency, float attackTime, float releaseTime,
116            float ratio, float threshold, float kneeWidth, float noiseGateThreshold,
117            float expanderRatio, float preGain, float postGain);
118    float getAttackTime() const {
119        return mAttackTimeMs;
120    }
121    void setAttackTime(float attackTime) {
122        mAttackTimeMs = attackTime;
123    }
124    float getReleaseTime() const {
125        return mReleaseTimeMs;
126    }
127    void setReleaseTime(float releaseTime) {
128        mReleaseTimeMs = releaseTime;
129    }
130    float getRatio() const {
131        return mRatio;
132    }
133    void setRatio(float ratio) {
134        mRatio = ratio;
135    }
136    float getThreshold() const {
137        return mThresholdDb;
138    }
139    void setThreshold(float threshold) {
140        mThresholdDb = threshold;
141    }
142    float getKneeWidth() const {
143        return mKneeWidthDb;
144    }
145    void setKneeWidth(float kneeWidth) {
146        mKneeWidthDb = kneeWidth;
147    }
148    float getNoiseGateThreshold() const {
149        return mNoiseGateThresholdDb;
150    }
151    void setNoiseGateThreshold(float noiseGateThreshold) {
152        mNoiseGateThresholdDb = noiseGateThreshold;
153    }
154    float getExpanderRatio() const {
155        return mExpanderRatio;
156    }
157    void setExpanderRatio(float expanderRatio) {
158        mExpanderRatio = expanderRatio;
159    }
160    float getPreGain() const {
161        return mPreGainDb;
162    }
163    void setPreGain(float preGain) {
164        mPreGainDb = preGain;
165    }
166    float getPostGain() const {
167        return mPostGainDb;
168    }
169    void setPostGain(float postGain) {
170        mPostGainDb = postGain;
171    }
172private:
173    float mAttackTimeMs;
174    float mReleaseTimeMs;
175    float mRatio;
176    float mThresholdDb;
177    float mKneeWidthDb;
178    float mNoiseGateThresholdDb;
179    float mExpanderRatio;
180    float mPreGainDb;
181    float mPostGainDb;
182};
183
184class DPEq : public DPBandStage {
185public:
186    DPEq();
187    ~DPEq() = default;
188    void init(bool inUse, bool enabled, uint32_t bandCount);
189    DPEqBand * getBand(uint32_t band);
190    void setBand(uint32_t band, DPEqBand &src);
191private:
192    std::vector<DPEqBand> mBands;
193};
194
195class DPMbc : public DPBandStage {
196public:
197    DPMbc();
198    ~DPMbc() = default;
199    void init(bool inUse, bool enabled, uint32_t bandCount);
200    DPMbcBand * getBand(uint32_t band);
201    void setBand(uint32_t band, DPMbcBand &src);
202private:
203    std::vector<DPMbcBand> mBands;
204};
205
206class DPLimiter : public DPStage {
207public:
208    DPLimiter();
209    ~DPLimiter() = default;
210    void init(bool inUse, bool enabled, uint32_t linkGroup, float attackTime, float releaseTime,
211            float ratio, float threshold, float postGain);
212    uint32_t getLinkGroup() const {
213        return mLinkGroup;
214    }
215    void setLinkGroup(uint32_t linkGroup) {
216        mLinkGroup = linkGroup;
217    }
218    float getAttackTime() const {
219        return mAttackTimeMs;
220    }
221    void setAttackTime(float attackTime) {
222        mAttackTimeMs = attackTime;
223    }
224    float getReleaseTime() const {
225        return mReleaseTimeMs;
226    }
227    void setReleaseTime(float releaseTime) {
228        mReleaseTimeMs = releaseTime;
229    }
230    float getRatio() const {
231        return mRatio;
232    }
233    void setRatio(float ratio) {
234        mRatio = ratio;
235    }
236    float getThreshold() const {
237        return mThresholdDb;
238    }
239    void setThreshold(float threshold) {
240        mThresholdDb = threshold;
241    }
242    float getPostGain() const {
243        return mPostGainDb;
244    }
245    void setPostGain(float postGain) {
246        mPostGainDb = postGain;
247    }
248private:
249    uint32_t mLinkGroup;
250    float mAttackTimeMs;
251    float mReleaseTimeMs;
252    float mRatio;
253    float mThresholdDb;
254    float mPostGainDb;
255};
256
257class DPChannel {
258public:
259    DPChannel();
260    ~DPChannel() = default;
261    void init(float inputGain, bool preEqInUse, uint32_t preEqBandCount,
262            bool mbcInUse, uint32_t mbcBandCount, bool postEqInUse, uint32_t postEqBandCount,
263            bool limiterInUse);
264
265    float getInputGain() const {
266        if (!mInitialized) {
267            return 0;
268        }
269        return mInputGainDb;
270    }
271    void setInputGain(float gain) {
272        mInputGainDb = gain;
273    }
274
275    float getOutputGain() const {
276        if (!mInitialized) {
277            return 0;
278        }
279        return mOutputGainDb;
280    }
281    void setOutputGain(float gain) {
282        mOutputGainDb = gain;
283    }
284
285    DPEq* getPreEq();
286    DPMbc* getMbc();
287    DPEq* getPostEq();
288    DPLimiter *getLimiter();
289    void setLimiter(DPLimiter &limiter);
290
291private:
292    bool mInitialized;
293    float mInputGainDb;
294    float mOutputGainDb;
295
296    DPEq mPreEq;
297    DPMbc mMbc;
298    DPEq mPostEq;
299    DPLimiter mLimiter;
300
301    bool mPreEqInUse;
302    bool mMbcInUse;
303    bool mPostEqInUse;
304    bool mLimiterInUse;
305};
306
307class DPBase {
308public:
309    DPBase();
310    virtual ~DPBase() = default;
311
312    void init(uint32_t channelCount, bool preEqInUse, uint32_t preEqBandCount,
313            bool mbcInUse, uint32_t mbcBandCount, bool postEqInUse, uint32_t postEqBandCount,
314            bool limiterInUse);
315    virtual size_t processSamples(const float *in, float *out, size_t samples) = 0;
316    virtual void reset() = 0;
317
318    DPChannel* getChannel(uint32_t channelIndex);
319    uint32_t getChannelCount() const {
320        return mChannelCount;
321    }
322    uint32_t getPreEqBandCount() const {
323        return mPreEqBandCount;
324    }
325    uint32_t getMbcBandCount() const {
326        return mMbcBandCount;
327    }
328    uint32_t getPostEqBandCount() const {
329        return mPostEqBandCount;
330    }
331    bool isPreEQInUse() const {
332        return mPreEqInUse;
333    }
334    bool isMbcInUse() const {
335        return mMbcInUse;
336    }
337    bool isPostEqInUse() const {
338        return mPostEqInUse;
339    }
340    bool isLimiterInUse() const {
341        return mLimiterInUse;
342    }
343
344private:
345    bool mInitialized;
346    //general
347    uint32_t mChannelCount;
348    bool mPreEqInUse;
349    uint32_t mPreEqBandCount;
350    bool mMbcInUse;
351    uint32_t mMbcBandCount;
352    bool mPostEqInUse;
353    uint32_t mPostEqBandCount;
354    bool mLimiterInUse;
355
356    std::vector<DPChannel> mChannel;
357};
358
359} //namespace dp_fx
360
361
362#endif  // DPBASE_H_
363