Biquad.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #pragma once
  2. #include <cmath>
  3. #include <mutex>
  4. #include <map>
  5. #include <unordered_map>
  6. #include "AudioTransform.h"
  7. extern "C" int dsps_biquad_f32_ae32(const float *input, float *output, int len, float *coef, float *w);
  8. namespace bell
  9. {
  10. class Biquad : public bell::AudioTransform
  11. {
  12. public:
  13. Biquad();
  14. ~Biquad(){};
  15. enum class Type
  16. {
  17. Free,
  18. Highpass,
  19. Lowpass,
  20. HighpassFO,
  21. LowpassFO,
  22. Peaking,
  23. Highshelf,
  24. HighshelfFO,
  25. Lowshelf,
  26. LowshelfFO,
  27. Notch,
  28. Bandpass,
  29. Allpass,
  30. AllpassFO
  31. };
  32. std::map<std::string, float> currentConfig;
  33. std::unordered_map<std::string, Type> const strMapType = {
  34. {"free", Type::Free},
  35. {"highpass", Type::Highpass},
  36. {"lowpass", Type::Lowpass},
  37. {"highpass_fo", Type::HighpassFO},
  38. {"lowpass_fo", Type::LowpassFO},
  39. {"peaking", Type::Peaking},
  40. {"highshelf", Type::Highshelf},
  41. {"highshelf_fo", Type::HighpassFO},
  42. {"lowshelf", Type::Lowshelf},
  43. {"lowshelf_fo", Type::LowpassFO},
  44. {"notch", Type::Notch},
  45. {"bandpass", Type::Bandpass},
  46. {"allpass", Type::Allpass},
  47. {"allpass_fo", Type::AllpassFO},
  48. };
  49. float freq, q, gain;
  50. int channel;
  51. Biquad::Type type;
  52. std::unique_ptr<StreamInfo> process(std::unique_ptr<StreamInfo> data) override;
  53. void configure(Type type, std::map<std::string, float> &config);
  54. void sampleRateChanged(uint32_t sampleRate) override;
  55. void reconfigure() override
  56. {
  57. std::scoped_lock lock(this->accessMutex);
  58. std::map<std::string, float> biquadConfig;
  59. this->channel = config->getChannels()[0];
  60. float invalid = -0x7C;
  61. auto type = config->getString("biquad_type");
  62. float bandwidth = config->getFloat("bandwidth", false, invalid);
  63. float slope = config->getFloat("slope", false, invalid);
  64. float gain = config->getFloat("gain", false, invalid);
  65. float frequency = config->getFloat("frequency", false, invalid);
  66. float q = config->getFloat("q", false, invalid);
  67. if (currentConfig["bandwidth"] == bandwidth &&
  68. currentConfig["slope"] == slope &&
  69. currentConfig["gain"] == gain &&
  70. currentConfig["frequency"] == frequency &&
  71. currentConfig["q"] == q)
  72. {
  73. return;
  74. }
  75. if (bandwidth != invalid)
  76. biquadConfig["bandwidth"] = bandwidth;
  77. if (slope != invalid)
  78. biquadConfig["slope"] = slope;
  79. if (gain != invalid)
  80. biquadConfig["gain"] = gain;
  81. if (frequency != invalid)
  82. biquadConfig["freq"] = frequency;
  83. if (q != invalid)
  84. biquadConfig["q"] = q;
  85. if (type == "free")
  86. {
  87. biquadConfig["a1"] = config->getFloat("a1");
  88. biquadConfig["a2"] = config->getFloat("a2");
  89. biquadConfig["b0"] = config->getFloat("b0");
  90. biquadConfig["b1"] = config->getFloat("b1");
  91. biquadConfig["b2"] = config->getFloat("b2");
  92. }
  93. auto typeElement = strMapType.find(type);
  94. if (typeElement != strMapType.end())
  95. {
  96. this->configure(typeElement->second, biquadConfig);
  97. }
  98. else
  99. {
  100. throw std::invalid_argument("No biquad of type " + type);
  101. }
  102. }
  103. private:
  104. float coeffs[5];
  105. float w[2] = {1.0, 1.0};
  106. float sampleRate = 44100;
  107. // Generator methods for different filter types
  108. void highPassCoEffs(float f, float q);
  109. void highPassFOCoEffs(float f);
  110. void lowPassCoEffs(float f, float q);
  111. void lowPassFOCoEffs(float f);
  112. void peakCoEffs(float f, float gain, float q);
  113. void peakCoEffsBandwidth(float f, float gain, float bandwidth);
  114. void highShelfCoEffs(float f, float gain, float q);
  115. void highShelfCoEffsSlope(float f, float gain, float slope);
  116. void highShelfFOCoEffs(float f, float gain);
  117. void lowShelfCoEffs(float f, float gain, float q);
  118. void lowShelfCoEffsSlope(float f, float gain, float slope);
  119. void lowShelfFOCoEffs(float f, float gain);
  120. void notchCoEffs(float f, float gain, float q);
  121. void notchCoEffsBandwidth(float f, float gain, float bandwidth);
  122. void bandPassCoEffs(float f, float q);
  123. void bandPassCoEffsBandwidth(float f, float bandwidth);
  124. void allPassCoEffs(float f, float q);
  125. void allPassCoEffsBandwidth(float f, float bandwidth);
  126. void allPassFOCoEffs(float f);
  127. void normalizeCoEffs(float a0, float a1, float a2, float b0, float b1, float b2);
  128. };
  129. }