1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-10 16:43:33 +02:00

Convert default resampler from libresample to libsoxr.

This commit is contained in:
v.audacity
2012-10-09 02:57:19 +00:00
parent de3bf9610a
commit 79faef4192
104 changed files with 13914 additions and 8 deletions

View File

@@ -0,0 +1,40 @@
/* SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
* Licence for this file: LGPL v2.1 See LICENCE for details. */
/* Example 1: `One-shot' resample a single block of data in memory.
*
* N.B. See example 2 for how to resample a stream (of blocks).
*
* Optional arguments are: INPUT-RATE OUTPUT-RATE
*/
#include "util.h"
#include <soxr.h>
const float in[] = { /* Input: 12 cycles of a sine wave with freq. = irate/4 */
0,1,0,-1, 0,1,0,-1, 0,1,0,-1, 0,1,0,-1, 0,1,0,-1, 0,1,0,-1,
0,1,0,-1, 0,1,0,-1, 0,1,0,-1, 0,1,0,-1, 0,1,0,-1, 0,1,0,-1};
int main(int argc, char const * arg[])
{
double irate = argc > 1? atof(arg[1]) : 1; /* Default to upsampling */
double orate = argc > 2? atof(arg[2]) : 2; /* by a factor of 2. */
size_t olen = (size_t)(AL(in) * orate / irate + .5); /* Assay output len. */
float * out = malloc(sizeof(*out) * olen); /* Allocate output buffer. */
size_t odone;
soxr_error_t error = soxr_oneshot(irate, orate, 1, /* Rates and # of chans. */
in, AL(in), NULL, /* Input. */
out, olen, &odone, /* Output. */
NULL, NULL, NULL); /* Default configuration.*/
unsigned i = 0; /* Print out the resampled data... */
while (i++ < odone)
printf("%5.2f%c", out[i-1], " \n"[!(i&7) || i == odone]);
puts(soxr_strerror(error)); /* ...and the reported result. */
free(out); /* Tidy up. */
return !!error;
(void)argc, (void)arg; /* Not used in this example. */
}

View File

@@ -0,0 +1,58 @@
/* SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
* Licence for this file: LGPL v2.1 See LICENCE for details. */
/* Example 2a: resample a raw, single-channel, floating-point data stream from
* stdin to stdout. The application uses the single function `soxr_process'
* for input and output to/from the resampler.
*
* Arguments are: INPUT-RATE OUTPUT-RATE
*/
#include "util.h"
#include <soxr.h>
int main(int argc, char const * arg[])
{
double irate = argc > 1? atof(arg[1]) : 44100.;
double orate = argc > 2? atof(arg[2]) : 96000.;
soxr_error_t error;
soxr_t resampler = soxr_create(irate, orate, 1, &error, 0, 0, 0);
STD_STDIO;
if (!error) {
#define buf_total_len 15000
/* Allocate resampling input and output buffers in proportion to the input
* and output rates: */
size_t ibuflen = (size_t)(irate * buf_total_len / (irate + orate) + .5);
size_t obuflen = buf_total_len - ibuflen;
char * ibuf = malloc(sizeof(float) * ibuflen), * iptr = 0;
void * obuf = malloc(sizeof(float) * obuflen);
size_t iavailable = 0;
size_t idone, odone, written;
do { /* Resample in blocks: */
if (!iavailable && ibuf) /* If ibuf empty, try to fill it: */
if (!(iavailable = fread(iptr = ibuf, sizeof(float), ibuflen, stdin)))
free(ibuf), ibuf = 0; /* If none available, don't retry. */
error = soxr_process(resampler,
iptr, iavailable, &idone, obuf, obuflen, &odone);
iptr += idone * sizeof(float), iavailable -= idone; /* Consumed input. */
written = fwrite(obuf, sizeof(float), odone, stdout); /* Consume output.*/
} while (!error && (ibuf || written));
/* Tidy up: */
free(obuf), free(ibuf);
soxr_delete(resampler);
}
/* Diagnostics: */
fprintf(stderr, "resampler: %s; I/O: %s\n",
soxr_strerror(error), errno? strerror(errno) : "no error");
return error || errno;
}

View File

