zenilib: zenilib/jni/external/openal-soft/Alc/bs2b.c Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
bs2b.c
Go to the documentation of this file.
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
24 #include "config.h"
25
26 #include <math.h>
27 #include <string.h>
28
29 #include "bs2b.h"
30
31 #ifndef M_PI
32 #define M_PI 3.14159265358979323846
33 #endif
34
35 /* Single pole IIR filter.
36 * O[n] = a0*I[n] + a1*I[n-1] + b1*O[n-1]
37 */
38
39 /* Lowpass filter */
40 #define lo_filter(in, out_1) (bs2b->a0_lo*(in) + bs2b->b1_lo*(out_1))
41
42 /* Highboost filter */
43 #define hi_filter(in, in_1, out_1) (bs2b->a0_hi*(in) + bs2b->a1_hi*(in_1) + bs2b->b1_hi*(out_1))
44
45 /* Set up all data. */
46 static void init(struct bs2b *bs2b)
47 {
48 double Fc_lo, Fc_hi;
49 double G_lo, G_hi;
50 double x;
51
52 if ((bs2b->srate > 192000) || (bs2b->srate < 2000))
53 bs2b->srate = BS2B_DEFAULT_SRATE;
54
55 switch(bs2b->level)
56 {
57 case BS2B_LOW_CLEVEL: /* Low crossfeed level */
58 Fc_lo = 360.0;
59 Fc_hi = 501.0;
60 G_lo = 0.398107170553497;
61 G_hi = 0.205671765275719;
62 break;
63
64 case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */
65 Fc_lo = 500.0;
66 Fc_hi = 711.0;
67 G_lo = 0.459726988530872;
68 G_hi = 0.228208484414988;
69 break;
70
71 case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */
72 Fc_lo = 700.0;
73 Fc_hi = 1021.0;
74 G_lo = 0.530884444230988;
75 G_hi = 0.250105790667544;
76 break;
77
78 case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */
79 Fc_lo = 360.0;
80 Fc_hi = 494.0;
81 G_lo = 0.316227766016838;
82 G_hi = 0.168236228897329;
83 break;
84
85 case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */
86 Fc_lo = 500.0;
87 Fc_hi = 689.0;
88 G_lo = 0.354813389233575;
89 G_hi = 0.187169483835901;
90 break;
91
92 default: /* High easy crossfeed level */
93 bs2b->level = BS2B_HIGH_ECLEVEL;
94
95 Fc_lo = 700.0;
96 Fc_hi = 975.0;
97 G_lo = 0.398107170553497;
98 G_hi = 0.205671765275719;
99 break;
100 } /* switch */
101
102 /* $fc = $Fc / $s;
103 * $d = 1 / 2 / pi / $fc;
104 * $x = exp(-1 / $d);
105 */
106
107 x = exp(-2.0 * M_PI * Fc_lo / bs2b->srate);
108 bs2b->b1_lo = x;
109 bs2b->a0_lo = G_lo * (1.0 - x);
110
111 x = exp(-2.0 * M_PI * Fc_hi / bs2b->srate);
112 bs2b->b1_hi = x;
113 bs2b->a0_hi = 1.0 - G_hi * (1.0 - x);
114 bs2b->a1_hi = -x;
115
116 bs2b->gain = 1.0f / (float)(1.0 - G_hi + G_lo);
117 } /* init */
118
119 /* Exported functions.
120 * See descriptions in "bs2b.h"
121 */
122
123 void bs2b_set_level(struct bs2b *bs2b, int level)
124 {
125 if(level == bs2b->level)
126 return;
127 bs2b->level = level;
128 init(bs2b);
129 } /* bs2b_set_level */
130
131 int bs2b_get_level(struct bs2b *bs2b)
132 {
133 return bs2b->level;
134 } /* bs2b_get_level */
135
136 void bs2b_set_srate(struct bs2b *bs2b, int srate)
137 {
138 if (srate == bs2b->srate)
139 return;
140 bs2b->srate = srate;
141 init(bs2b);
142 } /* bs2b_set_srate */
143
144 int bs2b_get_srate(struct bs2b *bs2b)
145 {
146 return bs2b->srate;
147 } /* bs2b_get_srate */
148
149 void bs2b_clear(struct bs2b *bs2b)
150 {
151 memset(&bs2b->last_sample, 0, sizeof(bs2b->last_sample));
152 } /* bs2b_clear */
153
154 void bs2b_cross_feed(struct bs2b *bs2b, float *sample)
155 {
156 /* Lowpass filter */
157 bs2b->last_sample.lo[0] = lo_filter(sample[0], bs2b->last_sample.lo[0]);
158 bs2b->last_sample.lo[1] = lo_filter(sample[1], bs2b->last_sample.lo[1]);
159
160 /* Highboost filter */
161 bs2b->last_sample.hi[0] = hi_filter(sample[0], bs2b->last_sample.asis[0], bs2b->last_sample.hi[0]);
162 bs2b->last_sample.hi[1] = hi_filter(sample[1], bs2b->last_sample.asis[1], bs2b->last_sample.hi[1]);
163 bs2b->last_sample.asis[0] = sample[0];
164 bs2b->last_sample.asis[1] = sample[1];
165
166 /* Crossfeed */
167 sample[0] = (float)(bs2b->last_sample.hi[0] + bs2b->last_sample.lo[1]);
168 sample[1] = (float)(bs2b->last_sample.hi[1] + bs2b->last_sample.lo[0]);
169
170 /* Bass boost cause allpass attenuation */
171 sample[0] *= bs2b->gain;
172 sample[1] *= bs2b->gain;
173
174 /* Clipping of overloaded samples */
175 #if 0
176 if (sample[0] > 1.0)
177 sample[0] = 1.0;
178 if (sample[0] < -1.0)
179 sample[0] = -1.0;
180 if (sample[1] > 1.0)
181 sample[1] = 1.0;
182 if (sample[1] < -1.0)
183 sample[1] = -1.0;
184 #endif
185 } /* bs2b_cross_feed */
bs2b::t_last_sample::hi
double hi[2]
Definition: bs2b.h:71
bs2b::gain
float gain
Definition: bs2b.h:63
bs2b
Definition: bs2b.h:49
BS2B_DEFAULT_SRATE
#define BS2B_DEFAULT_SRATE
Definition: bs2b.h:43
x
EGLSurface EGLint x
Definition: eglext.h:293
init
static void init(struct bs2b *bs2b)
Definition: bs2b.c:46
memset
#define memset
Definition: SDL_malloc.c:633
BS2B_MIDDLE_ECLEVEL
#define BS2B_MIDDLE_ECLEVEL
Definition: bs2b.h:37
bs2b::a1_hi
double a1_hi
Definition: bs2b.h:59
bs2b_clear
void bs2b_clear(struct bs2b *bs2b)
Definition: bs2b.c:149
bs2b::t_last_sample::asis
double asis[2]
Definition: bs2b.h:69
bs2b_set_srate
void bs2b_set_srate(struct bs2b *bs2b, int srate)
Definition: bs2b.c:136
bs2b::a0_hi
double a0_hi
Definition: bs2b.h:58
bs2b::a0_lo
double a0_lo
Definition: bs2b.h:54
bs2b_get_level
int bs2b_get_level(struct bs2b *bs2b)
Definition: bs2b.c:131
BS2B_LOW_CLEVEL
#define BS2B_LOW_CLEVEL
Definition: bs2b.h:33
BS2B_HIGH_ECLEVEL
#define BS2B_HIGH_ECLEVEL
Definition: bs2b.h:36
bs2b::srate
int srate
Definition: bs2b.h:51
bs2b::level
int level
Definition: bs2b.h:50
bs2b::b1_lo
double b1_lo
Definition: bs2b.h:55
BS2B_HIGH_CLEVEL
#define BS2B_HIGH_CLEVEL
Definition: bs2b.h:31
M_PI
#define M_PI
Definition: bs2b.c:32
hi_filter
#define hi_filter(in, in_1, out_1)
Definition: bs2b.c:43
BS2B_MIDDLE_CLEVEL
#define BS2B_MIDDLE_CLEVEL
Definition: bs2b.h:32
level
GLint level
Definition: gl2ext.h:845
BS2B_LOW_ECLEVEL
#define BS2B_LOW_ECLEVEL
Definition: bs2b.h:38
String.h
bs2b_cross_feed
void bs2b_cross_feed(struct bs2b *bs2b, float *sample)
Definition: bs2b.c:154
bs2b.h
bs2b::b1_hi
double b1_hi
Definition: bs2b.h:60
lo_filter
#define lo_filter(in, out_1)
Definition: bs2b.c:40
bs2b::last_sample
struct bs2b::t_last_sample last_sample
bs2b_set_level
void bs2b_set_level(struct bs2b *bs2b, int level)
Definition: bs2b.c:123
bs2b::t_last_sample::lo
double lo[2]
Definition: bs2b.h:70
bs2b_get_srate
int bs2b_get_srate(struct bs2b *bs2b)
Definition: bs2b.c:144