1
/* SoX Resampler Library Copyright (c) 2007-13 robs@users.sourceforge.net
2
* Licence for this file: LGPL v2.1 See LICENCE for details. */
4
/* Wrapper mostly compatible with `libsamplerate'. */
12
float *in,*out; long ilen,olen,idone,odone; int eoi; double oi_ratio;} io_t;
14
typedef struct soxr SRC_STATE;
15
#define src_callback_t soxr_input_fn_t
16
#define SRC_ERROR soxr_error_t
17
#define SRC_SRCTYPE unsigned
22
soxr_error_t src_simple(io_t * p, unsigned id, int channels)
26
soxr_quality_spec_t q_spec = soxr_quality_spec(SOXR_LSR0Q + id, 0);
27
char const * e = getenv("SOXR_LSR_NUM_THREADS");
28
soxr_runtime_spec_t r_spec = soxr_runtime_spec(!(e && atoi(e) != 1));
29
assert (channels > 0);
30
assert (p->ilen >= 0);
31
assert (p->olen >= 0);
32
error = soxr_oneshot(1, p->oi_ratio, (unsigned)channels,
33
p->in, (size_t)p->ilen, &idone, p->out, (size_t)p->olen, &odone,
35
p->idone = (long)idone, p->odone = (long)odone;
39
soxr_t src_callback_new(soxr_input_fn_t fn, unsigned id, int channels, SRC_ERROR * error0, void * p)
41
soxr_quality_spec_t q_spec = soxr_quality_spec(SOXR_LSR0Q + id, 0);
42
char const * e = getenv("SOXR_LSR_NUM_THREADS");
43
soxr_runtime_spec_t r_spec = soxr_runtime_spec(!(e && atoi(e) != 1));
46
assert (channels > 0);
47
/* To minimise latency e.g. for real-time playback:
49
r_spec.log2_large_dft_size = r_spec.log2_min_dft_size = 8;
51
soxr = soxr_create(0, 0, (unsigned)channels, &error, 0, &q_spec, &r_spec);
53
error = soxr_set_input_fn(soxr, fn, p, 0);
54
*(int *)error0 = (int)(ptrdiff_t)error;
58
soxr_error_t src_process(soxr_t p, io_t * io)
60
if (!p || !io) return "null pointer";
61
soxr_set_error(p, soxr_set_io_ratio(p, 1/io->oi_ratio, (size_t)io->olen));
63
{ size_t idone , odone;
64
soxr_process(p, io->in, (size_t)(io->eoi? ~io->ilen : io->ilen), /* hack */
65
&idone, io->out, (size_t)io->olen, &odone);
66
io->idone = (long)idone, io->odone = (long)odone;
67
return soxr_error(p); }
70
long src_callback_read(soxr_t p, double oi_ratio, long olen, float * obuf)
72
if (!p || olen < 0) return -1;
73
soxr_set_error(p, soxr_set_io_ratio(p, 1/oi_ratio, (size_t)olen));
74
return (long)soxr_output(p, obuf, (size_t)olen);
77
void src_float_to_short_array(float const * src, short * dest, int len)
79
double d, N = 1. + SHRT_MAX;
81
while (len--) d = src[len] * N, dest[len] = (short)(d > N - 1? (short)(N - 1) : d < -N? (short)-N : rint16(d));
84
void src_short_to_float_array(short const * src, float * dest, int len)
87
while (len--) dest[len] = (float)(src[len] * (1 / (1. + SHRT_MAX)));
90
void src_float_to_int_array(float const * src, int * dest, int len)
92
double d, N = 32768. * 65536.; /* N.B. int32, not int! (Also next fn.) */
94
while (len--) d = src[len] * N, dest[len] = d >= N - 1? (int)(N - 1) : d < -N? (int)(-N) : rint32(d);
97
void src_int_to_float_array(int const * src, float * dest, int len)
100
while (len--) dest[len] = (float)(src[len] * (1 / (32768. * 65536.)));
103
static char const * const names[] = {"LSR best sinc", "LSR medium sinc", "LSR fastest sinc", "LSR ZOH", "LSR linear", "SoX VHQ"};
104
char const * src_get_name(unsigned n) {return n < 5u + !getenv("SOXR_LSR_STRICT")? names[n] : 0;}
105
char const * src_get_description(unsigned id) {return src_get_name(id);}
106
char const * src_get_version(void) {return soxr_version();}
107
char const * src_strerror(soxr_error_t error) {return error == (soxr_error_t)1? "Placeholder." : sizeof(int) >= sizeof(char *) || !error ? soxr_strerror(error) : "soxr error";}
108
int src_is_valid_ratio(double oi_ratio) {return getenv("SOXR_LSR_STRICT")? oi_ratio >= 1./256 && oi_ratio <= 256 : oi_ratio > 0;}
109
soxr_error_t src_error(soxr_t p) {return soxr_error(p);}
110
soxr_error_t src_reset(soxr_t p) {return soxr_clear(p);}
111
soxr_t src_delete(soxr_t p) {soxr_delete(p); return 0;}
112
soxr_error_t src_set_ratio(soxr_t p, double oi_ratio) {return soxr_set_io_ratio(p, 1/oi_ratio, 0);}
113
soxr_t src_new(unsigned id, int channels, SRC_ERROR * error) {return src_callback_new(0, id, channels, error, 0);}