Blame pph/speex_resampler.h

Packit Service cd2a00
/* Copyright (C) 2007 Jean-Marc Valin
Packit Service cd2a00
      
Packit Service cd2a00
   File: speex_resampler.h
Packit Service cd2a00
   Resampling code
Packit Service cd2a00
      
Packit Service cd2a00
   The design goals of this code are:
Packit Service cd2a00
      - Very fast algorithm
Packit Service cd2a00
      - Low memory requirement
Packit Service cd2a00
      - Good *perceptual* quality (and not best SNR)
Packit Service cd2a00
Packit Service cd2a00
   Redistribution and use in source and binary forms, with or without
Packit Service cd2a00
   modification, are permitted provided that the following conditions are
Packit Service cd2a00
   met:
Packit Service cd2a00
Packit Service cd2a00
   1. Redistributions of source code must retain the above copyright notice,
Packit Service cd2a00
   this list of conditions and the following disclaimer.
Packit Service cd2a00
Packit Service cd2a00
   2. Redistributions in binary form must reproduce the above copyright
Packit Service cd2a00
   notice, this list of conditions and the following disclaimer in the
Packit Service cd2a00
   documentation and/or other materials provided with the distribution.
Packit Service cd2a00
Packit Service cd2a00
   3. The name of the author may not be used to endorse or promote products
Packit Service cd2a00
   derived from this software without specific prior written permission.
Packit Service cd2a00
Packit Service cd2a00
   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit Service cd2a00
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service cd2a00
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit Service cd2a00
   DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
Packit Service cd2a00
   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Packit Service cd2a00
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit Service cd2a00
   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service cd2a00
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit Service cd2a00
   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
Packit Service cd2a00
   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit Service cd2a00
   POSSIBILITY OF SUCH DAMAGE.
Packit Service cd2a00
*/
Packit Service cd2a00
Packit Service cd2a00
Packit Service cd2a00
#ifndef SPEEX_RESAMPLER_H
Packit Service cd2a00
#define SPEEX_RESAMPLER_H
Packit Service cd2a00
Packit Service cd2a00
#ifdef OUTSIDE_SPEEX
Packit Service cd2a00
Packit Service cd2a00
/********* WARNING: MENTAL SANITY ENDS HERE *************/
Packit Service cd2a00
Packit Service cd2a00
/* If the resampler is defined outside of Speex, we change the symbol names so that 
Packit Service cd2a00
   there won't be any clash if linking with Speex later on. */
Packit Service cd2a00
Packit Service cd2a00
/* #define RANDOM_PREFIX your software name here */
Packit Service cd2a00
#ifndef RANDOM_PREFIX
Packit Service cd2a00
#error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
Packit Service cd2a00
#endif
Packit Service cd2a00
Packit Service cd2a00
#define CAT_PREFIX2(a,b) a ## b
Packit Service cd2a00
#define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)
Packit Service cd2a00
      
Packit Service cd2a00
#define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init)
Packit Service cd2a00
#define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac)
Packit Service cd2a00
#define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy)
Packit Service cd2a00
#define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float)
Packit Service cd2a00
#define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int)
Packit Service cd2a00
#define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float)
Packit Service cd2a00
#define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int)
Packit Service cd2a00
#define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate)
Packit Service cd2a00
#define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate)
Packit Service cd2a00
#define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac)
Packit Service cd2a00
#define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio)
Packit Service cd2a00
#define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality)
Packit Service cd2a00
#define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality)
Packit Service cd2a00
#define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride)
Packit Service cd2a00
#define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride)
Packit Service cd2a00
#define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride)
Packit Service cd2a00
#define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride)
Packit Service cd2a00
#define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
Packit Service cd2a00
#define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
Packit Service cd2a00
#define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
Packit Service cd2a00
Packit Service cd2a00
#define spx_int16_t short
Packit Service cd2a00
#define spx_int32_t int
Packit Service cd2a00
#define spx_uint16_t unsigned short
Packit Service cd2a00
#define spx_uint32_t unsigned int
Packit Service cd2a00
      
