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 #ifndef SPEEX_RESAMPLER_H
39 #define SPEEX_RESAMPLER_H
40 
41 #define OUTSIDE_SPEEX 1
42 #define RANDOM_PREFIX ws_codec
43 #include "ws_symbol_export.h"
44 #define EXPORT
45 
46 #ifdef OUTSIDE_SPEEX
47 
48 /********* WARNING: MENTAL SANITY ENDS HERE *************/
49 
50 /* If the resampler is defined outside of Speex, we change the symbol names so that
51    there won't be any clash if linking with Speex later on. */
52 
53 /* #define RANDOM_PREFIX your software name here */
54 #ifndef RANDOM_PREFIX
55 #error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
56 #endif
57 
58 #define CAT_PREFIX2(a,b) a ## b
59 #define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)
60 
61 #define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init)
62 #define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac)
63 #define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy)
64 #define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float)
65 #define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int)
66 #define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float)
67 #define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int)
68 #define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate)
69 #define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate)
70 #define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac)
71 #define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio)
72 #define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality)
73 #define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality)
74 #define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride)
75 #define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride)
76 #define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride)
77 #define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride)
78 #define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency)
79 #define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency)
80 #define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
81 #define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
82 #define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
83 
84 #define spx_int16_t short
85 #define spx_int32_t int
86 #define spx_uint16_t unsigned short
87 #define spx_uint32_t unsigned int
88 
89 #else /* OUTSIDE_SPEEX */
90 
91 #include "speexdsp_types.h"
92 
93 #endif /* OUTSIDE_SPEEX */
94 
95 #ifdef __cplusplus
96 extern "C" {
97 #endif
98 
99 #define SPEEX_RESAMPLER_QUALITY_MAX 10
100 #define SPEEX_RESAMPLER_QUALITY_MIN 0
101 #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
102 #define SPEEX_RESAMPLER_QUALITY_VOIP 3
103 #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
104 
105 enum {
106    RESAMPLER_ERR_SUCCESS         = 0,
107    RESAMPLER_ERR_ALLOC_FAILED    = 1,
108    RESAMPLER_ERR_BAD_STATE       = 2,
109    RESAMPLER_ERR_INVALID_ARG     = 3,
110    RESAMPLER_ERR_PTR_OVERLAP     = 4,
111 
112    RESAMPLER_ERR_MAX_ERROR
113 };
114 
115 struct SpeexResamplerState_;
116 typedef struct SpeexResamplerState_ SpeexResamplerState;
117 
118 /** Create a new resampler with integer input and output rates.
119  * @param nb_channels Number of channels to be processed
120  * @param in_rate Input sampling rate (integer number of Hz).
121  * @param out_rate Output sampling rate (integer number of Hz).
122  * @param quality Resampling quality between 0 and 10, where 0 has poor quality
123  * and 10 has very high quality.
124  * @return Newly created resampler state
125  * @retval NULL Error: not enough memory
126  */
127 SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels,
128                                                         spx_uint32_t in_rate,
129                                                         spx_uint32_t out_rate,
130                                                         int quality,
131                                                         int *err);
132 
133 /** Create a new resampler with fractional input/output rates. The sampling
134  * rate ratio is an arbitrary rational number with both the numerator and
135  * denominator being 32-bit integers.
136  * @param nb_channels Number of channels to be processed
137  * @param ratio_num Numerator of the sampling rate ratio
138  * @param ratio_den Denominator of the sampling rate ratio
139  * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
140  * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
141  * @param quality Resampling quality between 0 and 10, where 0 has poor quality
142  * and 10 has very high quality.
143  * @return Newly created resampler state
144  * @retval NULL Error: not enough memory
145  */
146 SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
147                                                              spx_uint32_t ratio_num,
148                                                              spx_uint32_t ratio_den,
149                                                              spx_uint32_t in_rate,
150                                                              spx_uint32_t out_rate,
151                                                              int quality,
152                                                              int *err);
153 
154 /** Destroy a resampler state.
155  * @param st Resampler state
156  */
157 void speex_resampler_destroy(SpeexResamplerState *st);
158 
159 /** Resample a float array. The input and output buffers must *not* overlap.
160  * @param st Resampler state
161  * @param channel_index Index of the channel to process for the multi-channel
162  * base (0 otherwise)
163  * @param in Input buffer
164  * @param in_len Number of input samples in the input buffer. Returns the
165  * number of samples processed
166  * @param out Output buffer
167  * @param out_len Size of the output buffer. Returns the number of samples written
168  */
169 int speex_resampler_process_float(SpeexResamplerState *st,
170                                                 spx_uint32_t channel_index,
171                                                 const float *in,
172                                                 spx_uint32_t *in_len,
173                                                 float *out,
174                                                 spx_uint32_t *out_len);
175 
176 /** Resample an int array. The input and output buffers must *not* overlap.
177  * @param st Resampler state
178  * @param channel_index Index of the channel to process for the multi-channel
179  * base (0 otherwise)
180  * @param in Input buffer
181  * @param in_len Number of input samples in the input buffer. Returns the number
182  * of samples processed
183  * @param out Output buffer
184  * @param out_len Size of the output buffer. Returns the number of samples written
185  */
186 int speex_resampler_process_int(SpeexResamplerState *st,
187                                               spx_uint32_t channel_index,
188                                               const spx_int16_t *in,
189                                               spx_uint32_t *in_len,
190                                               spx_int16_t *out,
191                                               spx_uint32_t *out_len);
192 
193 /** Resample an interleaved float array. The input and output buffers must *not* overlap.
194  * @param st Resampler state
195  * @param in Input buffer
196  * @param in_len Number of input samples in the input buffer. Returns the number
197  * of samples processed. This is all per-channel.
198  * @param out Output buffer
199  * @param out_len Size of the output buffer. Returns the number of samples written.
200  * This is all per-channel.
201  */
202 int speex_resampler_process_interleaved_float(SpeexResamplerState *st,
203                                                             const float *in,
204                                                             spx_uint32_t *in_len,
205                                                             float *out,
206                                                             spx_uint32_t *out_len);
207 
208 /** Resample an interleaved int array. The input and output buffers must *not* overlap.
209  * @param st Resampler state
210  * @param in Input buffer
211  * @param in_len Number of input samples in the input buffer. Returns the number
212  * of samples processed. This is all per-channel.
213  * @param out Output buffer
214  * @param out_len Size of the output buffer. Returns the number of samples written.
215  * This is all per-channel.
216  */
217 int speex_resampler_process_interleaved_int(SpeexResamplerState *st,
218                                                           const spx_int16_t *in,
219                                                           spx_uint32_t *in_len,
220                                                           spx_int16_t *out,
221                                                           spx_uint32_t *out_len);
222 
223 /** Set (change) the input/output sampling rates (integer value).
224  * @param st Resampler state
225  * @param in_rate Input sampling rate (integer number of Hz).
226  * @param out_rate Output sampling rate (integer number of Hz).
227  */
228 int speex_resampler_set_rate(SpeexResamplerState *st,
229                                            spx_uint32_t in_rate,
230                                            spx_uint32_t out_rate);
231 
232 /** Get the current input/output sampling rates (integer value).
233  * @param st Resampler state
234  * @param in_rate Input sampling rate (integer number of Hz) copied.
235  * @param out_rate Output sampling rate (integer number of Hz) copied.
236  */
237 void speex_resampler_get_rate(SpeexResamplerState *st,
238                                             spx_uint32_t *in_rate,
239                                             spx_uint32_t *out_rate);
240 
241 /** Set (change) the input/output sampling rates and resampling ratio
242  * (fractional values in Hz supported).
243  * @param st Resampler state
244  * @param ratio_num Numerator of the sampling rate ratio
245  * @param ratio_den Denominator of the sampling rate ratio
246  * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
247  * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
248  */
249 int speex_resampler_set_rate_frac(SpeexResamplerState *st,
250                                                 spx_uint32_t ratio_num,
251                                                 spx_uint32_t ratio_den,
252                                                 spx_uint32_t in_rate,
253                                                 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,
263                                              spx_uint32_t *ratio_den);
264 
265 /** Set (change) the conversion quality.
266  * @param st Resampler state
267  * @param quality Resampling quality between 0 and 10, where 0 has poor
268  * quality and 10 has very high quality.
269  */
270 int speex_resampler_set_quality(SpeexResamplerState *st,
271                                               int quality);
272 
273 /** Get the conversion quality.
274  * @param st Resampler state
275  * @param quality Resampling quality between 0 and 10, where 0 has poor
276  * quality and 10 has very high quality.
277  */
278 void speex_resampler_get_quality(SpeexResamplerState *st,
279                                                int *quality);
280 
281 /** Set (change) the input stride.
282  * @param st Resampler state
283  * @param stride Input stride
284  */
285 void speex_resampler_set_input_stride(SpeexResamplerState *st,
286                                                     spx_uint32_t stride);
287 
288 /** Get the input stride.
289  * @param st Resampler state
290  * @param stride Input stride copied
291  */
292 void speex_resampler_get_input_stride(SpeexResamplerState *st,
293                                                     spx_uint32_t *stride);
294 
295 /** Set (change) the output stride.
296  * @param st Resampler state
297  * @param stride Output stride
298  */
299 void speex_resampler_set_output_stride(SpeexResamplerState *st,
300                                                      spx_uint32_t stride);
301 
302 /** Get the output stride.
303  * @param st Resampler state copied
304  * @param stride Output stride
305  */
306 void speex_resampler_get_output_stride(SpeexResamplerState *st,
307                                                      spx_uint32_t *stride);
308 
309 /** Get the latency introduced by the resampler measured in input samples.
310  * @param st Resampler state
311  */
312 int speex_resampler_get_input_latency(SpeexResamplerState *st);
313 
314 /** Get the latency introduced by the resampler measured in output samples.
315  * @param st Resampler state
316  */
317 int speex_resampler_get_output_latency(SpeexResamplerState *st);
318 
319 /** Make sure that the first samples to go out of the resamplers don't have
320  * leading zeros. This is only useful before starting to use a newly created
321  * resampler. It is recommended to use that when resampling an audio file, as
322  * it will generate a file with the same length. For real-time processing,
323  * it is probably easier not to use this call (so that the output duration
324  * is the same for the first frame).
325  * @param st Resampler state
326  */
327 int speex_resampler_skip_zeros(SpeexResamplerState *st);
328 
329 /** Reset a resampler so a new (unrelated) stream can be processed.
330  * @param st Resampler state
331  */
332 int speex_resampler_reset_mem(SpeexResamplerState *st);
333 
334 /** Returns the English meaning for an error code
335  * @param err Error code
336  * @return English string
337  */
338 const char *speex_resampler_strerror(int err);
339 
340 #ifdef __cplusplus
341 }
342 #endif
343 
344 #endif
345