Compressor.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include <math.h> // for expf
  3. #include <stdint.h> // for uint32_t
  4. #include <map> // for map
  5. #include <memory> // for unique_ptr
  6. #include <mutex> // for scoped_lock
  7. #include <string> // for string, operator<
  8. #include <vector> // for vector
  9. #include "AudioTransform.h" // for AudioTransform
  10. #include "StreamInfo.h" // for StreamInfo
  11. #include "TransformConfig.h" // for TransformConfig
  12. #define pow10f(x) expf(2.302585092994046f * x)
  13. // This is a fast approximation to log2()
  14. // Y = C[0]*F*F*F + C[1]*F*F + C[2]*F + C[3] + E;
  15. float log2f_approx(float X);
  16. #define log10f_fast(x) (log2f_approx(x) * 0.3010299956639812f)
  17. namespace bell {
  18. class Compressor : public bell::AudioTransform {
  19. private:
  20. std::vector<int> channels;
  21. std::vector<float> tmp;
  22. std::map<std::string, float> paramCache;
  23. float attack;
  24. float release;
  25. float threshold;
  26. float factor;
  27. float clipLimit;
  28. float makeupGain;
  29. float lastLoudness = -100.0f;
  30. float sampleRate = 44100;
  31. public:
  32. Compressor();
  33. ~Compressor(){};
  34. void configure(std::vector<int> channels, float attack, float release,
  35. float threshold, float factor, float makeupGain);
  36. void sumChannels(std::unique_ptr<StreamInfo>& data);
  37. void calLoudness();
  38. void calGain();
  39. void applyGain(std::unique_ptr<StreamInfo>& data);
  40. void reconfigure() override {
  41. std::scoped_lock lock(this->accessMutex);
  42. auto newChannels = config->getChannels();
  43. float newAttack = config->getFloat("attack");
  44. float newRelease = config->getFloat("release");
  45. float newThreshold = config->getFloat("threshold");
  46. float newFactor = config->getFloat("factor");
  47. float newMakeupGain = config->getFloat("makeup_gain");
  48. if (paramCache["attack"] == newAttack &&
  49. paramCache["release"] == newRelease &&
  50. paramCache["threshold"] == newThreshold &&
  51. paramCache["factor"] == newFactor &&
  52. paramCache["makeup_gain"] == newMakeupGain) {
  53. return;
  54. } else {
  55. paramCache["attack"] = newAttack;
  56. paramCache["release"] = newRelease;
  57. paramCache["threshold"] = newThreshold;
  58. paramCache["factor"] = newFactor;
  59. paramCache["makeup_gain"] = newMakeupGain;
  60. }
  61. this->configure(newChannels, newAttack, newRelease, newThreshold, newFactor,
  62. newMakeupGain);
  63. }
  64. // void fromJSON(cJSON* json) override {
  65. // // get field channels
  66. // channels = jsonGetChannels(json);
  67. // float attack = jsonGetNumber<float>(json, "attack", false, 0);
  68. // float release = jsonGetNumber<float>(json, "release", false, 0);
  69. // float factor = jsonGetNumber<float>(json, "factor", false, 4);
  70. // float makeupGain = jsonGetNumber<float>(json, "makeup_gain", false, 0);
  71. // float threshold = jsonGetNumber<float>(json, "threshold", false, 0);
  72. // this->configure(attack, release, clipLimit, threshold, factor, makeupGain);
  73. // }
  74. std::unique_ptr<StreamInfo> process(
  75. std::unique_ptr<StreamInfo> data) override;
  76. void sampleRateChanged(uint32_t sampleRate) override {
  77. this->sampleRate = sampleRate;
  78. };
  79. };
  80. }; // namespace bell