tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

speex_resampler.h (15769B)


      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 #if 1 /* 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 #define RANDOM_PREFIX moz_speex
     51 #ifndef RANDOM_PREFIX
     52 #error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
     53 #endif
     54 
     55 #define CAT_PREFIX2(a,b) a ## b
     56 #define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)
     57 
     58 #define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init)
     59 #define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac)
     60 #define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy)
     61 #define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float)
     62 #define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int)
     63 #define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float)
     64 #define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int)
     65 #define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate)
     66 #define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate)
     67 #define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac)
     68 #define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio)
     69 #define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality)
     70 #define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality)
     71 #define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride)
     72 #define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride)
     73 #define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride)
     74 #define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride)
     75 #define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency)
     76 #define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency)
     77 #define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
     78 #define speex_resampler_set_skip_frac_num CAT_PREFIX(RANDOM_PREFIX,_resampler_set_skip_frac_num)
     79 #define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
     80 #define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
     81 
     82 #define spx_int16_t short
     83 #define spx_int32_t int
     84 #define spx_uint16_t unsigned short
     85 #define spx_uint32_t unsigned int
     86 
     87 #define speex_assert(cond)
     88 
     89 #else /* OUTSIDE_SPEEX */
     90 
     91 #include "speexdsp_types.h"
     92 
     93 #endif /* OUTSIDE_SPEEX */
     94 
     95 #ifdef __cplusplus
     96 extern "C" {
     97 #endif
     98 
     99 #define SPEEX_RESAMPLER_QUALITY_MAX 10
    100 #define SPEEX_RESAMPLER_QUALITY_MIN 0
    101 #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
    102 #define SPEEX_RESAMPLER_QUALITY_VOIP 3
    103 #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
    104 
    105 enum {
    106   RESAMPLER_ERR_SUCCESS         = 0,
    107   RESAMPLER_ERR_ALLOC_FAILED    = 1,
    108   RESAMPLER_ERR_BAD_STATE       = 2,
    109   RESAMPLER_ERR_INVALID_ARG     = 3,
    110   RESAMPLER_ERR_PTR_OVERLAP     = 4,
    111   RESAMPLER_ERR_OVERFLOW        = 5,
    112 
    113   RESAMPLER_ERR_MAX_ERROR
    114 };
    115 
    116 struct SpeexResamplerState_;
    117 typedef struct SpeexResamplerState_ SpeexResamplerState;
    118 
    119 /** Create a new resampler with integer input and output rates.
    120 * @param nb_channels Number of channels to be processed
    121 * @param in_rate Input sampling rate (integer number of Hz).
    122 * @param out_rate Output sampling rate (integer number of Hz).
    123 * @param quality Resampling quality between 0 and 10, where 0 has poor quality
    124 * and 10 has very high quality.
    125 * @return Newly created resampler state
    126 * @retval NULL Error: not enough memory
    127 */
    128 SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels,
    129                                          spx_uint32_t in_rate,
    130                                          spx_uint32_t out_rate,
    131                                          int quality,
    132                                          int *err);
    133 
    134 /** Create a new resampler with fractional input/output rates. The sampling
    135 * rate ratio is an arbitrary rational number with both the numerator and
    136 * denominator being 32-bit integers.
    137 * @param nb_channels Number of channels to be processed
    138 * @param ratio_num Numerator of the sampling rate ratio
    139 * @param ratio_den Denominator of the sampling rate ratio
    140 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
    141 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
    142 * @param quality Resampling quality between 0 and 10, where 0 has poor quality
    143 * and 10 has very high quality.
    144 * @return Newly created resampler state
    145 * @retval NULL Error: not enough memory
    146 */
    147 SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
    148                                               spx_uint32_t ratio_num,
    149                                               spx_uint32_t ratio_den,
    150                                               spx_uint32_t in_rate,
    151                                               spx_uint32_t out_rate,
    152                                               int quality,
    153                                               int *err);
    154 
    155 /** Destroy a resampler state.
    156 * @param st Resampler state
    157 */
    158 void speex_resampler_destroy(SpeexResamplerState *st);
    159 
    160 /** Resample a float array. The input and output buffers must *not* overlap.
    161 * @param st Resampler state
    162 * @param channel_index Index of the channel to process for the multi-channel
    163 * base (0 otherwise)
    164 * @param in Input buffer
    165 * @param in_len Number of input samples in the input buffer. Returns the
    166 * number of samples processed
    167 * @param out Output buffer
    168 * @param out_len Size of the output buffer. Returns the number of samples written
    169 */
    170 int speex_resampler_process_float(SpeexResamplerState *st,
    171                                   spx_uint32_t channel_index,
    172                                   const float *in,
    173                                   spx_uint32_t *in_len,
    174                                   float *out,
    175                                   spx_uint32_t *out_len);
    176 
    177 /** Resample an int array. The input and output buffers must *not* overlap.
    178 * @param st Resampler state
    179 * @param channel_index Index of the channel to process for the multi-channel
    180 * base (0 otherwise)
    181 * @param in Input buffer
    182 * @param in_len Number of input samples in the input buffer. Returns the number
    183 * of samples processed
    184 * @param out Output buffer
    185 * @param out_len Size of the output buffer. Returns the number of samples written
    186 */
    187 int speex_resampler_process_int(SpeexResamplerState *st,
    188                                 spx_uint32_t channel_index,
    189                                 const spx_int16_t *in,
    190                                 spx_uint32_t *in_len,
    191                                 spx_int16_t *out,
    192                                 spx_uint32_t *out_len);
    193 
    194 /** Resample an interleaved float array. The input and output buffers must *not* overlap.
    195 * @param st Resampler state
    196 * @param in Input buffer
    197 * @param in_len Number of input samples in the input buffer. Returns the number
    198 * of samples processed. This is all per-channel.
    199 * @param out Output buffer
    200 * @param out_len Size of the output buffer. Returns the number of samples written.
    201 * This is all per-channel.
    202 */
    203 int speex_resampler_process_interleaved_float(SpeexResamplerState *st,
    204                                               const float *in,
    205                                               spx_uint32_t *in_len,
    206                                               float *out,
    207                                               spx_uint32_t *out_len);
    208 
    209 /** Resample an interleaved int array. The input and output buffers must *not* overlap.
    210 * @param st Resampler state
    211 * @param in Input buffer
    212 * @param in_len Number of input samples in the input buffer. Returns the number
    213 * of samples processed. This is all per-channel.
    214 * @param out Output buffer
    215 * @param out_len Size of the output buffer. Returns the number of samples written.
    216 * This is all per-channel.
    217 */
    218 int speex_resampler_process_interleaved_int(SpeexResamplerState *st,
    219                                             const spx_int16_t *in,
    220                                             spx_uint32_t *in_len,
    221                                             spx_int16_t *out,
    222                                             spx_uint32_t *out_len);
    223 
    224 /** Set (change) the input/output sampling rates (integer value).
    225 * @param st Resampler state
    226 * @param in_rate Input sampling rate (integer number of Hz).
    227 * @param out_rate Output sampling rate (integer number of Hz).
    228 */
    229 int speex_resampler_set_rate(SpeexResamplerState *st,
    230                              spx_uint32_t in_rate,
    231                              spx_uint32_t out_rate);
    232 
    233 /** Get the current input/output sampling rates (integer value).
    234 * @param st Resampler state
    235 * @param in_rate Input sampling rate (integer number of Hz) copied.
    236 * @param out_rate Output sampling rate (integer number of Hz) copied.
    237 */
    238 void speex_resampler_get_rate(SpeexResamplerState *st,
    239                              spx_uint32_t *in_rate,
    240                              spx_uint32_t *out_rate);
    241 
    242 /** Set (change) the input/output sampling rates and resampling ratio
    243 * (fractional values in Hz supported).
    244 * @param st Resampler state
    245 * @param ratio_num Numerator of the sampling rate ratio
    246 * @param ratio_den Denominator of the sampling rate ratio
    247 * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
    248 * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
    249 */
    250 int speex_resampler_set_rate_frac(SpeexResamplerState *st,
    251                                   spx_uint32_t ratio_num,
    252                                   spx_uint32_t ratio_den,
    253                                   spx_uint32_t in_rate,
    254                                   spx_uint32_t out_rate);
    255 
    256 /** Get the current resampling ratio. This will be reduced to the least
    257 * common denominator.
    258 * @param st Resampler state
    259 * @param ratio_num Numerator of the sampling rate ratio copied
    260 * @param ratio_den Denominator of the sampling rate ratio copied
    261 */
    262 void speex_resampler_get_ratio(SpeexResamplerState *st,
    263                               spx_uint32_t *ratio_num,
    264                               spx_uint32_t *ratio_den);
    265 
    266 /** Set (change) the conversion quality.
    267 * @param st Resampler state
    268 * @param quality Resampling quality between 0 and 10, where 0 has poor
    269 * quality and 10 has very high quality.
    270 */
    271 int speex_resampler_set_quality(SpeexResamplerState *st,
    272                                 int quality);
    273 
    274 /** Get the conversion quality.
    275 * @param st Resampler state
    276 * @param quality Resampling quality between 0 and 10, where 0 has poor
    277 * quality and 10 has very high quality.
    278 */
    279 void speex_resampler_get_quality(SpeexResamplerState *st,
    280                                 int *quality);
    281 
    282 /** Set (change) the input stride.
    283 * @param st Resampler state
    284 * @param stride Input stride
    285 */
    286 void speex_resampler_set_input_stride(SpeexResamplerState *st,
    287                                      spx_uint32_t stride);
    288 
    289 /** Get the input stride.
    290 * @param st Resampler state
    291 * @param stride Input stride copied
    292 */
    293 void speex_resampler_get_input_stride(SpeexResamplerState *st,
    294                                      spx_uint32_t *stride);
    295 
    296 /** Set (change) the output stride.
    297 * @param st Resampler state
    298 * @param stride Output stride
    299 */
    300 void speex_resampler_set_output_stride(SpeexResamplerState *st,
    301                                      spx_uint32_t stride);
    302 
    303 /** Get the output stride.
    304 * @param st Resampler state copied
    305 * @param stride Output stride
    306 */
    307 void speex_resampler_get_output_stride(SpeexResamplerState *st,
    308                                      spx_uint32_t *stride);
    309 
    310 /** Get the latency introduced by the resampler measured in input samples.
    311 * @param st Resampler state
    312 */
    313 int speex_resampler_get_input_latency(SpeexResamplerState *st);
    314 
    315 /** Get the latency introduced by the resampler measured in output samples.
    316 * @param st Resampler state
    317 */
    318 int speex_resampler_get_output_latency(SpeexResamplerState *st);
    319 
    320 /** Make sure that the first samples to go out of the resamplers don't have
    321 * leading zeros. This is only useful before starting to use a newly created
    322 * resampler. It is recommended to use that when resampling an audio file, as
    323 * it will generate a file with the same length. For real-time processing,
    324 * it is probably easier not to use this call (so that the output duration
    325 * is the same for the first frame).
    326 * @param st Resampler state
    327 */
    328 int speex_resampler_skip_zeros(SpeexResamplerState *st);
    329 
    330 /** Set the numerator in a fraction determining the advance through input
    331 * samples before writing any output samples. The denominator of the fraction
    332 * is the value returned from speex_resampler_get_ratio() in ratio_den. This
    333 * is only useful before starting to use a newly created or reset resampler.
    334 * If the first input sample is interpreted as the signal at time
    335 * input_latency*in_rate, then the first output sample represents the signal
    336 * at the time frac_num/ratio_num*out_rate.
    337 * This is intended for careful alignment of output sample points wrt input
    338 * sample points. Large values are not an efficient offset into the in buffer.
    339 * @param st Resampler state
    340 * @param skip_frac_num Numerator of the offset fraction,
    341 *                      between 0 and ratio_den-1.
    342 */
    343 int speex_resampler_set_skip_frac_num(SpeexResamplerState *st,
    344                                      spx_uint32_t skip_frac_num);
    345 
    346 /** Reset a resampler so a new (unrelated) stream can be processed.
    347 * @param st Resampler state
    348 */
    349 int speex_resampler_reset_mem(SpeexResamplerState *st);
    350 
    351 /** Returns the English meaning for an error code
    352 * @param err Error code
    353 * @return English string
    354 */
    355 const char *speex_resampler_strerror(int err);
    356 
    357 #ifdef __cplusplus
    358 }
    359 #endif
    360 
    361 #endif