samplerate.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /*
  19. ** This code is part of Secret Rabbit Code aka libsamplerate. A commercial
  20. ** use license for this code is available, please see:
  21. ** http://www.mega-nerd.com/SRC/procedure.html
  22. */
  23. /*
  24. ** API documentation is available here:
  25. ** http://www.mega-nerd.com/SRC/api.html
  26. */
  27. #ifndef SAMPLERATE_H
  28. #define SAMPLERATE_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif /* __cplusplus */
  32. /* Opaque data type SRC_STATE. */
  33. typedef struct SRC_STATE_tag SRC_STATE ;
  34. /* SRC_DATA is used to pass data to src_simple() and src_process(). */
  35. typedef struct
  36. { float *data_in, *data_out ;
  37. long input_frames, output_frames ;
  38. long input_frames_used, output_frames_gen ;
  39. int end_of_input ;
  40. double src_ratio ;
  41. } SRC_DATA ;
  42. /* SRC_CB_DATA is used with callback based API. */
  43. typedef struct
  44. { long frames ;
  45. float *data_in ;
  46. } SRC_CB_DATA ;
  47. /*
  48. ** User supplied callback function type for use with src_callback_new()
  49. ** and src_callback_read(). First parameter is the same pointer that was
  50. ** passed into src_callback_new(). Second parameter is pointer to a
  51. ** pointer. The user supplied callback function must modify *data to
  52. ** point to the start of the user supplied float array. The user supplied
  53. ** function must return the number of frames that **data points to.
  54. */
  55. typedef long (*src_callback_t) (void *cb_data, float **data) ;
  56. /*
  57. ** Standard initialisation function : return an anonymous pointer to the
  58. ** internal state of the converter. Choose a converter from the enums below.
  59. ** Error returned in *error.
  60. */
  61. SRC_STATE* src_new (int converter_type, int channels, int *error) ;
  62. /*
  63. ** Initilisation for callback based API : return an anonymous pointer to the
  64. ** internal state of the converter. Choose a converter from the enums below.
  65. ** The cb_data pointer can point to any data or be set to NULL. Whatever the
  66. ** value, when processing, user supplied function "func" gets called with
  67. ** cb_data as first parameter.
  68. */
  69. SRC_STATE* src_callback_new (src_callback_t func, int converter_type, int channels,
  70. int *error, void* cb_data) ;
  71. /*
  72. ** Cleanup all internal allocations.
  73. ** Always returns NULL.
  74. */
  75. SRC_STATE* src_delete (SRC_STATE *state) ;
  76. /*
  77. ** Standard processing function.
  78. ** Returns non zero on error.
  79. */
  80. int src_process (SRC_STATE *state, SRC_DATA *data) ;
  81. /*
  82. ** Callback based processing function. Read up to frames worth of data from
  83. ** the converter int *data and return frames read or -1 on error.
  84. */
  85. long src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data) ;
  86. /*
  87. ** Simple interface for performing a single conversion from input buffer to
  88. ** output buffer at a fixed conversion ratio.
  89. ** Simple interface does not require initialisation as it can only operate on
  90. ** a single buffer worth of audio.
  91. */
  92. int src_simple (SRC_DATA *data, int converter_type, int channels) ;
  93. /*
  94. ** This library contains a number of different sample rate converters,
  95. ** numbered 0 through N.
  96. **
  97. ** Return a string giving either a name or a more full description of each
  98. ** sample rate converter or NULL if no sample rate converter exists for
  99. ** the given value. The converters are sequentially numbered from 0 to N.
  100. */
  101. const char *src_get_name (int converter_type) ;
  102. const char *src_get_description (int converter_type) ;
  103. const char *src_get_version (void) ;
  104. /*
  105. ** Set a new SRC ratio. This allows step responses
  106. ** in the conversion ratio.
  107. ** Returns non zero on error.
  108. */
  109. int src_set_ratio (SRC_STATE *state, double new_ratio) ;
  110. /*
  111. ** Reset the internal SRC state.
  112. ** Does not modify the quality settings.
  113. ** Does not free any memory allocations.
  114. ** Returns non zero on error.
  115. */
  116. int src_reset (SRC_STATE *state) ;
  117. /*
  118. ** Return TRUE if ratio is a valid conversion ratio, FALSE
  119. ** otherwise.
  120. */
  121. int src_is_valid_ratio (double ratio) ;
  122. /*
  123. ** Return an error number.
  124. */
  125. int src_error (SRC_STATE *state) ;
  126. /*
  127. ** Convert the error number into a string.
  128. */
  129. const char* src_strerror (int error) ;
  130. /*
  131. ** The following enums can be used to set the interpolator type
  132. ** using the function src_set_converter().
  133. */
  134. enum
  135. {
  136. SRC_SINC_BEST_QUALITY = 0,
  137. SRC_SINC_MEDIUM_QUALITY = 1,
  138. SRC_SINC_FASTEST = 2,
  139. SRC_ZERO_ORDER_HOLD = 3,
  140. SRC_LINEAR = 4,
  141. } ;
  142. /*
  143. ** Extra helper functions for converting from short to float and
  144. ** back again.
  145. */
  146. void src_short_to_float_array (const short *in, float *out, int len) ;
  147. void src_float_to_short_array (const float *in, short *out, int len) ;
  148. void src_int_to_float_array (const int *in, float *out, int len) ;
  149. void src_float_to_int_array (const float *in, int *out, int len) ;
  150. #ifdef __cplusplus
  151. } /* extern "C" */
  152. #endif /* __cplusplus */
  153. #endif /* SAMPLERATE_H */