🛠️🐜 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.

188 lines
5.7 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 <cmath>
  25. #include <cstring>
  26. #include <algorithm>
  27. #include "bs2b.h"
  28. #include "math_defs.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::MathDefs<float>::Tau() * Fc_lo / bs2b->srate);
  81. bs2b->b1_lo = x;
  82. bs2b->a0_lo = G_lo * (1.0f - x) * g;
  83. x = std::exp(-al::MathDefs<float>::Tau() * Fc_hi / 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->last_sample), std::end(bs2b->last_sample), bs2b::t_last_sample{});
  109. } /* bs2b_clear */
  110. void bs2b_cross_feed(struct bs2b *bs2b, float *RESTRICT Left, float *RESTRICT Right, int SamplesToDo)
  111. {
  112. float lsamples[128][2];
  113. float rsamples[128][2];
  114. int base;
  115. for(base = 0;base < SamplesToDo;)
  116. {
  117. int todo = std::min(128, SamplesToDo-base);
  118. int i;
  119. /* Process left input */
  120. lsamples[0][0] = bs2b->a0_lo*Left[0] +
  121. bs2b->b1_lo*bs2b->last_sample[0].lo;
  122. lsamples[0][1] = bs2b->a0_hi*Left[0] +
  123. bs2b->a1_hi*bs2b->last_sample[0].asis +
  124. bs2b->b1_hi*bs2b->last_sample[0].hi;
  125. for(i = 1;i < todo;i++)
  126. {
  127. lsamples[i][0] = bs2b->a0_lo*Left[i] +
  128. bs2b->b1_lo*lsamples[i-1][0];
  129. lsamples[i][1] = bs2b->a0_hi*Left[i] +
  130. bs2b->a1_hi*Left[i-1] +
  131. bs2b->b1_hi*lsamples[i-1][1];
  132. }
  133. bs2b->last_sample[0].asis = Left[i-1];
  134. bs2b->last_sample[0].lo = lsamples[i-1][0];
  135. bs2b->last_sample[0].hi = lsamples[i-1][1];
  136. /* Process right input */
  137. rsamples[0][0] = bs2b->a0_lo*Right[0] +
  138. bs2b->b1_lo*bs2b->last_sample[1].lo;
  139. rsamples[0][1] = bs2b->a0_hi*Right[0] +
  140. bs2b->a1_hi*bs2b->last_sample[1].asis +
  141. bs2b->b1_hi*bs2b->last_sample[1].hi;
  142. for(i = 1;i < todo;i++)
  143. {
  144. rsamples[i][0] = bs2b->a0_lo*Right[i] +
  145. bs2b->b1_lo*rsamples[i-1][0];
  146. rsamples[i][1] = bs2b->a0_hi*Right[i] +
  147. bs2b->a1_hi*Right[i-1] +
  148. bs2b->b1_hi*rsamples[i-1][1];
  149. }
  150. bs2b->last_sample[1].asis = Right[i-1];
  151. bs2b->last_sample[1].lo = rsamples[i-1][0];
  152. bs2b->last_sample[1].hi = rsamples[i-1][1];
  153. /* Crossfeed */
  154. for(i = 0;i < todo;i++)
  155. *(Left++) = lsamples[i][1] + rsamples[i][0];
  156. for(i = 0;i < todo;i++)
  157. *(Right++) = rsamples[i][1] + lsamples[i][0];
  158. base += todo;
  159. }
  160. } /* bs2b_cross_feed */