bs2b.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifndef M_PI
  28. #define M_PI 3.14159265358979323846
  29. #endif
  30. /* Single pole IIR filter.
  31. * O[n] = a0*I[n] + a1*I[n-1] + b1*O[n-1]
  32. */
  33. /* Lowpass filter */
  34. #define lo_filter(in, out_1) (bs2b->a0_lo*(in) + bs2b->b1_lo*(out_1))
  35. /* Highboost filter */
  36. #define hi_filter(in, in_1, out_1) (bs2b->a0_hi*(in) + bs2b->a1_hi*(in_1) + bs2b->b1_hi*(out_1))
  37. /* Set up all data. */
  38. static void init(struct bs2b *bs2b)
  39. {
  40. double Fc_lo, Fc_hi;
  41. double G_lo, G_hi;
  42. double x;
  43. if ((bs2b->srate > 192000) || (bs2b->srate < 2000))
  44. bs2b->srate = BS2B_DEFAULT_SRATE;
  45. switch(bs2b->level)
  46. {
  47. case BS2B_LOW_CLEVEL: /* Low crossfeed level */
  48. Fc_lo = 360.0;
  49. Fc_hi = 501.0;
  50. G_lo = 0.398107170553497;
  51. G_hi = 0.205671765275719;
  52. break;
  53. case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */
  54. Fc_lo = 500.0;
  55. Fc_hi = 711.0;
  56. G_lo = 0.459726988530872;
  57. G_hi = 0.228208484414988;
  58. break;
  59. case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */
  60. Fc_lo = 700.0;
  61. Fc_hi = 1021.0;
  62. G_lo = 0.530884444230988;
  63. G_hi = 0.250105790667544;
  64. break;
  65. case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */
  66. Fc_lo = 360.0;
  67. Fc_hi = 494.0;
  68. G_lo = 0.316227766016838;
  69. G_hi = 0.168236228897329;
  70. break;
  71. case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */
  72. Fc_lo = 500.0;
  73. Fc_hi = 689.0;
  74. G_lo = 0.354813389233575;
  75. G_hi = 0.187169483835901;
  76. break;
  77. default: /* High easy crossfeed level */
  78. bs2b->level = BS2B_HIGH_ECLEVEL;
  79. Fc_lo = 700.0;
  80. Fc_hi = 975.0;
  81. G_lo = 0.398107170553497;
  82. G_hi = 0.205671765275719;
  83. break;
  84. } /* switch */
  85. /* $fc = $Fc / $s;
  86. * $d = 1 / 2 / pi / $fc;
  87. * $x = exp(-1 / $d);
  88. */
  89. x = exp(-2.0 * M_PI * Fc_lo / bs2b->srate);
  90. bs2b->b1_lo = x;
  91. bs2b->a0_lo = G_lo * (1.0 - x);
  92. x = exp(-2.0 * M_PI * Fc_hi / bs2b->srate);
  93. bs2b->b1_hi = x;
  94. bs2b->a0_hi = 1.0 - G_hi * (1.0 - x);
  95. bs2b->a1_hi = -x;
  96. bs2b->gain = 1.0f / (float)(1.0 - G_hi + G_lo);
  97. } /* init */
  98. /* Exported functions.
  99. * See descriptions in "bs2b.h"
  100. */
  101. void bs2b_set_level(struct bs2b *bs2b, int level)
  102. {
  103. if(level == bs2b->level)
  104. return;
  105. bs2b->level = level;
  106. init(bs2b);
  107. } /* bs2b_set_level */
  108. int bs2b_get_level(struct bs2b *bs2b)
  109. {
  110. return bs2b->level;
  111. } /* bs2b_get_level */
  112. void bs2b_set_srate(struct bs2b *bs2b, int srate)
  113. {
  114. if (srate == bs2b->srate)
  115. return;
  116. bs2b->srate = srate;
  117. init(bs2b);
  118. } /* bs2b_set_srate */
  119. int bs2b_get_srate(struct bs2b *bs2b)
  120. {
  121. return bs2b->srate;
  122. } /* bs2b_get_srate */
  123. void bs2b_clear(struct bs2b *bs2b)
  124. {
  125. memset(&bs2b->last_sample, 0, sizeof(bs2b->last_sample));
  126. } /* bs2b_clear */
  127. void bs2b_cross_feed(struct bs2b *bs2b, float *sample)
  128. {
  129. /* Lowpass filter */
  130. bs2b->last_sample.lo[0] = lo_filter(sample[0], bs2b->last_sample.lo[0]);
  131. bs2b->last_sample.lo[1] = lo_filter(sample[1], bs2b->last_sample.lo[1]);
  132. /* Highboost filter */
  133. bs2b->last_sample.hi[0] = hi_filter(sample[0], bs2b->last_sample.asis[0], bs2b->last_sample.hi[0]);
  134. bs2b->last_sample.hi[1] = hi_filter(sample[1], bs2b->last_sample.asis[1], bs2b->last_sample.hi[1]);
  135. bs2b->last_sample.asis[0] = sample[0];
  136. bs2b->last_sample.asis[1] = sample[1];
  137. /* Crossfeed */
  138. sample[0] = (float)(bs2b->last_sample.hi[0] + bs2b->last_sample.lo[1]);
  139. sample[1] = (float)(bs2b->last_sample.hi[1] + bs2b->last_sample.lo[0]);
  140. /* Bass boost cause allpass attenuation */
  141. sample[0] *= bs2b->gain;
  142. sample[1] *= bs2b->gain;
  143. /* Clipping of overloaded samples */
  144. #if 0
  145. if (sample[0] > 1.0)
  146. sample[0] = 1.0;
  147. if (sample[0] < -1.0)
  148. sample[0] = -1.0;
  149. if (sample[1] > 1.0)
  150. sample[1] = 1.0;
  151. if (sample[1] < -1.0)
  152. sample[1] = -1.0;
  153. #endif
  154. } /* bs2b_cross_feed */