bs2b.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <stdexcept>
  28. #include "alnumbers.h"
  29. #include "alspan.h"
  30. #include "bs2b.h"
  31. namespace {
  32. /* Set up all data. */
  33. void init(Bs2b::bs2b *bs2b)
  34. {
  35. float Fc_lo, Fc_hi;
  36. float G_lo, G_hi;
  37. switch(bs2b->level)
  38. {
  39. case Bs2b::LowCLevel: /* Low crossfeed level */
  40. Fc_lo = 360.0f;
  41. Fc_hi = 501.0f;
  42. G_lo = 0.398107170553497f;
  43. G_hi = 0.205671765275719f;
  44. break;
  45. case Bs2b::MiddleCLevel: /* Middle crossfeed level */
  46. Fc_lo = 500.0f;
  47. Fc_hi = 711.0f;
  48. G_lo = 0.459726988530872f;
  49. G_hi = 0.228208484414988f;
  50. break;
  51. case Bs2b::HighCLevel: /* High crossfeed level (virtual speakers are closer to itself) */
  52. Fc_lo = 700.0f;
  53. Fc_hi = 1021.0f;
  54. G_lo = 0.530884444230988f;
  55. G_hi = 0.250105790667544f;
  56. break;
  57. case Bs2b::LowECLevel: /* Low easy crossfeed level */
  58. Fc_lo = 360.0f;
  59. Fc_hi = 494.0f;
  60. G_lo = 0.316227766016838f;
  61. G_hi = 0.168236228897329f;
  62. break;
  63. case Bs2b::MiddleECLevel: /* Middle easy crossfeed level */
  64. Fc_lo = 500.0f;
  65. Fc_hi = 689.0f;
  66. G_lo = 0.354813389233575f;
  67. G_hi = 0.187169483835901f;
  68. break;
  69. case Bs2b::HighECLevel: /* High easy crossfeed level */
  70. default:
  71. bs2b->level = Bs2b::HighECLevel;
  72. Fc_lo = 700.0f;
  73. Fc_hi = 975.0f;
  74. G_lo = 0.398107170553497f;
  75. G_hi = 0.205671765275719f;
  76. break;
  77. }
  78. float g{1.0f / (1.0f - G_hi + G_lo)};
  79. /* $fc = $Fc / $s;
  80. * $d = 1 / 2 / pi / $fc;
  81. * $x = exp(-1 / $d);
  82. */
  83. float x{ std::exp(-al::numbers::pi_v<float>*2.0f*Fc_lo/static_cast<float>(bs2b->srate))};
  84. bs2b->b1_lo = x;
  85. bs2b->a0_lo = G_lo * (1.0f - x) * g;
  86. x = std::exp(-al::numbers::pi_v<float>*2.0f*Fc_hi/static_cast<float>(bs2b->srate));
  87. bs2b->b1_hi = x;
  88. bs2b->a0_hi = (1.0f - G_hi * (1.0f - x)) * g;
  89. bs2b->a1_hi = -x * g;
  90. }
  91. } // namespace
  92. /* Exported functions.
  93. * See descriptions in "bs2b.h"
  94. */
  95. namespace Bs2b {
  96. void bs2b::set_params(int level_, int srate_)
  97. {
  98. if(srate_ < 1)
  99. throw std::runtime_error{"BS2B srate < 1"};
  100. level = level_;
  101. srate = srate_;
  102. init(this);
  103. }
  104. void bs2b::clear()
  105. {
  106. history.fill(bs2b::t_last_sample{});
  107. }
  108. void bs2b::cross_feed(const al::span<float> Left, const al::span<float> Right)
  109. {
  110. const auto a0lo = a0_lo;
  111. const auto b1lo = b1_lo;
  112. const auto a0hi = a0_hi;
  113. const auto a1hi = a1_hi;
  114. const auto b1hi = b1_hi;
  115. auto lsamples = Left.first(std::min(Left.size(), Right.size()));
  116. auto rsamples = Right.first(lsamples.size());
  117. auto samples = std::array<std::array<float,2>,128>{};
  118. auto leftio = lsamples.begin();
  119. auto rightio = rsamples.begin();
  120. while(auto rem = std::distance(leftio, lsamples.end()))
  121. {
  122. const auto todo = std::min<ptrdiff_t>(samples.size(), rem);
  123. /* Process left input */
  124. auto z_lo = history[0].lo;
  125. auto z_hi = history[0].hi;
  126. std::transform(leftio, leftio+todo, samples.begin(),
  127. [a0hi,a1hi,b1hi,a0lo,b1lo,&z_lo,&z_hi](const float x) noexcept
  128. {
  129. const auto y0 = a0hi*x + z_hi;
  130. z_hi = a1hi*x + b1hi*y0;
  131. const auto y1 = a0lo*x + z_lo;
  132. z_lo = b1lo*y1;
  133. return std::array{y0, y1};
  134. });
  135. history[0].lo = z_lo;
  136. history[0].hi = z_hi;
  137. /* Process right input */
  138. z_lo = history[1].lo;
  139. z_hi = history[1].hi;
  140. std::transform(rightio, rightio+todo, samples.cbegin(), samples.begin(),
  141. [a0hi,a1hi,b1hi,a0lo,b1lo,&z_lo,&z_hi](const float x, const std::array<float,2> &out) noexcept
  142. {
  143. const auto y0 = a0lo*x + z_lo;
  144. z_lo = b1lo*y0;
  145. const auto y1 = a0hi*x + z_hi;
  146. z_hi = a1hi*x + b1hi*y1;
  147. return std::array{out[0]+y0, out[1]+y1};
  148. });
  149. history[1].lo = z_lo;
  150. history[1].hi = z_hi;
  151. leftio = std::transform(samples.cbegin(), samples.cbegin()+todo, leftio,
  152. [](const std::array<float,2> &in) { return in[0]; });
  153. rightio = std::transform(samples.cbegin(), samples.cbegin()+todo, rightio,
  154. [](const std::array<float,2> &in) { return in[1]; });
  155. }
  156. }
  157. } // namespace Bs2b