Add the Bauer stereophonic-to-binaural DSP (bs2b) code and hooks
[openal-soft.git] / Alc / bs2b.c
blob069ed614f175a0e9d81ac33fddd25a69efac58d5
1 /*-
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.
24 #include <math.h>
26 #include "bs2b.h"
28 #ifndef M_PI
29 #define M_PI 3.14159265358979323846
30 #endif
32 /* Single pole IIR filter.
33 * O[n] = a0*I[n] + a1*I[n-1] + b1*O[n-1]
36 /* Lowpass filter */
37 #define lo_filter(in, out_1) (bs2b->a0_lo*(in) + bs2b->b1_lo*(out_1))
39 /* Highboost filter */
40 #define hi_filter(in, in_1, out_1) (bs2b->a0_hi*(in) + bs2b->a1_hi*(in_1) + bs2b->b1_hi*(out_1))
42 /* Set up all data. */
43 static void init(struct bs2b *bs2b)
45 double Fc_lo, Fc_hi;
46 double G_lo, G_hi;
47 double x;
49 if ((bs2b->srate > 192000) || (bs2b->srate < 2000))
50 bs2b->srate = BS2B_DEFAULT_SRATE;
52 switch(bs2b->level)
54 case BS2B_LOW_CLEVEL: /* Low crossfeed level */
55 Fc_lo = 360.0;
56 Fc_hi = 501.0;
57 G_lo = 0.398107170553497;
58 G_hi = 0.205671765275719;
59 break;
61 case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */
62 Fc_lo = 500.0;
63 Fc_hi = 711.0;
64 G_lo = 0.459726988530872;
65 G_hi = 0.228208484414988;
66 break;
68 case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */
69 Fc_lo = 700.0;
70 Fc_hi = 1021.0;
71 G_lo = 0.530884444230988;
72 G_hi = 0.250105790667544;
73 break;
75 case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */
76 Fc_lo = 360.0;
77 Fc_hi = 494.0;
78 G_lo = 0.316227766016838;
79 G_hi = 0.168236228897329;
80 break;
82 case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */
83 Fc_lo = 500.0;
84 Fc_hi = 689.0;
85 G_lo = 0.354813389233575;
86 G_hi = 0.187169483835901;
87 break;
89 default: /* High easy crossfeed level */
90 bs2b->level = BS2B_HIGH_ECLEVEL;
92 Fc_lo = 700.0;
93 Fc_hi = 975.0;
94 G_lo = 0.398107170553497;
95 G_hi = 0.205671765275719;
96 break;
97 } /* switch */
99 /* $fc = $Fc / $s;
100 * $d = 1 / 2 / pi / $fc;
101 * $x = exp(-1 / $d);
104 x = exp(-2.0 * M_PI * Fc_lo / bs2b->srate);
105 bs2b->b1_lo = x;
106 bs2b->a0_lo = G_lo * (1.0 - x);
108 x = exp(-2.0 * M_PI * Fc_hi / bs2b->srate);
109 bs2b->b1_hi = x;
110 bs2b->a0_hi = 1.0 - G_hi * (1.0 - x);
111 bs2b->a1_hi = -x;
113 bs2b->gain = 1.0 / (1.0 - G_hi + G_lo);
115 bs2b_clear(bs2b);
116 } /* init */
118 /* Exported functions.
119 * See descriptions in "bs2b.h"
122 void bs2b_set_level(struct bs2b *bs2b, int level)
124 if(level == bs2b->level)
125 return;
126 bs2b->level = level;
127 init(bs2b);
128 } /* bs2b_set_level */
130 int bs2b_get_level(struct bs2b *bs2b)
132 return bs2b->level;
133 } /* bs2b_get_level */
135 void bs2b_set_srate(struct bs2b *bs2b, int srate)
137 if (srate == bs2b->srate)
138 return;
139 bs2b->srate = srate;
140 init(bs2b);
141 } /* bs2b_set_srate */
143 int bs2b_get_srate(struct bs2b *bs2b)
145 return bs2b->srate;
146 } /* bs2b_get_srate */
148 void bs2b_clear(struct bs2b *bs2b)
150 int loopv = sizeof(bs2b->last_sample);
152 while (loopv)
154 ((char *)&bs2b->last_sample)[--loopv] = 0;
156 } /* bs2b_clear */
158 int bs2b_is_clear(struct bs2b *bs2b)
160 int loopv = sizeof(bs2b->last_sample);
162 while (loopv)
164 if (((char *)&bs2b->last_sample)[--loopv] != 0)
165 return 0;
167 return 1;
168 } /* bs2b_is_clear */
170 void bs2b_cross_feed(struct bs2b *bs2b, float *sample)
172 /* Lowpass filter */
173 bs2b->last_sample.lo[0] = lo_filter(sample[0], bs2b->last_sample.lo[0]);
174 bs2b->last_sample.lo[1] = lo_filter(sample[1], bs2b->last_sample.lo[1]);
176 /* Highboost filter */
177 bs2b->last_sample.hi[0] = hi_filter(sample[0], bs2b->last_sample.asis[0], bs2b->last_sample.hi[0]);
178 bs2b->last_sample.hi[1] = hi_filter(sample[1], bs2b->last_sample.asis[1], bs2b->last_sample.hi[1]);
179 bs2b->last_sample.asis[0] = sample[0];
180 bs2b->last_sample.asis[1] = sample[1];
182 /* Crossfeed */
183 sample[0] = bs2b->last_sample.hi[0] + bs2b->last_sample.lo[1];
184 sample[1] = bs2b->last_sample.hi[1] + bs2b->last_sample.lo[0];
186 /* Bass boost cause allpass attenuation */
187 sample[0] *= bs2b->gain;
188 sample[1] *= bs2b->gain;
190 /* Clipping of overloaded samples */
191 #if 0
192 if (sample[0] > 1.0)
193 sample[0] = 1.0;
194 if (sample[0] < -1.0)
195 sample[0] = -1.0;
196 if (sample[1] > 1.0)
197 sample[1] = 1.0;
198 if (sample[1] < -1.0)
199 sample[1] = -1.0;
200 #endif
201 } /* bs2b_cross_feed */