1
0

EqBand.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "EqBand.h"
  2. #include <assert.h>
  3. #include <math.h>
  4. EqBand::EqBand() : sampleRate(44100), centerFrequency(1000), gain(1), _q(0.5), nch(0), channels(0), bypass(true)
  5. {
  6. }
  7. void EqBand::set_num_channels(int num_channels)
  8. {
  9. if (nch < num_channels)
  10. {
  11. nch = num_channels;
  12. delete[]channels;
  13. channels = new Biquad[nch];
  14. clear_buffers();
  15. set_parameters(centerFrequency, gain, _q);
  16. }
  17. }
  18. void EqBand::SetSampleRate(double sample_freq)
  19. {
  20. if (sample_freq != sampleRate)
  21. {
  22. sampleRate = sample_freq;
  23. for (int chn = 0; chn < nch; ++chn)
  24. {
  25. channels[chn].SetSampleRate(sampleRate);
  26. }
  27. clear_buffers();
  28. set_parameters(centerFrequency, gain, _q);
  29. }
  30. }
  31. void EqBand::set_parameters(double freq, double newGain, double q)
  32. {
  33. centerFrequency = freq;
  34. gain = newGain;
  35. _q = q;
  36. if (nch > 0)
  37. {
  38. Biquad & ref_filter = channels[0];
  39. ref_filter.set_freq(centerFrequency);
  40. double a[3] = { 1, 1/_q, 1 };
  41. double b[3] = {1, gain / _q, 1};
  42. ref_filter.set_s_eq(b, a);
  43. ref_filter.transform_s_to_z();
  44. for (int chn = 1; chn < nch; ++chn)
  45. channels[chn].copy_filter(ref_filter);
  46. }
  47. bypass = (fabs(gain - 1.0) < 0.02); // About 1/4 dB
  48. }
  49. void EqBand::process(float ** const out, float ** in, long nbr_spl, int nbr_chn)
  50. {
  51. assert(nbr_chn >= 0);
  52. assert(nbr_chn <= nch);
  53. if (!bypass)
  54. {
  55. for (int chn = 0; chn < nbr_chn; ++chn)
  56. {
  57. channels[chn].process_block(out[chn], in[chn], nbr_spl);
  58. }
  59. }
  60. }
  61. void EqBand::clear_buffers()
  62. {
  63. for (int chn = 0; chn < nch; ++chn)
  64. {
  65. channels[chn].clear_buffers();
  66. }
  67. }