@@ -0,0 +1,66 @@
/* SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
* Licence for this file: LGPL v2.1 See LICENCE for details. */
/* Example 2b: resample a raw, single-channel, floating-point data stream from
* stdin to stdout. The application provides an input function, called on
* demand by libsoxr, in response to calls to soxr_output().
*
* Arguments are: INPUT-RATE OUTPUT-RATE
*/
#include "util.h"
#include <soxr.h>
static size_t input_fn(
void * p, /* Generic pointer to any state variables neded here. */
soxr_cbuf_t * buf, /* To tell the resampler where the data is. */
size_t requested_len) /* In samples per channel. */
{
static float * ibuf; /* Static context; this could be changed using `p'. */
size_t actual;
/* Allocate the input buffer memory; check for errors: */
*buf = ibuf = realloc(ibuf, sizeof(float) * requested_len);
if (!ibuf)
return 0; /* Indicate failure (*buf is also 0). */
/* Read samples from the input stream; check for errors: */
actual = fread(ibuf, sizeof(float), requested_len, stdin);
if (!actual && ferror(stdin))
*buf = 0; /* Indicate failure (actual is also 0). */
return actual;
(void)p; /* Not used in this example. */
}
int main(int argc, char const * arg[])
{
double irate = argc > 1? atof(arg[1]) : 44100.;
double orate = argc > 2? atof(arg[2]) : 96000.;
soxr_error_t error;
soxr_t resampler = soxr_create(irate, orate, 1, &error, 0, 0, 0);
if (!error) /* Register input_fn with the resampler: */
error = soxr_set_input_fn(resampler, input_fn, 0);
STD_STDIO;
if (!error) { /* If all is good, run the resampler: */
#define olen 1000
float resampled[olen];
size_t actual;
/* Resample in blocks: */
do actual = soxr_output(resampler, resampled, olen);
while (fwrite(resampled, sizeof(float), actual, stdout));
error = soxr_error(resampler); /* Note: before deleting the resampler! */
soxr_delete(resampler);
}
/* Diagnostics: */
fprintf(stderr, "resampler: %s; I/O: %s\n",
soxr_strerror(error), errno? strerror(errno) : "no error");
return error || errno;
}

View File

@@ -0,0 +1,79 @@
/* SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
* Licence for this file: LGPL v2.1 See LICENCE for details. */
/* Example 3a: extends example 2a with multiple channels, multiple datatypes,
* and other options.
*
* The seven arguments are:
* INPUT-RATE As example 2a
* OUTPUT-RATE Ditto
* NUM-CHANNELS Number of interleaved channels
* IN-DATATYPE# 0:float32 1:float64 2:int32 3:int16
* OUT-DATATYPE# Ditto
* Q# Quality-recipe | (quality-flags << 16) (in hex) See soxr.h
* USE-THREADS 1 to use multi-threading
*/
#include "util.h"
#include <soxr.h>
int main(int n, char const * arg[])
{
char const * arg0 = n? --n, *arg++ : "";
double irate = n? --n, atof(*arg++) : 96000.;
double orate = n? --n, atof(*arg++) : 44100.;
unsigned chans = n? --n, (unsigned)atoi(*arg++) : 1;
soxr_datatype_t itype = n? --n, (soxr_datatype_t)atoi(*arg++) :SOXR_FLOAT32_I;
soxr_datatype_t otype = n? --n, (soxr_datatype_t)atoi(*arg++) :SOXR_FLOAT32_I;
unsigned long q_recipe= n? --n, strtoul(*arg++, 0, 16) : SOXR_HQ;
int use_threads = n? --n, atoi(*arg++) : 1;
size_t isize = soxr_datatype_size(itype) * chans;
size_t osize = soxr_datatype_size(otype) * chans;
size_t clips = 0;
soxr_error_t error;
soxr_quality_spec_t q_spec = soxr_quality_spec(q_recipe &65535, q_recipe>>16);
soxr_io_spec_t io_spec = soxr_io_spec(itype, otype);
soxr_runtime_spec_t runtime_spec = soxr_runtime_spec(!use_threads);
soxr_t resampler = soxr_create(
irate, orate, chans, &error, &io_spec, &q_spec, &runtime_spec);
STD_STDIO;
if (!error) {
#define buf_total_len 15000
/* Allocate resampling input and output buffers in proportion to the input
* and output rates: */
size_t ibuflen = (size_t)(irate * buf_total_len / (irate + orate) + .5);
size_t obuflen = buf_total_len - ibuflen;
char * ibuf = malloc(isize * ibuflen), * iptr = 0;
void * obuf = malloc(osize * obuflen);
size_t iavailable = 0;
size_t idone, odone, written;
do {
if (!iavailable && ibuf) /* If ibuf empty, try to fill it: */
if (!(iavailable = fread(iptr = ibuf, isize, ibuflen, stdin)))
free(ibuf), ibuf = 0; /* If none available, don't retry. */
error = soxr_process(resampler,
iptr, iavailable, &idone, obuf, obuflen, &odone);
iptr += idone * isize, iavailable -= idone; /* Consumed input. */
written = fwrite(obuf, osize, odone, stdout); /* Consume output. */
} while (!error && (ibuf || written));
free(obuf), free(ibuf);
clips = *soxr_num_clips(resampler); /* Can occur only with integer output.*/
soxr_delete(resampler);
}
fprintf(stderr, "%s %s; %lu clips; I/O: %s\n", arg0, soxr_strerror(error),
(long unsigned)clips, errno? strerror(errno) : "no error");
return error || errno;
}

