speex_resampler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #define speex_assert(cond)
  69. #else /* OUTSIDE_SPEEX */
  70. #include "speexdsp_types.h"
  71. #endif /* OUTSIDE_SPEEX */
  72. #ifdef __cplusplus
  73. extern "C" {
  74. #endif
  75. #define SPEEX_RESAMPLER_QUALITY_MAX 10
  76. #define SPEEX_RESAMPLER_QUALITY_MIN 0
  77. #define SPEEX_RESAMPLER_QUALITY_DEFAULT 4
  78. #define SPEEX_RESAMPLER_QUALITY_VOIP 3
  79. #define SPEEX_RESAMPLER_QUALITY_DESKTOP 5
  80. enum {
  81. RESAMPLER_ERR_SUCCESS = 0,
  82. RESAMPLER_ERR_ALLOC_FAILED = 1,
  83. RESAMPLER_ERR_BAD_STATE = 2,
  84. RESAMPLER_ERR_INVALID_ARG = 3,
  85. RESAMPLER_ERR_PTR_OVERLAP = 4,
  86. RESAMPLER_ERR_OVERFLOW = 5,
  87. RESAMPLER_ERR_MAX_ERROR
  88. };
  89. struct SpeexResamplerState_;
  90. typedef struct SpeexResamplerState_ SpeexResamplerState;
  91. /** Create a new resampler with integer input and output rates.
  92. * @param nb_channels Number of channels to be processed
  93. * @param in_rate Input sampling rate (integer number of Hz).
  94. * @param out_rate Output sampling rate (integer number of Hz).
  95. * @param quality Resampling quality between 0 and 10, where 0 has poor quality
  96. * and 10 has very high quality.
  97. * @return Newly created resampler state
  98. * @retval NULL Error: not enough memory
  99. */
  100. SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels,
  101. spx_uint32_t in_rate,
  102. spx_uint32_t out_rate,
  103. int quality,
  104. int *err);
  105. /** Create a new resampler with fractional input/output rates. The sampling
  106. * rate ratio is an arbitrary rational number with both the numerator and
  107. * denominator being 32-bit integers.
  108. * @param nb_channels Number of channels to be processed
  109. * @param ratio_num Numerator of the sampling rate ratio
  110. * @param ratio_den Denominator of the sampling rate ratio
  111. * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
  112. * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
  113. * @param quality Resampling quality between 0 and 10, where 0 has poor quality
  114. * and 10 has very high quality.
  115. * @return Newly created resampler state
  116. * @retval NULL Error: not enough memory
  117. */
  118. SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels,
  119. spx_uint32_t ratio_num,
  120. spx_uint32_t ratio_den,
  121. spx_uint32_t in_rate,
  122. spx_uint32_t out_rate,
  123. int quality,
  124. int *err);
  125. /** Destroy a resampler state.
  126. * @param st Resampler state
  127. */
  128. void speex_resampler_destroy(SpeexResamplerState *st);
  129. /** Resample a float array. The input and output buffers must *not* overlap.
  130. * @param st Resampler state
  131. * @param channel_index Index of the channel to process for the multi-channel
  132. * base (0 otherwise)
  133. * @param in Input buffer
  134. * @param in_len Number of input samples in the input buffer. Returns the
  135. * number of samples processed
  136. * @param out Output buffer
  137. * @param out_len Size of the output buffer. Returns the number of samples written
  138. */
  139. int speex_resampler_process_float(SpeexResamplerState *st,
  140. spx_uint32_t channel_index,
  141. const float *in,
  142. spx_uint32_t *in_len,
  143. float *out,
  144. spx_uint32_t *out_len);
  145. /** Resample an int array. The input and output buffers must *not* overlap.
  146. * @param st Resampler state
  147. * @param channel_index Index of the channel to process for the multi-channel
  148. * base (0 otherwise)
  149. * @param in Input buffer
  150. * @param in_len Number of input samples in the input buffer. Returns the number
  151. * of samples processed
  152. * @param out Output buffer
  153. * @param out_len Size of the output buffer. Returns the number of samples written
  154. */
  155. int speex_resampler_process_int(SpeexResamplerState *st,
  156. spx_uint32_t channel_index,
  157. const spx_int16_t *in,
  158. spx_uint32_t *in_len,
  159. spx_int16_t *out,
  160. spx_uint32_t *out_len);
  161. /** Resample an interleaved float array. The input and output buffers must *not* overlap.
  162. * @param st Resampler state
  163. * @param in Input buffer
  164. * @param in_len Number of input samples in the input buffer. Returns the number
  165. * of samples processed. This is all per-channel.
  166. * @param out Output buffer
  167. * @param out_len Size of the output buffer. Returns the number of samples written.
  168. * This is all per-channel.
  169. */
  170. int speex_resampler_process_interleaved_float(SpeexResamplerState *st,
  171. const float *in,
  172. spx_uint32_t *in_len,
  173. float *out,
  174. spx_uint32_t *out_len);
  175. /** Resample an interleaved int array. The input and output buffers must *not* overlap.
  176. * @param st Resampler state
  177. * @param in Input buffer
  178. * @param in_len Number of input samples in the input buffer. Returns the number
  179. * of samples processed. This is all per-channel.
  180. * @param out Output buffer
  181. * @param out_len Size of the output buffer. Returns the number of samples written.
  182. * This is all per-channel.
  183. */
  184. int speex_resampler_process_interleaved_int(SpeexResamplerState *st,
  185. const spx_int16_t *in,
  186. spx_uint32_t *in_len,
  187. spx_int16_t *out,
  188. spx_uint32_t *out_len);
  189. /** Set (change) the input/output sampling rates (integer value).
  190. * @param st Resampler state
  191. * @param in_rate Input sampling rate (integer number of Hz).
  192. * @param out_rate Output sampling rate (integer number of Hz).
  193. */
  194. int speex_resampler_set_rate(SpeexResamplerState *st,
  195. spx_uint32_t in_rate,
  196. spx_uint32_t out_rate);
  197. /** Get the current input/output sampling rates (integer value).
  198. * @param st Resampler state
  199. * @param in_rate Input sampling rate (integer number of Hz) copied.
  200. * @param out_rate Output sampling rate (integer number of Hz) copied.
  201. */
  202. void speex_resampler_get_rate(SpeexResamplerState *st,
  203. spx_uint32_t *in_rate,
  204. spx_uint32_t *out_rate);
  205. /** Set (change) the input/output sampling rates and resampling ratio
  206. * (fractional values in Hz supported).
  207. * @param st Resampler state
  208. * @param ratio_num Numerator of the sampling rate ratio
  209. * @param ratio_den Denominator of the sampling rate ratio
  210. * @param in_rate Input sampling rate rounded to the nearest integer (in Hz).
  211. * @param out_rate Output sampling rate rounded to the nearest integer (in Hz).
  212. */
  213. int speex_resampler_set_rate_frac(SpeexResamplerState *st,
  214. spx_uint32_t ratio_num,
  215. spx_uint32_t ratio_den,
  216. spx_uint32_t in_rate,
  217. spx_uint32_t out_rate);
  218. /** Get the current resampling ratio. This will be reduced to the least
  219. * common denominator.
  220. * @param st Resampler state
  221. * @param ratio_num Numerator of the sampling rate ratio copied
  222. * @param ratio_den Denominator of the sampling rate ratio copied
  223. */
  224. void speex_resampler_get_ratio(SpeexResamplerState *st,
  225. spx_uint32_t *ratio_num,
  226. spx_uint32_t *ratio_den);
  227. /** Set (change) the conversion quality.
  228. * @param st Resampler state
  229. * @param quality Resampling quality between 0 and 10, where 0 has poor
  230. * quality and 10 has very high quality.
  231. */
  232. int speex_resampler_set_quality(SpeexResamplerState *st,
  233. int quality);
  234. /** Get the conversion quality.
  235. * @param st Resampler state
  236. * @param quality Resampling quality between 0 and 10, where 0 has poor
  237. * quality and 10 has very high quality.
  238. */
  239. void speex_resampler_get_quality(SpeexResamplerState *st,
  240. int *quality);
  241. /** Set (change) the input stride.
  242. * @param st Resampler state
  243. * @param stride Input stride
  244. */
  245. void speex_resampler_set_input_stride(SpeexResamplerState *st,
  246. spx_uint32_t stride);
  247. /** Get the input stride.
  248. * @param st Resampler state
  249. * @param stride Input stride copied
  250. */
  251. void speex_resampler_get_input_stride(SpeexResamplerState *st,
  252. spx_uint32_t *stride);
  253. /** Set (change) the output stride.
  254. * @param st Resampler state
  255. * @param stride Output stride
  256. */
  257. void speex_resampler_set_output_stride(SpeexResamplerState *st,
  258. spx_uint32_t stride);
  259. /** Get the output stride.
  260. * @param st Resampler state copied
  261. * @param stride Output stride
  262. */
  263. void speex_resampler_get_output_stride(SpeexResamplerState *st,
  264. spx_uint32_t *stride);
  265. /** Get the latency introduced by the resampler measured in input samples.
  266. * @param st Resampler state
  267. */
  268. int speex_resampler_get_input_latency(SpeexResamplerState *st);
  269. /** Get the latency introduced by the resampler measured in output samples.
  270. * @param st Resampler state
  271. */
  272. int speex_resampler_get_output_latency(SpeexResamplerState *st);
  273. /** Make sure that the first samples to go out of the resamplers don't have
  274. * leading zeros. This is only useful before starting to use a newly created
  275. * resampler. It is recommended to use that when resampling an audio file, as
  276. * it will generate a file with the same length. For real-time processing,
  277. * it is probably easier not to use this call (so that the output duration
  278. * is the same for the first frame).
  279. * @param st Resampler state
  280. */
  281. int speex_resampler_skip_zeros(SpeexResamplerState *st);
  282. /** Reset a resampler so a new (unrelated) stream can be processed.
  283. * @param st Resampler state
  284. */
  285. int speex_resampler_reset_mem(SpeexResamplerState *st);
  286. /** Returns the English meaning for an error code
  287. * @param err Error code
  288. * @return English string
  289. */
  290. const char *speex_resampler_strerror(int err);
  291. #ifdef __cplusplus
  292. }
  293. #endif
  294. #endif