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