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