soxr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* SoX Resampler Library Copyright (c) 2007-13 robs@users.sourceforge.net
  2. *
  3. * This library is free software; you can redistribute it and/or modify it
  4. * under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation; either version 2.1 of the License, or (at
  6. * your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with this library; if not, write to the Free Software Foundation,
  15. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. /* -------------------------------- Gubbins --------------------------------- */
  18. #if !defined soxr_included
  19. #define soxr_included
  20. #if defined __cplusplus
  21. #include <cstddef>
  22. extern "C" {
  23. #else
  24. #include <stddef.h>
  25. #endif
  26. #if defined SOXR_DLL
  27. #if defined soxr_EXPORTS
  28. #define SOXR __declspec(dllexport)
  29. #else
  30. #define SOXR __declspec(dllimport)
  31. #endif
  32. #elif defined SOXR_VISIBILITY && defined __GNUC__ && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 1)
  33. #define SOXR __attribute__ ((visibility("default")))
  34. #else
  35. #define SOXR
  36. #endif
  37. typedef struct soxr_io_spec soxr_io_spec_t;
  38. typedef struct soxr_quality_spec soxr_quality_spec_t;
  39. typedef struct soxr_runtime_spec soxr_runtime_spec_t;
  40. /* ---------------------------- API conventions --------------------------------
  41. Buffer lengths (and occupancies) are expressed as the number of contained
  42. samples per channel.
  43. Parameter names for buffer lengths have the suffix `len'.
  44. A single-character `i' or 'o' is often used in names to give context as
  45. input or output (e.g. ilen, olen). */
  46. /* --------------------------- Version management --------------------------- */
  47. /* E.g. #if SOXR_THIS_VERSION >= SOXR_VERSION(0,1,1) ... */
  48. #define SOXR_VERSION(x,y,z) (((x)<<16)|((y)<<8)|(z))
  49. #define SOXR_THIS_VERSION SOXR_VERSION(0,1,2)
  50. #define SOXR_THIS_VERSION_STR "0.1.2"
  51. /* --------------------------- Type declarations ---------------------------- */
  52. typedef struct soxr * soxr_t; /* A resampler for 1 or more channels. */
  53. typedef char const * soxr_error_t; /* 0:no-error; non-0:error. */
  54. typedef void * soxr_buf_t; /* 1 buffer of channel-interleaved samples. */
  55. typedef void const * soxr_cbuf_t; /* Ditto; read-only. */
  56. typedef soxr_buf_t const * soxr_bufs_t;/* Or, a separate buffer for each ch. */
  57. typedef soxr_cbuf_t const * soxr_cbufs_t; /* Ditto; read-only. */
  58. typedef void const * soxr_in_t; /* Either a soxr_cbuf_t or soxr_cbufs_t,
  59. depending on itype in soxr_io_spec_t. */
  60. typedef void * soxr_out_t; /* Either a soxr_buf_t or soxr_bufs_t,
  61. depending on otype in soxr_io_spec_t. */
  62. /* --------------------------- API main functions --------------------------- */
  63. SOXR char const * soxr_version(void); /* Query library version: "libsoxr-x.y.z" */
  64. #define soxr_strerror(e) /* Soxr counterpart to strerror. */ \
  65. ((e)?(e):"no error")
  66. /* Create a stream resampler: */
  67. SOXR soxr_t soxr_create(
  68. double input_rate, /* Input sample-rate. */
  69. double output_rate, /* Output sample-rate. */
  70. unsigned num_channels, /* Number of channels to be used. */
  71. /* All following arguments are optional (may be set to NULL). */
  72. soxr_error_t *, /* To report any error during creation. */
  73. soxr_io_spec_t const *, /* To specify non-default I/O formats. */
  74. soxr_quality_spec_t const *, /* To specify non-default resampling quality.*/
  75. soxr_runtime_spec_t const *);/* To specify non-default runtime resources.
  76. Default io_spec is per soxr_io_spec(SOXR_FLOAT32_I, SOXR_FLOAT32_I)
  77. Default quality_spec is per soxr_quality_spec(SOXR_HQ, 0)
  78. Default runtime_spec is per soxr_runtime_spec(1) */
  79. /* If not using an app-supplied input function, after creating a stream
  80. * resampler, repeatedly call: */
  81. SOXR soxr_error_t soxr_process(
  82. soxr_t resampler, /* As returned by soxr_create. */
  83. /* Input (to be resampled): */
  84. soxr_in_t in, /* Input buffer(s); may be NULL (see below). */
  85. size_t ilen, /* Input buf. length (samples per channel). */
  86. size_t * idone, /* To return actual # samples used (<= ilen). */
  87. /* Output (resampled): */
  88. soxr_out_t out, /* Output buffer(s).*/
  89. size_t olen, /* Output buf. length (samples per channel). */
  90. size_t * odone); /* To return actual # samples out (<= olen).
  91. Note that no special meaning is associated with ilen or olen equal to
  92. zero. End-of-input (i.e. no data is available nor shall be available)
  93. may be indicated by seting `in' to NULL. */
  94. /* If using an app-supplied input function, it must look and behave like this:*/
  95. typedef size_t /* data_len */
  96. (* soxr_input_fn_t)( /* Supply data to be resampled. */
  97. void * input_fn_state, /* As given to soxr_set_input_fn (below). */
  98. soxr_in_t * data, /* Returned data; see below. N.B. ptr to ptr(s)*/
  99. size_t requested_len); /* Samples per channel, >= returned data_len.
  100. data_len *data Indicates Meaning
  101. ------- ------- ------------ -------------------------
  102. !=0 !=0 Success *data contains data to be
  103. input to the resampler.
  104. 0 !=0 (or End-of-input No data is available nor
  105. not set) shall be available.
  106. 0 0 Failure An error occurred whilst trying to
  107. source data to be input to the resampler. */
  108. /* and be registered with a previously created stream resampler using: */
  109. SOXR soxr_error_t soxr_set_input_fn(/* Set (or reset) an input function.*/
  110. soxr_t resampler, /* As returned by soxr_create. */
  111. soxr_input_fn_t, /* Function to supply data to be resampled.*/
  112. void * input_fn_state, /* If needed by the input function. */
  113. size_t max_ilen); /* Maximum value for input fn. requested_len.*/
  114. /* then repeatedly call: */
  115. SOXR size_t /*odone*/ soxr_output(/* Resample and output a block of data.*/
  116. soxr_t resampler, /* As returned by soxr_create. */
  117. soxr_out_t data, /* App-supplied buffer(s) for resampled data.*/
  118. size_t olen); /* Amount of data to output; >= odone. */
  119. /* Common stream resampler operations: */
  120. SOXR soxr_error_t soxr_error(soxr_t); /* Query error status. */
  121. SOXR size_t * soxr_num_clips(soxr_t); /* Query int. clip counter (for R/W). */
  122. SOXR double soxr_delay(soxr_t); /* Query current delay in output samples.*/
  123. SOXR char const * soxr_engine(soxr_t p); /* Query resampling engine name. */
  124. SOXR soxr_error_t soxr_clear(soxr_t); /* Ready for fresh signal, same config. */
  125. SOXR void soxr_delete(soxr_t); /* Free resources. */
  126. /* `Short-cut', single call to resample a (probably short) signal held entirely
  127. * in memory. See soxr_create and soxr_process above for parameter details.
  128. * Note that unlike soxr_create however, the default quality spec. for
  129. * soxr_oneshot is per soxr_quality_spec(SOXR_LQ, 0). */
  130. SOXR soxr_error_t soxr_oneshot(
  131. double input_rate,
  132. double output_rate,
  133. unsigned num_channels,
  134. soxr_in_t in , size_t ilen, size_t * idone,
  135. soxr_out_t out, size_t olen, size_t * odone,
  136. soxr_io_spec_t const *,
  137. soxr_quality_spec_t const *,
  138. soxr_runtime_spec_t const *);
  139. /* For variable-rate resampling. See example # 5 for how to create a
  140. * variable-rate resampler and how to use this function. */
  141. SOXR soxr_error_t soxr_set_io_ratio(soxr_t, double io_ratio, size_t slew_len);
  142. /* -------------------------- API type definitions -------------------------- */
  143. typedef enum { /* Datatypes supported for I/O to/from the resampler: */
  144. /* Internal; do not use: */
  145. SOXR_FLOAT32, SOXR_FLOAT64, SOXR_INT32, SOXR_INT16, SOXR_SPLIT = 4,
  146. /* Use for interleaved channels: */
  147. SOXR_FLOAT32_I = SOXR_FLOAT32, SOXR_FLOAT64_I, SOXR_INT32_I, SOXR_INT16_I,
  148. /* Use for split channels: */
  149. SOXR_FLOAT32_S = SOXR_SPLIT , SOXR_FLOAT64_S, SOXR_INT32_S, SOXR_INT16_S
  150. } soxr_datatype_t;
  151. #define soxr_datatype_size(x) /* Returns `sizeof' a soxr_datatype_t sample. */\
  152. ((unsigned char *)"\4\10\4\2")[(x)&3]
  153. struct soxr_io_spec { /* Typically */
  154. soxr_datatype_t itype; /* Input datatype. SOXR_FLOAT32_I */
  155. soxr_datatype_t otype; /* Output datatype. SOXR_FLOAT32_I */
  156. double scale; /* Linear gain to apply during resampling. 1 */
  157. void * e; /* Reserved for internal use 0 */
  158. unsigned long flags; /* Per the following #defines. 0 */
  159. };
  160. #define SOXR_TPDF 0 /* Applicable only if otype is INT16. */
  161. #define SOXR_NO_DITHER 8u /* Disable the above. */
  162. struct soxr_quality_spec { /* Typically */
  163. double precision; /* Conversion precision (in bits). 20 */
  164. double phase_response; /* 0=minimum, ... 50=linear, ... 100=maximum 50 */
  165. double passband_end; /* 0dB pt. bandwidth to preserve; nyquist=1 0.913*/
  166. double stopband_begin; /* Aliasing/imaging control; > passband_end 1 */
  167. void * e; /* Reserved for internal use. 0 */
  168. unsigned long flags; /* Per the following #defines. 0 */
  169. };
  170. #define SOXR_ROLLOFF_SMALL 0u /* <= 0.01 dB */
  171. #define SOXR_ROLLOFF_MEDIUM 1u /* <= 0.35 dB */
  172. #define SOXR_ROLLOFF_NONE 2u /* For Chebyshev bandwidth. */
  173. #define SOXR_MAINTAIN_3DB_PT 4u /* Reserved for internal use. */
  174. #define SOXR_HI_PREC_CLOCK 8u /* Increase `irrational' ratio accuracy. */
  175. #define SOXR_DOUBLE_PRECISION 16u /* Use D.P. calcs even if precision <= 20. */
  176. #define SOXR_VR 32u /* Variable-rate resampling. */
  177. struct soxr_runtime_spec { /* Typically */
  178. unsigned log2_min_dft_size;/* For DFT efficiency. [8,15] 10 */
  179. unsigned log2_large_dft_size;/* For DFT efficiency. [16,20] 17 */
  180. unsigned coef_size_kbytes; /* For SOXR_COEF_INTERP_AUTO (below). 400 */
  181. unsigned num_threads; /* If built so. 0 means `automatic'. 1 */
  182. void * e; /* Reserved for internal use. 0 */
  183. unsigned long flags; /* Per the following #defines. 0 */
  184. };
  185. /* For `irrational' ratios only: */
  186. #define SOXR_COEF_INTERP_AUTO 0u /* Auto select coef. interpolation. */
  187. #define SOXR_COEF_INTERP_LOW 2u /* Man. select: less CPU, more memory. */
  188. #define SOXR_COEF_INTERP_HIGH 3u /* Man. select: more CPU, less memory. */
  189. #define SOXR_STRICT_BUFFERING 4u /* Reserved for future use. */
  190. #define SOXR_NOSMALLINTOPT 8u /* For test purposes only. */
  191. /* -------------------------- API type constructors ------------------------- */
  192. /* These functions allow setting of the most commonly-used structure
  193. * parameters, with other parameters being given default values. The default
  194. * values may then be overridden, directly in the structure, if needed. */
  195. SOXR soxr_quality_spec_t soxr_quality_spec(
  196. unsigned long recipe, /* Per the #defines immediately below. */
  197. unsigned long flags); /* As soxr_quality_spec_t.flags. */
  198. /* The 5 standard qualities found in SoX: */
  199. #define SOXR_QQ 0 /* 'Quick' cubic interpolation. */
  200. #define SOXR_LQ 1 /* 'Low' 16-bit with larger rolloff. */
  201. #define SOXR_MQ 2 /* 'Medium' 16-bit with medium rolloff. */
  202. #define SOXR_HQ SOXR_20_BITQ /* 'High quality'. */
  203. #define SOXR_VHQ SOXR_28_BITQ /* 'Very high quality'. */
  204. #define SOXR_16_BITQ 3
  205. #define SOXR_20_BITQ 4
  206. #define SOXR_24_BITQ 5
  207. #define SOXR_28_BITQ 6
  208. #define SOXR_32_BITQ 7
  209. /* Libsamplerate equivalent qualities: */
  210. #define SOXR_LSR0Q 8 /* 'Best sinc'. */
  211. #define SOXR_LSR1Q 9 /* 'Medium sinc'. */
  212. #define SOXR_LSR2Q 10 /* 'Fast sinc'. */
  213. #define SOXR_LINEAR_PHASE 0x00
  214. #define SOXR_INTERMEDIATE_PHASE 0x10
  215. #define SOXR_MINIMUM_PHASE 0x30
  216. #define SOXR_STEEP_FILTER 0x40
  217. #define SOXR_ALLOW_ALIASING 0x80 /* Reserved for future use. */
  218. SOXR soxr_runtime_spec_t soxr_runtime_spec(
  219. unsigned num_threads);
  220. SOXR soxr_io_spec_t soxr_io_spec(
  221. soxr_datatype_t itype,
  222. soxr_datatype_t otype);
  223. /* --------------------------- Advanced use only ---------------------------- */
  224. /* For new designs, the following functions/usage will probably not be needed.
  225. * They might be useful when adding soxr into an existing design where values
  226. * for the resampling-rate and/or number-of-channels parameters to soxr_create
  227. * are not available when that function will be called. In such cases, the
  228. * relevant soxr_create parameter(s) can be given as 0, then one or both of the
  229. * following (as appropriate) later invoked (but prior to calling soxr_process
  230. * or soxr_output):
  231. *
  232. * soxr_set_error(soxr, soxr_set_io_ratio(soxr, io_ratio, 0));
  233. * soxr_set_error(soxr, soxr_set_num_channels(soxr, num_channels));
  234. */
  235. SOXR soxr_error_t soxr_set_error(soxr_t, soxr_error_t);
  236. SOXR soxr_error_t soxr_set_num_channels(soxr_t, unsigned);
  237. #undef SOXR
  238. #if defined __cplusplus
  239. }
  240. #endif
  241. #endif