Packit Service cd2a00
#else /* OUTSIDE_SPEEX */
Packit Service cd2a00
Packit Service cd2a00
#ifdef HAVE_SPEEX_SPEEXDSP_TYPES_H
Packit Service cd2a00
#include "speex/speexdsp_types.h"
Packit Service cd2a00
#else
Packit Service cd2a00
#include "speex/speex_types.h"
Packit Service cd2a00
#endif
Packit Service cd2a00
Packit Service cd2a00
#endif /* OUTSIDE_SPEEX */
Packit Service cd2a00
Packit Service cd2a00
#ifdef __cplusplus
Packit Service cd2a00
extern "C" {
Packit Service cd2a00
#endif
Packit Service cd2a00
Packit Service cd2a00
#define SPEEX_RESAMPLER_QUALITY_MAX 10
Packit Service cd2a00
#define SPEEX_RESAMPLER_QUALITY_MIN 0
Packit Service cd2a00
#define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
Packit Service cd2a00
#define SPEEX_RESAMPLER_QUALITY_VOIP 3
Packit Service cd2a00
#define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
Packit Service cd2a00
Packit Service cd2a00
enum {
Packit Service cd2a00
   RESAMPLER_ERR_SUCCESS         = 0,
Packit Service cd2a00
   RESAMPLER_ERR_ALLOC_FAILED    = 1,
Packit Service cd2a00
   RESAMPLER_ERR_BAD_STATE       = 2,
Packit Service cd2a00
   RESAMPLER_ERR_INVALID_ARG     = 3,
Packit Service cd2a00
   RESAMPLER_ERR_PTR_OVERLAP     = 4,
Packit Service cd2a00
   
Packit Service cd2a00
   RESAMPLER_ERR_MAX_ERROR
Packit Service cd2a00
};
Packit Service cd2a00
Packit Service cd2a00
struct SpeexResamplerState_;
Packit Service cd2a00
typedef struct SpeexResamplerState_ SpeexResamplerState;
Packit Service cd2a00
Packit Service cd2a00
/** Create a new resampler with integer input and output rates.
Packit Service cd2a00
 * @param nb_channels Number of channels to be processed
Packit Service cd2a00
 * @param in_rate Input sampling rate (integer number of Hz).
Packit Service cd2a00
 * @param out_rate Output sampling rate (integer number of Hz).
Packit Service cd2a00
 * @param quality Resampling quality between 0 and 10, where 0 has poor quality
Packit Service cd2a00
 * and 10 has very high quality.
Packit Service cd2a00
 * @return Newly created resampler state
Packit Service cd2a00
 * @retval NULL Error: not enough memory
Packit Service cd2a00
 */
Packit Service cd2a00
SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, 
Packit Service cd2a00
                                          spx_uint32_t in_rate, 
Packit Service cd2a00
                                          spx_uint32_t out_rate, 
Packit Service cd2a00
                                          int quality,
Packit Service cd2a00
                                          int *err);
Packit Service cd2a00
Packit Service cd2a00
/** Create a new resampler with fractional input/output rates. The sampling 
Packit Service cd2a00
 * rate ratio is an arbitrary rational number with both the numerator and 
Packit Service cd2a00
 * denominator being 32-bit integers.
Packit Service cd2a00
 * @param nb_channels Number of channels to be processed
Packit Service cd2a00
 * @param ratio_num Numerator of the sampling rate ratio
Packit Service cd2a00
 * @param ratio_den Denominator of the sampling rate ratio
Packit Service cd2a00
 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
Packit Service cd2a00
 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
Packit Service cd2a00
 * @param quality Resampling quality between 0 and 10, where 0 has poor quality
Packit Service cd2a00
 * and 10 has very high quality.
Packit Service cd2a00
 * @return Newly created resampler state
Packit Service cd2a00
 * @retval NULL Error: not enough memory
Packit Service cd2a00
 */
Packit Service cd2a00
SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, 
Packit Service cd2a00
                                               spx_uint32_t ratio_num, 
Packit Service cd2a00
                                               spx_uint32_t ratio_den, 
Packit Service cd2a00
                                               spx_uint32_t in_rate, 
Packit Service cd2a00
                                               spx_uint32_t out_rate, 
Packit Service cd2a00
                                               int quality,
Packit Service cd2a00
                                               int *err);
