fork/rust-sdks

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

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

livekit/livekit-ffi@0.13.0
4.3 KiB198 linesraw
1/* SoX Resampler Library      Copyright (c) 2007-18 robs@users.sourceforge.net
2 * Licence for this file: LGPL v2.1                  See LICENCE for details. */
3
4/* Wrapper mostly compatible with `libsamplerate'. */
5
6#include <assert.h>
7#include <stdlib.h>
8#include "soxr.h"
9#include "soxr-lsr.h"
10#include "rint.h"
11
12
13
14SRC_STATE *src_new(SRC_SRCTYPE id, int channels, SRC_ERROR * error)
15{
16  return src_callback_new(0, id, channels, error, 0);
17}
18
19
20
21SRC_ERROR src_process(SRC_STATE *p, SRC_DATA * io)
22{
23  size_t idone , odone;
24
25  if (!p || !io) return -1;
26
27  soxr_set_error(
28      p, soxr_set_io_ratio(p, 1/io->src_ratio, (size_t)io->output_frames));
29
30  soxr_process(p, io->data_in,                                  /* hack: */
31      (size_t)(io->end_of_input? ~io->input_frames : io->input_frames),
32      &idone, io->data_out, (size_t)io->output_frames, &odone);
33
34  io->input_frames_used = (long)idone, io->output_frames_gen = (long)odone;
35  return -!!soxr_error(p);
36}
37
38
39
40SRC_ERROR src_set_ratio(SRC_STATE * p, double oi_ratio)
41{
42  return -!!soxr_set_io_ratio(p, 1/oi_ratio, 0);
43}
44
45
46
47SRC_ERROR src_reset(SRC_STATE * p)
48{
49  return -!!soxr_clear(p);
50}
51
52
53
54SRC_ERROR src_error(SRC_STATE * p)
55{
56  return -!!soxr_error(p);
57}
58
59
60
61SRC_STATE * src_delete(SRC_STATE * p)
62{
63  soxr_delete(p);
64  return 0;
65}
66
67
68
69SRC_STATE *src_callback_new(src_callback_t fn,
70    SRC_SRCTYPE id, int channels, SRC_ERROR * error0, void * p)
71{
72  soxr_quality_spec_t q_spec = soxr_quality_spec(SOXR_LSR0Q + (unsigned)id, 0);
73  char const * e = getenv("SOXR_LSR_NUM_THREADS");
74  soxr_runtime_spec_t r_spec = soxr_runtime_spec(!(e && atoi(e) != 1));
75  soxr_error_t error;
76  soxr_t soxr = 0;
77
78  assert (channels > 0);
79  soxr = soxr_create(0, 0, (unsigned)channels, &error, 0, &q_spec, &r_spec);
80
81  if (soxr)
82    error = soxr_set_input_fn(soxr, (soxr_input_fn_t)fn, p, 0);
83
84  if (error0)
85    *error0 = -!!error;
86
87  return soxr;
88}
89
90
91
92long src_callback_read(SRC_STATE *p, double oi_ratio, long olen, float * obuf)
93{
94  if (!p || olen < 0) return -1;
95
96  soxr_set_error(p, soxr_set_io_ratio(p, 1/oi_ratio, (size_t)olen));
97  return (long)soxr_output(p, obuf, (size_t)olen);
98}
99
100
101
102SRC_ERROR src_simple(SRC_DATA * io, SRC_SRCTYPE id, int channels)
103{
104  size_t idone, odone;
105  soxr_error_t error;
106  soxr_quality_spec_t q_spec = soxr_quality_spec(SOXR_LSR0Q + (unsigned)id, 0);
107  char const * e = getenv("SOXR_LSR_NUM_THREADS");
108  soxr_runtime_spec_t r_spec = soxr_runtime_spec(!(e && atoi(e) != 1));
109
110  if (!io || channels<=0 || io->input_frames<0 || io->output_frames<0) return-1;
111
112  error = soxr_oneshot(1, io->src_ratio, (unsigned)channels, io->data_in,
113      (size_t)io->input_frames, &idone, io->data_out, (size_t)io->output_frames,
114      &odone, 0, &q_spec, &r_spec);
115
116  io->input_frames_used = (long)idone, io->output_frames_gen = (long)odone;
117
118  return -!!error;
119}
120
121
122
123char const * src_get_name(SRC_SRCTYPE id)
124{
125  static char const * const names[] = {
126    "LSR best sinc", "LSR medium sinc", "LSR fastest sinc",
127    "LSR ZOH", "LSR linear", "SoX VHQ"};
128
129  return (unsigned)id < 5u + !getenv("SOXR_LSR_STRICT")? names[id] : 0;
130}
131
132
133
134char const * src_get_description(SRC_SRCTYPE id)
135{
136  return src_get_name(id);
137}
138
139
140
141char const * src_get_version(void)
142{
143  return soxr_version();
144}
145
146
147
148char const * src_strerror(SRC_ERROR error)
149{
150  return error == 1? "Placeholder." : error ? "soxr error" : soxr_strerror(0);
151}
152
153
154
155int src_is_valid_ratio(double oi_ratio)
156{
157  return getenv("SOXR_LSR_STRICT")?
158    oi_ratio >= 1./256 && oi_ratio <= 256 : oi_ratio > 0;
159}
160
161
162
163void src_short_to_float_array(short const * src, float * dest, int len)
164{
165  assert (src && dest);
166
167  while (len--) dest[len] = (float)(src[len] * (1 / (1. + SHRT_MAX)));
168}
169
170
171
172void src_float_to_short_array(float const * src, short * dest, int len)
173{
174  double d, N = 1. + SHRT_MAX;
175  assert (src && dest);
176
177  while (len--) d = src[len] * N, dest[len] =
178    (short)(d > N - 1? (short)(N - 1) : d < -N? (short)-N : rint16(d));
179}
180
181
182
183void src_int_to_float_array(int const * src, float * dest, int len)
184{
185  assert (src && dest);
186  while (len--) dest[len] = (float)(src[len] * (1 / (32768. * 65536.)));
187}
188
189
190
191void src_float_to_int_array(float const * src, int * dest, int len)
192{
193  double d, N = 32768. * 65536.; /* N.B. int32, not int! (Also above fn.) */
194  assert (src && dest);
195
196  while (len--) d = src[len] * N, dest[len] =
197    d >= N - 1? (int)(N - 1) : d < -N? (int)(-N) : rint32(d);
198}