fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

Théo Monnomsoxr-sys (#438)2e4d385

livekit/livekit-ffi@0.13.0
5.2 KiB178 linesraw
1/* SoX Resampler Library      Copyright (c) 2007-16 robs@users.sourceforge.net
2 * Licence for this file: LGPL v2.1                  See LICENCE for details. */
3
4#if !defined soxr_cr_included
5#define soxr_cr_included
6
7#define  FIFO_SIZE_T int
8#include "fifo.h"
9
10typedef void real; /* float or double */
11struct stage;
12typedef void (* stage_fn_t)(struct stage * input, fifo_t * output);
13typedef struct half_fir_info {
14  int num_coefs;
15  real const * coefs;
16  stage_fn_t fn, dfn;
17  float att;
18} half_fir_info_t;
19typedef struct {float scalar; stage_fn_t fn;} poly_fir1_t;
20typedef struct {float beta; poly_fir1_t interp[3];} poly_fir_t;
21
22#define U100_l 42
23#define MULT32 (65536. * 65536.)
24
25/* Conceptually: coef_p is &coefs[num_phases][fir_len][interp_order+1]: */
26#define coef(coef_p, interp_order, fir_len, phase_num, coef_interp_num, fir_coef_num) (coef_p)[\
27  (fir_len) * ((interp_order) + 1) * (phase_num) + \
28  ((interp_order) + 1) * (fir_coef_num) + \
29  ((interp_order) - (coef_interp_num))]
30
31/* Conceptually: coef_p is &coefs[num_phases][fir_len/4][interp_order+1][4]: */
32#define coef4(coef_p, interp_order, fir_len, phase_num, coef_interp_num, fir_coef_num) (coef_p)[\
33  (fir_len) * ((interp_order) + 1) * (phase_num) + \
34  ((interp_order) + 1) * ((fir_coef_num) & ~3) + \
35  4 * ((interp_order) - (coef_interp_num)) + \
36  ((fir_coef_num) & 3)]
37
38typedef union { /* Int64 in parts */
39  #if HAVE_BIGENDIAN
40  struct {int32_t ms; uint32_t ls;} parts;
41  #else
42  struct {uint32_t ls; int32_t ms;} parts;
43  #endif
44  int64_t all;
45} int64p_t;
46
47typedef union { /* Uint64 in parts */
48  #if HAVE_BIGENDIAN
49  struct {uint32_t ms, ls;} parts;
50  #else
51  struct {uint32_t ls, ms;} parts;
52  #endif
53  uint64_t all;
54} uint64p_t;
55
56typedef struct {
57  int        dft_length, num_taps, post_peak;
58  void       * dft_forward_setup, * dft_backward_setup;
59  real   * coefs;
60} dft_filter_t;
61
62typedef struct { /* So generated filter coefs may be shared between channels */
63  real   * poly_fir_coefs;
64  dft_filter_t dft_filter[2];
65} rate_shared_t;
66
67typedef double float_step_t; /* Or long double or __float128. */
68
69typedef union { /* Fixed point arithmetic */
70  struct {uint64p_t ls; int64p_t ms;} fix;  /* Hi-prec has ~96 bits. */
71  float_step_t flt;
72} step_t;
73
74#define integer  fix.ms.parts.ms
75#define fraction fix.ms.parts.ls
76#define whole    fix.ms.all
77
78#define CORE_DBL       1
79#define CORE_SIMD_POLY 2
80#define CORE_SIMD_HALF 4
81#define CORE_SIMD_DFT  8
82#define LOG2_SIZEOF_REAL(core_flags) (2 + ((core_flags) & 1))
83
84typedef int core_flags_t;
85
86#if defined SOXR_LIB
87#include "rdft_t.h"
88#else
89typedef void fn_t;
90#endif
91
92typedef struct stage {
93  int        num;
94
95  /* Common to all stage types: */
96  core_flags_t   core_flags;
97  stage_fn_t fn;
98  fifo_t     fifo;
99  int        pre;       /* Number of past samples to store */
100  int        pre_post;  /* pre + number of future samples to store */
101  int        preload;   /* Number of zero samples to pre-load the fifo */
102  double     out_in_ratio; /* For buffer management. */
103  int        input_size;
104  bool       is_input;
105
106  /* For a stage with variable (run-time generated) filter coefs: */
107  fn_t const * rdft_cb;
108  rate_shared_t * shared;
109  unsigned   dft_filter_num; /* Which, if any, of the 2 DFT filters to use */
110  real       * dft_scratch;
111  float      * dft_out;
112  real const * coefs;
113
114  /* For a stage with variable L/M: */
115  step_t     at, step;
116  bool       use_hi_prec_clock;
117  int        L, remM;
118  int        n, phase_bits, block_len;
119  double     mult, phase0;
120} stage_t;
121
122#define stage_occupancy(s) max(0, fifo_occupancy(&(s)->fifo) - (s)->pre_post)
123#define stage_read_p(s) ((sample_t *)fifo_read_ptr(&(s)->fifo) + (s)->pre)
124
125#define lq_bw0  (1385/2048.) /* ~.67625, FP exact. */
126
127typedef enum {rolloff_small, rolloff_medium, rolloff_none} rolloff_t;
128
129typedef struct {
130  void * (* alloc)(size_t);
131  void * (* calloc)(size_t, size_t);
132  void (* free)(void *);
133} alloc_t;
134
135typedef struct {
136  alloc_t mem;
137  half_fir_info_t  const * half_firs;
138  size_t half_firs_len;
139  half_fir_info_t  const * doub_firs;
140  size_t doub_firs_len;
141  stage_fn_t cubic_stage_fn;
142  poly_fir_t const * poly_firs;
143  fn_t * rdft_cb;
144} cr_core_t;
145
146typedef struct rate rate_t;
147struct rate {
148  cr_core_t const * core;
149  double     io_ratio;
150  int64_t    samples_in, samples_out;
151  int        num_stages, flushing;
152  stage_t    * stages;
153};
154
155#if defined SOXR_LIB
156
157#include "soxr.h"
158
159char const * _soxr_init(
160  rate_t * const p,                /* Per audio channel.                            */
161  rate_shared_t * const shared,    /* Between channels (undergoing same rate change)*/
162  double const io_ratio,           /* Input rate divided by output rate.            */
163  soxr_quality_spec_t const * const q_spec,
164  soxr_runtime_spec_t const * const r_spec,
165  double multiplier,               /* Linear gain to apply during conversion.   1   */
166  cr_core_t const * const core,
167  core_flags_t const);
168
169void _soxr_process(struct rate * p, size_t olen);
170real * _soxr_input(struct rate * p, real const * samples, size_t n);
171real const * _soxr_output(struct rate * p, real * samples, size_t * n0);
172void _soxr_flush(struct rate * p);
173void _soxr_close(struct rate * p);
174double _soxr_delay(struct rate * p);
175void _soxr_sizes(size_t * shared, size_t * channel);
176#endif
177
178#endif