Packit Service cd2a00
Packit Service cd2a00
/** Destroy a resampler state.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 */
Packit Service cd2a00
void speex_resampler_destroy(SpeexResamplerState *st);
Packit Service cd2a00
Packit Service cd2a00
/** Resample a float array. The input and output buffers must *not* overlap.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param channel_index Index of the channel to process for the multi-channel 
Packit Service cd2a00
 * base (0 otherwise)
Packit Service cd2a00
 * @param in Input buffer
Packit Service cd2a00
 * @param in_len Number of input samples in the input buffer. Returns the 
Packit Service cd2a00
 * number of samples processed
Packit Service cd2a00
 * @param out Output buffer
Packit Service cd2a00
 * @param out_len Size of the output buffer. Returns the number of samples written
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_process_float(SpeexResamplerState *st, 
Packit Service cd2a00
                                   spx_uint32_t channel_index, 
Packit Service cd2a00
                                   const float *in, 
Packit Service cd2a00
                                   spx_uint32_t *in_len, 
Packit Service cd2a00
                                   float *out, 
Packit Service cd2a00
                                   spx_uint32_t *out_len);
Packit Service cd2a00
Packit Service cd2a00
/** Resample an int array. The input and output buffers must *not* overlap.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param channel_index Index of the channel to process for the multi-channel 
Packit Service cd2a00
 * base (0 otherwise)
Packit Service cd2a00
 * @param in Input buffer
Packit Service cd2a00
 * @param in_len Number of input samples in the input buffer. Returns the number
Packit Service cd2a00
 * of samples processed
Packit Service cd2a00
 * @param out Output buffer
Packit Service cd2a00
 * @param out_len Size of the output buffer. Returns the number of samples written
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_process_int(SpeexResamplerState *st, 
Packit Service cd2a00
                                 spx_uint32_t channel_index, 
Packit Service cd2a00
                                 const spx_int16_t *in, 
Packit Service cd2a00
                                 spx_uint32_t *in_len, 
Packit Service cd2a00
                                 spx_int16_t *out, 
Packit Service cd2a00
                                 spx_uint32_t *out_len);
Packit Service cd2a00
Packit Service cd2a00
/** Resample an interleaved float array. The input and output buffers must *not* overlap.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param in Input buffer
Packit Service cd2a00
 * @param in_len Number of input samples in the input buffer. Returns the number
Packit Service cd2a00
 * of samples processed. This is all per-channel.
Packit Service cd2a00
 * @param out Output buffer
Packit Service cd2a00
 * @param out_len Size of the output buffer. Returns the number of samples written.
Packit Service cd2a00
 * This is all per-channel.
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_process_interleaved_float(SpeexResamplerState *st, 
Packit Service cd2a00
                                               const float *in, 
Packit Service cd2a00
                                               spx_uint32_t *in_len, 
Packit Service cd2a00
                                               float *out, 
Packit Service cd2a00
                                               spx_uint32_t *out_len);
Packit Service cd2a00
Packit Service cd2a00
/** Resample an interleaved int array. The input and output buffers must *not* overlap.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param in Input buffer
Packit Service cd2a00
 * @param in_len Number of input samples in the input buffer. Returns the number
Packit Service cd2a00
 * of samples processed. This is all per-channel.
Packit Service cd2a00
 * @param out Output buffer
Packit Service cd2a00
 * @param out_len Size of the output buffer. Returns the number of samples written.
Packit Service cd2a00
 * This is all per-channel.
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_process_interleaved_int(SpeexResamplerState *st, 
Packit Service cd2a00
                                             const spx_int16_t *in, 
Packit Service cd2a00
                                             spx_uint32_t *in_len, 
Packit Service cd2a00
                                             spx_int16_t *out, 
Packit Service cd2a00
                                             spx_uint32_t *out_len);
Packit Service cd2a00
Packit Service cd2a00
/** Set (change) the input/output sampling rates (integer value).
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param in_rate Input sampling rate (integer number of Hz).
Packit Service cd2a00
 * @param out_rate Output sampling rate (integer number of Hz).
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_set_rate(SpeexResamplerState *st, 
Packit Service cd2a00
                              spx_uint32_t in_rate, 
Packit Service cd2a00
                              spx_uint32_t out_rate);
Packit Service cd2a00
Packit Service cd2a00
/** Get the current input/output sampling rates (integer value).
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param in_rate Input sampling rate (integer number of Hz) copied.
Packit Service cd2a00
 * @param out_rate Output sampling rate (integer number of Hz) copied.
Packit Service cd2a00
 */
Packit Service cd2a00
void speex_resampler_get_rate(SpeexResamplerState *st, 
Packit Service cd2a00
                              spx_uint32_t *in_rate, 
Packit Service cd2a00
                              spx_uint32_t *out_rate);
