speex_resampler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Copyright (C) 2007 Jean-Marc Valin
  2. File: speex_resampler.h
  3. Resampling code
  4. The design goals of this code are:
  5. - Very fast algorithm
  6. - Low memory requirement
  7. - Good *perceptual* quality (and not best SNR)
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are
  10. met:
  11. 1. Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. The name of the author may not be used to endorse or promote products
  17. derived from this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  26. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef SPEEX_RESAMPLER_H
  31. #define SPEEX_RESAMPLER_H
  32. #ifdef OUTSIDE_SPEEX
  33. /********* WARNING: MENTAL SANITY ENDS HERE *************/
  34. /* If the resampler is defined outside of Speex, we change the symbol names so that
  35. there won't be any clash if linking with Speex later on. */
  36. /* #define RANDOM_PREFIX your software name here */
  37. #ifndef RANDOM_PREFIX
  38. #error "Please define RANDOM_PREFIX (above) to something specific to your project to prevent symbol name clashes"
  39. #endif
  40. #define CAT_PREFIX2(a,b) a ## b
  41. #define CAT_PREFIX(a,b) CAT_PREFIX2(a, b)
  42. #define speex_resampler_init CAT_PREFIX(RANDOM_PREFIX,_resampler_init)
  43. #define speex_resampler_init_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_init_frac)
  44. #define speex_resampler_destroy CAT_PREFIX(RANDOM_PREFIX,_resampler_destroy)
  45. #define speex_resampler_process_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_float)
  46. #define speex_resampler_process_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_int)
  47. #define speex_resampler_process_interleaved_float CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_float)
  48. #define speex_resampler_process_interleaved_int CAT_PREFIX(RANDOM_PREFIX,_resampler_process_interleaved_int)
  49. #define speex_resampler_set_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate)
  50. #define speex_resampler_get_rate CAT_PREFIX(RANDOM_PREFIX,_resampler_get_rate)
  51. #define speex_resampler_set_rate_frac CAT_PREFIX(RANDOM_PREFIX,_resampler_set_rate_frac)
  52. #define speex_resampler_get_ratio CAT_PREFIX(RANDOM_PREFIX,_resampler_get_ratio)
  53. #define speex_resampler_set_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_set_quality)
  54. #define speex_resampler_get_quality CAT_PREFIX(RANDOM_PREFIX,_resampler_get_quality)
  55. #define speex_resampler_set_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_input_stride)
  56. #define speex_resampler_get_input_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_stride)
  57. #define speex_resampler_set_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_set_output_stride)
  58. #define speex_resampler_get_output_stride CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_stride)
  59. #define speex_resampler_get_input_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_input_latency)
  60. #define speex_resampler_get_output_latency CAT_PREFIX(RANDOM_PREFIX,_resampler_get_output_latency)
  61. #define speex_resampler_skip_zeros CAT_PREFIX(RANDOM_PREFIX,_resampler_skip_zeros)
  62. #define speex_resampler_reset_mem CAT_PREFIX(RANDOM_PREFIX,_resampler_reset_mem)
  63. #define speex_resampler_strerror CAT_PREFIX(RANDOM_PREFIX,_resampler_strerror)
  64. #define spx_int16_t short
  65. #define spx_int32_t int
  66. #define spx_uint16_t unsigned short
  67. #define spx_uint32_t unsigned int
  68. #else /* OUTSIDE_SPEEX */
  69. #include "speex/speex_types.h"
  70. #endif /* OUTSIDE_SPEEX */
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. #define SPEEX_RESAMPLER_QUALITY_MAX 10
  75. #define SPEEX_RESAMPLER_QUALITY_MIN 0
  76. #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
  77. #define SPEEX_RESAMPLER_QUALITY_VOIP 3
  78. #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
  79. enum {
  80. RESAMPLER_ERR_SUCCESS = 0,
  81. RESAMPLER_ERR_ALLOC_FAILED = 1,
  82. RESAMPLER_ERR_BAD_STATE = 2,
  83. RESAMPLER_ERR_INVALID_ARG = 3,
  84. RESAMPLER_ERR_PTR_OVERLAP = 4,
  85. RESAMPLER_ERR_MAX_ERROR
  86. };
  87. struct SpeexResamplerState_;
  88. typedef struct SpeexResamplerState_ SpeexResamplerState;
  89. /** Create a new resampler with integer input and output rates.
  90. * @param nb_channels Number of channels to be processed
  91. * @param in_rate Input sampling rate (integer number of Hz).
  92. * @param out_rate Output sampling rate (integer number of Hz).
  93. * @param quality Resampling quality between 0 and 10, where 0 has poor quality
  94. * and 10 has very high quality.
  95. * @return Newly created resampler state
  96. * @retval NULL Error: not enough memory
  97. */
  98. SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels,
  99. spx_uint32_t in_rate,
  100. spx_uint32_t out_rate,
  101. int quality,
  102. int *err);
  103. /** Create a new resampler with fractional input/output rates. The sampling
  104. * rate ratio is an arbitrary rational number with both the numerator and
  105. * denominator being 32-bit integers.
  106. * @param nb_channels Number of channels to be processed
  107. * @param ratio_num Numerator of the sampling rate ratio
  108. * @param ratio_den Denominator of the sampling rate ratio
  109. * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
  110. * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
  111. * @param quality Resampling quality between 0 and 10, where 0 has poor quality
  112. * and 10 has very high quality.
  113. * @return Newly created resampler state
  114. * @retval NULL Error: not enough memory
  115. */
  116. SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
  117. spx_uint32_t ratio_num,
  118. spx_uint32_t ratio_den,
  119. spx_uint32_t in_rate,
  120. spx_uint32_t out_rate,
  121. int quality,
  122. int *err);
  123. /** Destroy a resampler state.
  124. * @param st Resampler state
  125. */
  126. void speex_resampler_destroy(SpeexResamplerState *st);
  127. /** Resample a float array. The input and output buffers must *not* overlap.
  128. * @param st Resampler state
  129. * @param channel_index Index of the channel to process for the multi-channel
  130. * base (0 otherwise)
  131. * @param in Input buffer
  132. * @param in_len Number of input samples in the input buffer. Returns the
  133. * number of samples processed
  134. * @param out Output buffer
  135. * @param out_len Size of the output buffer. Returns the number of samples written
  136. */
  137. int speex_resampler_process_float(SpeexResamplerState *st,
  138. spx_uint32_t channel_index,
  139. const float *in,
  140. spx_uint32_t *in_len,
  141. float *out,
  142. spx_uint32_t *out_len);
  143. /** Resample an int array. The input and output buffers must *not* overlap.
  144. * @param st Resampler state
  145. * @param channel_index Index of the channel to process for the multi-channel
  146. * base (0 otherwise)
  147. * @param in Input buffer
  148. * @param in_len Number of input samples in the input buffer. Returns the number
  149. * of samples processed
  150. * @param out Output buffer
  151. * @param out_len Size of the output buffer. Returns the number of samples written
  152. */
  153. int speex_resampler_process_int(SpeexResamplerState *st,
  154. spx_uint32_t channel_index,
  155. const spx_int16_t *in,
  156. spx_uint32_t *in_len,
  157. spx_int16_t *out,
  158. spx_uint32_t *out_len);
  159. /** Resample an interleaved float array. The input and output buffers must *not* overlap.
  160. * @param st Resampler state
  161. * @param in Input buffer
  162. * @param in_len Number of input samples in the input buffer. Returns the number
  163. * of samples processed. This is all per-channel.
  164. * @param out Output buffer
  165. * @param out_len Size of the output buffer. Returns the number of samples written.
  166. * This is all per-channel.
  167. */
  168. int speex_resampler_process_interleaved_float(SpeexResamplerState *st,
  169. const float *in,
  170. spx_uint32_t *in_len,
  171. float *out,
  172. spx_uint32_t *out_len);
  173. /** Resample an interleaved int array. The input and output buffers must *not* overlap.
  174. * @param st Resampler state
  175. * @param in Input buffer
  176. * @param in_len Number of input samples in the input buffer. Returns the number
  177. * of samples processed. This is all per-channel.
  178. * @param out Output buffer
  179. * @param out_len Size of the output buffer. Returns the number of samples written.
  180. * This is all per-channel.
  181. */
  182. int speex_resampler_process_interleaved_int(SpeexResamplerState *st,
  183. const spx_int16_t *in,
  184. spx_uint32_t *in_len,
  185. spx_int16_t *out,
  186. spx_uint32_t *out_len);
  187. /** Set (change) the input/output sampling rates (integer value).
  188. * @param st Resampler state
  189. * @param in_rate Input sampling rate (integer number of Hz).
  190. * @param out_rate Output sampling rate (integer number of Hz).
  191. */
  192. int speex_resampler_set_rate(SpeexResamplerState *st,
  193. spx_uint32_t in_rate,
  194. spx_uint32_t out_rate);
  195. /** Get the current input/output sampling rates (integer value).
  196. * @param st Resampler state
  197. * @param in_rate Input sampling rate (integer number of Hz) copied.
  198. * @param out_rate Output sampling rate (integer number of Hz) copied.
  199. */
  200. void speex_resampler_get_rate(SpeexResamplerState *st,
  201. spx_uint32_t *in_rate,
  202. spx_uint32_t *out_rate);
  203. /** Set (change) the input/output sampling rates and resampling ratio
  204. * (fractional values in Hz supported).
  205. * @param st Resampler state
  206. * @param ratio_num Numerator of the sampling rate ratio
  207. * @param ratio_den Denominator of the sampling rate ratio
  208. * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
  209. * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
  210. */
  211. int speex_resampler_set_rate_frac(SpeexResamplerState *st,
  212. spx_uint32_t ratio_num,
  213. spx_uint32_t ratio_den,
  214. spx_uint32_t in_rate,
  215. spx_uint32_t out_rate);
  216. /** Get the current resampling ratio. This will be reduced to the least
  217. * common denominator.
  218. * @param st Resampler state
  219. * @param ratio_num Numerator of the sampling rate ratio copied
  220. * @param ratio_den Denominator of the sampling rate ratio copied
  221. */
  222. void speex_resampler_get_ratio(SpeexResamplerState *st,
  223. spx_uint32_t *ratio_num,
  224. spx_uint32_t *ratio_den);
  225. /** Set (change) the conversion quality.
  226. * @param st Resampler state
  227. * @param quality Resampling quality between 0 and 10, where 0 has poor
  228. * quality and 10 has very high quality.
  229. */
  230. int speex_resampler_set_quality(SpeexResamplerState *st,
  231. int quality);
  232. /** Get the conversion quality.
  233. * @param st Resampler state
  234. * @param quality Resampling quality between 0 and 10, where 0 has poor
  235. * quality and 10 has very high quality.
  236. */
  237. void speex_resampler_get_quality(SpeexResamplerState *st,
  238. int *quality);
  239. /** Set (change) the input stride.
  240. * @param st Resampler state
  241. * @param stride Input stride
  242. */
  243. void speex_resampler_set_input_stride(SpeexResamplerState *st,
  244. spx_uint32_t stride);
  245. /** Get the input stride.
  246. * @param st Resampler state
  247. * @param stride Input stride copied
  248. */
  249. void speex_resampler_get_input_stride(SpeexResamplerState *st,
  250. spx_uint32_t *stride);
  251. /** Set (change) the output stride.
  252. * @param st Resampler state
  253. * @param stride Output stride
  254. */
  255. void speex_resampler_set_output_stride(SpeexResamplerState *st,
  256. spx_uint32_t stride);
  257. /** Get the output stride.
  258. * @param st Resampler state copied
  259. * @param stride Output stride
  260. */
  261. void speex_resampler_get_output_stride(SpeexResamplerState *st,
  262. spx_uint32_t *stride);
  263. /** Get the latency in input samples introduced by the resampler.
  264. * @param st Resampler state
  265. */
  266. int speex_resampler_get_input_latency(SpeexResamplerState *st);
  267. /** Get the latency in output samples introduced by the resampler.
  268. * @param st Resampler state
  269. */
  270. int speex_resampler_get_output_latency(SpeexResamplerState *st);
  271. /** Make sure that the first samples to go out of the resamplers don't have
  272. * leading zeros. This is only useful before starting to use a newly created
  273. * resampler. It is recommended to use that when resampling an audio file, as
  274. * it will generate a file with the same length. For real-time processing,
  275. * it is probably easier not to use this call (so that the output duration
  276. * is the same for the first frame).
  277. * @param st Resampler state
  278. */
  279. int speex_resampler_skip_zeros(SpeexResamplerState *st);
  280. /** Reset a resampler so a new (unrelated) stream can be processed.
  281. * @param st Resampler state
  282. */
  283. int speex_resampler_reset_mem(SpeexResamplerState *st);
  284. /** Returns the English meaning for an error code
  285. * @param err Error code
  286. * @return English string
  287. */
  288. const char *speex_resampler_strerror(int err);
  289. #ifdef __cplusplus
  290. }
  291. #endif
  292. #endif