eq.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*************************************************************************/
  2. /* eq.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. // Author: [email protected] (C) 2006
  31. #ifndef EQ_FILTER_H
  32. #define EQ_FILTER_H
  33. #include "core/typedefs.h"
  34. #include "core/vector.h"
  35. /**
  36. @author Juan Linietsky
  37. */
  38. class EQ {
  39. public:
  40. enum Preset {
  41. PRESET_6_BANDS,
  42. PRESET_8_BANDS,
  43. PRESET_10_BANDS,
  44. PRESET_21_BANDS,
  45. PRESET_31_BANDS
  46. };
  47. class BandProcess {
  48. friend class EQ;
  49. float c1, c2, c3;
  50. struct History {
  51. float a1, a2, a3;
  52. float b1, b2, b3;
  53. } history;
  54. public:
  55. inline void process_one(float &p_data);
  56. BandProcess();
  57. };
  58. private:
  59. struct Band {
  60. float freq;
  61. float c1, c2, c3;
  62. };
  63. Vector<Band> band;
  64. float mix_rate;
  65. void recalculate_band_coefficients();
  66. public:
  67. void set_mix_rate(float p_mix_rate);
  68. int get_band_count() const;
  69. void set_preset_band_mode(Preset p_preset);
  70. void set_bands(const Vector<float> &p_bands);
  71. BandProcess get_band_processor(int p_band) const;
  72. float get_band_frequency(int p_band);
  73. EQ();
  74. ~EQ();
  75. };
  76. /* Inline Function */
  77. inline void EQ::BandProcess::process_one(float &p_data) {
  78. history.a1 = p_data;
  79. history.b1 = c1 * (history.a1 - history.a3) + c3 * history.b2 - c2 * history.b3;
  80. p_data = history.b1;
  81. history.a3 = history.a2;
  82. history.a2 = history.a1;
  83. history.b3 = history.b2;
  84. history.b2 = history.b1;
  85. }
  86. #endif