Update a comment
[openal-soft.git] / Alc / bs2b.c
blobd936f75ac86d38f1b151c0e12e65c47a167b9741
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 <math.h>
27 #include <string.h>
29 #include "bs2b.h"
30 #include "alu.h"
32 #ifndef M_PI
33 #define M_PI 3.14159265358979323846
34 #endif
36 /* Set up all data. */
37 static void init(struct bs2b *bs2b)
39 float Fc_lo, Fc_hi;
40 float G_lo, G_hi;
41 float x, g;
43 bs2b->srate = clampi(bs2b->srate, 2000, 192000);
45 switch(bs2b->level)
47 case BS2B_LOW_CLEVEL: /* Low crossfeed level */
48 Fc_lo = 360.0f;
49 Fc_hi = 501.0f;
50 G_lo = 0.398107170553497f;
51 G_hi = 0.205671765275719f;
52 break;
54 case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */
55 Fc_lo = 500.0f;
56 Fc_hi = 711.0f;
57 G_lo = 0.459726988530872f;
58 G_hi = 0.228208484414988f;
59 break;
61 case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */
62 Fc_lo = 700.0f;
63 Fc_hi = 1021.0f;
64 G_lo = 0.530884444230988f;
65 G_hi = 0.250105790667544f;
66 break;
68 case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */
69 Fc_lo = 360.0f;
70 Fc_hi = 494.0f;
71 G_lo = 0.316227766016838f;
72 G_hi = 0.168236228897329f;
73 break;
75 case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */
76 Fc_lo = 500.0f;
77 Fc_hi = 689.0f;
78 G_lo = 0.354813389233575f;
79 G_hi = 0.187169483835901f;
80 break;
82 default: /* High easy crossfeed level */
83 bs2b->level = BS2B_HIGH_ECLEVEL;
85 Fc_lo = 700.0f;
86 Fc_hi = 975.0f;
87 G_lo = 0.398107170553497f;
88 G_hi = 0.205671765275719f;
89 break;
90 } /* switch */
92 g = 1.0f / (1.0f - G_hi + G_lo);
94 /* $fc = $Fc / $s;
95 * $d = 1 / 2 / pi / $fc;
96 * $x = exp(-1 / $d);
98 x = expf(-2.0f * F_PI * Fc_lo / bs2b->srate);
99 bs2b->b1_lo = x;
100 bs2b->a0_lo = G_lo * (1.0f - x) * g;
102 x = expf(-2.0f * F_PI * Fc_hi / bs2b->srate);
103 bs2b->b1_hi = x;
104 bs2b->a0_hi = (1.0f - G_hi * (1.0f - x)) * g;
105 bs2b->a1_hi = -x * g;
106 } /* init */
108 /* Exported functions.
109 * See descriptions in "bs2b.h"
112 void bs2b_set_level(struct bs2b *bs2b, int level)
114 if(level == bs2b->level)
115 return;
116 bs2b->level = level;
117 init(bs2b);
118 } /* bs2b_set_level */
120 int bs2b_get_level(struct bs2b *bs2b)
122 return bs2b->level;
123 } /* bs2b_get_level */
125 void bs2b_set_srate(struct bs2b *bs2b, int srate)
127 if (srate == bs2b->srate)
128 return;
129 bs2b->srate = srate;
130 init(bs2b);
131 } /* bs2b_set_srate */
133 int bs2b_get_srate(struct bs2b *bs2b)
135 return bs2b->srate;
136 } /* bs2b_get_srate */
138 void bs2b_clear(struct bs2b *bs2b)
140 memset(&bs2b->last_sample, 0, sizeof(bs2b->last_sample));
141 } /* bs2b_clear */
143 extern inline void bs2b_cross_feed(struct bs2b *bs2b, float *restrict samples);