2 * Copyright (c) 2005 Boris Mikhaylov
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
31 #include "math_defs.h"
34 /* Set up all data. */
35 static void init(struct bs2b
*bs2b
)
43 case BS2B_LOW_CLEVEL
: /* Low crossfeed level */
46 G_lo
= 0.398107170553497f
;
47 G_hi
= 0.205671765275719f
;
50 case BS2B_MIDDLE_CLEVEL
: /* Middle crossfeed level */
53 G_lo
= 0.459726988530872f
;
54 G_hi
= 0.228208484414988f
;
57 case BS2B_HIGH_CLEVEL
: /* High crossfeed level (virtual speakers are closer to itself) */
60 G_lo
= 0.530884444230988f
;
61 G_hi
= 0.250105790667544f
;
64 case BS2B_LOW_ECLEVEL
: /* Low easy crossfeed level */
67 G_lo
= 0.316227766016838f
;
68 G_hi
= 0.168236228897329f
;
71 case BS2B_MIDDLE_ECLEVEL
: /* Middle easy crossfeed level */
74 G_lo
= 0.354813389233575f
;
75 G_hi
= 0.187169483835901f
;
78 default: /* High easy crossfeed level */
79 bs2b
->level
= BS2B_HIGH_ECLEVEL
;
83 G_lo
= 0.398107170553497f
;
84 G_hi
= 0.205671765275719f
;
88 g
= 1.0f
/ (1.0f
- G_hi
+ G_lo
);
91 * $d = 1 / 2 / pi / $fc;
94 x
= std::exp(-al::MathDefs
<float>::Tau() * Fc_lo
/ static_cast<float>(bs2b
->srate
));
96 bs2b
->a0_lo
= G_lo
* (1.0f
- x
) * g
;
98 x
= std::exp(-al::MathDefs
<float>::Tau() * Fc_hi
/ static_cast<float>(bs2b
->srate
));
100 bs2b
->a0_hi
= (1.0f
- G_hi
* (1.0f
- x
)) * g
;
101 bs2b
->a1_hi
= -x
* g
;
105 /* Exported functions.
106 * See descriptions in "bs2b.h"
109 void bs2b_set_params(struct bs2b
*bs2b
, int level
, int srate
)
111 if(srate
<= 0) srate
= 1;
116 } /* bs2b_set_params */
118 int bs2b_get_level(struct bs2b
*bs2b
)
121 } /* bs2b_get_level */
123 int bs2b_get_srate(struct bs2b
*bs2b
)
126 } /* bs2b_get_srate */
128 void bs2b_clear(struct bs2b
*bs2b
)
130 std::fill(std::begin(bs2b
->history
), std::end(bs2b
->history
), bs2b::t_last_sample
{});
133 void bs2b_cross_feed(struct bs2b
*bs2b
, float *Left
, float *Right
, size_t SamplesToDo
)
135 const float a0_lo
{bs2b
->a0_lo
};
136 const float b1_lo
{bs2b
->b1_lo
};
137 const float a0_hi
{bs2b
->a0_hi
};
138 const float a1_hi
{bs2b
->a1_hi
};
139 const float b1_hi
{bs2b
->b1_hi
};
140 float lsamples
[128][2];
141 float rsamples
[128][2];
143 for(size_t base
{0};base
< SamplesToDo
;)
145 const size_t todo
{std::min
<size_t>(128, SamplesToDo
-base
)};
147 /* Process left input */
148 float z_lo
{bs2b
->history
[0].lo
};
149 float z_hi
{bs2b
->history
[0].hi
};
150 for(size_t i
{0};i
< todo
;i
++)
152 lsamples
[i
][0] = a0_lo
*Left
[i
] + z_lo
;
153 z_lo
= b1_lo
*lsamples
[i
][0];
155 lsamples
[i
][1] = a0_hi
*Left
[i
] + z_hi
;
156 z_hi
= a1_hi
*Left
[i
] + b1_hi
*lsamples
[i
][1];
158 bs2b
->history
[0].lo
= z_lo
;
159 bs2b
->history
[0].hi
= z_hi
;
161 /* Process right input */
162 z_lo
= bs2b
->history
[1].lo
;
163 z_hi
= bs2b
->history
[1].hi
;
164 for(size_t i
{0};i
< todo
;i
++)
166 rsamples
[i
][0] = a0_lo
*Right
[i
] + z_lo
;
167 z_lo
= b1_lo
*rsamples
[i
][0];
169 rsamples
[i
][1] = a0_hi
*Right
[i
] + z_hi
;
170 z_hi
= a1_hi
*Right
[i
] + b1_hi
*rsamples
[i
][1];
172 bs2b
->history
[1].lo
= z_lo
;
173 bs2b
->history
[1].hi
= z_hi
;
176 for(size_t i
{0};i
< todo
;i
++)
177 *(Left
++) = lsamples
[i
][1] + rsamples
[i
][0];
178 for(size_t i
{0};i
< todo
;i
++)
179 *(Right
++) = rsamples
[i
][1] + lsamples
[i
][0];
183 } /* bs2b_cross_feed */