Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Copyright (C) 2007 Jean-Marc Valin |
michael@0 | 2 | |
michael@0 | 3 | File: speex_resampler.h |
michael@0 | 4 | Resampling code |
michael@0 | 5 | |
michael@0 | 6 | The design goals of this code are: |
michael@0 | 7 | - Very fast algorithm |
michael@0 | 8 | - Low memory requirement |
michael@0 | 9 | - Good *perceptual* quality (and not best SNR) |
michael@0 | 10 | |
michael@0 | 11 | Redistribution and use in source and binary forms, with or without |
michael@0 | 12 | modification, are permitted provided that the following conditions are |
michael@0 | 13 | met: |
michael@0 | 14 | |
michael@0 | 15 | 1. Redistributions of source code must retain the above copyright notice, |
michael@0 | 16 | this list of conditions and the following disclaimer. |
michael@0 | 17 | |
michael@0 | 18 | 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 19 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 20 | documentation and/or other materials provided with the distribution. |
michael@0 | 21 | |
michael@0 | 22 | 3. The name of the author may not be used to endorse or promote products |
michael@0 | 23 | derived from this software without specific prior written permission. |
michael@0 | 24 | |
michael@0 | 25 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 26 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 27 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
michael@0 | 28 | DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
michael@0 | 29 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 30 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
michael@0 | 31 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 32 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
michael@0 | 33 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
michael@0 | 34 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
michael@0 | 35 | POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 36 | */ |
michael@0 | 37 | |
michael@0 | 38 | |
michael@0 | 39 | #ifndef SPEEX_RESAMPLER_H |
michael@0 | 40 | #define SPEEX_RESAMPLER_H |
michael@0 | 41 | |
michael@0 | 42 | #ifdef OUTSIDE_SPEEX |
michael@0 | 43 | |
michael@0 | 44 | /********* WARNING: MENTAL SANITY ENDS HERE *************/ |
michael@0 | 45 | |
michael@0 | 46 | /* If the resampler is defined outside of Speex, we change the symbol names so that |
michael@0 | 47 | there won't be any clash if linking with Speex later on. */ |
michael@0 | 48 | |
michael@0 | 49 | /* #define RANDOM_PREFIX your software name here */ |
michael@0 | 50 | #ifndef RANDOM_PREFIX |
michael@0 | 51 | #error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes" |
michael@0 | 52 | #endif |
michael@0 | 53 | |
michael@0 | 54 | #define CAT_PREFIX2(a,b) a ## b |
michael@0 | 55 | #define CAT_PREFIX(a,b) CAT_PREFIX2(a, b) |
michael@0 | 56 | |
michael@0 | 57 | #define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init) |
michael@0 | 58 | #define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac) |
michael@0 | 59 | #define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy) |
michael@0 | 60 | #define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float) |
michael@0 | 61 | #define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int) |
michael@0 | 62 | #define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float) |
michael@0 | 63 | #define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int) |
michael@0 | 64 | #define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate) |
michael@0 | 65 | #define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate) |
michael@0 | 66 | #define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac) |
michael@0 | 67 | #define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio) |
michael@0 | 68 | #define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality) |
michael@0 | 69 | #define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality) |
michael@0 | 70 | #define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride) |
michael@0 | 71 | #define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride) |
michael@0 | 72 | #define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride) |
michael@0 | 73 | #define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride) |
michael@0 | 74 | #define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency) |
michael@0 | 75 | #define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency) |
michael@0 | 76 | #define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros) |
michael@0 | 77 | #define speex_resampler_set_skip_frac_num CAT_PREFIX(RANDOM_PREFIX,_resampler_set_skip_frac_num) |
michael@0 | 78 | #define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem) |
michael@0 | 79 | #define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror) |
michael@0 | 80 | |
michael@0 | 81 | #define spx_int16_t short |
michael@0 | 82 | #define spx_int32_t int |
michael@0 | 83 | #define spx_uint16_t unsigned short |
michael@0 | 84 | #define spx_uint32_t unsigned int |
michael@0 | 85 | |
michael@0 | 86 | #else /* OUTSIDE_SPEEX */ |
michael@0 | 87 | |
michael@0 | 88 | #ifdef _BUILD_SPEEX |
michael@0 | 89 | # include "speex_types.h" |
michael@0 | 90 | #else |
michael@0 | 91 | # include <speex/speex_types.h> |
michael@0 | 92 | #endif |
michael@0 | 93 | |
michael@0 | 94 | #endif /* OUTSIDE_SPEEX */ |
michael@0 | 95 | |
michael@0 | 96 | #ifdef __cplusplus |
michael@0 | 97 | extern "C" { |
michael@0 | 98 | #endif |
michael@0 | 99 | |
michael@0 | 100 | #define SPEEX_RESAMPLER_QUALITY_MAX 10 |
michael@0 | 101 | #define SPEEX_RESAMPLER_QUALITY_MIN 0 |
michael@0 | 102 | #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4 |
michael@0 | 103 | #define SPEEX_RESAMPLER_QUALITY_VOIP 3 |
michael@0 | 104 | #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5 |
michael@0 | 105 | |
michael@0 | 106 | enum { |
michael@0 | 107 | RESAMPLER_ERR_SUCCESS = 0, |
michael@0 | 108 | RESAMPLER_ERR_ALLOC_FAILED = 1, |
michael@0 | 109 | RESAMPLER_ERR_BAD_STATE = 2, |
michael@0 | 110 | RESAMPLER_ERR_INVALID_ARG = 3, |
michael@0 | 111 | RESAMPLER_ERR_PTR_OVERLAP = 4, |
michael@0 | 112 | |
michael@0 | 113 | RESAMPLER_ERR_MAX_ERROR |
michael@0 | 114 | }; |
michael@0 | 115 | |
michael@0 | 116 | struct SpeexResamplerState_; |
michael@0 | 117 | typedef struct SpeexResamplerState_ SpeexResamplerState; |
michael@0 | 118 | |
michael@0 | 119 | /** Create a new resampler with integer input and output rates. |
michael@0 | 120 | * @param nb_channels Number of channels to be processed |
michael@0 | 121 | * @param in_rate Input sampling rate (integer number of Hz). |
michael@0 | 122 | * @param out_rate Output sampling rate (integer number of Hz). |
michael@0 | 123 | * @param quality Resampling quality between 0 and 10, where 0 has poor quality |
michael@0 | 124 | * and 10 has very high quality. |
michael@0 | 125 | * @return Newly created resampler state |
michael@0 | 126 | * @retval NULL Error: not enough memory |
michael@0 | 127 | */ |
michael@0 | 128 | SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, |
michael@0 | 129 | spx_uint32_t in_rate, |
michael@0 | 130 | spx_uint32_t out_rate, |
michael@0 | 131 | int quality, |
michael@0 | 132 | int *err); |
michael@0 | 133 | |
michael@0 | 134 | /** Create a new resampler with fractional input/output rates. The sampling |
michael@0 | 135 | * rate ratio is an arbitrary rational number with both the numerator and |
michael@0 | 136 | * denominator being 32-bit integers. |
michael@0 | 137 | * @param nb_channels Number of channels to be processed |
michael@0 | 138 | * @param ratio_num Numerator of the sampling rate ratio |
michael@0 | 139 | * @param ratio_den Denominator of the sampling rate ratio |
michael@0 | 140 | * @param in_rate Input sampling rate rounded to the nearest integer (in Hz). |
michael@0 | 141 | * @param out_rate Output sampling rate rounded to the nearest integer (in Hz). |
michael@0 | 142 | * @param quality Resampling quality between 0 and 10, where 0 has poor quality |
michael@0 | 143 | * and 10 has very high quality. |
michael@0 | 144 | * @return Newly created resampler state |
michael@0 | 145 | * @retval NULL Error: not enough memory |
michael@0 | 146 | */ |
michael@0 | 147 | SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, |
michael@0 | 148 | spx_uint32_t ratio_num, |
michael@0 | 149 | spx_uint32_t ratio_den, |
michael@0 | 150 | spx_uint32_t in_rate, |
michael@0 | 151 | spx_uint32_t out_rate, |
michael@0 | 152 | int quality, |
michael@0 | 153 | int *err); |
michael@0 | 154 | |
michael@0 | 155 | /** Destroy a resampler state. |
michael@0 | 156 | * @param st Resampler state |
michael@0 | 157 | */ |
michael@0 | 158 | void speex_resampler_destroy(SpeexResamplerState *st); |
michael@0 | 159 | |
michael@0 | 160 | /** Resample a float array. The input and output buffers must *not* overlap. |
michael@0 | 161 | * @param st Resampler state |
michael@0 | 162 | * @param channel_index Index of the channel to process for the multi-channel |
michael@0 | 163 | * base (0 otherwise) |
michael@0 | 164 | * @param in Input buffer |
michael@0 | 165 | * @param in_len Number of input samples in the input buffer. Returns the |
michael@0 | 166 | * number of samples processed |
michael@0 | 167 | * @param out Output buffer |
michael@0 | 168 | * @param out_len Size of the output buffer. Returns the number of samples written |
michael@0 | 169 | */ |
michael@0 | 170 | int speex_resampler_process_float(SpeexResamplerState *st, |
michael@0 | 171 | spx_uint32_t channel_index, |
michael@0 | 172 | const float *in, |
michael@0 | 173 | spx_uint32_t *in_len, |
michael@0 | 174 | float *out, |
michael@0 | 175 | spx_uint32_t *out_len); |
michael@0 | 176 | |
michael@0 | 177 | /** Resample an int array. The input and output buffers must *not* overlap. |
michael@0 | 178 | * @param st Resampler state |
michael@0 | 179 | * @param channel_index Index of the channel to process for the multi-channel |
michael@0 | 180 | * base (0 otherwise) |
michael@0 | 181 | * @param in Input buffer |
michael@0 | 182 | * @param in_len Number of input samples in the input buffer. Returns the number |
michael@0 | 183 | * of samples processed |
michael@0 | 184 | * @param out Output buffer |
michael@0 | 185 | * @param out_len Size of the output buffer. Returns the number of samples written |
michael@0 | 186 | */ |
michael@0 | 187 | int speex_resampler_process_int(SpeexResamplerState *st, |
michael@0 | 188 | spx_uint32_t channel_index, |
michael@0 | 189 | const spx_int16_t *in, |
michael@0 | 190 | spx_uint32_t *in_len, |
michael@0 | 191 | spx_int16_t *out, |
michael@0 | 192 | spx_uint32_t *out_len); |
michael@0 | 193 | |
michael@0 | 194 | /** Resample an interleaved float array. The input and output buffers must *not* overlap. |
michael@0 | 195 | * @param st Resampler state |
michael@0 | 196 | * @param in Input buffer |
michael@0 | 197 | * @param in_len Number of input samples in the input buffer. Returns the number |
michael@0 | 198 | * of samples processed. This is all per-channel. |
michael@0 | 199 | * @param out Output buffer |
michael@0 | 200 | * @param out_len Size of the output buffer. Returns the number of samples written. |
michael@0 | 201 | * This is all per-channel. |
michael@0 | 202 | */ |
michael@0 | 203 | int speex_resampler_process_interleaved_float(SpeexResamplerState *st, |
michael@0 | 204 | const float *in, |
michael@0 | 205 | spx_uint32_t *in_len, |
michael@0 | 206 | float *out, |
michael@0 | 207 | spx_uint32_t *out_len); |
michael@0 | 208 | |
michael@0 | 209 | /** Resample an interleaved int array. The input and output buffers must *not* overlap. |
michael@0 | 210 | * @param st Resampler state |
michael@0 | 211 | * @param in Input buffer |
michael@0 | 212 | * @param in_len Number of input samples in the input buffer. Returns the number |
michael@0 | 213 | * of samples processed. This is all per-channel. |
michael@0 | 214 | * @param out Output buffer |
michael@0 | 215 | * @param out_len Size of the output buffer. Returns the number of samples written. |
michael@0 | 216 | * This is all per-channel. |
michael@0 | 217 | */ |
michael@0 | 218 | int speex_resampler_process_interleaved_int(SpeexResamplerState *st, |
michael@0 | 219 | const spx_int16_t *in, |
michael@0 | 220 | spx_uint32_t *in_len, |
michael@0 | 221 | spx_int16_t *out, |
michael@0 | 222 | spx_uint32_t *out_len); |
michael@0 | 223 | |
michael@0 | 224 | /** Set (change) the input/output sampling rates (integer value). |
michael@0 | 225 | * @param st Resampler state |
michael@0 | 226 | * @param in_rate Input sampling rate (integer number of Hz). |
michael@0 | 227 | * @param out_rate Output sampling rate (integer number of Hz). |
michael@0 | 228 | */ |
michael@0 | 229 | int speex_resampler_set_rate(SpeexResamplerState *st, |
michael@0 | 230 | spx_uint32_t in_rate, |
michael@0 | 231 | spx_uint32_t out_rate); |
michael@0 | 232 | |
michael@0 | 233 | /** Get the current input/output sampling rates (integer value). |
michael@0 | 234 | * @param st Resampler state |
michael@0 | 235 | * @param in_rate Input sampling rate (integer number of Hz) copied. |
michael@0 | 236 | * @param out_rate Output sampling rate (integer number of Hz) copied. |
michael@0 | 237 | */ |
michael@0 | 238 | void speex_resampler_get_rate(SpeexResamplerState *st, |
michael@0 | 239 | spx_uint32_t *in_rate, |
michael@0 | 240 | spx_uint32_t *out_rate); |
michael@0 | 241 | |
michael@0 | 242 | /** Set (change) the input/output sampling rates and resampling ratio |
michael@0 | 243 | * (fractional values in Hz supported). |
michael@0 | 244 | * @param st Resampler state |
michael@0 | 245 | * @param ratio_num Numerator of the sampling rate ratio |
michael@0 | 246 | * @param ratio_den Denominator of the sampling rate ratio |
michael@0 | 247 | * @param in_rate Input sampling rate rounded to the nearest integer (in Hz). |
michael@0 | 248 | * @param out_rate Output sampling rate rounded to the nearest integer (in Hz). |
michael@0 | 249 | */ |
michael@0 | 250 | int speex_resampler_set_rate_frac(SpeexResamplerState *st, |
michael@0 | 251 | spx_uint32_t ratio_num, |
michael@0 | 252 | spx_uint32_t ratio_den, |
michael@0 | 253 | spx_uint32_t in_rate, |
michael@0 | 254 | spx_uint32_t out_rate); |
michael@0 | 255 | |
michael@0 | 256 | /** Get the current resampling ratio. This will be reduced to the least |
michael@0 | 257 | * common denominator. |
michael@0 | 258 | * @param st Resampler state |
michael@0 | 259 | * @param ratio_num Numerator of the sampling rate ratio copied |
michael@0 | 260 | * @param ratio_den Denominator of the sampling rate ratio copied |
michael@0 | 261 | */ |
michael@0 | 262 | void speex_resampler_get_ratio(SpeexResamplerState *st, |
michael@0 | 263 | spx_uint32_t *ratio_num, |
michael@0 | 264 | spx_uint32_t *ratio_den); |
michael@0 | 265 | |
michael@0 | 266 | /** Set (change) the conversion quality. |
michael@0 | 267 | * @param st Resampler state |
michael@0 | 268 | * @param quality Resampling quality between 0 and 10, where 0 has poor |
michael@0 | 269 | * quality and 10 has very high quality. |
michael@0 | 270 | */ |
michael@0 | 271 | int speex_resampler_set_quality(SpeexResamplerState *st, |
michael@0 | 272 | int quality); |
michael@0 | 273 | |
michael@0 | 274 | /** Get the conversion quality. |
michael@0 | 275 | * @param st Resampler state |
michael@0 | 276 | * @param quality Resampling quality between 0 and 10, where 0 has poor |
michael@0 | 277 | * quality and 10 has very high quality. |
michael@0 | 278 | */ |
michael@0 | 279 | void speex_resampler_get_quality(SpeexResamplerState *st, |
michael@0 | 280 | int *quality); |
michael@0 | 281 | |
michael@0 | 282 | /** Set (change) the input stride. |
michael@0 | 283 | * @param st Resampler state |
michael@0 | 284 | * @param stride Input stride |
michael@0 | 285 | */ |
michael@0 | 286 | void speex_resampler_set_input_stride(SpeexResamplerState *st, |
michael@0 | 287 | spx_uint32_t stride); |
michael@0 | 288 | |
michael@0 | 289 | /** Get the input stride. |
michael@0 | 290 | * @param st Resampler state |
michael@0 | 291 | * @param stride Input stride copied |
michael@0 | 292 | */ |
michael@0 | 293 | void speex_resampler_get_input_stride(SpeexResamplerState *st, |
michael@0 | 294 | spx_uint32_t *stride); |
michael@0 | 295 | |
michael@0 | 296 | /** Set (change) the output stride. |
michael@0 | 297 | * @param st Resampler state |
michael@0 | 298 | * @param stride Output stride |
michael@0 | 299 | */ |
michael@0 | 300 | void speex_resampler_set_output_stride(SpeexResamplerState *st, |
michael@0 | 301 | spx_uint32_t stride); |
michael@0 | 302 | |
michael@0 | 303 | /** Get the output stride. |
michael@0 | 304 | * @param st Resampler state copied |
michael@0 | 305 | * @param stride Output stride |
michael@0 | 306 | */ |
michael@0 | 307 | void speex_resampler_get_output_stride(SpeexResamplerState *st, |
michael@0 | 308 | spx_uint32_t *stride); |
michael@0 | 309 | |
michael@0 | 310 | /** Get the latency introduced by the resampler measured in input samples. |
michael@0 | 311 | * @param st Resampler state |
michael@0 | 312 | */ |
michael@0 | 313 | int speex_resampler_get_input_latency(SpeexResamplerState *st); |
michael@0 | 314 | |
michael@0 | 315 | /** Get the latency introduced by the resampler measured in output samples. |
michael@0 | 316 | * @param st Resampler state |
michael@0 | 317 | */ |
michael@0 | 318 | int speex_resampler_get_output_latency(SpeexResamplerState *st); |
michael@0 | 319 | |
michael@0 | 320 | /** Make sure that the first samples to go out of the resamplers don't have |
michael@0 | 321 | * leading zeros. This is only useful before starting to use a newly created |
michael@0 | 322 | * resampler. It is recommended to use that when resampling an audio file, as |
michael@0 | 323 | * it will generate a file with the same length. For real-time processing, |
michael@0 | 324 | * it is probably easier not to use this call (so that the output duration |
michael@0 | 325 | * is the same for the first frame). |
michael@0 | 326 | * @param st Resampler state |
michael@0 | 327 | */ |
michael@0 | 328 | int speex_resampler_skip_zeros(SpeexResamplerState *st); |
michael@0 | 329 | |
michael@0 | 330 | /** Set the numerator in a fraction determining the advance through input |
michael@0 | 331 | * samples before writing any output samples. The denominator of the fraction |
michael@0 | 332 | * is the value returned from speex_resampler_get_ratio() in ratio_den. This |
michael@0 | 333 | * is only useful before starting to use a newly created or reset resampler. |
michael@0 | 334 | * If the first input sample is interpreted as the signal at time |
michael@0 | 335 | * input_latency*in_rate, then the first output sample represents the signal |
michael@0 | 336 | * at the time frac_num/ratio_num*out_rate. |
michael@0 | 337 | * This is intended for careful alignment of output sample points wrt input |
michael@0 | 338 | * sample points. Large values are not an efficient offset into the in buffer. |
michael@0 | 339 | * @param st Resampler state |
michael@0 | 340 | * @param skip_frac_num Numerator of the offset fraction, |
michael@0 | 341 | * between 0 and ratio_den-1. |
michael@0 | 342 | */ |
michael@0 | 343 | int speex_resampler_set_skip_frac_num(SpeexResamplerState *st, |
michael@0 | 344 | spx_uint32_t skip_frac_num); |
michael@0 | 345 | |
michael@0 | 346 | /** Reset a resampler so a new (unrelated) stream can be processed. |
michael@0 | 347 | * @param st Resampler state |
michael@0 | 348 | */ |
michael@0 | 349 | int speex_resampler_reset_mem(SpeexResamplerState *st); |
michael@0 | 350 | |
michael@0 | 351 | /** Returns the English meaning for an error code |
michael@0 | 352 | * @param err Error code |
michael@0 | 353 | * @return English string |
michael@0 | 354 | */ |
michael@0 | 355 | const char *speex_resampler_strerror(int err); |
michael@0 | 356 | |
michael@0 | 357 | #ifdef __cplusplus |
michael@0 | 358 | } |
michael@0 | 359 | #endif |
michael@0 | 360 | |
michael@0 | 361 | #endif |