audio_filter_sw.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*************************************************************************/
  2. /* audio_filter_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef AUDIO_FILTER_SW_H
  31. #define AUDIO_FILTER_SW_H
  32. #include "core/math/math_funcs.h"
  33. class AudioFilterSW {
  34. public:
  35. struct Coeffs {
  36. float a1, a2;
  37. float b0, b1, b2;
  38. //bool operator==(const Coeffs &p_rv) { return (FLOATS_EQ(a1,p_rv.a1) && FLOATS_EQ(a2,p_rv.a2) && FLOATS_EQ(b1,p_rv.b1) && FLOATS_EQ(b2,p_rv.b2) && FLOATS_EQ(b0,p_rv.b0) ); }
  39. Coeffs() { a1 = a2 = b0 = b1 = b2 = 0.0; }
  40. };
  41. enum Mode {
  42. BANDPASS,
  43. HIGHPASS,
  44. LOWPASS,
  45. NOTCH,
  46. PEAK,
  47. BANDLIMIT,
  48. LOWSHELF,
  49. HIGHSHELF
  50. };
  51. class Processor { // simple filter processor
  52. AudioFilterSW *filter;
  53. Coeffs coeffs;
  54. float ha1, ha2, hb1, hb2; //history
  55. Coeffs incr_coeffs;
  56. public:
  57. void set_filter(AudioFilterSW *p_filter, bool p_clear_history = true);
  58. void process(float *p_samples, int p_amount, int p_stride = 1, bool p_interpolate = false);
  59. void update_coeffs(int p_interp_buffer_len = 0);
  60. _ALWAYS_INLINE_ void process_one(float &p_sample);
  61. _ALWAYS_INLINE_ void process_one_interp(float &p_sample);
  62. Processor();
  63. };
  64. private:
  65. float cutoff;
  66. float resonance;
  67. float gain;
  68. float sampling_rate;
  69. int stages;
  70. Mode mode;
  71. public:
  72. float get_response(float p_freq, Coeffs *p_coeffs);
  73. void set_mode(Mode p_mode);
  74. void set_cutoff(float p_cutoff);
  75. void set_resonance(float p_resonance);
  76. void set_gain(float p_gain);
  77. void set_sampling_rate(float p_srate);
  78. void set_stages(int p_stages); //adjust for multiple stages
  79. void prepare_coefficients(Coeffs *p_coeffs);
  80. AudioFilterSW();
  81. };
  82. /* inline methods */
  83. void AudioFilterSW::Processor::process_one(float &p_sample) {
  84. float pre = p_sample;
  85. p_sample = (p_sample * coeffs.b0 + hb1 * coeffs.b1 + hb2 * coeffs.b2 + ha1 * coeffs.a1 + ha2 * coeffs.a2);
  86. ha2 = ha1;
  87. hb2 = hb1;
  88. hb1 = pre;
  89. ha1 = p_sample;
  90. }
  91. void AudioFilterSW::Processor::process_one_interp(float &p_sample) {
  92. float pre = p_sample;
  93. p_sample = (p_sample * coeffs.b0 + hb1 * coeffs.b1 + hb2 * coeffs.b2 + ha1 * coeffs.a1 + ha2 * coeffs.a2);
  94. ha2 = ha1;
  95. hb2 = hb1;
  96. hb1 = pre;
  97. ha1 = p_sample;
  98. coeffs.b0 += incr_coeffs.b0;
  99. coeffs.b1 += incr_coeffs.b1;
  100. coeffs.b2 += incr_coeffs.b2;
  101. coeffs.a1 += incr_coeffs.a1;
  102. coeffs.a2 += incr_coeffs.a2;
  103. }
  104. #endif // AUDIO_FILTER_SW_H