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 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 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 #define speex_assert(cond) 86 87 #else /* OUTSIDE_SPEEX */ 88 89 #include "speexdsp_types.h" 90 91 #endif /* OUTSIDE_SPEEX */ 92 93 #ifdef __cplusplus 94 extern "C" { 95 #endif 96 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 102 103 enum { 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, 109 RESAMPLER_ERR_OVERFLOW = 5, 110 111 RESAMPLER_ERR_MAX_ERROR 112 }; 113 114 struct SpeexResamplerState_; 115 typedef struct SpeexResamplerState_ SpeexResamplerState; 116 117 /** Create a new resampler with integer input and output rates. 118 * @param nb_channels Number of channels to be processed 119 * @param in_rate Input sampling rate (integer number of Hz). 120 * @param out_rate Output sampling rate (integer number of Hz). 121 * @param quality Resampling quality between 0 and 10, where 0 has poor quality 122 * and 10 has very high quality. 123 * @return Newly created resampler state 124 * @retval NULL Error: not enough memory 125 */ 126 SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, 127 spx_uint32_t in_rate, 128 spx_uint32_t out_rate, 129 int quality, 130 int *err); 131 132 /** Create a new resampler with fractional input/output rates. The sampling 133 * rate ratio is an arbitrary rational number with both the numerator and 134 * denominator being 32-bit integers. 135 * @param nb_channels Number of channels to be processed 136 * @param ratio_num Numerator of the sampling rate ratio 137 * @param ratio_den Denominator of the sampling rate ratio 138 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz). 139 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz). 140 * @param quality Resampling quality between 0 and 10, where 0 has poor quality 141 * and 10 has very high quality. 142 * @return Newly created resampler state 143 * @retval NULL Error: not enough memory 144 */ 145 SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, 146 spx_uint32_t ratio_num, 147 spx_uint32_t ratio_den, 148 spx_uint32_t in_rate, 149 spx_uint32_t out_rate, 150 int quality, 151 int *err); 152 153 /** Destroy a resampler state. 154 * @param st Resampler state 155 */ 156 void speex_resampler_destroy(SpeexResamplerState *st); 157 158 /** Resample a float array. The input and output buffers must *not* overlap. 159 * @param st Resampler state 160 * @param channel_index Index of the channel to process for the multi-channel 161 * base (0 otherwise) 162 * @param in Input buffer 163 * @param in_len Number of input samples in the input buffer. Returns the 164 * number of samples processed 165 * @param out Output buffer 166 * @param out_len Size of the output buffer. Returns the number of samples written 167 */ 168 int speex_resampler_process_float(SpeexResamplerState *st, 169 spx_uint32_t channel_index, 170 const float *in, 171 spx_uint32_t *in_len, 172 float *out, 173 spx_uint32_t *out_len); 174 175 /** Resample an int array. The input and output buffers must *not* overlap. 176 * @param st Resampler state 177 * @param channel_index Index of the channel to process for the multi-channel 178 * base (0 otherwise) 179 * @param in Input buffer 180 * @param in_len Number of input samples in the input buffer. Returns the number 181 * of samples processed 182 * @param out Output buffer 183 * @param out_len Size of the output buffer. Returns the number of samples written 184 */ 185 int speex_resampler_process_int(SpeexResamplerState *st, 186 spx_uint32_t channel_index, 187 const spx_int16_t *in, 188 spx_uint32_t *in_len, 189 spx_int16_t *out, 190 spx_uint32_t *out_len); 191 192 /** Resample an interleaved float array. The input and output buffers must *not* overlap. 193 * @param st Resampler state 194 * @param in Input buffer 195 * @param in_len Number of input samples in the input buffer. Returns the number 196 * of samples processed. This is all per-channel. 197 * @param out Output buffer 198 * @param out_len Size of the output buffer. Returns the number of samples written. 199 * This is all per-channel. 200 */ 201 int speex_resampler_process_interleaved_float(SpeexResamplerState *st, 202 const float *in, 203 spx_uint32_t *in_len, 204 float *out, 205 spx_uint32_t *out_len); 206 207 /** Resample an interleaved int array. The input and output buffers must *not* overlap. 208 * @param st Resampler state 209 * @param in Input buffer 210 * @param in_len Number of input samples in the input buffer. Returns the number 211 * of samples processed. This is all per-channel. 212 * @param out Output buffer 213 * @param out_len Size of the output buffer. Returns the number of samples written. 214 * This is all per-channel. 215 */ 216 int speex_resampler_process_interleaved_int(SpeexResamplerState *st, 217 const spx_int16_t *in, 218 spx_uint32_t *in_len, 219 spx_int16_t *out, 220 spx_uint32_t *out_len); 221 222 /** Set (change) the input/output sampling rates (integer value). 223 * @param st Resampler state 224 * @param in_rate Input sampling rate (integer number of Hz). 225 * @param out_rate Output sampling rate (integer number of Hz). 226 */ 227 int speex_resampler_set_rate(SpeexResamplerState *st, 228 spx_uint32_t in_rate, 229 spx_uint32_t out_rate); 230 231 /** Get the current input/output sampling rates (integer value). 232 * @param st Resampler state 233 * @param in_rate Input sampling rate (integer number of Hz) copied. 234 * @param out_rate Output sampling rate (integer number of Hz) copied. 235 */ 236 void speex_resampler_get_rate(SpeexResamplerState *st, 237 spx_uint32_t *in_rate, 238 spx_uint32_t *out_rate); 239 240 /** Set (change) the input/output sampling rates and resampling ratio 241 * (fractional values in Hz supported). 242 * @param st Resampler state 243 * @param ratio_num Numerator of the sampling rate ratio 244 * @param ratio_den Denominator of the sampling rate ratio 245 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz). 246 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz). 247 */ 248 int speex_resampler_set_rate_frac(SpeexResamplerState *st, 249 spx_uint32_t ratio_num, 250 spx_uint32_t ratio_den, 251 spx_uint32_t in_rate, 252 spx_uint32_t out_rate); 253 254 /** Get the current resampling ratio. This will be reduced to the least 255 * common denominator. 256 * @param st Resampler state 257 * @param ratio_num Numerator of the sampling rate ratio copied 258 * @param ratio_den Denominator of the sampling rate ratio copied 259 */ 260 void speex_resampler_get_ratio(SpeexResamplerState *st, 261 spx_uint32_t *ratio_num, 262 spx_uint32_t *ratio_den); 263 264 /** Set (change) the conversion quality. 265 * @param st Resampler state 266 * @param quality Resampling quality between 0 and 10, where 0 has poor 267 * quality and 10 has very high quality. 268 */ 269 int speex_resampler_set_quality(SpeexResamplerState *st, 270 int quality); 271 272 /** Get the conversion quality. 273 * @param st Resampler state 274 * @param quality Resampling quality between 0 and 10, where 0 has poor 275 * quality and 10 has very high quality. 276 */ 277 void speex_resampler_get_quality(SpeexResamplerState *st, 278 int *quality); 279 280 /** Set (change) the input stride. 281 * @param st Resampler state 282 * @param stride Input stride 283 */ 284 void speex_resampler_set_input_stride(SpeexResamplerState *st, 285 spx_uint32_t stride); 286 287 /** Get the input stride. 288 * @param st Resampler state 289 * @param stride Input stride copied 290 */ 291 void speex_resampler_get_input_stride(SpeexResamplerState *st, 292 spx_uint32_t *stride); 293 294 /** Set (change) the output stride. 295 * @param st Resampler state 296 * @param stride Output stride 297 */ 298 void speex_resampler_set_output_stride(SpeexResamplerState *st, 299 spx_uint32_t stride); 300 301 /** Get the output stride. 302 * @param st Resampler state copied 303 * @param stride Output stride 304 */ 305 void speex_resampler_get_output_stride(SpeexResamplerState *st, 306 spx_uint32_t *stride); 307 308 /** Get the latency introduced by the resampler measured in input samples. 309 * @param st Resampler state 310 */ 311 int speex_resampler_get_input_latency(SpeexResamplerState *st); 312 313 /** Get the latency introduced by the resampler measured in output samples. 314 * @param st Resampler state 315 */ 316 int speex_resampler_get_output_latency(SpeexResamplerState *st); 317 318 /** Make sure that the first samples to go out of the resamplers don't have 319 * leading zeros. This is only useful before starting to use a newly created 320 * resampler. It is recommended to use that when resampling an audio file, as 321 * it will generate a file with the same length. For real-time processing, 322 * it is probably easier not to use this call (so that the output duration 323 * is the same for the first frame). 324 * @param st Resampler state 325 */ 326 int speex_resampler_skip_zeros(SpeexResamplerState *st); 327 328 /** Reset a resampler so a new (unrelated) stream can be processed. 329 * @param st Resampler state 330 */ 331 int speex_resampler_reset_mem(SpeexResamplerState *st); 332 333 /** Returns the English meaning for an error code 334 * @param err Error code 335 * @return English string 336 */ 337 const char *speex_resampler_strerror(int err); 338 339 #ifdef __cplusplus 340 } 341 #endif 342 343 #endif 344