Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* Copyright (C) 2007 Jean-Marc Valin
3 File: speex_resampler.h
4 Resampling code
6 The design goals of this code are:
7 - Very fast algorithm
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
13 met:
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.
36 */
39 #ifndef SPEEX_RESAMPLER_H
40 #define SPEEX_RESAMPLER_H
42 #ifdef OUTSIDE_SPEEX
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 */
50 #ifndef RANDOM_PREFIX
51 #error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
52 #endif
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_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
77 #define speex_resampler_set_skip_frac_num CAT_PREFIX(RANDOM_PREFIX,_resampler_set_skip_frac_num)
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 short
82 #define spx_int32_t int
83 #define spx_uint16_t unsigned short
84 #define spx_uint32_t unsigned int
86 #else /* OUTSIDE_SPEEX */
88 #ifdef _BUILD_SPEEX
89 # include "speex_types.h"
90 #else
91 # include <speex/speex_types.h>
92 #endif
94 #endif /* OUTSIDE_SPEEX */
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
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
106 enum {
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
114 };
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
127 */
128 SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels,
129 spx_uint32_t in_rate,
130 spx_uint32_t out_rate,
131 int quality,
132 int *err);
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
146 */
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,
152 int quality,
153 int *err);
155 /** Destroy a resampler state.
156 * @param st Resampler state
157 */
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
163 * base (0 otherwise)
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
169 */
170 int speex_resampler_process_float(SpeexResamplerState *st,
171 spx_uint32_t channel_index,
172 const float *in,
173 spx_uint32_t *in_len,
174 float *out,
175 spx_uint32_t *out_len);
177 /** Resample an int array. The input and output buffers must *not* overlap.
178 * @param st Resampler state
179 * @param channel_index Index of the channel to process for the multi-channel
180 * base (0 otherwise)
181 * @param in Input buffer
182 * @param in_len Number of input samples in the input buffer. Returns the number
183 * of samples processed
184 * @param out Output buffer
185 * @param out_len Size of the output buffer. Returns the number of samples written
186 */
187 int speex_resampler_process_int(SpeexResamplerState *st,
188 spx_uint32_t channel_index,
189 const spx_int16_t *in,
190 spx_uint32_t *in_len,
191 spx_int16_t *out,
192 spx_uint32_t *out_len);
194 /** Resample an interleaved float array. The input and output buffers must *not* overlap.
195 * @param st Resampler state
196 * @param in Input buffer
197 * @param in_len Number of input samples in the input buffer. Returns the number
198 * of samples processed. This is all per-channel.
199 * @param out Output buffer
200 * @param out_len Size of the output buffer. Returns the number of samples written.
201 * This is all per-channel.
202 */
203 int speex_resampler_process_interleaved_float(SpeexResamplerState *st,
204 const float *in,
205 spx_uint32_t *in_len,
206 float *out,
207 spx_uint32_t *out_len);
209 /** Resample an interleaved int array. The input and output buffers must *not* overlap.
210 * @param st Resampler state
211 * @param in Input buffer
212 * @param in_len Number of input samples in the input buffer. Returns the number
213 * of samples processed. This is all per-channel.
214 * @param out Output buffer
215 * @param out_len Size of the output buffer. Returns the number of samples written.
216 * This is all per-channel.
217 */
218 int speex_resampler_process_interleaved_int(SpeexResamplerState *st,
219 const spx_int16_t *in,
220 spx_uint32_t *in_len,
221 spx_int16_t *out,
222 spx_uint32_t *out_len);
224 /** Set (change) the input/output sampling rates (integer value).
225 * @param st Resampler state
226 * @param in_rate Input sampling rate (integer number of Hz).
227 * @param out_rate Output sampling rate (integer number of Hz).
228 */
229 int speex_resampler_set_rate(SpeexResamplerState *st,
230 spx_uint32_t in_rate,
231 spx_uint32_t out_rate);
233 /** Get the current input/output sampling rates (integer value).
234 * @param st Resampler state
235 * @param in_rate Input sampling rate (integer number of Hz) copied.
236 * @param out_rate Output sampling rate (integer number of Hz) copied.
237 */
238 void speex_resampler_get_rate(SpeexResamplerState *st,
239 spx_uint32_t *in_rate,
240 spx_uint32_t *out_rate);
242 /** Set (change) the input/output sampling rates and resampling ratio
243 * (fractional values in Hz supported).
244 * @param st Resampler state
245 * @param ratio_num Numerator of the sampling rate ratio
246 * @param ratio_den Denominator of the sampling rate ratio
247 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
248 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
249 */
250 int speex_resampler_set_rate_frac(SpeexResamplerState *st,
251 spx_uint32_t ratio_num,
252 spx_uint32_t ratio_den,
253 spx_uint32_t in_rate,
254 spx_uint32_t out_rate);
256 /** Get the current resampling ratio. This will be reduced to the least
257 * common denominator.
258 * @param st Resampler state
259 * @param ratio_num Numerator of the sampling rate ratio copied
260 * @param ratio_den Denominator of the sampling rate ratio copied
261 */
262 void speex_resampler_get_ratio(SpeexResamplerState *st,
263 spx_uint32_t *ratio_num,
264 spx_uint32_t *ratio_den);
266 /** Set (change) the conversion quality.
267 * @param st Resampler state
268 * @param quality Resampling quality between 0 and 10, where 0 has poor
269 * quality and 10 has very high quality.
270 */
271 int speex_resampler_set_quality(SpeexResamplerState *st,
272 int quality);
274 /** Get the conversion quality.
275 * @param st Resampler state
276 * @param quality Resampling quality between 0 and 10, where 0 has poor
277 * quality and 10 has very high quality.
278 */
279 void speex_resampler_get_quality(SpeexResamplerState *st,
280 int *quality);
282 /** Set (change) the input stride.
283 * @param st Resampler state
284 * @param stride Input stride
285 */
286 void speex_resampler_set_input_stride(SpeexResamplerState *st,
287 spx_uint32_t stride);
289 /** Get the input stride.
290 * @param st Resampler state
291 * @param stride Input stride copied
292 */
293 void speex_resampler_get_input_stride(SpeexResamplerState *st,
294 spx_uint32_t *stride);
296 /** Set (change) the output stride.
297 * @param st Resampler state
298 * @param stride Output stride
299 */
300 void speex_resampler_set_output_stride(SpeexResamplerState *st,
301 spx_uint32_t stride);
303 /** Get the output stride.
304 * @param st Resampler state copied
305 * @param stride Output stride
306 */
307 void speex_resampler_get_output_stride(SpeexResamplerState *st,
308 spx_uint32_t *stride);
310 /** Get the latency introduced by the resampler measured in input samples.
311 * @param st Resampler state
312 */
313 int speex_resampler_get_input_latency(SpeexResamplerState *st);
315 /** Get the latency introduced by the resampler measured in output samples.
316 * @param st Resampler state
317 */
318 int speex_resampler_get_output_latency(SpeexResamplerState *st);
320 /** Make sure that the first samples to go out of the resamplers don't have
321 * leading zeros. This is only useful before starting to use a newly created
322 * resampler. It is recommended to use that when resampling an audio file, as
323 * it will generate a file with the same length. For real-time processing,
324 * it is probably easier not to use this call (so that the output duration
325 * is the same for the first frame).
326 * @param st Resampler state
327 */
328 int speex_resampler_skip_zeros(SpeexResamplerState *st);
330 /** Set the numerator in a fraction determining the advance through input
331 * samples before writing any output samples. The denominator of the fraction
332 * is the value returned from speex_resampler_get_ratio() in ratio_den. This
333 * is only useful before starting to use a newly created or reset resampler.
334 * If the first input sample is interpreted as the signal at time
335 * input_latency*in_rate, then the first output sample represents the signal
336 * at the time frac_num/ratio_num*out_rate.
337 * This is intended for careful alignment of output sample points wrt input
338 * sample points. Large values are not an efficient offset into the in buffer.
339 * @param st Resampler state
340 * @param skip_frac_num Numerator of the offset fraction,
341 * between 0 and ratio_den-1.
342 */
343 int speex_resampler_set_skip_frac_num(SpeexResamplerState *st,
344 spx_uint32_t skip_frac_num);
346 /** Reset a resampler so a new (unrelated) stream can be processed.
347 * @param st Resampler state
348 */
349 int speex_resampler_reset_mem(SpeexResamplerState *st);
351 /** Returns the English meaning for an error code
352 * @param err Error code
353 * @return English string
354 */
355 const char *speex_resampler_strerror(int err);
357 #ifdef __cplusplus
358 }
359 #endif
361 #endif