BiquadCombo.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "BiquadCombo.h"
  2. using namespace bell;
  3. BiquadCombo::BiquadCombo()
  4. {
  5. }
  6. void BiquadCombo::sampleRateChanged(uint32_t sampleRate)
  7. {
  8. for (auto &biquad : biquads)
  9. {
  10. biquad->sampleRateChanged(sampleRate);
  11. }
  12. }
  13. std::vector<float> BiquadCombo::calculateBWQ(int order)
  14. {
  15. std::vector<float> qValues;
  16. for (int n = 0; n < order / 2; n++)
  17. {
  18. float q = 1.0f / (2.0f * sinf(M_PI / order * (((float)n) + 0.5)));
  19. qValues.push_back(q);
  20. }
  21. if (order % 2 > 0)
  22. {
  23. qValues.push_back(-1.0);
  24. }
  25. printf("%d\n", qValues.size());
  26. return qValues;
  27. }
  28. std::vector<float> BiquadCombo::calculateLRQ(int order)
  29. {
  30. auto qValues = calculateBWQ(order / 2);
  31. if (order % 4 > 0)
  32. {
  33. qValues.pop_back();
  34. qValues.insert(qValues.end(), qValues.begin(), qValues.end());
  35. qValues.push_back(0.5);
  36. }
  37. else
  38. {
  39. qValues.insert(qValues.end(), qValues.begin(), qValues.end());
  40. }
  41. return qValues;
  42. }
  43. void BiquadCombo::butterworth(float freq, int order, FilterType type)
  44. {
  45. std::vector<float> qValues = calculateBWQ(order);
  46. for (auto &q : qValues)
  47. {
  48. }
  49. }
  50. void BiquadCombo::linkwitzRiley(float freq, int order, FilterType type)
  51. {
  52. std::vector<float> qValues = calculateLRQ(order);
  53. for (auto &q : qValues)
  54. {
  55. auto filter = std::make_unique<Biquad>();
  56. filter->channel = channel;
  57. auto config = std::map<std::string, float>();
  58. config["freq"] = freq;
  59. config["q"] = q;
  60. if (q >= 0.0)
  61. {
  62. if (type == FilterType::Highpass)
  63. {
  64. filter->configure(Biquad::Type::Highpass, config);
  65. }
  66. else
  67. {
  68. filter->configure(Biquad::Type::Lowpass, config);
  69. }
  70. }
  71. else
  72. {
  73. if (type == FilterType::Highpass)
  74. {
  75. filter->configure(Biquad::Type::HighpassFO, config);
  76. }
  77. else
  78. {
  79. filter->configure(Biquad::Type::LowpassFO, config);
  80. }
  81. }
  82. this->biquads.push_back(std::move(filter));
  83. }
  84. }
  85. std::unique_ptr<StreamInfo> BiquadCombo::process(std::unique_ptr<StreamInfo> data) {
  86. std::scoped_lock lock(this->accessMutex);
  87. for (auto &transform : this->biquads) {
  88. data = transform->process(std::move(data));
  89. }
  90. return data;
  91. }