BiquadCombo.cpp 2.2 KB

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