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 
26 namespace 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 
44 class DPStage {
45 public:
46     DPStage();
47     ~DPStage() = default;
48     void init(bool inUse, bool enabled);
isInUse()49     bool isInUse() const {
50         return mInUse;
51     }
isEnabled()52     bool isEnabled() const {
53         return mEnabled;
54     }
setEnabled(bool enabled)55     void setEnabled(bool enabled) {
56         mEnabled = enabled;
57     }
58 private:
59     bool mInUse;
60     bool mEnabled;
61 };
62 
63 class DPBandStage : public DPStage {
64 public:
65     DPBandStage();
66     ~DPBandStage() = default;
67     void init(bool inUse, bool enabled, int bandCount);
getBandCount()68     uint32_t getBandCount() const {
69         return mBandCount;
70     }
setBandCount(uint32_t bandCount)71     void setBandCount(uint32_t bandCount) {
72         mBandCount = bandCount;
73     }
74 private:
75     uint32_t mBandCount;
76 };
77 
78 class DPBandBase {
79 public:
80     DPBandBase();
81     ~DPBandBase() = default;
82     void init(bool enabled, float cutoffFrequency);
isEnabled()83     bool isEnabled() const {
84         return mEnabled;
85     }
setEnabled(bool enabled)86     void setEnabled(bool enabled) {
87         mEnabled = enabled;
88     }
getCutoffFrequency()89     float getCutoffFrequency() const {
90         return mCutoofFrequencyHz;
91     }
setCutoffFrequency(float cutoffFrequency)92     void setCutoffFrequency(float cutoffFrequency) {
93         mCutoofFrequencyHz = cutoffFrequency;
94     }
95 private:
96     bool mEnabled;
97     float mCutoofFrequencyHz;
98 };
99 
100 class DPEqBand : public DPBandBase {
101 public:
102     DPEqBand();
103     ~DPEqBand() = default;
104     void init(bool enabled, float cutoffFrequency, float gain);
105     float getGain() const;
106     void setGain(float gain);
107 private:
108     float mGainDb;
109 };
110 
111 class DPMbcBand : public DPBandBase {
112 public:
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);
getAttackTime()118     float getAttackTime() const {
119         return mAttackTimeMs;
120     }
setAttackTime(float attackTime)121     void setAttackTime(float attackTime) {
122         mAttackTimeMs = attackTime;
123     }
getReleaseTime()124     float getReleaseTime() const {
125         return mReleaseTimeMs;
126     }
setReleaseTime(float releaseTime)127     void setReleaseTime(float releaseTime) {
128         mReleaseTimeMs = releaseTime;
129     }
getRatio()130     float getRatio() const {
131         return mRatio;
132     }
setRatio(float ratio)133     void setRatio(float ratio) {
134         mRatio = ratio;
135     }
getThreshold()136     float getThreshold() const {
137         return mThresholdDb;
138     }
setThreshold(float threshold)139     void setThreshold(float threshold) {
140         mThresholdDb = threshold;
141     }
getKneeWidth()142     float getKneeWidth() const {
143         return mKneeWidthDb;
144     }
setKneeWidth(float kneeWidth)145     void setKneeWidth(float kneeWidth) {
146         mKneeWidthDb = kneeWidth;
147     }
getNoiseGateThreshold()148     float getNoiseGateThreshold() const {
149         return mNoiseGateThresholdDb;
150     }
setNoiseGateThreshold(float noiseGateThreshold)151     void setNoiseGateThreshold(float noiseGateThreshold) {
152         mNoiseGateThresholdDb = noiseGateThreshold;
153     }
getExpanderRatio()154     float getExpanderRatio() const {
155         return mExpanderRatio;
156     }
setExpanderRatio(float expanderRatio)157     void setExpanderRatio(float expanderRatio) {
158         mExpanderRatio = expanderRatio;
159     }
getPreGain()160     float getPreGain() const {
161         return mPreGainDb;
162     }
setPreGain(float preGain)163     void setPreGain(float preGain) {
164         mPreGainDb = preGain;
165     }
getPostGain()166     float getPostGain() const {
167         return mPostGainDb;
168     }
setPostGain(float postGain)169     void setPostGain(float postGain) {
170         mPostGainDb = postGain;
171     }
172 private:
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 
184 class DPEq : public DPBandStage {
185 public:
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);
191 private:
192     std::vector<DPEqBand> mBands;
193 };
194 
195 class DPMbc : public DPBandStage {
196 public:
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);
202 private:
203     std::vector<DPMbcBand> mBands;
204 };
205 
206 class DPLimiter : public DPStage {
207 public:
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);
getLinkGroup()212     uint32_t getLinkGroup() const {
213         return mLinkGroup;
214     }
setLinkGroup(uint32_t linkGroup)215     void setLinkGroup(uint32_t linkGroup) {
216         mLinkGroup = linkGroup;
217     }
getAttackTime()218     float getAttackTime() const {
219         return mAttackTimeMs;
220     }
setAttackTime(float attackTime)221     void setAttackTime(float attackTime) {
222         mAttackTimeMs = attackTime;
223     }
getReleaseTime()224     float getReleaseTime() const {
225         return mReleaseTimeMs;
226     }
setReleaseTime(float releaseTime)227     void setReleaseTime(float releaseTime) {
228         mReleaseTimeMs = releaseTime;
229     }
getRatio()230     float getRatio() const {
231         return mRatio;
232     }
setRatio(float ratio)233     void setRatio(float ratio) {
234         mRatio = ratio;
235     }
getThreshold()236     float getThreshold() const {
237         return mThresholdDb;
238     }
setThreshold(float threshold)239     void setThreshold(float threshold) {
240         mThresholdDb = threshold;
241     }
getPostGain()242     float getPostGain() const {
243         return mPostGainDb;
244     }
setPostGain(float postGain)245     void setPostGain(float postGain) {
246         mPostGainDb = postGain;
247     }
248 private:
249     uint32_t mLinkGroup;
250     float mAttackTimeMs;
251     float mReleaseTimeMs;
252     float mRatio;
253     float mThresholdDb;
254     float mPostGainDb;
255 };
256 
257 class DPChannel {
258 public:
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 
getInputGain()265     float getInputGain() const {
266         if (!mInitialized) {
267             return 0;
268         }
269         return mInputGainDb;
270     }
setInputGain(float gain)271     void setInputGain(float gain) {
272         mInputGainDb = gain;
273     }
274 
getOutputGain()275     float getOutputGain() const {
276         if (!mInitialized) {
277             return 0;
278         }
279         return mOutputGainDb;
280     }
setOutputGain(float gain)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 
291 private:
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 
307 class DPBase {
308 public:
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);
getChannelCount()319     uint32_t getChannelCount() const {
320         return mChannelCount;
321     }
getPreEqBandCount()322     uint32_t getPreEqBandCount() const {
323         return mPreEqBandCount;
324     }
getMbcBandCount()325     uint32_t getMbcBandCount() const {
326         return mMbcBandCount;
327     }
getPostEqBandCount()328     uint32_t getPostEqBandCount() const {
329         return mPostEqBandCount;
330     }
isPreEQInUse()331     bool isPreEQInUse() const {
332         return mPreEqInUse;
333     }
isMbcInUse()334     bool isMbcInUse() const {
335         return mMbcInUse;
336     }
isPostEqInUse()337     bool isPostEqInUse() const {
338         return mPostEqInUse;
339     }
isLimiterInUse()340     bool isLimiterInUse() const {
341         return mLimiterInUse;
342     }
343 
344 private:
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