Path: blob/master/dep/cubeb/subprojects/speex/speex_resampler.h
4247 views
/* Copyright (C) 2007 Jean-Marc Valin12File: speex_resampler.h3Resampling code45The design goals of this code are:6- Very fast algorithm7- Low memory requirement8- Good *perceptual* quality (and not best SNR)910Redistribution and use in source and binary forms, with or without11modification, are permitted provided that the following conditions are12met:13141. Redistributions of source code must retain the above copyright notice,15this list of conditions and the following disclaimer.16172. Redistributions in binary form must reproduce the above copyright18notice, this list of conditions and the following disclaimer in the19documentation and/or other materials provided with the distribution.20213. The name of the author may not be used to endorse or promote products22derived from this software without specific prior written permission.2324THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR25IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES26OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE27DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,28INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR30SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,32STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN33ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE34POSSIBILITY OF SUCH DAMAGE.35*/363738#ifndef SPEEX_RESAMPLER_H39#define SPEEX_RESAMPLER_H4041#ifdef OUTSIDE_SPEEX4243/********* WARNING: MENTAL SANITY ENDS HERE *************/4445/* If the resampler is defined outside of Speex, we change the symbol names so that46there won't be any clash if linking with Speex later on. */4748/* #define RANDOM_PREFIX your software name here */49#ifndef RANDOM_PREFIX50#error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"51#endif5253#define CAT_PREFIX2(a,b) a ## b54#define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)5556#define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init)57#define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac)58#define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy)59#define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float)60#define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int)61#define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float)62#define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int)63#define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate)64#define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate)65#define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac)66#define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio)67#define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality)68#define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality)69#define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride)70#define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride)71#define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride)72#define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride)73#define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency)74#define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency)75#define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)76#define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)77#define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)7879#define spx_int16_t short80#define spx_int32_t int81#define spx_uint16_t unsigned short82#define spx_uint32_t unsigned int8384#define speex_assert(cond)8586#else /* OUTSIDE_SPEEX */8788#include "speexdsp_types.h"8990#endif /* OUTSIDE_SPEEX */9192#ifdef __cplusplus93extern "C" {94#endif9596#define SPEEX_RESAMPLER_QUALITY_MAX 1097#define SPEEX_RESAMPLER_QUALITY_MIN 098#define SPEEX_RESAMPLER_QUALITY_DEFAULT 499#define SPEEX_RESAMPLER_QUALITY_VOIP 3100#define SPEEX_RESAMPLER_QUALITY_DESKTOP 5101102enum {103RESAMPLER_ERR_SUCCESS = 0,104RESAMPLER_ERR_ALLOC_FAILED = 1,105RESAMPLER_ERR_BAD_STATE = 2,106RESAMPLER_ERR_INVALID_ARG = 3,107RESAMPLER_ERR_PTR_OVERLAP = 4,108RESAMPLER_ERR_OVERFLOW = 5,109110RESAMPLER_ERR_MAX_ERROR111};112113struct SpeexResamplerState_;114typedef struct SpeexResamplerState_ SpeexResamplerState;115116/** Create a new resampler with integer input and output rates.117* @param nb_channels Number of channels to be processed118* @param in_rate Input sampling rate (integer number of Hz).119* @param out_rate Output sampling rate (integer number of Hz).120* @param quality Resampling quality between 0 and 10, where 0 has poor quality121* and 10 has very high quality.122* @return Newly created resampler state123* @retval NULL Error: not enough memory124*/125SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels,126spx_uint32_t in_rate,127spx_uint32_t out_rate,128int quality,129int *err);130131/** Create a new resampler with fractional input/output rates. The sampling132* rate ratio is an arbitrary rational number with both the numerator and133* denominator being 32-bit integers.134* @param nb_channels Number of channels to be processed135* @param ratio_num Numerator of the sampling rate ratio136* @param ratio_den Denominator of the sampling rate ratio137* @param in_rate Input sampling rate rounded to the nearest integer (in Hz).138* @param out_rate Output sampling rate rounded to the nearest integer (in Hz).139* @param quality Resampling quality between 0 and 10, where 0 has poor quality140* and 10 has very high quality.141* @return Newly created resampler state142* @retval NULL Error: not enough memory143*/144SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,145spx_uint32_t ratio_num,146spx_uint32_t ratio_den,147spx_uint32_t in_rate,148spx_uint32_t out_rate,149int quality,150int *err);151152/** Destroy a resampler state.153* @param st Resampler state154*/155void speex_resampler_destroy(SpeexResamplerState *st);156157/** Resample a float array. The input and output buffers must *not* overlap.158* @param st Resampler state159* @param channel_index Index of the channel to process for the multi-channel160* base (0 otherwise)161* @param in Input buffer162* @param in_len Number of input samples in the input buffer. Returns the163* number of samples processed164* @param out Output buffer165* @param out_len Size of the output buffer. Returns the number of samples written166*/167int speex_resampler_process_float(SpeexResamplerState *st,168spx_uint32_t channel_index,169const float *in,170spx_uint32_t *in_len,171float *out,172spx_uint32_t *out_len);173174/** Resample an int array. The input and output buffers must *not* overlap.175* @param st Resampler state176* @param channel_index Index of the channel to process for the multi-channel177* base (0 otherwise)178* @param in Input buffer179* @param in_len Number of input samples in the input buffer. Returns the number180* of samples processed181* @param out Output buffer182* @param out_len Size of the output buffer. Returns the number of samples written183*/184int speex_resampler_process_int(SpeexResamplerState *st,185spx_uint32_t channel_index,186const spx_int16_t *in,187spx_uint32_t *in_len,188spx_int16_t *out,189spx_uint32_t *out_len);190191/** Resample an interleaved float array. The input and output buffers must *not* overlap.192* @param st Resampler state193* @param in Input buffer194* @param in_len Number of input samples in the input buffer. Returns the number195* of samples processed. This is all per-channel.196* @param out Output buffer197* @param out_len Size of the output buffer. Returns the number of samples written.198* This is all per-channel.199*/200int speex_resampler_process_interleaved_float(SpeexResamplerState *st,201const float *in,202spx_uint32_t *in_len,203float *out,204spx_uint32_t *out_len);205206/** Resample an interleaved int array. The input and output buffers must *not* overlap.207* @param st Resampler state208* @param in Input buffer209* @param in_len Number of input samples in the input buffer. Returns the number210* of samples processed. This is all per-channel.211* @param out Output buffer212* @param out_len Size of the output buffer. Returns the number of samples written.213* This is all per-channel.214*/215int speex_resampler_process_interleaved_int(SpeexResamplerState *st,216const spx_int16_t *in,217spx_uint32_t *in_len,218spx_int16_t *out,219spx_uint32_t *out_len);220221/** Set (change) the input/output sampling rates (integer value).222* @param st Resampler state223* @param in_rate Input sampling rate (integer number of Hz).224* @param out_rate Output sampling rate (integer number of Hz).225*/226int speex_resampler_set_rate(SpeexResamplerState *st,227spx_uint32_t in_rate,228spx_uint32_t out_rate);229230/** Get the current input/output sampling rates (integer value).231* @param st Resampler state232* @param in_rate Input sampling rate (integer number of Hz) copied.233* @param out_rate Output sampling rate (integer number of Hz) copied.234*/235void speex_resampler_get_rate(SpeexResamplerState *st,236spx_uint32_t *in_rate,237spx_uint32_t *out_rate);238239/** Set (change) the input/output sampling rates and resampling ratio240* (fractional values in Hz supported).241* @param st Resampler state242* @param ratio_num Numerator of the sampling rate ratio243* @param ratio_den Denominator of the sampling rate ratio244* @param in_rate Input sampling rate rounded to the nearest integer (in Hz).245* @param out_rate Output sampling rate rounded to the nearest integer (in Hz).246*/247int speex_resampler_set_rate_frac(SpeexResamplerState *st,248spx_uint32_t ratio_num,249spx_uint32_t ratio_den,250spx_uint32_t in_rate,251spx_uint32_t out_rate);252253/** Get the current resampling ratio. This will be reduced to the least254* common denominator.255* @param st Resampler state256* @param ratio_num Numerator of the sampling rate ratio copied257* @param ratio_den Denominator of the sampling rate ratio copied258*/259void speex_resampler_get_ratio(SpeexResamplerState *st,260spx_uint32_t *ratio_num,261spx_uint32_t *ratio_den);262263/** Set (change) the conversion quality.264* @param st Resampler state265* @param quality Resampling quality between 0 and 10, where 0 has poor266* quality and 10 has very high quality.267*/268int speex_resampler_set_quality(SpeexResamplerState *st,269int quality);270271/** Get the conversion quality.272* @param st Resampler state273* @param quality Resampling quality between 0 and 10, where 0 has poor274* quality and 10 has very high quality.275*/276void speex_resampler_get_quality(SpeexResamplerState *st,277int *quality);278279/** Set (change) the input stride.280* @param st Resampler state281* @param stride Input stride282*/283void speex_resampler_set_input_stride(SpeexResamplerState *st,284spx_uint32_t stride);285286/** Get the input stride.287* @param st Resampler state288* @param stride Input stride copied289*/290void speex_resampler_get_input_stride(SpeexResamplerState *st,291spx_uint32_t *stride);292293/** Set (change) the output stride.294* @param st Resampler state295* @param stride Output stride296*/297void speex_resampler_set_output_stride(SpeexResamplerState *st,298spx_uint32_t stride);299300/** Get the output stride.301* @param st Resampler state copied302* @param stride Output stride303*/304void speex_resampler_get_output_stride(SpeexResamplerState *st,305spx_uint32_t *stride);306307/** Get the latency introduced by the resampler measured in input samples.308* @param st Resampler state309*/310int speex_resampler_get_input_latency(SpeexResamplerState *st);311312/** Get the latency introduced by the resampler measured in output samples.313* @param st Resampler state314*/315int speex_resampler_get_output_latency(SpeexResamplerState *st);316317/** Make sure that the first samples to go out of the resamplers don't have318* leading zeros. This is only useful before starting to use a newly created319* resampler. It is recommended to use that when resampling an audio file, as320* it will generate a file with the same length. For real-time processing,321* it is probably easier not to use this call (so that the output duration322* is the same for the first frame).323* @param st Resampler state324*/325int speex_resampler_skip_zeros(SpeexResamplerState *st);326327/** Reset a resampler so a new (unrelated) stream can be processed.328* @param st Resampler state329*/330int speex_resampler_reset_mem(SpeexResamplerState *st);331332/** Returns the English meaning for an error code333* @param err Error code334* @return English string335*/336const char *speex_resampler_strerror(int err);337338#ifdef __cplusplus339}340#endif341342#endif343344345