eq.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*************************************************************************/
  2. /* eq.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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: reduzio@gmail.com (C) 2006
  31. #include "eq.h"
  32. #include "core/error_macros.h"
  33. #include "core/math/math_funcs.h"
  34. #include <math.h>
  35. #define POW2(v) ((v) * (v))
  36. /* Helper */
  37. static int solve_quadratic(double a, double b, double c, double *r1, double *r2) {
  38. //solves quadractic and returns number of roots
  39. double base = 2 * a;
  40. if (base == 0.0f)
  41. return 0;
  42. double squared = b * b - 4 * a * c;
  43. if (squared < 0.0)
  44. return 0;
  45. squared = sqrt(squared);
  46. *r1 = (-b + squared) / base;
  47. *r2 = (-b - squared) / base;
  48. if (*r1 == *r2)
  49. return 1;
  50. else
  51. return 2;
  52. }
  53. EQ::BandProcess::BandProcess() {
  54. c1 = c2 = c3 = history.a1 = history.a2 = history.a3 = 0;
  55. history.b1 = history.b2 = history.b3 = 0;
  56. }
  57. void EQ::recalculate_band_coefficients() {
  58. #define BAND_LOG(m_f) (log((m_f)) / log(2.))
  59. for (int i = 0; i < band.size(); i++) {
  60. double octave_size;
  61. double frq = band[i].freq;
  62. if (i == 0) {
  63. octave_size = BAND_LOG(band[1].freq) - BAND_LOG(frq);
  64. } else if (i == (band.size() - 1)) {
  65. octave_size = BAND_LOG(frq) - BAND_LOG(band[i - 1].freq);
  66. } else {
  67. double next = BAND_LOG(band[i + 1].freq) - BAND_LOG(frq);
  68. double prev = BAND_LOG(frq) - BAND_LOG(band[i - 1].freq);
  69. octave_size = (next + prev) / 2.0;
  70. }
  71. double frq_l = round(frq / pow(2.0, octave_size / 2.0));
  72. double side_gain2 = POW2(Math_SQRT12);
  73. double th = 2.0 * Math_PI * frq / mix_rate;
  74. double th_l = 2.0 * Math_PI * frq_l / mix_rate;
  75. double c2a = side_gain2 * POW2(cos(th)) - 2.0 * side_gain2 * cos(th_l) * cos(th) + side_gain2 - POW2(sin(th_l));
  76. double c2b = 2.0 * side_gain2 * POW2(cos(th_l)) + side_gain2 * POW2(cos(th)) - 2.0 * side_gain2 * cos(th_l) * cos(th) - side_gain2 + POW2(sin(th_l));
  77. double c2c = 0.25 * side_gain2 * POW2(cos(th)) - 0.5 * side_gain2 * cos(th_l) * cos(th) + 0.25 * side_gain2 - 0.25 * POW2(sin(th_l));
  78. //printf("band %i, precoefs = %f,%f,%f\n",i,c2a,c2b,c2c);
  79. // Default initializing to silence compiler warning about potential uninitialized use.
  80. // Both variables are properly set in _solve_quadratic before use, or we continue if roots == 0.
  81. double r1 = 0, r2 = 0; //roots
  82. int roots = solve_quadratic(c2a, c2b, c2c, &r1, &r2);
  83. ERR_CONTINUE(roots == 0);
  84. band.write[i].c1 = 2.0 * ((0.5 - r1) / 2.0);
  85. band.write[i].c2 = 2.0 * r1;
  86. band.write[i].c3 = 2.0 * (0.5 + r1) * cos(th);
  87. //printf("band %i, coefs = %f,%f,%f\n",i,(float)bands[i].c1,(float)bands[i].c2,(float)bands[i].c3);
  88. }
  89. }
  90. void EQ::set_preset_band_mode(Preset p_preset) {
  91. band.clear();
  92. #define PUSH_BANDS(m_bands) \
  93. for (int i = 0; i < m_bands; i++) { \
  94. Band b; \
  95. b.freq = bands[i]; \
  96. b.c1 = b.c2 = b.c3 = 0; \
  97. band.push_back(b); \
  98. }
  99. switch (p_preset) {
  100. case PRESET_6_BANDS: {
  101. static const double bands[] = { 32, 100, 320, 1e3, 3200, 10e3 };
  102. PUSH_BANDS(6);
  103. } break;
  104. case PRESET_8_BANDS: {
  105. static const double bands[] = { 32, 72, 192, 512, 1200, 3000, 7500, 16e3 };
  106. PUSH_BANDS(8);
  107. } break;
  108. case PRESET_10_BANDS: {
  109. static const double bands[] = { 31.25, 62.5, 125, 250, 500, 1e3, 2e3, 4e3, 8e3, 16e3 };
  110. PUSH_BANDS(10);
  111. } break;
  112. case PRESET_21_BANDS: {
  113. static const double bands[] = { 22, 32, 44, 63, 90, 125, 175, 250, 350, 500, 700, 1e3, 1400, 2e3, 2800, 4e3, 5600, 8e3, 11e3, 16e3, 22e3 };
  114. PUSH_BANDS(21);
  115. } break;
  116. case PRESET_31_BANDS: {
  117. static const double bands[] = { 20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1e3, 1250, 1600, 2e3, 2500, 3150, 4e3, 5e3, 6300, 8e3, 10e3, 12500, 16e3, 20e3 };
  118. PUSH_BANDS(31);
  119. } break;
  120. };
  121. recalculate_band_coefficients();
  122. }
  123. int EQ::get_band_count() const {
  124. return band.size();
  125. }
  126. float EQ::get_band_frequency(int p_band) {
  127. ERR_FAIL_INDEX_V(p_band, band.size(), 0);
  128. return band[p_band].freq;
  129. }
  130. void EQ::set_bands(const Vector<float> &p_bands) {
  131. band.resize(p_bands.size());
  132. for (int i = 0; i < p_bands.size(); i++) {
  133. band.write[i].freq = p_bands[i];
  134. }
  135. recalculate_band_coefficients();
  136. }
  137. void EQ::set_mix_rate(float p_mix_rate) {
  138. mix_rate = p_mix_rate;
  139. recalculate_band_coefficients();
  140. }
  141. EQ::BandProcess EQ::get_band_processor(int p_band) const {
  142. EQ::BandProcess band_proc;
  143. ERR_FAIL_INDEX_V(p_band, band.size(), band_proc);
  144. band_proc.c1 = band[p_band].c1;
  145. band_proc.c2 = band[p_band].c2;
  146. band_proc.c3 = band[p_band].c3;
  147. return band_proc;
  148. }
  149. EQ::EQ() {
  150. mix_rate = 44100;
  151. }
  152. EQ::~EQ() {
  153. }