bs2b.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(float *Left, float *Right, size_t SamplesToDo)
  109. {
  110. const float a0lo{a0_lo};
  111. const float b1lo{b1_lo};
  112. const float a0hi{a0_hi};
  113. const float a1hi{a1_hi};
  114. const float b1hi{b1_hi};
  115. std::array<std::array<float,2>,128> samples{};
  116. al::span<float> lsamples{Left, SamplesToDo};
  117. al::span<float> rsamples{Right, SamplesToDo};
  118. while(!lsamples.empty())
  119. {
  120. const size_t todo{std::min(samples.size(), lsamples.size())};
  121. /* Process left input */
  122. float z_lo{history[0].lo};
  123. float z_hi{history[0].hi};
  124. std::transform(lsamples.cbegin(), lsamples.cbegin()+ptrdiff_t(todo), samples.begin(),
  125. [a0hi,a1hi,b1hi,a0lo,b1lo,&z_lo,&z_hi](const float x) -> std::array<float,2>
  126. {
  127. float y0{a0hi*x + z_hi};
  128. z_hi = a1hi*x + b1hi*y0;
  129. float y1{a0lo*x + z_lo};
  130. z_lo = b1lo*y1;
  131. return {y0, y1};
  132. });
  133. history[0].lo = z_lo;
  134. history[0].hi = z_hi;
  135. /* Process right input */
  136. z_lo = history[1].lo;
  137. z_hi = history[1].hi;
  138. std::transform(rsamples.cbegin(), rsamples.cbegin()+ptrdiff_t(todo), samples.begin(),
  139. samples.begin(),
  140. [a0hi,a1hi,b1hi,a0lo,b1lo,&z_lo,&z_hi](const float x, const std::array<float,2> out) -> std::array<float,2>
  141. {
  142. float y0{a0lo*x + z_lo};
  143. z_lo = b1lo*y0;
  144. float y1{a0hi*x + z_hi};
  145. z_hi = a1hi*x + b1hi*y1;
  146. return {out[0]+y0, out[1]+y1};
  147. });
  148. history[1].lo = z_lo;
  149. history[1].hi = z_hi;
  150. auto iter = std::transform(samples.cbegin(), samples.cbegin()+todo, lsamples.begin(),
  151. [](const std::array<float,2> &in) { return in[0]; });
  152. lsamples = {iter, lsamples.end()};
  153. iter = std::transform(samples.cbegin(), samples.cbegin()+todo, rsamples.begin(),
  154. [](const std::array<float,2> &in) { return in[1]; });
  155. rsamples = {iter, rsamples.end()};
  156. }
  157. }
  158. } // namespace Bs2b