View File

@@ -0,0 +1,81 @@
/* SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
* Licence for this file: LGPL v2.1 See LICENCE for details. */
/* Example 3a: extends example 2a with multiple channels, multiple datatypes,
* and other options.
*
* The seven arguments are:
* INPUT-RATE As example 2a
* OUTPUT-RATE Ditto
* NUM-CHANNELS Number of interleaved channels
* IN-DATATYPE# 0:float32 1:float64 2:int32 3:int16
* OUT-DATATYPE# Ditto
* Q# Quality-recipe | (quality-flags << 16) (in hex) See soxr.h
* USE-THREADS 1 to use multi-threading
*/
#include "util.h"
#include <soxr.h>
static size_t input_fn(void * p, soxr_cbuf_t * buf, size_t len)
{
static float * ibuf;
size_t isize = *(size_t *)p;
*buf = ibuf = realloc(ibuf, isize * len);
if (!ibuf)
return 0;
len = fread(ibuf, isize, len, stdin);
if (!len) {
if (ferror(stdin))
*buf = 0;
free(ibuf), ibuf = 0;
}
return len;
}
int main(int n, char const * arg[])
{
char const * arg0 = n? --n, *arg++ : "";
double irate = n? --n, atof(*arg++) : 96000.;
double orate = n? --n, atof(*arg++) : 44100.;
unsigned chans = n? --n, (unsigned)atoi(*arg++) : 1;
soxr_datatype_t itype = n? --n, (soxr_datatype_t)atoi(*arg++) :SOXR_FLOAT32_I;
soxr_datatype_t otype = n? --n, (soxr_datatype_t)atoi(*arg++) :SOXR_FLOAT32_I;
unsigned long q_recipe= n? --n, strtoul(*arg++, 0, 16) : SOXR_HQ;
int use_threads = n? --n, atoi(*arg++) : 1;
size_t isize = soxr_datatype_size(itype) * chans;
size_t osize = soxr_datatype_size(otype) * chans;
size_t clips = 0;
soxr_error_t error;
soxr_quality_spec_t q_spec = soxr_quality_spec(q_recipe &65535, q_recipe>>16);
soxr_io_spec_t io_spec = soxr_io_spec(itype, otype);
soxr_runtime_spec_t runtime_spec = soxr_runtime_spec(!use_threads);
soxr_t resampler = soxr_create(
irate, orate, chans, &error, &io_spec, &q_spec, &runtime_spec);
if (!error) error = soxr_set_input_fn(resampler, input_fn, &isize);
STD_STDIO;
if (!error) {
#define olen 8000
void * resampled = malloc(osize * olen);
size_t actual;
do actual = soxr_output(resampler, resampled, olen);
while (fwrite(resampled, osize, actual, stdout));
free(resampled);
error = soxr_error(resampler);
clips = *soxr_num_clips(resampler); /* Can occur only with integer output.*/
soxr_delete(resampler);
}
fprintf(stderr, "%s %s; %lu clips; I/O: %s\n", arg0, soxr_strerror(error),
(long unsigned)clips, errno? strerror(errno) : "no error");
return error || errno;
}

