1 /* Copyright (C) 2007 Jean-Marc Valin
3 File: speex_resampler.h
6 The design goals of this code are:
8 - Low memory requirement
9 - Good *perceptual* quality (and not best SNR)
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are
15 1. Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
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.
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.
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.
39 #ifndef SPEEX_RESAMPLER_H
40 #define SPEEX_RESAMPLER_H
44 /********* WARNING: MENTAL SANITY ENDS HERE *************/
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. */
49 /* #define RANDOM_PREFIX your software name here */
51 #error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
54 #define CAT_PREFIX2(a,b) a ## b
55 #define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)
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_get_filt_len CAT_PREFIX(RANDOM_PREFIX,_resampler_get_filt_len)
77 #define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
78 #define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
79 #define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
81 #define spx_int16_t gint16
82 #define spx_int32_t gint32
83 #define spx_uint16_t guint16
84 #define spx_uint32_t guint32
86 #else /* OUTSIDE_SPEEX */
89 # include "speex_types.h"
91 # include <speex/speex_types.h>
94 #endif /* OUTSIDE_SPEEX */
100 #define SPEEX_RESAMPLER_QUALITY_MAX 10
101 #define SPEEX_RESAMPLER_QUALITY_MIN 0
102 #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
103 #define SPEEX_RESAMPLER_QUALITY_VOIP 3
104 #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
107 RESAMPLER_ERR_SUCCESS = 0,
108 RESAMPLER_ERR_ALLOC_FAILED = 1,
109 RESAMPLER_ERR_BAD_STATE = 2,
110 RESAMPLER_ERR_INVALID_ARG = 3,
111 RESAMPLER_ERR_PTR_OVERLAP = 4,
113 RESAMPLER_ERR_MAX_ERROR
116 struct SpeexResamplerState_;
117 typedef struct SpeexResamplerState_ SpeexResamplerState;
119 /** Create a new resampler with integer input and output rates.
120 * @param nb_channels Number of channels to be processed
121 * @param in_rate Input sampling rate (integer number of Hz).
122 * @param out_rate Output sampling rate (integer number of Hz).
123 * @param quality Resampling quality between 0 and 10, where 0 has poor quality
124 * and 10 has very high quality.
125 * @return Newly created resampler state
126 * @retval NULL Error: not enough memory
128 SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels,
129 spx_uint32_t in_rate,
130 spx_uint32_t out_rate,
134 /** Create a new resampler with fractional input/output rates. The sampling
135 * rate ratio is an arbitrary rational number with both the numerator and
136 * denominator being 32-bit integers.
137 * @param nb_channels Number of channels to be processed
138 * @param ratio_num Numerator of the sampling rate ratio
139 * @param ratio_den Denominator of the sampling rate ratio
140 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
141 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
142 * @param quality Resampling quality between 0 and 10, where 0 has poor quality
143 * and 10 has very high quality.
144 * @return Newly created resampler state
145 * @retval NULL Error: not enough memory
147 SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
148 spx_uint32_t ratio_num,
149 spx_uint32_t ratio_den,
150 spx_uint32_t in_rate,
151 spx_uint32_t out_rate,
155 /** Destroy a resampler state.
156 * @param st Resampler state
158 void speex_resampler_destroy(SpeexResamplerState *st);
160 /** Resample a float array. The input and output buffers must *not* overlap.
161 * @param st Resampler state
162 * @param channel_index Index of the channel to process for the multi-channel
164 * @param in Input buffer
165 * @param in_len Number of input samples in the input buffer. Returns the
166 * number of samples processed
167 * @param out Output buffer
168 * @param out_len Size of the output buffer. Returns the number of samples written
170 #ifdef DOUBLE_PRECISION
171 int speex_resampler_process_float(SpeexResamplerState *st,
172 spx_uint32_t channel_index,
174 spx_uint32_t *in_len,
176 spx_uint32_t *out_len);
178 int speex_resampler_process_float(SpeexResamplerState *st,
179 spx_uint32_t channel_index,
181 spx_uint32_t *in_len,
183 spx_uint32_t *out_len);
186 /** Resample an int array. The input and output buffers must *not* overlap.
187 * @param st Resampler state
188 * @param channel_index Index of the channel to process for the multi-channel
190 * @param in Input buffer
191 * @param in_len Number of input samples in the input buffer. Returns the number
192 * of samples processed
193 * @param out Output buffer
194 * @param out_len Size of the output buffer. Returns the number of samples written
196 int speex_resampler_process_int(SpeexResamplerState *st,
197 spx_uint32_t channel_index,
198 const spx_int16_t *in,
199 spx_uint32_t *in_len,
201 spx_uint32_t *out_len);
203 /** Resample an interleaved float array. The input and output buffers must *not* overlap.
204 * @param st Resampler state
205 * @param in Input buffer
206 * @param in_len Number of input samples in the input buffer. Returns the number
207 * of samples processed. This is all per-channel.
208 * @param out Output buffer
209 * @param out_len Size of the output buffer. Returns the number of samples written.
210 * This is all per-channel.
212 #ifdef DOUBLE_PRECISION
213 int speex_resampler_process_interleaved_float(SpeexResamplerState *st,
215 spx_uint32_t *in_len,
217 spx_uint32_t *out_len);
219 int speex_resampler_process_interleaved_float(SpeexResamplerState *st,
221 spx_uint32_t *in_len,
223 spx_uint32_t *out_len);
226 /** Resample an interleaved int array. The input and output buffers must *not* overlap.
227 * @param st Resampler state
228 * @param in Input buffer
229 * @param in_len Number of input samples in the input buffer. Returns the number
230 * of samples processed. This is all per-channel.
231 * @param out Output buffer
232 * @param out_len Size of the output buffer. Returns the number of samples written.
233 * This is all per-channel.
235 int speex_resampler_process_interleaved_int(SpeexResamplerState *st,
236 const spx_int16_t *in,
237 spx_uint32_t *in_len,
239 spx_uint32_t *out_len);
241 /** Set (change) the input/output sampling rates (integer value).
242 * @param st Resampler state
243 * @param in_rate Input sampling rate (integer number of Hz).
244 * @param out_rate Output sampling rate (integer number of Hz).
246 int speex_resampler_set_rate(SpeexResamplerState *st,
247 spx_uint32_t in_rate,
248 spx_uint32_t out_rate);
250 /** Get the current input/output sampling rates (integer value).
251 * @param st Resampler state
252 * @param in_rate Input sampling rate (integer number of Hz) copied.
253 * @param out_rate Output sampling rate (integer number of Hz) copied.
255 void speex_resampler_get_rate(SpeexResamplerState *st,
256 spx_uint32_t *in_rate,
257 spx_uint32_t *out_rate);
259 /** Set (change) the input/output sampling rates and resampling ratio
260 * (fractional values in Hz supported).
261 * @param st Resampler state
262 * @param ratio_num Numerator of the sampling rate ratio
263 * @param ratio_den Denominator of the sampling rate ratio
264 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
265 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
267 int speex_resampler_set_rate_frac(SpeexResamplerState *st,
268 spx_uint32_t ratio_num,
269 spx_uint32_t ratio_den,
270 spx_uint32_t in_rate,
271 spx_uint32_t out_rate);
273 /** Get the current resampling ratio. This will be reduced to the least
274 * common denominator.
275 * @param st Resampler state
276 * @param ratio_num Numerator of the sampling rate ratio copied
277 * @param ratio_den Denominator of the sampling rate ratio copied
279 void speex_resampler_get_ratio(SpeexResamplerState *st,
280 spx_uint32_t *ratio_num,
281 spx_uint32_t *ratio_den);
283 /** Set (change) the conversion quality.
284 * @param st Resampler state
285 * @param quality Resampling quality between 0 and 10, where 0 has poor
286 * quality and 10 has very high quality.
288 int speex_resampler_set_quality(SpeexResamplerState *st,
291 /** Get the conversion quality.
292 * @param st Resampler state
293 * @param quality Resampling quality between 0 and 10, where 0 has poor
294 * quality and 10 has very high quality.
296 void speex_resampler_get_quality(SpeexResamplerState *st,
299 /** Set (change) the input stride.
300 * @param st Resampler state
301 * @param stride Input stride
303 void speex_resampler_set_input_stride(SpeexResamplerState *st,
304 spx_uint32_t stride);
306 /** Get the input stride.
307 * @param st Resampler state
308 * @param stride Input stride copied
310 void speex_resampler_get_input_stride(SpeexResamplerState *st,
311 spx_uint32_t *stride);
313 /** Set (change) the output stride.
314 * @param st Resampler state
315 * @param stride Output stride
317 void speex_resampler_set_output_stride(SpeexResamplerState *st,
318 spx_uint32_t stride);
320 /** Get the output stride.
321 * @param st Resampler state copied
322 * @param stride Output stride
324 void speex_resampler_get_output_stride(SpeexResamplerState *st,
325 spx_uint32_t *stride);
327 /** Get the latency introduced by the resampler measured in input samples.
328 * @param st Resampler state
330 int speex_resampler_get_input_latency(SpeexResamplerState *st);
332 /** Get the latency introduced by the resampler measured in output samples.
333 * @param st Resampler state
335 int speex_resampler_get_output_latency(SpeexResamplerState *st);
337 /** Get the length of the filter in input samples.
338 * @param st Resampler state
340 int speex_resampler_get_filt_len(SpeexResamplerState *st);
342 /** Make sure that the first samples to go out of the resamplers don't have
343 * leading zeros. This is only useful before starting to use a newly created
344 * resampler. It is recommended to use that when resampling an audio file, as
345 * it will generate a file with the same length. For real-time processing,
346 * it is probably easier not to use this call (so that the output duration
347 * is the same for the first frame).
348 * @param st Resampler state
350 int speex_resampler_skip_zeros(SpeexResamplerState *st);
352 /** Reset a resampler so a new (unrelated) stream can be processed.
353 * @param st Resampler state
355 int speex_resampler_reset_mem(SpeexResamplerState *st);
357 /** Returns the English meaning for an error code
358 * @param err Error code
359 * @return English string
361 const char *speex_resampler_strerror(int err);