Packit Service cd2a00
Packit Service cd2a00
/** Set (change) the input/output sampling rates and resampling ratio 
Packit Service cd2a00
 * (fractional values in Hz supported).
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param ratio_num Numerator of the sampling rate ratio
Packit Service cd2a00
 * @param ratio_den Denominator of the sampling rate ratio
Packit Service cd2a00
 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
Packit Service cd2a00
 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_set_rate_frac(SpeexResamplerState *st, 
Packit Service cd2a00
                                   spx_uint32_t ratio_num, 
Packit Service cd2a00
                                   spx_uint32_t ratio_den, 
Packit Service cd2a00
                                   spx_uint32_t in_rate, 
Packit Service cd2a00
                                   spx_uint32_t out_rate);
Packit Service cd2a00
Packit Service cd2a00
/** Get the current resampling ratio. This will be reduced to the least
Packit Service cd2a00
 * common denominator.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param ratio_num Numerator of the sampling rate ratio copied
Packit Service cd2a00
 * @param ratio_den Denominator of the sampling rate ratio copied
Packit Service cd2a00
 */
Packit Service cd2a00
void speex_resampler_get_ratio(SpeexResamplerState *st, 
Packit Service cd2a00
                               spx_uint32_t *ratio_num, 
Packit Service cd2a00
                               spx_uint32_t *ratio_den);
Packit Service cd2a00
Packit Service cd2a00
/** Set (change) the conversion quality.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param quality Resampling quality between 0 and 10, where 0 has poor 
Packit Service cd2a00
 * quality and 10 has very high quality.
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_set_quality(SpeexResamplerState *st, 
Packit Service cd2a00
                                 int quality);
Packit Service cd2a00
Packit Service cd2a00
/** Get the conversion quality.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param quality Resampling quality between 0 and 10, where 0 has poor 
Packit Service cd2a00
 * quality and 10 has very high quality.
Packit Service cd2a00
 */
Packit Service cd2a00
void speex_resampler_get_quality(SpeexResamplerState *st, 
Packit Service cd2a00
                                 int *quality);
Packit Service cd2a00
Packit Service cd2a00
/** Set (change) the input stride.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param stride Input stride
Packit Service cd2a00
 */
Packit Service cd2a00
void speex_resampler_set_input_stride(SpeexResamplerState *st, 
Packit Service cd2a00
                                      spx_uint32_t stride);
Packit Service cd2a00
Packit Service cd2a00
/** Get the input stride.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param stride Input stride copied
Packit Service cd2a00
 */
Packit Service cd2a00
void speex_resampler_get_input_stride(SpeexResamplerState *st, 
Packit Service cd2a00
                                      spx_uint32_t *stride);
Packit Service cd2a00
Packit Service cd2a00
/** Set (change) the output stride.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 * @param stride Output stride
Packit Service cd2a00
 */
Packit Service cd2a00
void speex_resampler_set_output_stride(SpeexResamplerState *st, 
Packit Service cd2a00
                                      spx_uint32_t stride);
Packit Service cd2a00
Packit Service cd2a00
/** Get the output stride.
Packit Service cd2a00
 * @param st Resampler state copied
Packit Service cd2a00
 * @param stride Output stride
Packit Service cd2a00
 */
Packit Service cd2a00
void speex_resampler_get_output_stride(SpeexResamplerState *st, 
Packit Service cd2a00
                                      spx_uint32_t *stride);
Packit Service cd2a00
Packit Service cd2a00
/** Make sure that the first samples to go out of the resamplers don't have 
Packit Service cd2a00
 * leading zeros. This is only useful before starting to use a newly created 
Packit Service cd2a00
 * resampler. It is recommended to use that when resampling an audio file, as
Packit Service cd2a00
 * it will generate a file with the same length. For real-time processing,
Packit Service cd2a00
 * it is probably easier not to use this call (so that the output duration
Packit Service cd2a00
 * is the same for the first frame).
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_skip_zeros(SpeexResamplerState *st);
Packit Service cd2a00
Packit Service cd2a00
/** Reset a resampler so a new (unrelated) stream can be processed.
Packit Service cd2a00
 * @param st Resampler state
Packit Service cd2a00
 */
Packit Service cd2a00
int speex_resampler_reset_mem(SpeexResamplerState *st);
Packit Service cd2a00
Packit Service cd2a00
/** Returns the English meaning for an error code
Packit Service cd2a00
 * @param err Error code
Packit Service cd2a00
 * @return English string
Packit Service cd2a00
 */
Packit Service cd2a00
const char *speex_resampler_strerror(int err);
Packit Service cd2a00
Packit Service cd2a00
#ifdef __cplusplus
Packit Service cd2a00
}
Packit Service cd2a00
#endif
Packit Service cd2a00
Packit Service cd2a00
#endif