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 #ifndef BS2B_H
25 #define BS2B_H
26 
27 #include "bs2bversion.h"
28 #include "bs2btypes.h"
29 
30 /* Minimum/maximum sample rate (Hz) */
31 #define BS2B_MINSRATE 2000
32 #define BS2B_MAXSRATE 384000
33 
34 /* Minimum/maximum cut frequency (Hz) */
35 /* bs2b_set_level_fcut() */
36 #define BS2B_MINFCUT 300
37 #define BS2B_MAXFCUT 2000
38 
39 /* Minimum/maximum feed level (dB * 10 @ low frequencies) */
40 /* bs2b_set_level_feed() */
41 #define BS2B_MINFEED 10   /* 1 dB */
42 #define BS2B_MAXFEED 150  /* 15 dB */
43 
44 /* Normal crossfeed levels (Obsolete) */
45 #define BS2B_HIGH_CLEVEL     ( ( uint32_t )700 | ( ( uint32_t )30 << 16 ) )
46 #define BS2B_MIDDLE_CLEVEL   ( ( uint32_t )500 | ( ( uint32_t )45 << 16 ) )
47 #define BS2B_LOW_CLEVEL      ( ( uint32_t )360 | ( ( uint32_t )60 << 16 ) )
48 
49 /* Easy crossfeed levels (Obsolete) */
50 #define BS2B_HIGH_ECLEVEL    ( ( uint32_t )700 | ( ( uint32_t )60 << 16 ) )
51 #define BS2B_MIDDLE_ECLEVEL  ( ( uint32_t )500 | ( ( uint32_t )72 << 16 ) )
52 #define BS2B_LOW_ECLEVEL     ( ( uint32_t )360 | ( ( uint32_t )84 << 16 ) )
53 
54 /* Default crossfeed levels */
55 /* bs2b_set_level() */
56 #define BS2B_DEFAULT_CLEVEL  ( ( uint32_t )700 | ( ( uint32_t )45 << 16 ) )
57 #define BS2B_CMOY_CLEVEL     ( ( uint32_t )700 | ( ( uint32_t )60 << 16 ) )
58 #define BS2B_JMEIER_CLEVEL   ( ( uint32_t )650 | ( ( uint32_t )95 << 16 ) )
59 
60 /* Default sample rate (Hz) */
61 #define BS2B_DEFAULT_SRATE   44100
62 
63 /* A delay at low frequency by microseconds according to cut frequency */
64 #define bs2b_level_delay( fcut ) ( ( 18700 / fcut ) * 10 )
65 
66 typedef struct
67 {
68 	uint32_t level;              /* Crossfeed level */
69 	uint32_t srate;              /* Sample rate (Hz) */
70 	double a0_lo, b1_lo;         /* Lowpass IIR filter coefficients */
71 	double a0_hi, a1_hi, b1_hi;  /* Highboost IIR filter coefficients */
72 	double gain;                 /* Global gain against overloading */
73 	/* Buffer of last filtered sample: [0] 1-st channel, [1] 2-d channel */
74 	struct { double asis[ 2 ], lo[ 2 ], hi[ 2 ]; } lfs;
75 } t_bs2bd;
76 
77 typedef t_bs2bd *t_bs2bdp;
78 
79 #ifdef __cplusplus
80 extern "C"
81 {
82 #endif	/* __cplusplus */
83 
84 /* Allocates and sets a data to defaults.
85  * Return NULL on error.
86  */
87 t_bs2bdp bs2b_open( void );
88 
89 /* Close */
90 void bs2b_close( t_bs2bdp bs2bdp );
91 
92 /* Sets a new coefficients by new crossfeed value.
93  * level = ( ( uint32_t )fcut | ( ( uint32_t )feed << 16 ) )
94  * where 'feed' is crossfeeding level at low frequencies (dB * 10)
95  * and 'fcut' is cut frecuency (Hz)
96  */
97 void bs2b_set_level( t_bs2bdp bs2bdp, uint32_t level );
98 
99 /* Return a current crossfeed level value. */
100 uint32_t bs2b_get_level( t_bs2bdp bs2bdp );
101 
102 /* Sets a new coefficients by new cut frecuency value (Hz). */
103 void bs2b_set_level_fcut( t_bs2bdp bs2bdp, int fcut );
104 
105 /* Return a current cut frecuency value (Hz). */
106 int bs2b_get_level_fcut( t_bs2bdp bs2bdp );
107 
108 /* Sets a new coefficients by new crossfeeding level value (dB * 10). */
109 void bs2b_set_level_feed( t_bs2bdp bs2bdp, int feed );
110 
111 /* Return a current crossfeeding level value (dB * 10). */
112 int bs2b_get_level_feed( t_bs2bdp bs2bdp );
113 
114 /* Return a current delay value at low frequencies (micro seconds). */
115 int bs2b_get_level_delay( t_bs2bdp bs2bdp );
116 
117 /* Clear buffers and sets a new coefficients with new sample rate value.
118  * srate - sample rate by Hz.
119  */
120 void bs2b_set_srate( t_bs2bdp bs2bdp, uint32_t srate );
121 
122 /* Return current sample rate value */
123 uint32_t bs2b_get_srate( t_bs2bdp bs2bdp );
124 
125 /* Clear buffer */
126 void bs2b_clear( t_bs2bdp bs2bdp );
127 
128 /* Return 1 if buffer is clear */
129 int bs2b_is_clear( t_bs2bdp bs2bdp );
130 
131 /* Return bs2b version string */
132 char const *bs2b_runtime_version( void );
133 
134 /* Return bs2b version integer */
135 uint32_t bs2b_runtime_version_int( void );
136 
137 /* 'bs2b_cross_feed_*' crossfeeds buffer of 'n' stereo samples
138  * pointed by 'sample'.
139  * sample[i]   - first channel,
140  * sample[i+1] - second channel.
141  * Where 'i' is ( i = 0; i < n * 2; i += 2 )
142  */
143 
144 /* sample poits to double floats native endians */
145 void bs2b_cross_feed_d( t_bs2bdp bs2bdp, double *sample, int n );
146 
147 /* sample poits to double floats big endians */
148 void bs2b_cross_feed_dbe( t_bs2bdp bs2bdp, double *sample, int n );
149 
150 /* sample poits to double floats little endians */
151 void bs2b_cross_feed_dle( t_bs2bdp bs2bdp, double *sample, int n );
152 
153 /* sample poits to floats native endians */
154 void bs2b_cross_feed_f( t_bs2bdp bs2bdp, float *sample, int n );
155 
156 /* sample poits to floats big endians */
157 void bs2b_cross_feed_fbe( t_bs2bdp bs2bdp, float *sample, int n );
158 
159 /* sample poits to floats little endians */
160 void bs2b_cross_feed_fle( t_bs2bdp bs2bdp, float *sample, int n );
161 
162 /* sample poits to 32bit signed integers native endians */
163 void bs2b_cross_feed_s32( t_bs2bdp bs2bdp, int32_t *sample, int n );
164 
165 /* sample poits to 32bit unsigned integers native endians */
166 void bs2b_cross_feed_u32( t_bs2bdp bs2bdp, uint32_t *sample, int n );
167 
168 /* sample poits to 32bit signed integers big endians */
169 void bs2b_cross_feed_s32be( t_bs2bdp bs2bdp, int32_t *sample, int n );
170 
171 /* sample poits to 32bit unsigned integers big endians */
172 void bs2b_cross_feed_u32be( t_bs2bdp bs2bdp, uint32_t *sample, int n );
173 
174 /* sample poits to 32bit signed integers little endians */
175 void bs2b_cross_feed_s32le( t_bs2bdp bs2bdp, int32_t *sample, int n );
176 
177 /* sample poits to 32bit unsigned integers little endians */
178 void bs2b_cross_feed_u32le( t_bs2bdp bs2bdp, uint32_t *sample, int n );
179 
180 /* sample poits to 16bit signed integers native endians */
181 void bs2b_cross_feed_s16( t_bs2bdp bs2bdp, int16_t *sample, int n );
182 
183 /* sample poits to 16bit unsigned integers native endians */
184 void bs2b_cross_feed_u16( t_bs2bdp bs2bdp, uint16_t *sample, int n );
185 
186 /* sample poits to 16bit signed integers big endians */
187 void bs2b_cross_feed_s16be( t_bs2bdp bs2bdp, int16_t *sample, int n );
188 
189 /* sample poits to 16bit unsigned integers big endians */
190 void bs2b_cross_feed_u16be( t_bs2bdp bs2bdp, uint16_t *sample, int n );
191 
192 /* sample poits to 16bit signed integers little endians */
193 void bs2b_cross_feed_s16le( t_bs2bdp bs2bdp, int16_t *sample, int n );
194 
195 /* sample poits to 16bit unsigned integers little endians */
196 void bs2b_cross_feed_u16le( t_bs2bdp bs2bdp, uint16_t *sample, int n );
197 
198 /* sample poits to 8bit signed integers */
199 void bs2b_cross_feed_s8( t_bs2bdp bs2bdp, int8_t *sample, int n );
200 
201 /* sample poits to 8bit unsigned integers */
202 void bs2b_cross_feed_u8( t_bs2bdp bs2bdp, uint8_t *sample, int n );
203 
204 /* sample poits to 24bit signed integers native endians */
205 void bs2b_cross_feed_s24( t_bs2bdp bs2bdp, bs2b_int24_t *sample, int n );
206 
207 /* sample poits to 24bit unsigned integers native endians */
208 void bs2b_cross_feed_u24( t_bs2bdp bs2bdp, bs2b_uint24_t *sample, int n );
209 
210 /* sample poits to 24bit signed integers be endians */
211 void bs2b_cross_feed_s24be( t_bs2bdp bs2bdp, bs2b_int24_t *sample, int n );
212 
213 /* sample poits to 24bit unsigned integers be endians */
214 void bs2b_cross_feed_u24be( t_bs2bdp bs2bdp, bs2b_uint24_t *sample, int n );
215 
216 /* sample poits to 24bit signed integers little endians */
217 void bs2b_cross_feed_s24le( t_bs2bdp bs2bdp, bs2b_int24_t *sample, int n );
218 
219 /* sample poits to 24bit unsigned integers little endians */
220 void bs2b_cross_feed_u24le( t_bs2bdp bs2bdp, bs2b_uint24_t *sample, int n );
221 
222 #ifdef __cplusplus
223 }	/* extern "C" */
224 #endif /* __cplusplus */
225 
226 #endif	/* BS2B_H */
227