You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 satır
4.4KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef filter_biquad_h_
  27. #define filter_biquad_h_
  28. #include "AudioStream.h"
  29. class AudioFilterBiquad : public AudioStream
  30. {
  31. public:
  32. AudioFilterBiquad(void) : AudioStream(1, inputQueueArray) {
  33. // by default, the filter will not pass anything
  34. for (int i=0; i<32; i++) definition[i] = 0;
  35. }
  36. virtual void update(void);
  37. // Set the biquad coefficients directly
  38. void setCoefficients(uint32_t stage, const int *coefficients);
  39. void setCoefficients(uint32_t stage, const double *coefficients) {
  40. int coef[5];
  41. coef[0] = coefficients[0] * 1073741824.0;
  42. coef[1] = coefficients[1] * 1073741824.0;
  43. coef[2] = coefficients[2] * 1073741824.0;
  44. coef[3] = coefficients[3] * 1073741824.0;
  45. coef[4] = coefficients[4] * 1073741824.0;
  46. setCoefficients(stage, coef);
  47. }
  48. // Compute common filter functions
  49. // http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  50. void setLowpass(uint32_t stage, float frequency, float q = 0.7071) {
  51. int coef[5];
  52. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  53. double sinW0 = sin(w0);
  54. double alpha = sinW0 / ((double)q * 2.0);
  55. double cosW0 = cos(w0);
  56. double scale = 1073741824.0 / (1.0 + alpha);
  57. /* b0 */ coef[0] = ((1.0 - cosW0) / 2.0) * scale;
  58. /* b1 */ coef[1] = (1.0 - cosW0) * scale;
  59. /* b2 */ coef[2] = coef[0];
  60. /* a1 */ coef[3] = (-2.0 * cosW0) * scale;
  61. /* a2 */ coef[4] = (1.0 - alpha) * scale;
  62. setCoefficients(stage, coef);
  63. }
  64. void setHighpass(uint32_t stage, float frequency, float q = 0.7071) {
  65. int coef[5];
  66. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  67. double sinW0 = sin(w0);
  68. double alpha = sinW0 / ((double)q * 2.0);
  69. double cosW0 = cos(w0);
  70. double scale = 1073741824.0 / (1.0 + alpha);
  71. /* b0 */ coef[0] = ((1.0 + cosW0) / 2.0) * scale;
  72. /* b1 */ coef[1] = -(1.0 + cosW0) * scale;
  73. /* b2 */ coef[2] = coef[0];
  74. /* a1 */ coef[3] = (-2.0 * cosW0) * scale;
  75. /* a2 */ coef[4] = (1.0 - alpha) * scale;
  76. setCoefficients(stage, coef);
  77. }
  78. void setBandpass(uint32_t stage, float frequency, float q = 1.0) {
  79. int coef[5];
  80. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  81. double sinW0 = sin(w0);
  82. double alpha = sinW0 / ((double)q * 2.0);
  83. double cosW0 = cos(w0);
  84. double scale = 1073741824.0 / (1.0 + alpha);
  85. /* b0 */ coef[0] = alpha * scale;
  86. /* b1 */ coef[1] = 0;
  87. /* b2 */ coef[2] = (-alpha) * scale;
  88. /* a1 */ coef[3] = (-2.0 * cosW0) * scale;
  89. /* a2 */ coef[4] = (1.0 - alpha) * scale;
  90. setCoefficients(stage, coef);
  91. }
  92. void setNotch(uint32_t stage, float frequency, float q = 1.0) {
  93. int coef[5];
  94. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  95. double sinW0 = sin(w0);
  96. double alpha = sinW0 / ((double)q * 2.0);
  97. double cosW0 = cos(w0);
  98. double scale = 1073741824.0 / (1.0 + alpha);
  99. /* b0 */ coef[0] = scale;
  100. /* b1 */ coef[1] = (-2.0 * cosW0) * scale;
  101. /* b2 */ coef[2] = coef[0];
  102. /* a1 */ coef[3] = (-2.0 * cosW0) * scale;
  103. /* a2 */ coef[4] = (1.0 - alpha) * scale;
  104. setCoefficients(stage, coef);
  105. }
  106. private:
  107. int32_t definition[32]; // up to 4 cascaded biquads
  108. audio_block_t *inputQueueArray[1];
  109. };
  110. #endif