bs2b.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef BS2B_H
  24. #define BS2B_H
  25. /* Number of crossfeed levels */
  26. #define BS2B_CLEVELS 3
  27. /* Normal crossfeed levels */
  28. #define BS2B_HIGH_CLEVEL 3
  29. #define BS2B_MIDDLE_CLEVEL 2
  30. #define BS2B_LOW_CLEVEL 1
  31. /* Easy crossfeed levels */
  32. #define BS2B_HIGH_ECLEVEL BS2B_HIGH_CLEVEL + BS2B_CLEVELS
  33. #define BS2B_MIDDLE_ECLEVEL BS2B_MIDDLE_CLEVEL + BS2B_CLEVELS
  34. #define BS2B_LOW_ECLEVEL BS2B_LOW_CLEVEL + BS2B_CLEVELS
  35. /* Default crossfeed levels */
  36. #define BS2B_DEFAULT_CLEVEL BS2B_HIGH_ECLEVEL
  37. /* Default sample rate (Hz) */
  38. #define BS2B_DEFAULT_SRATE 44100
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif /* __cplusplus */
  42. struct bs2b {
  43. int level; /* Crossfeed level */
  44. int srate; /* Sample rate (Hz) */
  45. /* Lowpass IIR filter coefficients */
  46. float a0_lo;
  47. float b1_lo;
  48. /* Highboost IIR filter coefficients */
  49. float a0_hi;
  50. float a1_hi;
  51. float b1_hi;
  52. /* Buffer of last filtered sample.
  53. * [0] - first channel, [1] - second channel
  54. */
  55. struct t_last_sample {
  56. float asis[2];
  57. float lo[2];
  58. float hi[2];
  59. } last_sample;
  60. };
  61. /* Clear buffers and set new coefficients with new crossfeed level and sample
  62. * rate values.
  63. * level - crossfeed level of *LEVEL values.
  64. * srate - sample rate by Hz.
  65. */
  66. void bs2b_set_params(struct bs2b *bs2b, int level, int srate);
  67. /* Return current crossfeed level value */
  68. int bs2b_get_level(struct bs2b *bs2b);
  69. /* Return current sample rate value */
  70. int bs2b_get_srate(struct bs2b *bs2b);
  71. /* Clear buffer */
  72. void bs2b_clear(struct bs2b *bs2b);
  73. /* Crossfeeds one stereo sample that are pointed by sample.
  74. * [0] - first channel, [1] - second channel.
  75. * Returns crossfided sample by sample pointer.
  76. */
  77. inline void bs2b_cross_feed(struct bs2b *bs2b, float *restrict sample)
  78. {
  79. /* Single pole IIR filter.
  80. * O[n] = a0*I[n] + a1*I[n-1] + b1*O[n-1]
  81. */
  82. /* Lowpass filter */
  83. #define lo_filter(in, out_1) (bs2b->a0_lo*(in) + bs2b->b1_lo*(out_1))
  84. /* Highboost filter */
  85. #define hi_filter(in, in_1, out_1) (bs2b->a0_hi*(in) + bs2b->a1_hi*(in_1) + bs2b->b1_hi*(out_1))
  86. /* Lowpass filter */
  87. bs2b->last_sample.lo[0] = lo_filter(sample[0], bs2b->last_sample.lo[0]);
  88. bs2b->last_sample.lo[1] = lo_filter(sample[1], bs2b->last_sample.lo[1]);
  89. /* Highboost filter */
  90. bs2b->last_sample.hi[0] = hi_filter(sample[0], bs2b->last_sample.asis[0], bs2b->last_sample.hi[0]);
  91. bs2b->last_sample.hi[1] = hi_filter(sample[1], bs2b->last_sample.asis[1], bs2b->last_sample.hi[1]);
  92. bs2b->last_sample.asis[0] = sample[0];
  93. bs2b->last_sample.asis[1] = sample[1];
  94. /* Crossfeed */
  95. sample[0] = bs2b->last_sample.hi[0] + bs2b->last_sample.lo[1];
  96. sample[1] = bs2b->last_sample.hi[1] + bs2b->last_sample.lo[0];
  97. #undef hi_filter
  98. #undef lo_filter
  99. } /* bs2b_cross_feed */
  100. #ifdef __cplusplus
  101. } /* extern "C" */
  102. #endif /* __cplusplus */
  103. #endif /* BS2B_H */