DPBase.h 8.7 KB

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