Multiply samples with the cubic coeffs before transposing
[openal-soft.git] / OpenAL32 / Include / bs2b.h
blobf1f3627a274752b66bf36784f833c0636a0140b3
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 #ifndef BS2B_H
25 #define BS2B_H
27 /* Number of crossfeed levels */
28 #define BS2B_CLEVELS 3
30 /* Normal crossfeed levels */
31 #define BS2B_HIGH_CLEVEL 3
32 #define BS2B_MIDDLE_CLEVEL 2
33 #define BS2B_LOW_CLEVEL 1
35 /* Easy crossfeed levels */
36 #define BS2B_HIGH_ECLEVEL BS2B_HIGH_CLEVEL + BS2B_CLEVELS
37 #define BS2B_MIDDLE_ECLEVEL BS2B_MIDDLE_CLEVEL + BS2B_CLEVELS
38 #define BS2B_LOW_ECLEVEL BS2B_LOW_CLEVEL + BS2B_CLEVELS
40 /* Default crossfeed levels */
41 #define BS2B_DEFAULT_CLEVEL BS2B_HIGH_ECLEVEL
42 /* Default sample rate (Hz) */
43 #define BS2B_DEFAULT_SRATE 44100
45 #ifdef __cplusplus
46 extern "C" {
47 #endif /* __cplusplus */
49 struct bs2b {
50 int level; /* Crossfeed level */
51 int srate; /* Sample rate (Hz) */
53 /* Lowpass IIR filter coefficients */
54 float a0_lo;
55 float b1_lo;
57 /* Highboost IIR filter coefficients */
58 float a0_hi;
59 float a1_hi;
60 float b1_hi;
62 /* Buffer of last filtered sample.
63 * [0] - first channel, [1] - second channel
65 struct t_last_sample {
66 float asis[2];
67 float lo[2];
68 float hi[2];
69 } last_sample;
72 /* Clear buffers and set new coefficients with new crossfeed level value.
73 * level - crossfeed level of *LEVEL values.
75 void bs2b_set_level(struct bs2b *bs2b, int level);
77 /* Return current crossfeed level value */
78 int bs2b_get_level(struct bs2b *bs2b);
80 /* Clear buffers and set new coefficients with new sample rate value.
81 * srate - sample rate by Hz.
83 void bs2b_set_srate(struct bs2b *bs2b, int srate);
85 /* Return current sample rate value */
86 int bs2b_get_srate(struct bs2b *bs2b);
88 /* Clear buffer */
89 void bs2b_clear(struct bs2b *bs2b);
91 /* Crossfeeds one stereo sample that are pointed by sample.
92 * [0] - first channel, [1] - second channel.
93 * Returns crossfided sample by sample pointer.
95 inline void bs2b_cross_feed(struct bs2b *bs2b, float *restrict sample)
97 /* Single pole IIR filter.
98 * O[n] = a0*I[n] + a1*I[n-1] + b1*O[n-1]
101 /* Lowpass filter */
102 #define lo_filter(in, out_1) (bs2b->a0_lo*(in) + bs2b->b1_lo*(out_1))
104 /* Highboost filter */
105 #define hi_filter(in, in_1, out_1) (bs2b->a0_hi*(in) + bs2b->a1_hi*(in_1) + bs2b->b1_hi*(out_1))
107 /* Lowpass filter */
108 bs2b->last_sample.lo[0] = lo_filter(sample[0], bs2b->last_sample.lo[0]);
109 bs2b->last_sample.lo[1] = lo_filter(sample[1], bs2b->last_sample.lo[1]);
111 /* Highboost filter */
112 bs2b->last_sample.hi[0] = hi_filter(sample[0], bs2b->last_sample.asis[0], bs2b->last_sample.hi[0]);
113 bs2b->last_sample.hi[1] = hi_filter(sample[1], bs2b->last_sample.asis[1], bs2b->last_sample.hi[1]);
114 bs2b->last_sample.asis[0] = sample[0];
115 bs2b->last_sample.asis[1] = sample[1];
117 /* Crossfeed */
118 sample[0] = bs2b->last_sample.hi[0] + bs2b->last_sample.lo[1];
119 sample[1] = bs2b->last_sample.hi[1] + bs2b->last_sample.lo[0];
120 #undef hi_filter
121 #undef lo_filter
122 } /* bs2b_cross_feed */
124 #ifdef __cplusplus
125 } /* extern "C" */
126 #endif /* __cplusplus */
128 #endif /* BS2B_H */