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
46
/********* WARNING: MENTAL SANITY ENDS HERE *************/
48
/* If the resampler is defined outside of Speex, we change the symbol names so that
49
there won't be any clash if linking with Speex later on. */
51
/* #define RANDOM_PREFIX your software name here */
53
#error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
56
#define CAT_PREFIX2(a,b) a ## b
57
#define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)
59
#define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init)
60
#define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac)
61
#define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy)
62
#define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float)
63
#define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int)
64
#define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float)
65
#define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int)
66
#define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate)
67
#define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate)
68
#define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac)
69
#define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio)
70
#define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality)
71
#define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality)
72
#define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride)
73
#define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride)
74
#define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride)
75
#define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride)
76
#define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency)
77
#define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency)
78
#define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
79
#define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
80
#define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
82
#define spx_int16_t gint16
83
#define spx_int32_t gint32
84
#define spx_uint16_t guint16
85
#define spx_uint32_t guint32
87
#else /* OUTSIDE_SPEEX */
89
#include "speex/speex_types.h"
91
#endif /* OUTSIDE_SPEEX */
97
#define SPEEX_RESAMPLER_QUALITY_MAX 10
98
#define SPEEX_RESAMPLER_QUALITY_MIN 0
99
#define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
100
#define SPEEX_RESAMPLER_QUALITY_VOIP 3
101
#define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
104
RESAMPLER_ERR_SUCCESS = 0,
105
RESAMPLER_ERR_ALLOC_FAILED = 1,
106
RESAMPLER_ERR_BAD_STATE = 2,
107
RESAMPLER_ERR_INVALID_ARG = 3,
108
RESAMPLER_ERR_PTR_OVERLAP = 4,
110
RESAMPLER_ERR_MAX_ERROR
113
struct SpeexResamplerState_;
114
typedef struct SpeexResamplerState_ SpeexResamplerState;
116
/** Create a new resampler with integer input and output rates.
117
* @param nb_channels Number of channels to be processed
118
* @param in_rate Input sampling rate (integer number of Hz).
119
* @param out_rate Output sampling rate (integer number of Hz).
120
* @param quality Resampling quality between 0 and 10, where 0 has poor quality
121
* and 10 has very high quality.
122
* @return Newly created resampler state
123
* @retval NULL Error: not enough memory
125
SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels,
126
spx_uint32_t in_rate,
127
spx_uint32_t out_rate,
131
/** Create a new resampler with fractional input/output rates. The sampling
132
* rate ratio is an arbitrary rational number with both the numerator and
133
* denominator being 32-bit integers.
134
* @param nb_channels Number of channels to be processed
135
* @param ratio_num Numerator of the sampling rate ratio
136
* @param ratio_den Denominator of the sampling rate ratio
137
* @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
138
* @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
139
* @param quality Resampling quality between 0 and 10, where 0 has poor quality
140
* and 10 has very high quality.
141
* @return Newly created resampler state
142
* @retval NULL Error: not enough memory
144
SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
145
spx_uint32_t ratio_num,
146
spx_uint32_t ratio_den,
147
spx_uint32_t in_rate,
148
spx_uint32_t out_rate,
152
/** Destroy a resampler state.
153
* @param st Resampler state
155
void speex_resampler_destroy(SpeexResamplerState *st);
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
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
167
int speex_resampler_process_float(SpeexResamplerState *st,
168
spx_uint32_t channel_index,
170
spx_uint32_t *in_len,
172
spx_uint32_t *out_len);
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
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
184
int speex_resampler_process_int(SpeexResamplerState *st,
185
spx_uint32_t channel_index,
186
const spx_int16_t *in,
187
spx_uint32_t *in_len,
189
spx_uint32_t *out_len);
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.
200
int speex_resampler_process_interleaved_float(SpeexResamplerState *st,
202
spx_uint32_t *in_len,
204
spx_uint32_t *out_len);
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.
215
int speex_resampler_process_interleaved_int(SpeexResamplerState *st,
216
const spx_int16_t *in,
217
spx_uint32_t *in_len,
219
spx_uint32_t *out_len);
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).
226
int speex_resampler_set_rate(SpeexResamplerState *st,
227
spx_uint32_t in_rate,
228
spx_uint32_t out_rate);
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.
235
void speex_resampler_get_rate(SpeexResamplerState *st,
236
spx_uint32_t *in_rate,
237
spx_uint32_t *out_rate);
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).
247
int speex_resampler_set_rate_frac(SpeexResamplerState *st,
248
spx_uint32_t ratio_num,
249
spx_uint32_t ratio_den,
250
spx_uint32_t in_rate,
251
spx_uint32_t out_rate);
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
259
void speex_resampler_get_ratio(SpeexResamplerState *st,
260
spx_uint32_t *ratio_num,
261
spx_uint32_t *ratio_den);
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.
268
int speex_resampler_set_quality(SpeexResamplerState *st,
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.
276
void speex_resampler_get_quality(SpeexResamplerState *st,
279
/** Set (change) the input stride.
280
* @param st Resampler state
281
* @param stride Input stride
283
void speex_resampler_set_input_stride(SpeexResamplerState *st,
284
spx_uint32_t stride);
286
/** Get the input stride.
287
* @param st Resampler state
288
* @param stride Input stride copied
290
void speex_resampler_get_input_stride(SpeexResamplerState *st,
291
spx_uint32_t *stride);
293
/** Set (change) the output stride.
294
* @param st Resampler state
295
* @param stride Output stride
297
void speex_resampler_set_output_stride(SpeexResamplerState *st,
298
spx_uint32_t stride);
300
/** Get the output stride.
301
* @param st Resampler state copied
302
* @param stride Output stride
304
void speex_resampler_get_output_stride(SpeexResamplerState *st,
305
spx_uint32_t *stride);
307
/** Get the latency in input samples introduced by the resampler.
308
* @param st Resampler state
310
int speex_resampler_get_input_latency(SpeexResamplerState *st);
312
/** Get the latency in output samples introduced by the resampler.
313
* @param st Resampler state
315
int speex_resampler_get_output_latency(SpeexResamplerState *st);
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
325
int speex_resampler_skip_zeros(SpeexResamplerState *st);
327
/** Reset a resampler so a new (unrelated) stream can be processed.
328
* @param st Resampler state
330
int speex_resampler_reset_mem(SpeexResamplerState *st);
332
/** Returns the English meaning for an error code
333
* @param err Error code
334
* @return English string
336
const char *speex_resampler_strerror(int err);