|
1 /* Copyright (C) 2007 Jean-Marc Valin |
|
2 |
|
3 File: speex_resampler.h |
|
4 Resampling code |
|
5 |
|
6 The design goals of this code are: |
|
7 - Very fast algorithm |
|
8 - Low memory requirement |
|
9 - Good *perceptual* quality (and not best SNR) |
|
10 |
|
11 Redistribution and use in source and binary forms, with or without |
|
12 modification, are permitted provided that the following conditions are |
|
13 met: |
|
14 |
|
15 1. Redistributions of source code must retain the above copyright notice, |
|
16 this list of conditions and the following disclaimer. |
|
17 |
|
18 2. Redistributions in binary form must reproduce the above copyright |
|
19 notice, this list of conditions and the following disclaimer in the |
|
20 documentation and/or other materials provided with the distribution. |
|
21 |
|
22 3. The name of the author may not be used to endorse or promote products |
|
23 derived from this software without specific prior written permission. |
|
24 |
|
25 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
27 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
28 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
31 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
32 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
33 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
34 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
35 POSSIBILITY OF SUCH DAMAGE. |
|
36 */ |
|
37 |
|
38 |
|
39 #ifndef SPEEX_RESAMPLER_H |
|
40 #define SPEEX_RESAMPLER_H |
|
41 |
|
42 #include <glib.h> |
|
43 |
|
44 #define OUTSIDE_SPEEX |
|
45 //#define RANDOM_PREFIX gst_audioresample |
|
46 #ifdef OUTSIDE_SPEEX |
|
47 |
|
48 /********* WARNING: MENTAL SANITY ENDS HERE *************/ |
|
49 /* If the resampler is defined outside of Speex, we change the symbol names so that |
|
50 there won't be any clash if linking with Speex later on. */ |
|
51 |
|
52 /* #define RANDOM_PREFIX your software name here */ |
|
53 #ifndef RANDOM_PREFIX |
|
54 #error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes" |
|
55 #endif |
|
56 |
|
57 #define CAT_PREFIX2(a,b) a ## b |
|
58 #define CAT_PREFIX(a,b) CAT_PREFIX2(a, b) |
|
59 |
|
60 #define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init) |
|
61 #define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac) |
|
62 #define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy) |
|
63 #define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float) |
|
64 #define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int) |
|
65 #define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float) |
|
66 #define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int) |
|
67 #define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate) |
|
68 #define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate) |
|
69 #define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac) |
|
70 #define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio) |
|
71 #define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality) |
|
72 #define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality) |
|
73 #define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride) |
|
74 #define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride) |
|
75 #define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride) |
|
76 #define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride) |
|
77 #define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency) |
|
78 #define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency) |
|
79 #define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros) |
|
80 #define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem) |
|
81 #define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror) |
|
82 |
|
83 #define spx_int16_t gint16 |
|
84 #define spx_int32_t gint32 |
|
85 #define spx_uint16_t guint16 |
|
86 #define spx_uint32_t guint32 |
|
87 |
|
88 #else /* OUTSIDE_SPEEX */ |
|
89 |
|
90 #ifdef _BUILD_SPEEX |
|
91 # include "speex_types.h" |
|
92 #else |
|
93 # include <speex/speex_types.h> |
|
94 #endif |
|
95 |
|
96 #endif /* OUTSIDE_SPEEX */ |
|
97 |
|
98 #ifdef __cplusplus |
|
99 extern "C" |
|
100 { |
|
101 #endif |
|
102 |
|
103 #define SPEEX_RESAMPLER_QUALITY_MAX 10 |
|
104 #define SPEEX_RESAMPLER_QUALITY_MIN 0 |
|
105 #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4 |
|
106 #define SPEEX_RESAMPLER_QUALITY_VOIP 3 |
|
107 #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5 |
|
108 |
|
109 enum |
|
110 { |
|
111 RESAMPLER_ERR_SUCCESS = 0, |
|
112 RESAMPLER_ERR_ALLOC_FAILED = 1, |
|
113 RESAMPLER_ERR_BAD_STATE = 2, |
|
114 RESAMPLER_ERR_INVALID_ARG = 3, |
|
115 RESAMPLER_ERR_PTR_OVERLAP = 4, |
|
116 |
|
117 RESAMPLER_ERR_MAX_ERROR |
|
118 }; |
|
119 |
|
120 struct SpeexResamplerState_; |
|
121 typedef struct SpeexResamplerState_ SpeexResamplerState; |
|
122 |
|
123 /** Create a new resampler with integer input and output rates. |
|
124 * @param nb_channels Number of channels to be processed |
|
125 * @param in_rate Input sampling rate (integer number of Hz). |
|
126 * @param out_rate Output sampling rate (integer number of Hz). |
|
127 * @param quality Resampling quality between 0 and 10, where 0 has poor quality |
|
128 * and 10 has very high quality. |
|
129 * @return Newly created resampler state |
|
130 * @retval NULL Error: not enough memory |
|
131 */ |
|
132 SpeexResamplerState *speex_resampler_init (spx_uint32_t nb_channels, |
|
133 spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err); |
|
134 |
|
135 /** Create a new resampler with fractional input/output rates. The sampling |
|
136 * rate ratio is an arbitrary rational number with both the numerator and |
|
137 * denominator being 32-bit integers. |
|
138 * @param nb_channels Number of channels to be processed |
|
139 * @param ratio_num Numerator of the sampling rate ratio |
|
140 * @param ratio_den Denominator of the sampling rate ratio |
|
141 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz). |
|
142 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz). |
|
143 * @param quality Resampling quality between 0 and 10, where 0 has poor quality |
|
144 * and 10 has very high quality. |
|
145 * @return Newly created resampler state |
|
146 * @retval NULL Error: not enough memory |
|
147 */ |
|
148 SpeexResamplerState *speex_resampler_init_frac (spx_uint32_t nb_channels, |
|
149 spx_uint32_t ratio_num, |
|
150 spx_uint32_t ratio_den, |
|
151 spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err); |
|
152 |
|
153 /** Destroy a resampler state. |
|
154 * @param st Resampler state |
|
155 */ |
|
156 void speex_resampler_destroy (SpeexResamplerState * st); |
|
157 |
|
158 /** Resample a float array. The input and output buffers must *not* overlap. |
|
159 * @param st Resampler state |
|
160 * @param channel_index Index of the channel to process for the multi-channel |
|
161 * base (0 otherwise) |
|
162 * @param in Input buffer |
|
163 * @param in_len Number of input samples in the input buffer. Returns the |
|
164 * number of samples processed |
|
165 * @param out Output buffer |
|
166 * @param out_len Size of the output buffer. Returns the number of samples written |
|
167 */ |
|
168 #ifdef DOUBLE_PRECISION |
|
169 int speex_resampler_process_float (SpeexResamplerState * st, |
|
170 spx_uint32_t channel_index, |
|
171 const double *in, |
|
172 spx_uint32_t * in_len, double *out, spx_uint32_t * out_len); |
|
173 #else |
|
174 int speex_resampler_process_float (SpeexResamplerState * st, |
|
175 spx_uint32_t channel_index, |
|
176 const float *in, |
|
177 spx_uint32_t * in_len, float *out, spx_uint32_t * out_len); |
|
178 #endif |
|
179 |
|
180 /** Resample an int array. The input and output buffers must *not* overlap. |
|
181 * @param st Resampler state |
|
182 * @param channel_index Index of the channel to process for the multi-channel |
|
183 * base (0 otherwise) |
|
184 * @param in Input buffer |
|
185 * @param in_len Number of input samples in the input buffer. Returns the number |
|
186 * of samples processed |
|
187 * @param out Output buffer |
|
188 * @param out_len Size of the output buffer. Returns the number of samples written |
|
189 */ |
|
190 int speex_resampler_process_int (SpeexResamplerState * st, |
|
191 spx_uint32_t channel_index, |
|
192 const spx_int16_t * in, |
|
193 spx_uint32_t * in_len, spx_int16_t * out, spx_uint32_t * out_len); |
|
194 |
|
195 /** Resample an interleaved float array. The input and output buffers must *not* overlap. |
|
196 * @param st Resampler state |
|
197 * @param in Input buffer |
|
198 * @param in_len Number of input samples in the input buffer. Returns the number |
|
199 * of samples processed. This is all per-channel. |
|
200 * @param out Output buffer |
|
201 * @param out_len Size of the output buffer. Returns the number of samples written. |
|
202 * This is all per-channel. |
|
203 */ |
|
204 #ifdef DOUBLE_PRECISION |
|
205 int speex_resampler_process_interleaved_float (SpeexResamplerState * st, |
|
206 const double *in, |
|
207 spx_uint32_t * in_len, double *out, spx_uint32_t * out_len); |
|
208 #else |
|
209 int speex_resampler_process_interleaved_float (SpeexResamplerState * st, |
|
210 const float *in, |
|
211 spx_uint32_t * in_len, float *out, spx_uint32_t * out_len); |
|
212 #endif |
|
213 |
|
214 /** Resample an interleaved int array. The input and output buffers must *not* overlap. |
|
215 * @param st Resampler state |
|
216 * @param in Input buffer |
|
217 * @param in_len Number of input samples in the input buffer. Returns the number |
|
218 * of samples processed. This is all per-channel. |
|
219 * @param out Output buffer |
|
220 * @param out_len Size of the output buffer. Returns the number of samples written. |
|
221 * This is all per-channel. |
|
222 */ |
|
223 int speex_resampler_process_interleaved_int (SpeexResamplerState * st, |
|
224 const spx_int16_t * in, |
|
225 spx_uint32_t * in_len, spx_int16_t * out, spx_uint32_t * out_len); |
|
226 |
|
227 /** Set (change) the input/output sampling rates (integer value). |
|
228 * @param st Resampler state |
|
229 * @param in_rate Input sampling rate (integer number of Hz). |
|
230 * @param out_rate Output sampling rate (integer number of Hz). |
|
231 */ |
|
232 int speex_resampler_set_rate (SpeexResamplerState * st, |
|
233 spx_uint32_t in_rate, spx_uint32_t out_rate); |
|
234 |
|
235 /** Get the current input/output sampling rates (integer value). |
|
236 * @param st Resampler state |
|
237 * @param in_rate Input sampling rate (integer number of Hz) copied. |
|
238 * @param out_rate Output sampling rate (integer number of Hz) copied. |
|
239 */ |
|
240 void speex_resampler_get_rate (SpeexResamplerState * st, |
|
241 spx_uint32_t * in_rate, spx_uint32_t * out_rate); |
|
242 |
|
243 /** Set (change) the input/output sampling rates and resampling ratio |
|
244 * (fractional values in Hz supported). |
|
245 * @param st Resampler state |
|
246 * @param ratio_num Numerator of the sampling rate ratio |
|
247 * @param ratio_den Denominator of the sampling rate ratio |
|
248 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz). |
|
249 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz). |
|
250 */ |
|
251 int speex_resampler_set_rate_frac (SpeexResamplerState * st, |
|
252 spx_uint32_t ratio_num, |
|
253 spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate); |
|
254 |
|
255 /** Get the current resampling ratio. This will be reduced to the least |
|
256 * common denominator. |
|
257 * @param st Resampler state |
|
258 * @param ratio_num Numerator of the sampling rate ratio copied |
|
259 * @param ratio_den Denominator of the sampling rate ratio copied |
|
260 */ |
|
261 void speex_resampler_get_ratio (SpeexResamplerState * st, |
|
262 spx_uint32_t * ratio_num, spx_uint32_t * ratio_den); |
|
263 |
|
264 /** Set (change) the conversion quality. |
|
265 * @param st Resampler state |
|
266 * @param quality Resampling quality between 0 and 10, where 0 has poor |
|
267 * quality and 10 has very high quality. |
|
268 */ |
|
269 int speex_resampler_set_quality (SpeexResamplerState * st, int quality); |
|
270 |
|
271 /** Get the conversion quality. |
|
272 * @param st Resampler state |
|
273 * @param quality Resampling quality between 0 and 10, where 0 has poor |
|
274 * quality and 10 has very high quality. |
|
275 */ |
|
276 void speex_resampler_get_quality (SpeexResamplerState * st, int *quality); |
|
277 |
|
278 /** Set (change) the input stride. |
|
279 * @param st Resampler state |
|
280 * @param stride Input stride |
|
281 */ |
|
282 void speex_resampler_set_input_stride (SpeexResamplerState * st, |
|
283 spx_uint32_t stride); |
|
284 |
|
285 /** Get the input stride. |
|
286 * @param st Resampler state |
|
287 * @param stride Input stride copied |
|
288 */ |
|
289 void speex_resampler_get_input_stride (SpeexResamplerState * st, |
|
290 spx_uint32_t * stride); |
|
291 |
|
292 /** Set (change) the output stride. |
|
293 * @param st Resampler state |
|
294 * @param stride Output stride |
|
295 */ |
|
296 void speex_resampler_set_output_stride (SpeexResamplerState * st, |
|
297 spx_uint32_t stride); |
|
298 |
|
299 /** Get the output stride. |
|
300 * @param st Resampler state copied |
|
301 * @param stride Output stride |
|
302 */ |
|
303 void speex_resampler_get_output_stride (SpeexResamplerState * st, |
|
304 spx_uint32_t * stride); |
|
305 |
|
306 /** Get the latency in input samples introduced by the resampler. |
|
307 * @param st Resampler state |
|
308 */ |
|
309 int speex_resampler_get_input_latency (SpeexResamplerState * st); |
|
310 |
|
311 /** Get the latency in output samples introduced by the resampler. |
|
312 * @param st Resampler state |
|
313 */ |
|
314 int speex_resampler_get_output_latency (SpeexResamplerState * st); |
|
315 |
|
316 /** Make sure that the first samples to go out of the resamplers don't have |
|
317 * leading zeros. This is only useful before starting to use a newly created |
|
318 * resampler. It is recommended to use that when resampling an audio file, as |
|
319 * it will generate a file with the same length. For real-time processing, |
|
320 * it is probably easier not to use this call (so that the output duration |
|
321 * is the same for the first frame). |
|
322 * @param st Resampler state |
|
323 */ |
|
324 int speex_resampler_skip_zeros (SpeexResamplerState * st); |
|
325 |
|
326 /** Reset a resampler so a new (unrelated) stream can be processed. |
|
327 * @param st Resampler state |
|
328 */ |
|
329 int speex_resampler_reset_mem (SpeexResamplerState * st); |
|
330 |
|
331 /** Returns the English meaning for an error code |
|
332 * @param err Error code |
|
333 * @return English string |
|
334 */ |
|
335 const char *speex_resampler_strerror (int err); |
|
336 |
|
337 #ifdef __cplusplus |
|
338 } |
|
339 #endif |
|
340 |
|
341 #endif |