bs2b.c 5.6 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 <math.h>
  25. #include <string.h>
  26. #include "bs2b.h"
  27. #include "alu.h"
  28. /* Set up all data. */
  29. static void init(struct bs2b *bs2b)
  30. {
  31. float Fc_lo, Fc_hi;
  32. float G_lo, G_hi;
  33. float x, g;
  34. switch(bs2b->level)
  35. {
  36. case BS2B_LOW_CLEVEL: /* Low crossfeed level */
  37. Fc_lo = 360.0f;
  38. Fc_hi = 501.0f;
  39. G_lo = 0.398107170553497f;
  40. G_hi = 0.205671765275719f;
  41. break;
  42. case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */
  43. Fc_lo = 500.0f;
  44. Fc_hi = 711.0f;
  45. G_lo = 0.459726988530872f;
  46. G_hi = 0.228208484414988f;
  47. break;
  48. case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */
  49. Fc_lo = 700.0f;
  50. Fc_hi = 1021.0f;
  51. G_lo = 0.530884444230988f;
  52. G_hi = 0.250105790667544f;
  53. break;
  54. case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */
  55. Fc_lo = 360.0f;
  56. Fc_hi = 494.0f;
  57. G_lo = 0.316227766016838f;
  58. G_hi = 0.168236228897329f;
  59. break;
  60. case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */
  61. Fc_lo = 500.0f;
  62. Fc_hi = 689.0f;
  63. G_lo = 0.354813389233575f;
  64. G_hi = 0.187169483835901f;
  65. break;
  66. default: /* High easy crossfeed level */
  67. bs2b->level = BS2B_HIGH_ECLEVEL;
  68. Fc_lo = 700.0f;
  69. Fc_hi = 975.0f;
  70. G_lo = 0.398107170553497f;
  71. G_hi = 0.205671765275719f;
  72. break;
  73. } /* switch */
  74. g = 1.0f / (1.0f - G_hi + G_lo);
  75. /* $fc = $Fc / $s;
  76. * $d = 1 / 2 / pi / $fc;
  77. * $x = exp(-1 / $d);
  78. */
  79. x = expf(-2.0f * F_PI * Fc_lo / bs2b->srate);
  80. bs2b->b1_lo = x;
  81. bs2b->a0_lo = G_lo * (1.0f - x) * g;
  82. x = expf(-2.0f * F_PI * Fc_hi / bs2b->srate);
  83. bs2b->b1_hi = x;
  84. bs2b->a0_hi = (1.0f - G_hi * (1.0f - x)) * g;
  85. bs2b->a1_hi = -x * g;
  86. } /* init */
  87. /* Exported functions.
  88. * See descriptions in "bs2b.h"
  89. */
  90. void bs2b_set_params(struct bs2b *bs2b, int level, int srate)
  91. {
  92. if(srate <= 0) srate = 1;
  93. bs2b->level = level;
  94. bs2b->srate = srate;
  95. init(bs2b);
  96. } /* bs2b_set_params */
  97. int bs2b_get_level(struct bs2b *bs2b)
  98. {
  99. return bs2b->level;
  100. } /* bs2b_get_level */
  101. int bs2b_get_srate(struct bs2b *bs2b)
  102. {
  103. return bs2b->srate;
  104. } /* bs2b_get_srate */
  105. void bs2b_clear(struct bs2b *bs2b)
  106. {
  107. memset(&bs2b->last_sample, 0, sizeof(bs2b->last_sample));
  108. } /* bs2b_clear */
  109. void bs2b_cross_feed(struct bs2b *bs2b, float *restrict Left, float *restrict Right, unsigned int SamplesToDo)
  110. {
  111. float lsamples[128][2];
  112. float rsamples[128][2];
  113. unsigned int base;
  114. for(base = 0;base < SamplesToDo;)
  115. {
  116. unsigned int todo = minu(128, SamplesToDo-base);
  117. unsigned int i;
  118. /* Process left input */
  119. lsamples[0][0] = bs2b->a0_lo*Left[0] +
  120. bs2b->b1_lo*bs2b->last_sample[0].lo;
  121. lsamples[0][1] = bs2b->a0_hi*Left[0] +
  122. bs2b->a1_hi*bs2b->last_sample[0].asis +
  123. bs2b->b1_hi*bs2b->last_sample[0].hi;
  124. for(i = 1;i < todo;i++)
  125. {
  126. lsamples[i][0] = bs2b->a0_lo*Left[i] +
  127. bs2b->b1_lo*lsamples[i-1][0];
  128. lsamples[i][1] = bs2b->a0_hi*Left[i] +
  129. bs2b->a1_hi*Left[i-1] +
  130. bs2b->b1_hi*lsamples[i-1][1];
  131. }
  132. bs2b->last_sample[0].asis = Left[i-1];
  133. bs2b->last_sample[0].lo = lsamples[i-1][0];
  134. bs2b->last_sample[0].hi = lsamples[i-1][1];
  135. /* Process right input */
  136. rsamples[0][0] = bs2b->a0_lo*Right[0] +
  137. bs2b->b1_lo*bs2b->last_sample[1].lo;
  138. rsamples[0][1] = bs2b->a0_hi*Right[0] +
  139. bs2b->a1_hi*bs2b->last_sample[1].asis +
  140. bs2b->b1_hi*bs2b->last_sample[1].hi;
  141. for(i = 1;i < todo;i++)
  142. {
  143. rsamples[i][0] = bs2b->a0_lo*Right[i] +
  144. bs2b->b1_lo*rsamples[i-1][0];
  145. rsamples[i][1] = bs2b->a0_hi*Right[i] +
  146. bs2b->a1_hi*Right[i-1] +
  147. bs2b->b1_hi*rsamples[i-1][1];
  148. }
  149. bs2b->last_sample[1].asis = Right[i-1];
  150. bs2b->last_sample[1].lo = rsamples[i-1][0];
  151. bs2b->last_sample[1].hi = rsamples[i-1][1];
  152. /* Crossfeed */
  153. for(i = 0;i < todo;i++)
  154. *(Left++) = lsamples[i][1] + rsamples[i][0];
  155. for(i = 0;i < todo;i++)
  156. *(Right++) = rsamples[i][1] + lsamples[i][0];
  157. base += todo;
  158. }
  159. } /* bs2b_cross_feed */