BiquadCombo.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #include <vector>
  3. #include <memory>
  4. #include <cmath>
  5. #include <mutex>
  6. #include <map>
  7. #include "Biquad.h"
  8. #include "AudioTransform.h"
  9. namespace bell
  10. {
  11. class BiquadCombo : public bell::AudioTransform
  12. {
  13. private:
  14. std::vector<std::unique_ptr<bell::Biquad>> biquads;
  15. // Calculates Q values for Nth order Butterworth / Linkwitz-Riley filters
  16. std::vector<float> calculateBWQ(int order);
  17. std::vector<float> calculateLRQ(int order);
  18. public:
  19. BiquadCombo();
  20. ~BiquadCombo(){};
  21. int channel;
  22. std::map<std::string, float> paramCache = {
  23. {"order", 0.0f},
  24. {"frequency", 0.0f}
  25. };
  26. enum class FilterType
  27. {
  28. Highpass,
  29. Lowpass
  30. };
  31. void linkwitzRiley(float freq, int order, FilterType type);
  32. void butterworth(float freq, int order, FilterType type);
  33. std::unique_ptr<StreamInfo> process(std::unique_ptr<StreamInfo> data) override;
  34. void sampleRateChanged(uint32_t sampleRate) override;
  35. void reconfigure() override
  36. {
  37. std::scoped_lock lock(this->accessMutex);
  38. float freq = config->getFloat("frequency");
  39. int order = config->getInt("order");
  40. if (paramCache["frequency"] == freq && paramCache["order"] == order)
  41. {
  42. return;
  43. } else {
  44. paramCache["frequency"] = freq;
  45. paramCache["order"] = order;
  46. }
  47. this->channel = config->getChannels()[0];
  48. this->biquads = std::vector<std::unique_ptr<bell::Biquad>>();
  49. auto type = config->getString("combo_type");
  50. if (type == "lr_lowpass")
  51. {
  52. this->linkwitzRiley(freq, order, FilterType::Lowpass);
  53. }
  54. else if (type == "lr_highpass")
  55. {
  56. this->linkwitzRiley(freq, order, FilterType::Highpass);
  57. }
  58. else if (type == "bw_highpass")
  59. {
  60. this->butterworth(freq, order, FilterType::Highpass);
  61. }
  62. else if (type == "bw_lowpass")
  63. {
  64. this->butterworth(freq, order, FilterType::Highpass);
  65. }
  66. else
  67. {
  68. throw std::invalid_argument("Invalid combo filter type");
  69. }
  70. }
  71. };
  72. };