View File

@@ -0,0 +1,140 @@
/* SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
* Licence for this file: LGPL v2.1 See LICENCE for details. */
/* Example 4a: variant of example 3a demonstrating I/O with split channels. */
#include <soxr.h>
#include "util.h"
#define DEINTERLEAVE(T) do { \
unsigned i; \
size_t j; \
T * const * dest = (T * const *)dest0; \
T const * src = src0; \
if (ch == 1) memcpy(dest[0], src, n * sizeof(dest[0][0])); \
else for (j = 0; j < n; ++j) for (i = 0; i < ch; ++i) dest[i][j] = *src++; \
return; \
} while (0)
static void deinterleave(soxr_datatype_t data_type,
void * const * dest0,
void const * src0,
size_t n, unsigned ch)
{
switch (data_type & 3) {
case SOXR_FLOAT32: DEINTERLEAVE(float);
case SOXR_FLOAT64: DEINTERLEAVE(double);
case SOXR_INT32 : DEINTERLEAVE(int32_t);
case SOXR_INT16 : DEINTERLEAVE(int16_t);
default: break;
}
}
#define INTERLEAVE(T) do { \
unsigned i; \
size_t j; \
T * dest = dest0; \
T const * const * src = (T const * const *)src0; \
if (ch == 1) memcpy(dest, src[0], n * sizeof(dest[0])); \
else for (j = 0; j < n; ++j) for (i = 0; i < ch; ++i) *dest++ = src[i][j]; \
return; \
} while (0)
static void interleave(soxr_datatype_t data_type, void * dest0,
void * const * src0, size_t n, unsigned ch)
{
switch (data_type & 3) {
case SOXR_FLOAT32: INTERLEAVE(float);
case SOXR_FLOAT64: INTERLEAVE(double);
case SOXR_INT32 : INTERLEAVE(int32_t);
case SOXR_INT16 : INTERLEAVE(int16_t);
default: break;
}
}
int main(int n, char const * arg[])
{
char const * arg0 = n? --n, *arg++ : "";
double irate = n? --n, atof(*arg++) : 96000.;
double orate = n? --n, atof(*arg++) : 44100.;
unsigned chans = n? --n, (unsigned)atoi(*arg++) : 1;
soxr_datatype_t itype = n? --n, (soxr_datatype_t)atoi(*arg++) :SOXR_FLOAT32_I;
soxr_datatype_t otype = n? --n, (soxr_datatype_t)atoi(*arg++) :SOXR_FLOAT32_I;
unsigned long q_recipe= n? --n, strtoul(*arg++, 0, 16) : SOXR_HQ;
int use_threads = n? --n, atoi(*arg++) : 1;
size_t isize = soxr_datatype_size(itype) * chans;
size_t osize = soxr_datatype_size(otype) * chans;
size_t clips = 0;
soxr_error_t error;
soxr_quality_spec_t q_spec = soxr_quality_spec(q_recipe &65535, q_recipe>>16);
soxr_io_spec_t io_spec = soxr_io_spec(itype|SOXR_SPLIT, otype|SOXR_SPLIT);
soxr_runtime_spec_t runtime_spec = soxr_runtime_spec(!use_threads);
soxr_t resampler = soxr_create(
irate, orate, chans, &error, &io_spec, &q_spec, &runtime_spec);
STD_STDIO;
if (!error) {
#define buf_total_len 15000
/* Allocate resampling input and output buffers in proportion to the input
* and output rates: */
size_t ibuflen = (size_t)(irate * buf_total_len / (irate + orate) + .5);
size_t obuflen = buf_total_len - ibuflen;
/* For split channels: */
void * * ibuf_ptrs = malloc(sizeof(void *) * chans);
void * * obuf_ptrs = malloc(sizeof(void *) * chans);
char * * ibuf_offset_ptrs = malloc(sizeof(void *) * chans);
char * ibufs = malloc(isize * ibuflen), * iptr = ibufs;
char * obufs = malloc(osize * obuflen), * optr = obufs;
/* For interleaved channels: */
char * ibuf = malloc(isize * ibuflen);
char * obuf = malloc(osize * obuflen);
size_t iavailable = 0;
size_t idone, odone, written;
unsigned i;
for (i = 0; i < chans; ++i) {
ibuf_ptrs[i] = iptr;
obuf_ptrs[i] = optr;
iptr += ibuflen * soxr_datatype_size(itype);
optr += obuflen * soxr_datatype_size(otype);
}
do {
if (!iavailable && ibuf_offset_ptrs) { /* If ibuf empty, try to fill it: */
if (!(iavailable = fread(ibuf, isize, ibuflen, stdin)))
free(ibuf_offset_ptrs), ibuf_offset_ptrs = 0; /* If none available, don't retry. */
else {
memcpy(ibuf_offset_ptrs, ibuf_ptrs, sizeof(void *) * chans);
deinterleave(itype, ibuf_ptrs, ibuf, iavailable, chans);
}
}
error = soxr_process(resampler,
ibuf_offset_ptrs, iavailable, &idone, obuf_ptrs, obuflen, &odone);
if (ibuf_offset_ptrs) for (i = 0; i < chans; ++i) /* Consumed input. */
ibuf_offset_ptrs[i] += idone * isize;
iavailable -= idone;
interleave(otype, obuf, obuf_ptrs, odone, chans); /* Consume output. */
written = fwrite(obuf, osize, odone, stdout);
} while (!error && (ibuf_offset_ptrs || written));
free(obuf), free(ibuf), free(obufs), free(ibufs);
free(obuf_ptrs), free(ibuf_ptrs), free(ibuf_offset_ptrs);
clips = *soxr_num_clips(resampler);
soxr_delete(resampler);
}
fprintf(stderr, "%s %s; %lu clips; I/O: %s\n", arg0, soxr_strerror(error),
(long unsigned)clips, errno? strerror(errno) : "no error");
return error || errno;
}

