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