1*1480c0b3SChristos Margiolis /* 2*1480c0b3SChristos Margiolis ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com> 3*1480c0b3SChristos Margiolis ** All rights reserved. 4*1480c0b3SChristos Margiolis ** 5*1480c0b3SChristos Margiolis ** This code is released under 2-clause BSD license. Please see the 6*1480c0b3SChristos Margiolis ** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING 7*1480c0b3SChristos Margiolis */ 8*1480c0b3SChristos Margiolis 9*1480c0b3SChristos Margiolis /* 10*1480c0b3SChristos Margiolis ** API documentation is available here: 11*1480c0b3SChristos Margiolis ** http://libsndfile.github.io/libsamplerate/api.html 12*1480c0b3SChristos Margiolis */ 13*1480c0b3SChristos Margiolis 14*1480c0b3SChristos Margiolis #ifndef SAMPLERATE_H 15*1480c0b3SChristos Margiolis #define SAMPLERATE_H 16*1480c0b3SChristos Margiolis 17*1480c0b3SChristos Margiolis #ifdef __cplusplus 18*1480c0b3SChristos Margiolis extern "C" { 19*1480c0b3SChristos Margiolis #endif /* __cplusplus */ 20*1480c0b3SChristos Margiolis 21*1480c0b3SChristos Margiolis 22*1480c0b3SChristos Margiolis /* Opaque data type SRC_STATE. */ 23*1480c0b3SChristos Margiolis typedef struct SRC_STATE_tag SRC_STATE ; 24*1480c0b3SChristos Margiolis 25*1480c0b3SChristos Margiolis /* SRC_DATA is used to pass data to src_simple() and src_process(). */ 26*1480c0b3SChristos Margiolis typedef struct 27*1480c0b3SChristos Margiolis { const float *data_in ; 28*1480c0b3SChristos Margiolis float *data_out ; 29*1480c0b3SChristos Margiolis 30*1480c0b3SChristos Margiolis long input_frames, output_frames ; 31*1480c0b3SChristos Margiolis long input_frames_used, output_frames_gen ; 32*1480c0b3SChristos Margiolis 33*1480c0b3SChristos Margiolis int end_of_input ; 34*1480c0b3SChristos Margiolis 35*1480c0b3SChristos Margiolis double src_ratio ; 36*1480c0b3SChristos Margiolis } SRC_DATA ; 37*1480c0b3SChristos Margiolis 38*1480c0b3SChristos Margiolis /* 39*1480c0b3SChristos Margiolis ** User supplied callback function type for use with src_callback_new() 40*1480c0b3SChristos Margiolis ** and src_callback_read(). First parameter is the same pointer that was 41*1480c0b3SChristos Margiolis ** passed into src_callback_new(). Second parameter is pointer to a 42*1480c0b3SChristos Margiolis ** pointer. The user supplied callback function must modify *data to 43*1480c0b3SChristos Margiolis ** point to the start of the user supplied float array. The user supplied 44*1480c0b3SChristos Margiolis ** function must return the number of frames that **data points to. 45*1480c0b3SChristos Margiolis */ 46*1480c0b3SChristos Margiolis 47*1480c0b3SChristos Margiolis typedef long (*src_callback_t) (void *cb_data, float **data) ; 48*1480c0b3SChristos Margiolis 49*1480c0b3SChristos Margiolis /* 50*1480c0b3SChristos Margiolis ** Standard initialisation function : return an anonymous pointer to the 51*1480c0b3SChristos Margiolis ** internal state of the converter. Choose a converter from the enums below. 52*1480c0b3SChristos Margiolis ** Error returned in *error. 53*1480c0b3SChristos Margiolis */ 54*1480c0b3SChristos Margiolis 55*1480c0b3SChristos Margiolis SRC_STATE* src_new (int converter_type, int channels, int *error) ; 56*1480c0b3SChristos Margiolis 57*1480c0b3SChristos Margiolis /* 58*1480c0b3SChristos Margiolis ** Clone a handle : return an anonymous pointer to a new converter 59*1480c0b3SChristos Margiolis ** containing the same internal state as orig. Error returned in *error. 60*1480c0b3SChristos Margiolis */ 61*1480c0b3SChristos Margiolis SRC_STATE* src_clone (SRC_STATE* orig, int *error) ; 62*1480c0b3SChristos Margiolis 63*1480c0b3SChristos Margiolis /* 64*1480c0b3SChristos Margiolis ** Initilisation for callback based API : return an anonymous pointer to the 65*1480c0b3SChristos Margiolis ** internal state of the converter. Choose a converter from the enums below. 66*1480c0b3SChristos Margiolis ** The cb_data pointer can point to any data or be set to NULL. Whatever the 67*1480c0b3SChristos Margiolis ** value, when processing, user supplied function "func" gets called with 68*1480c0b3SChristos Margiolis ** cb_data as first parameter. 69*1480c0b3SChristos Margiolis */ 70*1480c0b3SChristos Margiolis 71*1480c0b3SChristos Margiolis SRC_STATE* src_callback_new (src_callback_t func, int converter_type, int channels, 72*1480c0b3SChristos Margiolis int *error, void* cb_data) ; 73*1480c0b3SChristos Margiolis 74*1480c0b3SChristos Margiolis /* 75*1480c0b3SChristos Margiolis ** Cleanup all internal allocations. 76*1480c0b3SChristos Margiolis ** Always returns NULL. 77*1480c0b3SChristos Margiolis */ 78*1480c0b3SChristos Margiolis 79*1480c0b3SChristos Margiolis SRC_STATE* src_delete (SRC_STATE *state) ; 80*1480c0b3SChristos Margiolis 81*1480c0b3SChristos Margiolis /* 82*1480c0b3SChristos Margiolis ** Standard processing function. 83*1480c0b3SChristos Margiolis ** Returns non zero on error. 84*1480c0b3SChristos Margiolis */ 85*1480c0b3SChristos Margiolis 86*1480c0b3SChristos Margiolis int src_process (SRC_STATE *state, SRC_DATA *data) ; 87*1480c0b3SChristos Margiolis 88*1480c0b3SChristos Margiolis /* 89*1480c0b3SChristos Margiolis ** Callback based processing function. Read up to frames worth of data from 90*1480c0b3SChristos Margiolis ** the converter int *data and return frames read or -1 on error. 91*1480c0b3SChristos Margiolis */ 92*1480c0b3SChristos Margiolis long src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data) ; 93*1480c0b3SChristos Margiolis 94*1480c0b3SChristos Margiolis /* 95*1480c0b3SChristos Margiolis ** Simple interface for performing a single conversion from input buffer to 96*1480c0b3SChristos Margiolis ** output buffer at a fixed conversion ratio. 97*1480c0b3SChristos Margiolis ** Simple interface does not require initialisation as it can only operate on 98*1480c0b3SChristos Margiolis ** a single buffer worth of audio. 99*1480c0b3SChristos Margiolis */ 100*1480c0b3SChristos Margiolis 101*1480c0b3SChristos Margiolis int src_simple (SRC_DATA *data, int converter_type, int channels) ; 102*1480c0b3SChristos Margiolis 103*1480c0b3SChristos Margiolis /* 104*1480c0b3SChristos Margiolis ** This library contains a number of different sample rate converters, 105*1480c0b3SChristos Margiolis ** numbered 0 through N. 106*1480c0b3SChristos Margiolis ** 107*1480c0b3SChristos Margiolis ** Return a string giving either a name or a more full description of each 108*1480c0b3SChristos Margiolis ** sample rate converter or NULL if no sample rate converter exists for 109*1480c0b3SChristos Margiolis ** the given value. The converters are sequentially numbered from 0 to N. 110*1480c0b3SChristos Margiolis */ 111*1480c0b3SChristos Margiolis 112*1480c0b3SChristos Margiolis const char *src_get_name (int converter_type) ; 113*1480c0b3SChristos Margiolis const char *src_get_description (int converter_type) ; 114*1480c0b3SChristos Margiolis const char *src_get_version (void) ; 115*1480c0b3SChristos Margiolis 116*1480c0b3SChristos Margiolis /* 117*1480c0b3SChristos Margiolis ** Set a new SRC ratio. This allows step responses 118*1480c0b3SChristos Margiolis ** in the conversion ratio. 119*1480c0b3SChristos Margiolis ** Returns non zero on error. 120*1480c0b3SChristos Margiolis */ 121*1480c0b3SChristos Margiolis 122*1480c0b3SChristos Margiolis int src_set_ratio (SRC_STATE *state, double new_ratio) ; 123*1480c0b3SChristos Margiolis 124*1480c0b3SChristos Margiolis /* 125*1480c0b3SChristos Margiolis ** Get the current channel count. 126*1480c0b3SChristos Margiolis ** Returns negative on error, positive channel count otherwise 127*1480c0b3SChristos Margiolis */ 128*1480c0b3SChristos Margiolis 129*1480c0b3SChristos Margiolis int src_get_channels (SRC_STATE *state) ; 130*1480c0b3SChristos Margiolis 131*1480c0b3SChristos Margiolis /* 132*1480c0b3SChristos Margiolis ** Reset the internal SRC state. 133*1480c0b3SChristos Margiolis ** Does not modify the quality settings. 134*1480c0b3SChristos Margiolis ** Does not free any memory allocations. 135*1480c0b3SChristos Margiolis ** Returns non zero on error. 136*1480c0b3SChristos Margiolis */ 137*1480c0b3SChristos Margiolis 138*1480c0b3SChristos Margiolis int src_reset (SRC_STATE *state) ; 139*1480c0b3SChristos Margiolis 140*1480c0b3SChristos Margiolis /* 141*1480c0b3SChristos Margiolis ** Return TRUE if ratio is a valid conversion ratio, FALSE 142*1480c0b3SChristos Margiolis ** otherwise. 143*1480c0b3SChristos Margiolis */ 144*1480c0b3SChristos Margiolis 145*1480c0b3SChristos Margiolis int src_is_valid_ratio (double ratio) ; 146*1480c0b3SChristos Margiolis 147*1480c0b3SChristos Margiolis /* 148*1480c0b3SChristos Margiolis ** Return an error number. 149*1480c0b3SChristos Margiolis */ 150*1480c0b3SChristos Margiolis 151*1480c0b3SChristos Margiolis int src_error (SRC_STATE *state) ; 152*1480c0b3SChristos Margiolis 153*1480c0b3SChristos Margiolis /* 154*1480c0b3SChristos Margiolis ** Convert the error number into a string. 155*1480c0b3SChristos Margiolis */ 156*1480c0b3SChristos Margiolis const char* src_strerror (int error) ; 157*1480c0b3SChristos Margiolis 158*1480c0b3SChristos Margiolis /* 159*1480c0b3SChristos Margiolis ** The following enums can be used to set the interpolator type 160*1480c0b3SChristos Margiolis ** using the function src_set_converter(). 161*1480c0b3SChristos Margiolis */ 162*1480c0b3SChristos Margiolis 163*1480c0b3SChristos Margiolis enum 164*1480c0b3SChristos Margiolis { 165*1480c0b3SChristos Margiolis SRC_SINC_BEST_QUALITY = 0, 166*1480c0b3SChristos Margiolis SRC_SINC_MEDIUM_QUALITY = 1, 167*1480c0b3SChristos Margiolis SRC_SINC_FASTEST = 2, 168*1480c0b3SChristos Margiolis SRC_ZERO_ORDER_HOLD = 3, 169*1480c0b3SChristos Margiolis SRC_LINEAR = 4, 170*1480c0b3SChristos Margiolis } ; 171*1480c0b3SChristos Margiolis 172*1480c0b3SChristos Margiolis /* 173*1480c0b3SChristos Margiolis ** Extra helper functions for converting from short to float and 174*1480c0b3SChristos Margiolis ** back again. 175*1480c0b3SChristos Margiolis */ 176*1480c0b3SChristos Margiolis 177*1480c0b3SChristos Margiolis void src_short_to_float_array (const short *in, float *out, int len) ; 178*1480c0b3SChristos Margiolis void src_float_to_short_array (const float *in, short *out, int len) ; 179*1480c0b3SChristos Margiolis 180*1480c0b3SChristos Margiolis void src_int_to_float_array (const int *in, float *out, int len) ; 181*1480c0b3SChristos Margiolis void src_float_to_int_array (const float *in, int *out, int len) ; 182*1480c0b3SChristos Margiolis 183*1480c0b3SChristos Margiolis 184*1480c0b3SChristos Margiolis #ifdef __cplusplus 185*1480c0b3SChristos Margiolis } /* extern "C" */ 186*1480c0b3SChristos Margiolis #endif /* __cplusplus */ 187*1480c0b3SChristos Margiolis 188*1480c0b3SChristos Margiolis #endif /* SAMPLERATE_H */ 189*1480c0b3SChristos Margiolis 190