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