🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

183 lines
5.2 KiB

  1. /*-
  2. * Copyright (c) 2005 Boris Mikhaylov
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #include <algorithm>
  25. #include <cmath>
  26. #include <iterator>
  27. #include "alnumbers.h"
  28. #include "bs2b.h"
  29. /* Set up all data. */
  30. static void init(struct bs2b *bs2b)
  31. {
  32. float Fc_lo, Fc_hi;
  33. float G_lo, G_hi;
  34. float x, g;
  35. switch(bs2b->level)
  36. {
  37. case BS2B_LOW_CLEVEL: /* Low crossfeed level */
  38. Fc_lo = 360.0f;
  39. Fc_hi = 501.0f;
  40. G_lo = 0.398107170553497f;
  41. G_hi = 0.205671765275719f;
  42. break;
  43. case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */
  44. Fc_lo = 500.0f;
  45. Fc_hi = 711.0f;
  46. G_lo = 0.459726988530872f;
  47. G_hi = 0.228208484414988f;
  48. break;
  49. case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */
  50. Fc_lo = 700.0f;
  51. Fc_hi = 1021.0f;
  52. G_lo = 0.530884444230988f;
  53. G_hi = 0.250105790667544f;
  54. break;
  55. case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */
  56. Fc_lo = 360.0f;
  57. Fc_hi = 494.0f;
  58. G_lo = 0.316227766016838f;
  59. G_hi = 0.168236228897329f;
  60. break;
  61. case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */
  62. Fc_lo = 500.0f;
  63. Fc_hi = 689.0f;
  64. G_lo = 0.354813389233575f;
  65. G_hi = 0.187169483835901f;
  66. break;
  67. default: /* High easy crossfeed level */
  68. bs2b->level = BS2B_HIGH_ECLEVEL;
  69. Fc_lo = 700.0f;
  70. Fc_hi = 975.0f;
  71. G_lo = 0.398107170553497f;
  72. G_hi = 0.205671765275719f;
  73. break;
  74. } /* switch */
  75. g = 1.0f / (1.0f - G_hi + G_lo);
  76. /* $fc = $Fc / $s;
  77. * $d = 1 / 2 / pi / $fc;
  78. * $x = exp(-1 / $d);
  79. */
  80. x = std::exp(-al::numbers::pi_v<float>*2.0f*Fc_lo/static_cast<float>(bs2b->srate));
  81. bs2b->b1_lo = x;
  82. bs2b->a0_lo = G_lo * (1.0f - x) * g;
  83. x = std::exp(-al::numbers::pi_v<float>*2.0f*Fc_hi/static_cast<float>(bs2b->srate));
  84. bs2b->b1_hi = x;
  85. bs2b->a0_hi = (1.0f - G_hi * (1.0f - x)) * g;
  86. bs2b->a1_hi = -x * g;
  87. } /* init */
  88. /* Exported functions.
  89. * See descriptions in "bs2b.h"
  90. */
  91. void bs2b_set_params(struct bs2b *bs2b, int level, int srate)
  92. {
  93. if(srate <= 0) srate = 1;
  94. bs2b->level = level;
  95. bs2b->srate = srate;
  96. init(bs2b);
  97. } /* bs2b_set_params */
  98. int bs2b_get_level(struct bs2b *bs2b)
  99. {
  100. return bs2b->level;
  101. } /* bs2b_get_level */
  102. int bs2b_get_srate(struct bs2b *bs2b)
  103. {
  104. return bs2b->srate;
  105. } /* bs2b_get_srate */
  106. void bs2b_clear(struct bs2b *bs2b)
  107. {
  108. std::fill(std::begin(bs2b->history), std::end(bs2b->history), bs2b::t_last_sample{});
  109. } /* bs2b_clear */
  110. void bs2b_cross_feed(struct bs2b *bs2b, float *Left, float *Right, size_t SamplesToDo)
  111. {
  112. const float a0_lo{bs2b->a0_lo};
  113. const float b1_lo{bs2b->b1_lo};
  114. const float a0_hi{bs2b->a0_hi};
  115. const float a1_hi{bs2b->a1_hi};
  116. const float b1_hi{bs2b->b1_hi};
  117. float lsamples[128][2];
  118. float rsamples[128][2];
  119. for(size_t base{0};base < SamplesToDo;)
  120. {
  121. const size_t todo{std::min<size_t>(128, SamplesToDo-base)};
  122. /* Process left input */
  123. float z_lo{bs2b->history[0].lo};
  124. float z_hi{bs2b->history[0].hi};
  125. for(size_t i{0};i < todo;i++)
  126. {
  127. lsamples[i][0] = a0_lo*Left[i] + z_lo;
  128. z_lo = b1_lo*lsamples[i][0];
  129. lsamples[i][1] = a0_hi*Left[i] + z_hi;
  130. z_hi = a1_hi*Left[i] + b1_hi*lsamples[i][1];
  131. }
  132. bs2b->history[0].lo = z_lo;
  133. bs2b->history[0].hi = z_hi;
  134. /* Process right input */
  135. z_lo = bs2b->history[1].lo;
  136. z_hi = bs2b->history[1].hi;
  137. for(size_t i{0};i < todo;i++)
  138. {
  139. rsamples[i][0] = a0_lo*Right[i] + z_lo;
  140. z_lo = b1_lo*rsamples[i][0];
  141. rsamples[i][1] = a0_hi*Right[i] + z_hi;
  142. z_hi = a1_hi*Right[i] + b1_hi*rsamples[i][1];
  143. }
  144. bs2b->history[1].lo = z_lo;
  145. bs2b->history[1].hi = z_hi;
  146. /* Crossfeed */
  147. for(size_t i{0};i < todo;i++)
  148. *(Left++) = lsamples[i][1] + rsamples[i][0];
  149. for(size_t i{0};i < todo;i++)
  150. *(Right++) = rsamples[i][1] + lsamples[i][0];
  151. base += todo;
  152. }
  153. } /* bs2b_cross_feed */