bs2b.c 5.4 KB

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