View File

@@ -0,0 +1,14 @@
# SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
# Licence for this file: LGPL v2.1 See LICENCE for details.
add_definitions (${PROJECT_DEFS})
link_libraries (${PROJECT_NAME})
file (GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
foreach (fe ${SOURCES})
get_filename_component (f ${fe} NAME_WE)
add_executable (${f} ${fe})
if (NOT ${f} MATCHES "^3b")
set_target_properties(${f} PROPERTIES EXCLUDE_FROM_ALL TRUE)
endif ()
endforeach ()

View File

@@ -0,0 +1,18 @@
SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
These simple examples show the different ways that an application may
interface with libsoxr. Note that real-world applications may also have to
deal with file-formats, codecs, (more sophisticated) dithering, etc., which
are not covered here.
With libsoxr installed, the examples may be built using commands similar to
the following. On unix-like systems:
cc 1-single-block.c -lsoxr
or, on MS-Windows:
cl 1-single-block.c -I"C:/Program Files/soxr/include" "C:/Program Files/soxr/lib/soxr.lib"
IDEs may hide such commands behind configuration screens and build menus --
where applicable, consult your IDE's user-manual.

View File

@@ -0,0 +1,43 @@
/* SoX Resampler Library Copyright (c) 2007-12 robs@users.sourceforge.net
* Licence for this file: LGPL v2.1 See LICENCE for details. */
/* Common includes etc. for the examples. */
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#undef int16_t
#define int16_t short
#undef int32_t
#if LONG_MAX > 2147483647L
#define int32_t int
#elif LONG_MAX < 2147483647L
#error this programme requires that 'long int' has at least 32-bits
#else
#define int32_t long
#endif
#undef min
#define min(x,y) ((x)<(y)?(x):(y))
#define AL(a) (sizeof(a)/sizeof((a)[0])) /* Array Length */
#ifdef _WIN32
#undef M_PI
#define M_PI 3.14159265358979323846
/* Work-around for broken file-I/O on MS-Windows: */
#include <io.h>
#include <fcntl.h>
#define STD_STDIO _setmode(_fileno(stdout), _O_BINARY), \
_setmode(_fileno(stdin ), _O_BINARY);
#else
#define STD_STDIO
#endif