media/libspeex_resampler/src/speex_resampler.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libspeex_resampler/src/speex_resampler.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,361 @@
     1.4 +/* Copyright (C) 2007 Jean-Marc Valin
     1.5 +      
     1.6 +   File: speex_resampler.h
     1.7 +   Resampling code
     1.8 +      
     1.9 +   The design goals of this code are:
    1.10 +      - Very fast algorithm
    1.11 +      - Low memory requirement
    1.12 +      - Good *perceptual* quality (and not best SNR)
    1.13 +
    1.14 +   Redistribution and use in source and binary forms, with or without
    1.15 +   modification, are permitted provided that the following conditions are
    1.16 +   met:
    1.17 +
    1.18 +   1. Redistributions of source code must retain the above copyright notice,
    1.19 +   this list of conditions and the following disclaimer.
    1.20 +
    1.21 +   2. Redistributions in binary form must reproduce the above copyright
    1.22 +   notice, this list of conditions and the following disclaimer in the
    1.23 +   documentation and/or other materials provided with the distribution.
    1.24 +
    1.25 +   3. The name of the author may not be used to endorse or promote products
    1.26 +   derived from this software without specific prior written permission.
    1.27 +
    1.28 +   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.29 +   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.30 +   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    1.31 +   DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    1.32 +   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.33 +   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.34 +   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.35 +   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.36 +   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    1.37 +   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.38 +   POSSIBILITY OF SUCH DAMAGE.
    1.39 +*/
    1.40 +
    1.41 +
    1.42 +#ifndef SPEEX_RESAMPLER_H
    1.43 +#define SPEEX_RESAMPLER_H
    1.44 +
    1.45 +#ifdef OUTSIDE_SPEEX
    1.46 +
    1.47 +/********* WARNING: MENTAL SANITY ENDS HERE *************/
    1.48 +
    1.49 +/* If the resampler is defined outside of Speex, we change the symbol names so that 
    1.50 +   there won't be any clash if linking with Speex later on. */
    1.51 +
    1.52 +/* #define RANDOM_PREFIX your software name here */
    1.53 +#ifndef RANDOM_PREFIX
    1.54 +#error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
    1.55 +#endif
    1.56 +
    1.57 +#define CAT_PREFIX2(a,b) a ## b
    1.58 +#define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)
    1.59 +      
    1.60 +#define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init)
    1.61 +#define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac)
    1.62 +#define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy)
    1.63 +#define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float)
    1.64 +#define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int)
    1.65 +#define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float)
    1.66 +#define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int)
    1.67 +#define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate)
    1.68 +#define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate)
    1.69 +#define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac)
    1.70 +#define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio)
    1.71 +#define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality)
    1.72 +#define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality)
    1.73 +#define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride)
    1.74 +#define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride)
    1.75 +#define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride)
    1.76 +#define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride)
    1.77 +#define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency)
    1.78 +#define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency)
    1.79 +#define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
    1.80 +#define speex_resampler_set_skip_frac_num CAT_PREFIX(RANDOM_PREFIX,_resampler_set_skip_frac_num)
    1.81 +#define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
    1.82 +#define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
    1.83 +
    1.84 +#define spx_int16_t short
    1.85 +#define spx_int32_t int
    1.86 +#define spx_uint16_t unsigned short
    1.87 +#define spx_uint32_t unsigned int
    1.88 +      
    1.89 +#else /* OUTSIDE_SPEEX */
    1.90 +
    1.91 +#ifdef _BUILD_SPEEX
    1.92 +# include "speex_types.h"
    1.93 +#else
    1.94 +# include <speex/speex_types.h>
    1.95 +#endif
    1.96 +
    1.97 +#endif /* OUTSIDE_SPEEX */
    1.98 +
    1.99 +#ifdef __cplusplus
   1.100 +extern "C" {
   1.101 +#endif
   1.102 +
   1.103 +#define SPEEX_RESAMPLER_QUALITY_MAX 10
   1.104 +#define SPEEX_RESAMPLER_QUALITY_MIN 0
   1.105 +#define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
   1.106 +#define SPEEX_RESAMPLER_QUALITY_VOIP 3
   1.107 +#define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
   1.108 +
   1.109 +enum {
   1.110 +   RESAMPLER_ERR_SUCCESS         = 0,
   1.111 +   RESAMPLER_ERR_ALLOC_FAILED    = 1,
   1.112 +   RESAMPLER_ERR_BAD_STATE       = 2,
   1.113 +   RESAMPLER_ERR_INVALID_ARG     = 3,
   1.114 +   RESAMPLER_ERR_PTR_OVERLAP     = 4,
   1.115 +   
   1.116 +   RESAMPLER_ERR_MAX_ERROR
   1.117 +};
   1.118 +
   1.119 +struct SpeexResamplerState_;
   1.120 +typedef struct SpeexResamplerState_ SpeexResamplerState;
   1.121 +
   1.122 +/** Create a new resampler with integer input and output rates.
   1.123 + * @param nb_channels Number of channels to be processed
   1.124 + * @param in_rate Input sampling rate (integer number of Hz).
   1.125 + * @param out_rate Output sampling rate (integer number of Hz).
   1.126 + * @param quality Resampling quality between 0 and 10, where 0 has poor quality
   1.127 + * and 10 has very high quality.
   1.128 + * @return Newly created resampler state
   1.129 + * @retval NULL Error: not enough memory
   1.130 + */
   1.131 +SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, 
   1.132 +                                          spx_uint32_t in_rate, 
   1.133 +                                          spx_uint32_t out_rate, 
   1.134 +                                          int quality,
   1.135 +                                          int *err);
   1.136 +
   1.137 +/** Create a new resampler with fractional input/output rates. The sampling 
   1.138 + * rate ratio is an arbitrary rational number with both the numerator and 
   1.139 + * denominator being 32-bit integers.
   1.140 + * @param nb_channels Number of channels to be processed
   1.141 + * @param ratio_num Numerator of the sampling rate ratio
   1.142 + * @param ratio_den Denominator of the sampling rate ratio
   1.143 + * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
   1.144 + * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
   1.145 + * @param quality Resampling quality between 0 and 10, where 0 has poor quality
   1.146 + * and 10 has very high quality.
   1.147 + * @return Newly created resampler state
   1.148 + * @retval NULL Error: not enough memory
   1.149 + */
   1.150 +SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, 
   1.151 +                                               spx_uint32_t ratio_num, 
   1.152 +                                               spx_uint32_t ratio_den, 
   1.153 +                                               spx_uint32_t in_rate, 
   1.154 +                                               spx_uint32_t out_rate, 
   1.155 +                                               int quality,
   1.156 +                                               int *err);
   1.157 +
   1.158 +/** Destroy a resampler state.
   1.159 + * @param st Resampler state
   1.160 + */
   1.161 +void speex_resampler_destroy(SpeexResamplerState *st);
   1.162 +
   1.163 +/** Resample a float array. The input and output buffers must *not* overlap.
   1.164 + * @param st Resampler state
   1.165 + * @param channel_index Index of the channel to process for the multi-channel 
   1.166 + * base (0 otherwise)
   1.167 + * @param in Input buffer
   1.168 + * @param in_len Number of input samples in the input buffer. Returns the 
   1.169 + * number of samples processed
   1.170 + * @param out Output buffer
   1.171 + * @param out_len Size of the output buffer. Returns the number of samples written
   1.172 + */
   1.173 +int speex_resampler_process_float(SpeexResamplerState *st, 
   1.174 +                                   spx_uint32_t channel_index, 
   1.175 +                                   const float *in, 
   1.176 +                                   spx_uint32_t *in_len, 
   1.177 +                                   float *out, 
   1.178 +                                   spx_uint32_t *out_len);
   1.179 +
   1.180 +/** Resample an int array. The input and output buffers must *not* overlap.
   1.181 + * @param st Resampler state
   1.182 + * @param channel_index Index of the channel to process for the multi-channel 
   1.183 + * base (0 otherwise)
   1.184 + * @param in Input buffer
   1.185 + * @param in_len Number of input samples in the input buffer. Returns the number
   1.186 + * of samples processed
   1.187 + * @param out Output buffer
   1.188 + * @param out_len Size of the output buffer. Returns the number of samples written
   1.189 + */
   1.190 +int speex_resampler_process_int(SpeexResamplerState *st, 
   1.191 +                                 spx_uint32_t channel_index, 
   1.192 +                                 const spx_int16_t *in, 
   1.193 +                                 spx_uint32_t *in_len, 
   1.194 +                                 spx_int16_t *out, 
   1.195 +                                 spx_uint32_t *out_len);
   1.196 +
   1.197 +/** Resample an interleaved float array. The input and output buffers must *not* overlap.
   1.198 + * @param st Resampler state
   1.199 + * @param in Input buffer
   1.200 + * @param in_len Number of input samples in the input buffer. Returns the number
   1.201 + * of samples processed. This is all per-channel.
   1.202 + * @param out Output buffer
   1.203 + * @param out_len Size of the output buffer. Returns the number of samples written.
   1.204 + * This is all per-channel.
   1.205 + */
   1.206 +int speex_resampler_process_interleaved_float(SpeexResamplerState *st, 
   1.207 +                                               const float *in, 
   1.208 +                                               spx_uint32_t *in_len, 
   1.209 +                                               float *out, 
   1.210 +                                               spx_uint32_t *out_len);
   1.211 +
   1.212 +/** Resample an interleaved int array. The input and output buffers must *not* overlap.
   1.213 + * @param st Resampler state
   1.214 + * @param in Input buffer
   1.215 + * @param in_len Number of input samples in the input buffer. Returns the number
   1.216 + * of samples processed. This is all per-channel.
   1.217 + * @param out Output buffer
   1.218 + * @param out_len Size of the output buffer. Returns the number of samples written.
   1.219 + * This is all per-channel.
   1.220 + */
   1.221 +int speex_resampler_process_interleaved_int(SpeexResamplerState *st, 
   1.222 +                                             const spx_int16_t *in, 
   1.223 +                                             spx_uint32_t *in_len, 
   1.224 +                                             spx_int16_t *out, 
   1.225 +                                             spx_uint32_t *out_len);
   1.226 +
   1.227 +/** Set (change) the input/output sampling rates (integer value).
   1.228 + * @param st Resampler state
   1.229 + * @param in_rate Input sampling rate (integer number of Hz).
   1.230 + * @param out_rate Output sampling rate (integer number of Hz).
   1.231 + */
   1.232 +int speex_resampler_set_rate(SpeexResamplerState *st, 
   1.233 +                              spx_uint32_t in_rate, 
   1.234 +                              spx_uint32_t out_rate);
   1.235 +
   1.236 +/** Get the current input/output sampling rates (integer value).
   1.237 + * @param st Resampler state
   1.238 + * @param in_rate Input sampling rate (integer number of Hz) copied.
   1.239 + * @param out_rate Output sampling rate (integer number of Hz) copied.
   1.240 + */
   1.241 +void speex_resampler_get_rate(SpeexResamplerState *st, 
   1.242 +                              spx_uint32_t *in_rate, 
   1.243 +                              spx_uint32_t *out_rate);
   1.244 +
   1.245 +/** Set (change) the input/output sampling rates and resampling ratio 
   1.246 + * (fractional values in Hz supported).
   1.247 + * @param st Resampler state
   1.248 + * @param ratio_num Numerator of the sampling rate ratio
   1.249 + * @param ratio_den Denominator of the sampling rate ratio
   1.250 + * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
   1.251 + * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
   1.252 + */
   1.253 +int speex_resampler_set_rate_frac(SpeexResamplerState *st, 
   1.254 +                                   spx_uint32_t ratio_num, 
   1.255 +                                   spx_uint32_t ratio_den, 
   1.256 +                                   spx_uint32_t in_rate, 
   1.257 +                                   spx_uint32_t out_rate);
   1.258 +
   1.259 +/** Get the current resampling ratio. This will be reduced to the least
   1.260 + * common denominator.
   1.261 + * @param st Resampler state
   1.262 + * @param ratio_num Numerator of the sampling rate ratio copied
   1.263 + * @param ratio_den Denominator of the sampling rate ratio copied
   1.264 + */
   1.265 +void speex_resampler_get_ratio(SpeexResamplerState *st, 
   1.266 +                               spx_uint32_t *ratio_num, 
   1.267 +                               spx_uint32_t *ratio_den);
   1.268 +
   1.269 +/** Set (change) the conversion quality.
   1.270 + * @param st Resampler state
   1.271 + * @param quality Resampling quality between 0 and 10, where 0 has poor 
   1.272 + * quality and 10 has very high quality.
   1.273 + */
   1.274 +int speex_resampler_set_quality(SpeexResamplerState *st, 
   1.275 +                                 int quality);
   1.276 +
   1.277 +/** Get the conversion quality.
   1.278 + * @param st Resampler state
   1.279 + * @param quality Resampling quality between 0 and 10, where 0 has poor 
   1.280 + * quality and 10 has very high quality.
   1.281 + */
   1.282 +void speex_resampler_get_quality(SpeexResamplerState *st, 
   1.283 +                                 int *quality);
   1.284 +
   1.285 +/** Set (change) the input stride.
   1.286 + * @param st Resampler state
   1.287 + * @param stride Input stride
   1.288 + */
   1.289 +void speex_resampler_set_input_stride(SpeexResamplerState *st, 
   1.290 +                                      spx_uint32_t stride);
   1.291 +
   1.292 +/** Get the input stride.
   1.293 + * @param st Resampler state
   1.294 + * @param stride Input stride copied
   1.295 + */
   1.296 +void speex_resampler_get_input_stride(SpeexResamplerState *st, 
   1.297 +                                      spx_uint32_t *stride);
   1.298 +
   1.299 +/** Set (change) the output stride.
   1.300 + * @param st Resampler state
   1.301 + * @param stride Output stride
   1.302 + */
   1.303 +void speex_resampler_set_output_stride(SpeexResamplerState *st, 
   1.304 +                                      spx_uint32_t stride);
   1.305 +
   1.306 +/** Get the output stride.
   1.307 + * @param st Resampler state copied
   1.308 + * @param stride Output stride
   1.309 + */
   1.310 +void speex_resampler_get_output_stride(SpeexResamplerState *st, 
   1.311 +                                      spx_uint32_t *stride);
   1.312 +
   1.313 +/** Get the latency introduced by the resampler measured in input samples.
   1.314 + * @param st Resampler state
   1.315 + */
   1.316 +int speex_resampler_get_input_latency(SpeexResamplerState *st);
   1.317 +
   1.318 +/** Get the latency introduced by the resampler measured in output samples.
   1.319 + * @param st Resampler state
   1.320 + */
   1.321 +int speex_resampler_get_output_latency(SpeexResamplerState *st);
   1.322 +
   1.323 +/** Make sure that the first samples to go out of the resamplers don't have 
   1.324 + * leading zeros. This is only useful before starting to use a newly created 
   1.325 + * resampler. It is recommended to use that when resampling an audio file, as
   1.326 + * it will generate a file with the same length. For real-time processing,
   1.327 + * it is probably easier not to use this call (so that the output duration
   1.328 + * is the same for the first frame).
   1.329 + * @param st Resampler state
   1.330 + */
   1.331 +int speex_resampler_skip_zeros(SpeexResamplerState *st);
   1.332 +
   1.333 +/** Set the numerator in a fraction determining the advance through input
   1.334 + * samples before writing any output samples. The denominator of the fraction
   1.335 + * is the value returned from speex_resampler_get_ratio() in ratio_den. This
   1.336 + * is only useful before starting to use a newly created or reset resampler.
   1.337 + * If the first input sample is interpreted as the signal at time
   1.338 + * input_latency*in_rate, then the first output sample represents the signal
   1.339 + * at the time frac_num/ratio_num*out_rate.
   1.340 + * This is intended for careful alignment of output sample points wrt input
   1.341 + * sample points. Large values are not an efficient offset into the in buffer.
   1.342 + * @param st Resampler state
   1.343 + * @param skip_frac_num Numerator of the offset fraction,
   1.344 + *                      between 0 and ratio_den-1.
   1.345 + */
   1.346 +int speex_resampler_set_skip_frac_num(SpeexResamplerState *st,
   1.347 +                                      spx_uint32_t skip_frac_num);
   1.348 +
   1.349 +/** Reset a resampler so a new (unrelated) stream can be processed.
   1.350 + * @param st Resampler state
   1.351 + */
   1.352 +int speex_resampler_reset_mem(SpeexResamplerState *st);
   1.353 +
   1.354 +/** Returns the English meaning for an error code
   1.355 + * @param err Error code
   1.356 + * @return English string
   1.357 + */
   1.358 +const char *speex_resampler_strerror(int err);
   1.359 +
   1.360 +#ifdef __cplusplus
   1.361 +}
   1.362 +#endif
   1.363 +
   1.364 +#endif

mercurial