Move some ambisonic-related macros to a separate header
[openal-soft.git] / Alc / bs2b.cpp
blobb1833b8c8059d7bf6583e047dacdfb3ce63e1033
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 "config.h"
26 #include <cmath>
27 #include <cstring>
28 #include <algorithm>
30 #include "bs2b.h"
31 #include "math_defs.h"
34 /* Set up all data. */
35 static void init(struct bs2b *bs2b)
37 float Fc_lo, Fc_hi;
38 float G_lo, G_hi;
39 float x, g;
41 switch(bs2b->level)
43 case BS2B_LOW_CLEVEL: /* Low crossfeed level */
44 Fc_lo = 360.0f;
45 Fc_hi = 501.0f;
46 G_lo = 0.398107170553497f;
47 G_hi = 0.205671765275719f;
48 break;
50 case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */
51 Fc_lo = 500.0f;
52 Fc_hi = 711.0f;
53 G_lo = 0.459726988530872f;
54 G_hi = 0.228208484414988f;
55 break;
57 case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */
58 Fc_lo = 700.0f;
59 Fc_hi = 1021.0f;
60 G_lo = 0.530884444230988f;
61 G_hi = 0.250105790667544f;
62 break;
64 case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */
65 Fc_lo = 360.0f;
66 Fc_hi = 494.0f;
67 G_lo = 0.316227766016838f;
68 G_hi = 0.168236228897329f;
69 break;
71 case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */
72 Fc_lo = 500.0f;
73 Fc_hi = 689.0f;
74 G_lo = 0.354813389233575f;
75 G_hi = 0.187169483835901f;
76 break;
78 default: /* High easy crossfeed level */
79 bs2b->level = BS2B_HIGH_ECLEVEL;
81 Fc_lo = 700.0f;
82 Fc_hi = 975.0f;
83 G_lo = 0.398107170553497f;
84 G_hi = 0.205671765275719f;
85 break;
86 } /* switch */
88 g = 1.0f / (1.0f - G_hi + G_lo);
90 /* $fc = $Fc / $s;
91 * $d = 1 / 2 / pi / $fc;
92 * $x = exp(-1 / $d);
94 x = std::exp(-2.0f * F_PI * Fc_lo / bs2b->srate);
95 bs2b->b1_lo = x;
96 bs2b->a0_lo = G_lo * (1.0f - x) * g;
98 x = std::exp(-2.0f * F_PI * Fc_hi / bs2b->srate);
99 bs2b->b1_hi = x;
100 bs2b->a0_hi = (1.0f - G_hi * (1.0f - x)) * g;
101 bs2b->a1_hi = -x * g;
102 } /* init */
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;
113 bs2b->level = level;
114 bs2b->srate = srate;
115 init(bs2b);
116 } /* bs2b_set_params */
118 int bs2b_get_level(struct bs2b *bs2b)
120 return bs2b->level;
121 } /* bs2b_get_level */
123 int bs2b_get_srate(struct bs2b *bs2b)
125 return bs2b->srate;
126 } /* bs2b_get_srate */
128 void bs2b_clear(struct bs2b *bs2b)
130 std::fill(std::begin(bs2b->last_sample), std::end(bs2b->last_sample), bs2b::t_last_sample{});
131 } /* bs2b_clear */
133 void bs2b_cross_feed(struct bs2b *bs2b, float *RESTRICT Left, float *RESTRICT Right, int SamplesToDo)
135 float lsamples[128][2];
136 float rsamples[128][2];
137 int base;
139 for(base = 0;base < SamplesToDo;)
141 int todo = std::min(128, SamplesToDo-base);
142 int i;
144 /* Process left input */
145 lsamples[0][0] = bs2b->a0_lo*Left[0] +
146 bs2b->b1_lo*bs2b->last_sample[0].lo;
147 lsamples[0][1] = bs2b->a0_hi*Left[0] +
148 bs2b->a1_hi*bs2b->last_sample[0].asis +
149 bs2b->b1_hi*bs2b->last_sample[0].hi;
150 for(i = 1;i < todo;i++)
152 lsamples[i][0] = bs2b->a0_lo*Left[i] +
153 bs2b->b1_lo*lsamples[i-1][0];
154 lsamples[i][1] = bs2b->a0_hi*Left[i] +
155 bs2b->a1_hi*Left[i-1] +
156 bs2b->b1_hi*lsamples[i-1][1];
158 bs2b->last_sample[0].asis = Left[i-1];
159 bs2b->last_sample[0].lo = lsamples[i-1][0];
160 bs2b->last_sample[0].hi = lsamples[i-1][1];
162 /* Process right input */
163 rsamples[0][0] = bs2b->a0_lo*Right[0] +
164 bs2b->b1_lo*bs2b->last_sample[1].lo;
165 rsamples[0][1] = bs2b->a0_hi*Right[0] +
166 bs2b->a1_hi*bs2b->last_sample[1].asis +
167 bs2b->b1_hi*bs2b->last_sample[1].hi;
168 for(i = 1;i < todo;i++)
170 rsamples[i][0] = bs2b->a0_lo*Right[i] +
171 bs2b->b1_lo*rsamples[i-1][0];
172 rsamples[i][1] = bs2b->a0_hi*Right[i] +
173 bs2b->a1_hi*Right[i-1] +
174 bs2b->b1_hi*rsamples[i-1][1];
176 bs2b->last_sample[1].asis = Right[i-1];
177 bs2b->last_sample[1].lo = rsamples[i-1][0];
178 bs2b->last_sample[1].hi = rsamples[i-1][1];
180 /* Crossfeed */
181 for(i = 0;i < todo;i++)
182 *(Left++) = lsamples[i][1] + rsamples[i][0];
183 for(i = 0;i < todo;i++)
184 *(Right++) = rsamples[i][1] + lsamples[i][0];
186 base += todo;
188 } /* bs2b_cross_feed */