initial add
This commit is contained in:
308
convert.c
Normal file
308
convert.c
Normal file
@@ -0,0 +1,308 @@
|
||||
/*
|
||||
* convert.c - Cell bindings for libsamplerate
|
||||
*
|
||||
* Usage:
|
||||
* var samplerate = use('libsamplerate/convert')
|
||||
*
|
||||
* Functions:
|
||||
* samplerate.resample(blob, src_rate, dst_rate, channels, quality?)
|
||||
* - blob: stoned f32 PCM blob
|
||||
* - src_rate: source sample rate (e.g. 48000)
|
||||
* - dst_rate: destination sample rate (e.g. 44100)
|
||||
* - channels: number of channels (1=mono, 2=stereo)
|
||||
* - quality: optional, one of QUALITY_* constants (default: LINEAR)
|
||||
* - returns: stoned f32 PCM blob at new sample rate
|
||||
*
|
||||
* samplerate.get_name(quality) - get converter name
|
||||
* samplerate.get_description(quality) - get converter description
|
||||
* samplerate.is_valid_ratio(ratio) - check if ratio is valid
|
||||
* samplerate.version() - get library version string
|
||||
*
|
||||
* Quality constants (exported on module):
|
||||
* SINC_BEST (0) - highest quality, slowest
|
||||
* SINC_MEDIUM (1) - good quality, moderate speed
|
||||
* SINC_FASTEST (2) - lower quality sinc, faster
|
||||
* ZERO_ORDER (3) - zero order hold, very fast, poor quality
|
||||
* LINEAR (4) - linear interpolation, very fast, poor quality
|
||||
*/
|
||||
|
||||
#include "cell.h"
|
||||
#include "samplerate.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
// resample(blob, src_rate, dst_rate, channels, quality?)
|
||||
// Returns a new stoned blob with resampled audio
|
||||
JSC_CCALL(samplerate_resample,
|
||||
if (argc < 4)
|
||||
return JS_ThrowTypeError(js, "resample(blob, src_rate, dst_rate, channels, quality?) requires at least 4 arguments");
|
||||
|
||||
// Get input blob
|
||||
size_t in_bytes;
|
||||
float *in_data = (float*)js_get_blob_data(js, &in_bytes, argv[0]);
|
||||
if (in_data == (void*)-1) return JS_EXCEPTION;
|
||||
if (in_bytes == 0) return js_new_blob_stoned_copy(js, NULL, 0);
|
||||
|
||||
// Get rates and channels
|
||||
int32_t src_rate, dst_rate, channels;
|
||||
if (JS_ToInt32(js, &src_rate, argv[1]) < 0) return JS_EXCEPTION;
|
||||
if (JS_ToInt32(js, &dst_rate, argv[2]) < 0) return JS_EXCEPTION;
|
||||
if (JS_ToInt32(js, &channels, argv[3]) < 0) return JS_EXCEPTION;
|
||||
|
||||
if (src_rate <= 0 || dst_rate <= 0)
|
||||
return JS_ThrowRangeError(js, "sample rates must be positive");
|
||||
if (channels <= 0)
|
||||
return JS_ThrowRangeError(js, "channels must be positive");
|
||||
|
||||
// Quality (default to LINEAR for speed)
|
||||
int32_t quality = SRC_LINEAR;
|
||||
if (argc >= 5 && !JS_IsNull(argv[4])) {
|
||||
if (JS_ToInt32(js, &quality, argv[4]) < 0) return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
// Calculate conversion ratio and output size
|
||||
double ratio = (double)dst_rate / (double)src_rate;
|
||||
if (!src_is_valid_ratio(ratio))
|
||||
return JS_ThrowRangeError(js, "invalid sample rate ratio");
|
||||
|
||||
size_t in_samples = in_bytes / sizeof(float);
|
||||
size_t in_frames = in_samples / channels;
|
||||
|
||||
// Output frames = input frames * ratio, with some margin
|
||||
size_t out_frames = (size_t)ceil((double)in_frames * ratio) + 32;
|
||||
size_t out_samples = out_frames * channels;
|
||||
size_t out_bytes = out_samples * sizeof(float);
|
||||
|
||||
float *out_data = (float*)malloc(out_bytes);
|
||||
if (!out_data) return JS_ThrowOutOfMemory(js);
|
||||
|
||||
// Set up SRC_DATA
|
||||
SRC_DATA data;
|
||||
memset(&data, 0, sizeof(data));
|
||||
data.data_in = in_data;
|
||||
data.data_out = out_data;
|
||||
data.input_frames = (long)in_frames;
|
||||
data.output_frames = (long)out_frames;
|
||||
data.src_ratio = ratio;
|
||||
|
||||
// Perform conversion
|
||||
int error = src_simple(&data, quality, channels);
|
||||
if (error != 0) {
|
||||
free(out_data);
|
||||
return JS_ThrowInternalError(js, "resample failed: %s", src_strerror(error));
|
||||
}
|
||||
|
||||
// Create result blob with actual output size
|
||||
size_t actual_bytes = (size_t)data.output_frames_gen * channels * sizeof(float);
|
||||
JSValue result = js_new_blob_stoned_copy(js, out_data, actual_bytes);
|
||||
free(out_data);
|
||||
|
||||
return result;
|
||||
)
|
||||
|
||||
// get_name(quality) - get converter name
|
||||
JSC_CCALL(samplerate_get_name,
|
||||
int32_t quality = SRC_LINEAR;
|
||||
if (argc >= 1) JS_ToInt32(js, &quality, argv[0]);
|
||||
|
||||
const char *name = src_get_name(quality);
|
||||
if (!name) return JS_NULL;
|
||||
return JS_NewString(js, name);
|
||||
)
|
||||
|
||||
// get_description(quality) - get converter description
|
||||
JSC_CCALL(samplerate_get_description,
|
||||
int32_t quality = SRC_LINEAR;
|
||||
if (argc >= 1) JS_ToInt32(js, &quality, argv[0]);
|
||||
|
||||
const char *desc = src_get_description(quality);
|
||||
if (!desc) return JS_NULL;
|
||||
return JS_NewString(js, desc);
|
||||
)
|
||||
|
||||
// is_valid_ratio(ratio) - check if ratio is valid
|
||||
JSC_CCALL(samplerate_is_valid_ratio,
|
||||
double ratio = 1.0;
|
||||
if (argc >= 1) JS_ToFloat64(js, &ratio, argv[0]);
|
||||
return JS_NewBool(js, src_is_valid_ratio(ratio));
|
||||
)
|
||||
|
||||
// Streaming resampler state wrapper
|
||||
typedef struct {
|
||||
SRC_STATE *state;
|
||||
int channels;
|
||||
} Resampler;
|
||||
|
||||
void Resampler_free(JSRuntime *rt, Resampler *r) {
|
||||
if (r) {
|
||||
if (r->state) src_delete(r->state);
|
||||
free(r);
|
||||
}
|
||||
}
|
||||
|
||||
QJSCLASS(Resampler,)
|
||||
|
||||
// new_resampler(channels, quality?) - create streaming resampler
|
||||
JSC_CCALL(samplerate_new_resampler,
|
||||
if (argc < 1)
|
||||
return JS_ThrowTypeError(js, "new_resampler(channels, quality?) requires at least 1 argument");
|
||||
|
||||
int32_t channels;
|
||||
if (JS_ToInt32(js, &channels, argv[0]) < 0) return JS_EXCEPTION;
|
||||
if (channels <= 0)
|
||||
return JS_ThrowRangeError(js, "channels must be positive");
|
||||
|
||||
int32_t quality = SRC_LINEAR;
|
||||
if (argc >= 2 && !JS_IsNull(argv[1])) {
|
||||
if (JS_ToInt32(js, &quality, argv[1]) < 0) return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
int error = 0;
|
||||
SRC_STATE *state = src_new(quality, channels, &error);
|
||||
if (!state)
|
||||
return JS_ThrowInternalError(js, "failed to create resampler: %s", src_strerror(error));
|
||||
|
||||
Resampler *r = (Resampler*)malloc(sizeof(Resampler));
|
||||
if (!r) {
|
||||
src_delete(state);
|
||||
return JS_ThrowOutOfMemory(js);
|
||||
}
|
||||
r->state = state;
|
||||
r->channels = channels;
|
||||
|
||||
return Resampler2js(js, r);
|
||||
)
|
||||
|
||||
// resampler.process(blob, ratio, end_of_input?)
|
||||
// Returns { output: blob, input_used: int, output_gen: int }
|
||||
JSC_CCALL(resampler_process,
|
||||
Resampler *r = js2Resampler(js, self);
|
||||
if (!r || !r->state)
|
||||
return JS_ThrowTypeError(js, "invalid resampler");
|
||||
|
||||
if (argc < 2)
|
||||
return JS_ThrowTypeError(js, "process(blob, ratio, end_of_input?) requires at least 2 arguments");
|
||||
|
||||
size_t in_bytes;
|
||||
float *in_data = (float*)js_get_blob_data(js, &in_bytes, argv[0]);
|
||||
if (in_data == (void*)-1) return JS_EXCEPTION;
|
||||
|
||||
double ratio;
|
||||
if (JS_ToFloat64(js, &ratio, argv[1]) < 0) return JS_EXCEPTION;
|
||||
|
||||
int end_of_input = 0;
|
||||
if (argc >= 3) end_of_input = JS_ToBool(js, argv[2]);
|
||||
|
||||
if (!src_is_valid_ratio(ratio))
|
||||
return JS_ThrowRangeError(js, "invalid ratio");
|
||||
|
||||
size_t in_samples = in_bytes / sizeof(float);
|
||||
size_t in_frames = in_samples / r->channels;
|
||||
size_t out_frames = (size_t)ceil((double)in_frames * ratio) + 32;
|
||||
size_t out_bytes = out_frames * r->channels * sizeof(float);
|
||||
|
||||
float *out_data = (float*)malloc(out_bytes);
|
||||
if (!out_data) return JS_ThrowOutOfMemory(js);
|
||||
|
||||
SRC_DATA data;
|
||||
memset(&data, 0, sizeof(data));
|
||||
data.data_in = in_data;
|
||||
data.data_out = out_data;
|
||||
data.input_frames = (long)in_frames;
|
||||
data.output_frames = (long)out_frames;
|
||||
data.src_ratio = ratio;
|
||||
data.end_of_input = end_of_input;
|
||||
|
||||
int error = src_process(r->state, &data);
|
||||
if (error != 0) {
|
||||
free(out_data);
|
||||
return JS_ThrowInternalError(js, "process failed: %s", src_strerror(error));
|
||||
}
|
||||
|
||||
size_t actual_bytes = (size_t)data.output_frames_gen * r->channels * sizeof(float);
|
||||
JSValue out_blob = js_new_blob_stoned_copy(js, out_data, actual_bytes);
|
||||
free(out_data);
|
||||
|
||||
JSValue result = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js, result, "output", out_blob);
|
||||
JS_SetPropertyStr(js, result, "input_used", JS_NewInt64(js, data.input_frames_used));
|
||||
JS_SetPropertyStr(js, result, "output_gen", JS_NewInt64(js, data.output_frames_gen));
|
||||
|
||||
return result;
|
||||
)
|
||||
|
||||
// resampler.reset()
|
||||
JSC_CCALL(resampler_reset,
|
||||
Resampler *r = js2Resampler(js, self);
|
||||
if (!r || !r->state)
|
||||
return JS_ThrowTypeError(js, "invalid resampler");
|
||||
|
||||
int error = src_reset(r->state);
|
||||
if (error != 0)
|
||||
return JS_ThrowInternalError(js, "reset failed: %s", src_strerror(error));
|
||||
|
||||
return JS_NULL;
|
||||
)
|
||||
|
||||
// resampler.set_ratio(ratio)
|
||||
JSC_CCALL(resampler_set_ratio,
|
||||
Resampler *r = js2Resampler(js, self);
|
||||
if (!r || !r->state)
|
||||
return JS_ThrowTypeError(js, "invalid resampler");
|
||||
|
||||
double ratio;
|
||||
if (JS_ToFloat64(js, &ratio, argv[0]) < 0) return JS_EXCEPTION;
|
||||
|
||||
int error = src_set_ratio(r->state, ratio);
|
||||
if (error != 0)
|
||||
return JS_ThrowInternalError(js, "set_ratio failed: %s", src_strerror(error));
|
||||
|
||||
return JS_NULL;
|
||||
)
|
||||
|
||||
// resampler.channels getter
|
||||
JSValue js_resampler_get_channels(JSContext *js, JSValue self) {
|
||||
Resampler *r = js2Resampler(js, self);
|
||||
if (!r) return JS_NULL;
|
||||
return JS_NewInt32(js, r->channels);
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_Resampler_funcs[] = {
|
||||
JS_CFUNC_DEF("process", 3, js_resampler_process),
|
||||
JS_CFUNC_DEF("reset", 0, js_resampler_reset),
|
||||
JS_CFUNC_DEF("set_ratio", 1, js_resampler_set_ratio),
|
||||
JS_CGETSET_DEF("channels", js_resampler_get_channels, NULL),
|
||||
};
|
||||
|
||||
static const JSCFunctionListEntry js_samplerate_funcs[] = {
|
||||
MIST_FUNC_DEF(samplerate, resample, 5),
|
||||
MIST_FUNC_DEF(samplerate, get_name, 1),
|
||||
MIST_FUNC_DEF(samplerate, get_description, 1),
|
||||
MIST_FUNC_DEF(samplerate, is_valid_ratio, 1),
|
||||
MIST_FUNC_DEF(samplerate, new_resampler, 2),
|
||||
};
|
||||
|
||||
#define countof(x) (sizeof(x)/sizeof((x)[0]))
|
||||
|
||||
CELL_USE_INIT(
|
||||
// Set up Resampler class
|
||||
JS_NewClassID(&js_Resampler_id);
|
||||
JS_NewClass(JS_GetRuntime(js), js_Resampler_id, &js_Resampler_class);
|
||||
JSValue proto = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, proto, js_Resampler_funcs, countof(js_Resampler_funcs));
|
||||
JS_SetClassProto(js, js_Resampler_id, proto);
|
||||
|
||||
// Create export object
|
||||
JSValue export = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, export, js_samplerate_funcs, countof(js_samplerate_funcs));
|
||||
|
||||
// Export quality constants
|
||||
JS_SetPropertyStr(js, export, "SINC_BEST", JS_NewInt32(js, SRC_SINC_BEST_QUALITY));
|
||||
JS_SetPropertyStr(js, export, "SINC_MEDIUM", JS_NewInt32(js, SRC_SINC_MEDIUM_QUALITY));
|
||||
JS_SetPropertyStr(js, export, "SINC_FASTEST", JS_NewInt32(js, SRC_SINC_FASTEST));
|
||||
JS_SetPropertyStr(js, export, "ZERO_ORDER", JS_NewInt32(js, SRC_ZERO_ORDER_HOLD));
|
||||
JS_SetPropertyStr(js, export, "LINEAR", JS_NewInt32(js, SRC_LINEAR));
|
||||
|
||||
return export;
|
||||
)
|
||||
189
samplerate.h
Normal file
189
samplerate.h
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
|
||||
** All rights reserved.
|
||||
**
|
||||
** This code is released under 2-clause BSD license. Please see the
|
||||
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
|
||||
*/
|
||||
|
||||
/*
|
||||
** API documentation is available here:
|
||||
** http://libsndfile.github.io/libsamplerate/api.html
|
||||
*/
|
||||
|
||||
#ifndef SAMPLERATE_H
|
||||
#define SAMPLERATE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
|
||||
/* Opaque data type SRC_STATE. */
|
||||
typedef struct SRC_STATE_tag SRC_STATE ;
|
||||
|
||||
/* SRC_DATA is used to pass data to src_simple() and src_process(). */
|
||||
typedef struct
|
||||
{ const float *data_in ;
|
||||
float *data_out ;
|
||||
|
||||
long input_frames, output_frames ;
|
||||
long input_frames_used, output_frames_gen ;
|
||||
|
||||
int end_of_input ;
|
||||
|
||||
double src_ratio ;
|
||||
} SRC_DATA ;
|
||||
|
||||
/*
|
||||
** User supplied callback function type for use with src_callback_new()
|
||||
** and src_callback_read(). First parameter is the same pointer that was
|
||||
** passed into src_callback_new(). Second parameter is pointer to a
|
||||
** pointer. The user supplied callback function must modify *data to
|
||||
** point to the start of the user supplied float array. The user supplied
|
||||
** function must return the number of frames that **data points to.
|
||||
*/
|
||||
|
||||
typedef long (*src_callback_t) (void *cb_data, float **data) ;
|
||||
|
||||
/*
|
||||
** Standard initialisation function : return an anonymous pointer to the
|
||||
** internal state of the converter. Choose a converter from the enums below.
|
||||
** Error returned in *error.
|
||||
*/
|
||||
|
||||
SRC_STATE* src_new (int converter_type, int channels, int *error) ;
|
||||
|
||||
/*
|
||||
** Clone a handle : return an anonymous pointer to a new converter
|
||||
** containing the same internal state as orig. Error returned in *error.
|
||||
*/
|
||||
SRC_STATE* src_clone (SRC_STATE* orig, int *error) ;
|
||||
|
||||
/*
|
||||
** Initilisation for callback based API : return an anonymous pointer to the
|
||||
** internal state of the converter. Choose a converter from the enums below.
|
||||
** The cb_data pointer can point to any data or be set to NULL. Whatever the
|
||||
** value, when processing, user supplied function "func" gets called with
|
||||
** cb_data as first parameter.
|
||||
*/
|
||||
|
||||
SRC_STATE* src_callback_new (src_callback_t func, int converter_type, int channels,
|
||||
int *error, void* cb_data) ;
|
||||
|
||||
/*
|
||||
** Cleanup all internal allocations.
|
||||
** Always returns NULL.
|
||||
*/
|
||||
|
||||
SRC_STATE* src_delete (SRC_STATE *state) ;
|
||||
|
||||
/*
|
||||
** Standard processing function.
|
||||
** Returns non zero on error.
|
||||
*/
|
||||
|
||||
int src_process (SRC_STATE *state, SRC_DATA *data) ;
|
||||
|
||||
/*
|
||||
** Callback based processing function. Read up to frames worth of data from
|
||||
** the converter int *data and return frames read or -1 on error.
|
||||
*/
|
||||
long src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data) ;
|
||||
|
||||
/*
|
||||
** Simple interface for performing a single conversion from input buffer to
|
||||
** output buffer at a fixed conversion ratio.
|
||||
** Simple interface does not require initialisation as it can only operate on
|
||||
** a single buffer worth of audio.
|
||||
*/
|
||||
|
||||
int src_simple (SRC_DATA *data, int converter_type, int channels) ;
|
||||
|
||||
/*
|
||||
** This library contains a number of different sample rate converters,
|
||||
** numbered 0 through N.
|
||||
**
|
||||
** Return a string giving either a name or a more full description of each
|
||||
** sample rate converter or NULL if no sample rate converter exists for
|
||||
** the given value. The converters are sequentially numbered from 0 to N.
|
||||
*/
|
||||
|
||||
const char *src_get_name (int converter_type) ;
|
||||
const char *src_get_description (int converter_type) ;
|
||||
const char *src_get_version (void) ;
|
||||
|
||||
/*
|
||||
** Set a new SRC ratio. This allows step responses
|
||||
** in the conversion ratio.
|
||||
** Returns non zero on error.
|
||||
*/
|
||||
|
||||
int src_set_ratio (SRC_STATE *state, double new_ratio) ;
|
||||
|
||||
/*
|
||||
** Get the current channel count.
|
||||
** Returns negative on error, positive channel count otherwise
|
||||
*/
|
||||
|
||||
int src_get_channels (SRC_STATE *state) ;
|
||||
|
||||
/*
|
||||
** Reset the internal SRC state.
|
||||
** Does not modify the quality settings.
|
||||
** Does not free any memory allocations.
|
||||
** Returns non zero on error.
|
||||
*/
|
||||
|
||||
int src_reset (SRC_STATE *state) ;
|
||||
|
||||
/*
|
||||
** Return TRUE if ratio is a valid conversion ratio, FALSE
|
||||
** otherwise.
|
||||
*/
|
||||
|
||||
int src_is_valid_ratio (double ratio) ;
|
||||
|
||||
/*
|
||||
** Return an error number.
|
||||
*/
|
||||
|
||||
int src_error (SRC_STATE *state) ;
|
||||
|
||||
/*
|
||||
** Convert the error number into a string.
|
||||
*/
|
||||
const char* src_strerror (int error) ;
|
||||
|
||||
/*
|
||||
** The following enums can be used to set the interpolator type
|
||||
** using the function src_set_converter().
|
||||
*/
|
||||
|
||||
enum
|
||||
{
|
||||
SRC_SINC_BEST_QUALITY = 0,
|
||||
SRC_SINC_MEDIUM_QUALITY = 1,
|
||||
SRC_SINC_FASTEST = 2,
|
||||
SRC_ZERO_ORDER_HOLD = 3,
|
||||
SRC_LINEAR = 4,
|
||||
} ;
|
||||
|
||||
/*
|
||||
** Extra helper functions for converting from short to float and
|
||||
** back again.
|
||||
*/
|
||||
|
||||
void src_short_to_float_array (const short *in, float *out, int len) ;
|
||||
void src_float_to_short_array (const float *in, short *out, int len) ;
|
||||
|
||||
void src_int_to_float_array (const int *in, float *out, int len) ;
|
||||
void src_float_to_int_array (const float *in, int *out, int len) ;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* SAMPLERATE_H */
|
||||
|
||||
252
src/common.h
Normal file
252
src/common.h
Normal file
@@ -0,0 +1,252 @@
|
||||
/*
|
||||
** Copyright (c) 2002-2021, Erik de Castro Lopo <erikd@mega-nerd.com>
|
||||
** All rights reserved.
|
||||
**
|
||||
** This code is released under 2-clause BSD license. Please see the
|
||||
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
|
||||
*/
|
||||
|
||||
#ifndef COMMON_H_INCLUDED
|
||||
#define COMMON_H_INCLUDED
|
||||
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_STDBOOL_H
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#if defined(__x86_64__) || defined(_M_X64)
|
||||
# define HAVE_SSE2_INTRINSICS
|
||||
#elif defined(ENABLE_SSE2_LRINT) && (defined(_M_IX86) || defined(__i386__))
|
||||
# if defined(_MSC_VER)
|
||||
# define HAVE_SSE2_INTRINSICS
|
||||
# elif defined(__clang__)
|
||||
# ifdef __SSE2__
|
||||
# define HAVE_SSE2_INTRINSICS
|
||||
# elif (__has_attribute(target))
|
||||
# define HAVE_SSE2_INTRINSICS
|
||||
# define USE_TARGET_ATTRIBUTE
|
||||
# endif
|
||||
# elif defined(__GNUC__)
|
||||
# ifdef __SSE2__
|
||||
# define HAVE_SSE2_INTRINSICS
|
||||
# elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))
|
||||
# define HAVE_SSE2_INTRINSICS
|
||||
# define USE_TARGET_ATTRIBUTE
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SSE2_INTRINSICS
|
||||
#ifdef HAVE_IMMINTRIN_H
|
||||
#include <immintrin.h>
|
||||
#else
|
||||
#include <emmintrin.h>
|
||||
#endif
|
||||
#endif /* HAVE_SSE2_INTRINSICS */
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#ifdef HAVE_VISIBILITY
|
||||
#define LIBSAMPLERATE_DLL_PRIVATE __attribute__ ((visibility ("hidden")))
|
||||
#elif defined (__APPLE__)
|
||||
#define LIBSAMPLERATE_DLL_PRIVATE __private_extern__
|
||||
#else
|
||||
#define LIBSAMPLERATE_DLL_PRIVATE
|
||||
#endif
|
||||
|
||||
#define SRC_MAX_RATIO 256
|
||||
#define SRC_MAX_RATIO_STR "256"
|
||||
|
||||
#define SRC_MIN_RATIO_DIFF (1e-20)
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0])))
|
||||
#define OFFSETOF(type,member) ((int) (&((type*) 0)->member))
|
||||
|
||||
#define MAKE_MAGIC(a,b,c,d,e,f) ((a) + ((b) << 4) + ((c) << 8) + ((d) << 12) + ((e) << 16) + ((f) << 20))
|
||||
|
||||
/*
|
||||
** Inspiration : http://sourcefrog.net/weblog/software/languages/C/unused.html
|
||||
*/
|
||||
#ifdef UNUSED
|
||||
#elif defined (__GNUC__)
|
||||
# define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
|
||||
#elif defined (__LCLINT__)
|
||||
# define UNUSED(x) /*@unused@*/ x
|
||||
#else
|
||||
# define UNUSED(x) x
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
# define WARN_UNUSED __attribute__ ((warn_unused_result))
|
||||
#else
|
||||
# define WARN_UNUSED
|
||||
#endif
|
||||
|
||||
#include "samplerate.h"
|
||||
|
||||
enum
|
||||
{ SRC_FALSE = 0,
|
||||
SRC_TRUE = 1,
|
||||
} ;
|
||||
|
||||
enum SRC_MODE
|
||||
{
|
||||
SRC_MODE_PROCESS = 0,
|
||||
SRC_MODE_CALLBACK = 1
|
||||
} ;
|
||||
|
||||
typedef enum SRC_ERROR
|
||||
{
|
||||
SRC_ERR_NO_ERROR = 0,
|
||||
|
||||
SRC_ERR_MALLOC_FAILED,
|
||||
SRC_ERR_BAD_STATE,
|
||||
SRC_ERR_BAD_DATA,
|
||||
SRC_ERR_BAD_DATA_PTR,
|
||||
SRC_ERR_NO_PRIVATE,
|
||||
SRC_ERR_BAD_SRC_RATIO,
|
||||
SRC_ERR_BAD_PROC_PTR,
|
||||
SRC_ERR_SHIFT_BITS,
|
||||
SRC_ERR_FILTER_LEN,
|
||||
SRC_ERR_BAD_CONVERTER,
|
||||
SRC_ERR_BAD_CHANNEL_COUNT,
|
||||
SRC_ERR_SINC_BAD_BUFFER_LEN,
|
||||
SRC_ERR_SIZE_INCOMPATIBILITY,
|
||||
SRC_ERR_BAD_PRIV_PTR,
|
||||
SRC_ERR_BAD_SINC_STATE,
|
||||
SRC_ERR_DATA_OVERLAP,
|
||||
SRC_ERR_BAD_CALLBACK,
|
||||
SRC_ERR_BAD_MODE,
|
||||
SRC_ERR_NULL_CALLBACK,
|
||||
SRC_ERR_NO_VARIABLE_RATIO,
|
||||
SRC_ERR_SINC_PREPARE_DATA_BAD_LEN,
|
||||
SRC_ERR_BAD_INTERNAL_STATE,
|
||||
|
||||
/* This must be the last error number. */
|
||||
SRC_ERR_MAX_ERROR
|
||||
} SRC_ERROR ;
|
||||
|
||||
typedef struct SRC_STATE_VT_tag
|
||||
{
|
||||
/* Varispeed process function. */
|
||||
SRC_ERROR (*vari_process) (SRC_STATE *state, SRC_DATA *data) ;
|
||||
|
||||
/* Constant speed process function. */
|
||||
SRC_ERROR (*const_process) (SRC_STATE *state, SRC_DATA *data) ;
|
||||
|
||||
/* State reset. */
|
||||
void (*reset) (SRC_STATE *state) ;
|
||||
|
||||
/* State clone. */
|
||||
SRC_STATE *(*copy) (SRC_STATE *state) ;
|
||||
|
||||
/* State close. */
|
||||
void (*close) (SRC_STATE *state) ;
|
||||
} SRC_STATE_VT ;
|
||||
|
||||
struct SRC_STATE_tag
|
||||
{
|
||||
SRC_STATE_VT *vt ;
|
||||
|
||||
double last_ratio, last_position ;
|
||||
|
||||
SRC_ERROR error ;
|
||||
int channels ;
|
||||
|
||||
/* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */
|
||||
enum SRC_MODE mode ;
|
||||
|
||||
/* Data specific to SRC_MODE_CALLBACK. */
|
||||
src_callback_t callback_func ;
|
||||
void *user_callback_data ;
|
||||
long saved_frames ;
|
||||
const float *saved_data ;
|
||||
|
||||
/* Pointer to data to converter specific data. */
|
||||
void *private_data ;
|
||||
} ;
|
||||
|
||||
/* In src_sinc.c */
|
||||
const char* sinc_get_name (int src_enum) ;
|
||||
const char* sinc_get_description (int src_enum) ;
|
||||
|
||||
SRC_STATE *sinc_state_new (int converter_type, int channels, SRC_ERROR *error) ;
|
||||
|
||||
/* In src_linear.c */
|
||||
const char* linear_get_name (int src_enum) ;
|
||||
const char* linear_get_description (int src_enum) ;
|
||||
|
||||
SRC_STATE *linear_state_new (int channels, SRC_ERROR *error) ;
|
||||
|
||||
/* In src_zoh.c */
|
||||
const char* zoh_get_name (int src_enum) ;
|
||||
const char* zoh_get_description (int src_enum) ;
|
||||
|
||||
SRC_STATE *zoh_state_new (int channels, SRC_ERROR *error) ;
|
||||
|
||||
/*----------------------------------------------------------
|
||||
** SIMD optimized math functions.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_SSE2_INTRINSICS
|
||||
static inline int
|
||||
#ifdef USE_TARGET_ATTRIBUTE
|
||||
__attribute__((target("sse2")))
|
||||
#endif
|
||||
psf_lrintf (float x)
|
||||
{
|
||||
return _mm_cvtss_si32 (_mm_load_ss (&x)) ;
|
||||
}
|
||||
static inline int
|
||||
#ifdef USE_TARGET_ATTRIBUTE
|
||||
__attribute__((target("sse2")))
|
||||
#endif
|
||||
psf_lrint (double x)
|
||||
{
|
||||
return _mm_cvtsd_si32 (_mm_load_sd (&x)) ;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static inline int psf_lrintf (float x)
|
||||
{
|
||||
return lrintf (x) ;
|
||||
} /* psf_lrintf */
|
||||
|
||||
static inline int psf_lrint (double x)
|
||||
{
|
||||
return lrint (x) ;
|
||||
} /* psf_lrint */
|
||||
#endif
|
||||
|
||||
/*----------------------------------------------------------
|
||||
** Common static inline functions.
|
||||
*/
|
||||
|
||||
static inline double
|
||||
fmod_one (double x)
|
||||
{ double res ;
|
||||
|
||||
res = x - psf_lrint (x) ;
|
||||
if (res < 0.0)
|
||||
return res + 1.0 ;
|
||||
|
||||
return res ;
|
||||
} /* fmod_one */
|
||||
|
||||
static inline int
|
||||
is_bad_src_ratio (double ratio)
|
||||
{ return (ratio < (1.0 / SRC_MAX_RATIO) || ratio > (1.0 * SRC_MAX_RATIO)) ;
|
||||
} /* is_bad_src_ratio */
|
||||
|
||||
|
||||
#endif /* COMMON_H_INCLUDED */
|
||||
|
||||
2498
src/fastest_coeffs.h
Normal file
2498
src/fastest_coeffs.h
Normal file
File diff suppressed because it is too large
Load Diff
340273
src/high_qual_coeffs.h
Normal file
340273
src/high_qual_coeffs.h
Normal file
File diff suppressed because it is too large
Load Diff
22472
src/mid_qual_coeffs.h
Normal file
22472
src/mid_qual_coeffs.h
Normal file
File diff suppressed because it is too large
Load Diff
523
src/samplerate.c
Normal file
523
src/samplerate.c
Normal file
@@ -0,0 +1,523 @@
|
||||
/*
|
||||
** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
|
||||
** All rights reserved.
|
||||
**
|
||||
** This code is released under 2-clause BSD license. Please see the
|
||||
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "samplerate.h"
|
||||
#include "common.h"
|
||||
|
||||
static SRC_STATE *psrc_set_converter (int converter_type, int channels, int *error) ;
|
||||
|
||||
|
||||
SRC_STATE *
|
||||
src_new (int converter_type, int channels, int *error)
|
||||
{
|
||||
return psrc_set_converter (converter_type, channels, error) ;
|
||||
} /* src_new */
|
||||
|
||||
SRC_STATE*
|
||||
src_clone (SRC_STATE* orig, int *error)
|
||||
{
|
||||
if (!orig)
|
||||
{
|
||||
if (error)
|
||||
*error = SRC_ERR_BAD_STATE ;
|
||||
return NULL ;
|
||||
}
|
||||
if (error)
|
||||
*error = SRC_ERR_NO_ERROR ;
|
||||
|
||||
SRC_STATE *state = orig->vt->copy (orig) ;
|
||||
if (!state)
|
||||
if (error)
|
||||
*error = SRC_ERR_MALLOC_FAILED ;
|
||||
|
||||
return state ;
|
||||
}
|
||||
|
||||
SRC_STATE*
|
||||
src_callback_new (src_callback_t func, int converter_type, int channels, int *error, void* cb_data)
|
||||
{ SRC_STATE *state ;
|
||||
|
||||
if (func == NULL)
|
||||
{ if (error)
|
||||
*error = SRC_ERR_BAD_CALLBACK ;
|
||||
return NULL ;
|
||||
} ;
|
||||
|
||||
if (error != NULL)
|
||||
*error = 0 ;
|
||||
|
||||
if ((state = src_new (converter_type, channels, error)) == NULL)
|
||||
return NULL ;
|
||||
|
||||
src_reset (state) ;
|
||||
|
||||
state->mode = SRC_MODE_CALLBACK ;
|
||||
state->callback_func = func ;
|
||||
state->user_callback_data = cb_data ;
|
||||
|
||||
return state ;
|
||||
} /* src_callback_new */
|
||||
|
||||
SRC_STATE *
|
||||
src_delete (SRC_STATE *state)
|
||||
{
|
||||
if (state)
|
||||
state->vt->close (state) ;
|
||||
|
||||
return NULL ;
|
||||
} /* src_state */
|
||||
|
||||
int
|
||||
src_process (SRC_STATE *state, SRC_DATA *data)
|
||||
{
|
||||
int error ;
|
||||
|
||||
if (state == NULL)
|
||||
return SRC_ERR_BAD_STATE ;
|
||||
|
||||
if (state->mode != SRC_MODE_PROCESS)
|
||||
return SRC_ERR_BAD_MODE ;
|
||||
|
||||
/* Check for valid SRC_DATA first. */
|
||||
if (data == NULL)
|
||||
return SRC_ERR_BAD_DATA ;
|
||||
|
||||
/* And that data_in and data_out are valid. */
|
||||
if ((data->data_in == NULL && data->input_frames > 0)
|
||||
|| (data->data_out == NULL && data->output_frames > 0))
|
||||
return SRC_ERR_BAD_DATA_PTR ;
|
||||
|
||||
/* Check src_ratio is in range. */
|
||||
if (is_bad_src_ratio (data->src_ratio))
|
||||
return SRC_ERR_BAD_SRC_RATIO ;
|
||||
|
||||
if (data->input_frames < 0)
|
||||
data->input_frames = 0 ;
|
||||
if (data->output_frames < 0)
|
||||
data->output_frames = 0 ;
|
||||
|
||||
if (data->data_in < data->data_out)
|
||||
{ if (data->data_in + data->input_frames * state->channels > data->data_out)
|
||||
{ /*-printf ("\n\ndata_in: %p data_out: %p\n",
|
||||
(void*) (data->data_in + data->input_frames * psrc->channels), (void*) data->data_out) ;-*/
|
||||
return SRC_ERR_DATA_OVERLAP ;
|
||||
} ;
|
||||
}
|
||||
else if (data->data_out + data->output_frames * state->channels > data->data_in)
|
||||
{ /*-printf ("\n\ndata_in : %p ouput frames: %ld data_out: %p\n", (void*) data->data_in, data->output_frames, (void*) data->data_out) ;
|
||||
|
||||
printf ("data_out: %p (%p) data_in: %p\n", (void*) data->data_out,
|
||||
(void*) (data->data_out + data->input_frames * psrc->channels), (void*) data->data_in) ;-*/
|
||||
return SRC_ERR_DATA_OVERLAP ;
|
||||
} ;
|
||||
|
||||
/* Set the input and output counts to zero. */
|
||||
data->input_frames_used = 0 ;
|
||||
data->output_frames_gen = 0 ;
|
||||
|
||||
/* Special case for when last_ratio has not been set. */
|
||||
if (state->last_ratio < (1.0 / SRC_MAX_RATIO))
|
||||
state->last_ratio = data->src_ratio ;
|
||||
|
||||
/* Now process. */
|
||||
if (fabs (state->last_ratio - data->src_ratio) < 1e-15)
|
||||
error = state->vt->const_process (state, data) ;
|
||||
else
|
||||
error = state->vt->vari_process (state, data) ;
|
||||
|
||||
return error ;
|
||||
} /* src_process */
|
||||
|
||||
long
|
||||
src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data)
|
||||
{
|
||||
SRC_DATA src_data ;
|
||||
|
||||
long output_frames_gen ;
|
||||
int error = 0 ;
|
||||
|
||||
if (state == NULL)
|
||||
return 0 ;
|
||||
|
||||
if (frames <= 0)
|
||||
return 0 ;
|
||||
|
||||
if (state->mode != SRC_MODE_CALLBACK)
|
||||
{ state->error = SRC_ERR_BAD_MODE ;
|
||||
return 0 ;
|
||||
} ;
|
||||
|
||||
if (state->callback_func == NULL)
|
||||
{ state->error = SRC_ERR_NULL_CALLBACK ;
|
||||
return 0 ;
|
||||
} ;
|
||||
|
||||
memset (&src_data, 0, sizeof (src_data)) ;
|
||||
|
||||
/* Check src_ratio is in range. */
|
||||
if (is_bad_src_ratio (src_ratio))
|
||||
{ state->error = SRC_ERR_BAD_SRC_RATIO ;
|
||||
return 0 ;
|
||||
} ;
|
||||
|
||||
/* Switch modes temporarily. */
|
||||
src_data.src_ratio = src_ratio ;
|
||||
src_data.data_out = data ;
|
||||
src_data.output_frames = frames ;
|
||||
|
||||
src_data.data_in = state->saved_data ;
|
||||
src_data.input_frames = state->saved_frames ;
|
||||
|
||||
output_frames_gen = 0 ;
|
||||
while (output_frames_gen < frames)
|
||||
{ /* Use a dummy array for the case where the callback function
|
||||
** returns without setting the ptr.
|
||||
*/
|
||||
float dummy [1] ;
|
||||
|
||||
if (src_data.input_frames == 0)
|
||||
{ float *ptr = dummy ;
|
||||
|
||||
src_data.input_frames = state->callback_func (state->user_callback_data, &ptr) ;
|
||||
src_data.data_in = ptr ;
|
||||
|
||||
if (src_data.input_frames == 0)
|
||||
src_data.end_of_input = 1 ;
|
||||
} ;
|
||||
|
||||
/*
|
||||
** Now call process function. However, we need to set the mode
|
||||
** to SRC_MODE_PROCESS first and when we return set it back to
|
||||
** SRC_MODE_CALLBACK.
|
||||
*/
|
||||
state->mode = SRC_MODE_PROCESS ;
|
||||
error = src_process (state, &src_data) ;
|
||||
state->mode = SRC_MODE_CALLBACK ;
|
||||
|
||||
if (error != 0)
|
||||
break ;
|
||||
|
||||
src_data.data_in += src_data.input_frames_used * state->channels ;
|
||||
src_data.input_frames -= src_data.input_frames_used ;
|
||||
|
||||
src_data.data_out += src_data.output_frames_gen * state->channels ;
|
||||
src_data.output_frames -= src_data.output_frames_gen ;
|
||||
|
||||
output_frames_gen += src_data.output_frames_gen ;
|
||||
|
||||
if (src_data.end_of_input == SRC_TRUE && src_data.output_frames_gen == 0)
|
||||
break ;
|
||||
} ;
|
||||
|
||||
state->saved_data = src_data.data_in ;
|
||||
state->saved_frames = src_data.input_frames ;
|
||||
|
||||
if (error != 0)
|
||||
{ state->error = (SRC_ERROR) error ;
|
||||
return 0 ;
|
||||
} ;
|
||||
|
||||
return output_frames_gen ;
|
||||
} /* src_callback_read */
|
||||
|
||||
/*==========================================================================
|
||||
*/
|
||||
|
||||
int
|
||||
src_set_ratio (SRC_STATE *state, double new_ratio)
|
||||
{
|
||||
if (state == NULL)
|
||||
return SRC_ERR_BAD_STATE ;
|
||||
|
||||
if (is_bad_src_ratio (new_ratio))
|
||||
return SRC_ERR_BAD_SRC_RATIO ;
|
||||
|
||||
state->last_ratio = new_ratio ;
|
||||
|
||||
return SRC_ERR_NO_ERROR ;
|
||||
} /* src_set_ratio */
|
||||
|
||||
int
|
||||
src_get_channels (SRC_STATE *state)
|
||||
{
|
||||
if (state == NULL)
|
||||
return -SRC_ERR_BAD_STATE ;
|
||||
|
||||
return state->channels ;
|
||||
} /* src_get_channels */
|
||||
|
||||
int
|
||||
src_reset (SRC_STATE *state)
|
||||
{
|
||||
if (state == NULL)
|
||||
return SRC_ERR_BAD_STATE ;
|
||||
|
||||
state->vt->reset (state) ;
|
||||
|
||||
state->last_position = 0.0 ;
|
||||
state->last_ratio = 0.0 ;
|
||||
|
||||
state->saved_data = NULL ;
|
||||
state->saved_frames = 0 ;
|
||||
|
||||
state->error = SRC_ERR_NO_ERROR ;
|
||||
|
||||
return SRC_ERR_NO_ERROR ;
|
||||
} /* src_reset */
|
||||
|
||||
/*==============================================================================
|
||||
** Control functions.
|
||||
*/
|
||||
|
||||
const char *
|
||||
src_get_name (int converter_type)
|
||||
{ const char *desc ;
|
||||
|
||||
if ((desc = sinc_get_name (converter_type)) != NULL)
|
||||
return desc ;
|
||||
|
||||
if ((desc = zoh_get_name (converter_type)) != NULL)
|
||||
return desc ;
|
||||
|
||||
if ((desc = linear_get_name (converter_type)) != NULL)
|
||||
return desc ;
|
||||
|
||||
return NULL ;
|
||||
} /* src_get_name */
|
||||
|
||||
const char *
|
||||
src_get_description (int converter_type)
|
||||
{ const char *desc ;
|
||||
|
||||
if ((desc = sinc_get_description (converter_type)) != NULL)
|
||||
return desc ;
|
||||
|
||||
if ((desc = zoh_get_description (converter_type)) != NULL)
|
||||
return desc ;
|
||||
|
||||
if ((desc = linear_get_description (converter_type)) != NULL)
|
||||
return desc ;
|
||||
|
||||
return NULL ;
|
||||
} /* src_get_description */
|
||||
|
||||
int
|
||||
src_is_valid_ratio (double ratio)
|
||||
{
|
||||
if (is_bad_src_ratio (ratio))
|
||||
return SRC_FALSE ;
|
||||
|
||||
return SRC_TRUE ;
|
||||
} /* src_is_valid_ratio */
|
||||
|
||||
/*==============================================================================
|
||||
** Error reporting functions.
|
||||
*/
|
||||
|
||||
int
|
||||
src_error (SRC_STATE *state)
|
||||
{ if (state)
|
||||
return state->error ;
|
||||
return SRC_ERR_NO_ERROR ;
|
||||
} /* src_error */
|
||||
|
||||
const char*
|
||||
src_strerror (int error)
|
||||
{
|
||||
switch (error)
|
||||
{ case SRC_ERR_NO_ERROR :
|
||||
return "No error." ;
|
||||
case SRC_ERR_MALLOC_FAILED :
|
||||
return "Malloc failed." ;
|
||||
case SRC_ERR_BAD_STATE :
|
||||
return "SRC_STATE pointer is NULL." ;
|
||||
case SRC_ERR_BAD_DATA :
|
||||
return "SRC_DATA pointer is NULL." ;
|
||||
case SRC_ERR_BAD_DATA_PTR :
|
||||
return "SRC_DATA->data_out or SRC_DATA->data_in is NULL." ;
|
||||
case SRC_ERR_NO_PRIVATE :
|
||||
return "Internal error. No private data." ;
|
||||
|
||||
case SRC_ERR_BAD_SRC_RATIO :
|
||||
return "SRC ratio outside [1/" SRC_MAX_RATIO_STR ", " SRC_MAX_RATIO_STR "] range." ;
|
||||
|
||||
case SRC_ERR_BAD_SINC_STATE :
|
||||
return "src_process() called without reset after end_of_input." ;
|
||||
case SRC_ERR_BAD_PROC_PTR :
|
||||
return "Internal error. No process pointer." ;
|
||||
case SRC_ERR_SHIFT_BITS :
|
||||
return "Internal error. SHIFT_BITS too large." ;
|
||||
case SRC_ERR_FILTER_LEN :
|
||||
return "Internal error. Filter length too large." ;
|
||||
case SRC_ERR_BAD_CONVERTER :
|
||||
return "Bad converter number." ;
|
||||
case SRC_ERR_BAD_CHANNEL_COUNT :
|
||||
return "Channel count must be >= 1." ;
|
||||
case SRC_ERR_SINC_BAD_BUFFER_LEN :
|
||||
return "Internal error. Bad buffer length. Please report this." ;
|
||||
case SRC_ERR_SIZE_INCOMPATIBILITY :
|
||||
return "Internal error. Input data / internal buffer size difference. Please report this." ;
|
||||
case SRC_ERR_BAD_PRIV_PTR :
|
||||
return "Internal error. Private pointer is NULL. Please report this." ;
|
||||
case SRC_ERR_DATA_OVERLAP :
|
||||
return "Input and output data arrays overlap." ;
|
||||
case SRC_ERR_BAD_CALLBACK :
|
||||
return "Supplied callback function pointer is NULL." ;
|
||||
case SRC_ERR_BAD_MODE :
|
||||
return "Calling mode differs from initialisation mode (ie process v callback)." ;
|
||||
case SRC_ERR_NULL_CALLBACK :
|
||||
return "Callback function pointer is NULL in src_callback_read ()." ;
|
||||
case SRC_ERR_NO_VARIABLE_RATIO :
|
||||
return "This converter only allows constant conversion ratios." ;
|
||||
case SRC_ERR_SINC_PREPARE_DATA_BAD_LEN :
|
||||
return "Internal error : Bad length in prepare_data ()." ;
|
||||
case SRC_ERR_BAD_INTERNAL_STATE :
|
||||
return "Error : Someone is trampling on my internal state." ;
|
||||
|
||||
case SRC_ERR_MAX_ERROR :
|
||||
return "Placeholder. No error defined for this error number." ;
|
||||
|
||||
default : break ;
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
} /* src_strerror */
|
||||
|
||||
/*==============================================================================
|
||||
** Simple interface for performing a single conversion from input buffer to
|
||||
** output buffer at a fixed conversion ratio.
|
||||
*/
|
||||
|
||||
int
|
||||
src_simple (SRC_DATA *src_data, int converter, int channels)
|
||||
{ SRC_STATE *src_state ;
|
||||
int error ;
|
||||
|
||||
if ((src_state = src_new (converter, channels, &error)) == NULL)
|
||||
return error ;
|
||||
|
||||
src_data->end_of_input = 1 ; /* Only one buffer worth of input. */
|
||||
|
||||
error = src_process (src_state, src_data) ;
|
||||
|
||||
src_delete (src_state) ;
|
||||
|
||||
return error ;
|
||||
} /* src_simple */
|
||||
|
||||
void
|
||||
src_short_to_float_array (const short *in, float *out, int len)
|
||||
{
|
||||
for (int i = 0 ; i < len ; i++)
|
||||
{ out [i] = (float) (in [i] / (1.0 * 0x8000)) ;
|
||||
} ;
|
||||
|
||||
return ;
|
||||
} /* src_short_to_float_array */
|
||||
|
||||
void
|
||||
src_float_to_short_array (const float *in, short *out, int len)
|
||||
{
|
||||
for (int i = 0 ; i < len ; i++)
|
||||
{ float scaled_value ;
|
||||
scaled_value = in [i] * 32768.f ;
|
||||
if (scaled_value >= 32767.f)
|
||||
out [i] = 32767 ;
|
||||
else if (scaled_value <= -32768.f)
|
||||
out [i] = -32768 ;
|
||||
else
|
||||
out [i] = (short) (psf_lrintf (scaled_value)) ;
|
||||
}
|
||||
} /* src_float_to_short_array */
|
||||
|
||||
void
|
||||
src_int_to_float_array (const int *in, float *out, int len)
|
||||
{
|
||||
for (int i = 0 ; i < len ; i++)
|
||||
{ out [i] = (float) (in [i] / (8.0 * 0x10000000)) ;
|
||||
} ;
|
||||
|
||||
return ;
|
||||
} /* src_int_to_float_array */
|
||||
|
||||
void
|
||||
src_float_to_int_array (const float *in, int *out, int len)
|
||||
{ double scaled_value ;
|
||||
|
||||
for (int i = 0 ; i < len ; i++)
|
||||
{ scaled_value = in [i] * (8.0 * 0x10000000) ;
|
||||
#if CPU_CLIPS_POSITIVE == 0
|
||||
if (scaled_value >= (1.0 * 0x7FFFFFFF))
|
||||
{ out [i] = 0x7fffffff ;
|
||||
continue ;
|
||||
} ;
|
||||
#endif
|
||||
#if CPU_CLIPS_NEGATIVE == 0
|
||||
if (scaled_value <= (-8.0 * 0x10000000))
|
||||
{ out [i] = -1 - 0x7fffffff ;
|
||||
continue ;
|
||||
} ;
|
||||
#endif
|
||||
out [i] = (int) psf_lrint (scaled_value) ;
|
||||
} ;
|
||||
|
||||
} /* src_float_to_int_array */
|
||||
|
||||
/*==============================================================================
|
||||
** Private functions.
|
||||
*/
|
||||
|
||||
static SRC_STATE *
|
||||
psrc_set_converter (int converter_type, int channels, int *error)
|
||||
{
|
||||
SRC_ERROR temp_error;
|
||||
SRC_STATE *state ;
|
||||
switch (converter_type)
|
||||
{
|
||||
#ifdef ENABLE_SINC_BEST_CONVERTER
|
||||
case SRC_SINC_BEST_QUALITY :
|
||||
state = sinc_state_new (converter_type, channels, &temp_error) ;
|
||||
break ;
|
||||
#endif
|
||||
#ifdef ENABLE_SINC_MEDIUM_CONVERTER
|
||||
case SRC_SINC_MEDIUM_QUALITY :
|
||||
state = sinc_state_new (converter_type, channels, &temp_error) ;
|
||||
break ;
|
||||
#endif
|
||||
#ifdef ENABLE_SINC_FAST_CONVERTER
|
||||
case SRC_SINC_FASTEST :
|
||||
state = sinc_state_new (converter_type, channels, &temp_error) ;
|
||||
break ;
|
||||
#endif
|
||||
case SRC_ZERO_ORDER_HOLD :
|
||||
state = zoh_state_new (channels, &temp_error) ;
|
||||
break ;
|
||||
case SRC_LINEAR :
|
||||
state = linear_state_new (channels, &temp_error) ;
|
||||
break ;
|
||||
default :
|
||||
temp_error = SRC_ERR_BAD_CONVERTER ;
|
||||
state = NULL ;
|
||||
break ;
|
||||
}
|
||||
|
||||
if (error)
|
||||
*error = (int) temp_error ;
|
||||
|
||||
return state ;
|
||||
} /* psrc_set_converter */
|
||||
|
||||
302
src/src_linear.c
Normal file
302
src/src_linear.c
Normal file
@@ -0,0 +1,302 @@
|
||||
/*
|
||||
** Copyright (c) 2002-2021, Erik de Castro Lopo <erikd@mega-nerd.com>
|
||||
** All rights reserved.
|
||||
**
|
||||
** This code is released under 2-clause BSD license. Please see the
|
||||
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
static SRC_ERROR linear_vari_process (SRC_STATE *state, SRC_DATA *data) ;
|
||||
static void linear_reset (SRC_STATE *state) ;
|
||||
static SRC_STATE *linear_copy (SRC_STATE *state) ;
|
||||
static void linear_close (SRC_STATE *state) ;
|
||||
|
||||
/*========================================================================================
|
||||
*/
|
||||
|
||||
#define LINEAR_MAGIC_MARKER MAKE_MAGIC ('l', 'i', 'n', 'e', 'a', 'r')
|
||||
|
||||
#define SRC_DEBUG 0
|
||||
|
||||
typedef struct
|
||||
{ int linear_magic_marker ;
|
||||
bool dirty ;
|
||||
long in_count, in_used ;
|
||||
long out_count, out_gen ;
|
||||
float *last_value ;
|
||||
} LINEAR_DATA ;
|
||||
|
||||
static SRC_STATE_VT linear_state_vt =
|
||||
{
|
||||
linear_vari_process,
|
||||
linear_vari_process,
|
||||
linear_reset,
|
||||
linear_copy,
|
||||
linear_close
|
||||
} ;
|
||||
|
||||
/*----------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static SRC_ERROR
|
||||
linear_vari_process (SRC_STATE *state, SRC_DATA *data)
|
||||
{ LINEAR_DATA *priv ;
|
||||
double src_ratio, input_index, rem ;
|
||||
int ch ;
|
||||
|
||||
if (data->input_frames <= 0)
|
||||
return SRC_ERR_NO_ERROR ;
|
||||
|
||||
if (state->private_data == NULL)
|
||||
return SRC_ERR_NO_PRIVATE ;
|
||||
|
||||
priv = (LINEAR_DATA*) state->private_data ;
|
||||
|
||||
if (!priv->dirty)
|
||||
{ /* If we have just been reset, set the last_value data. */
|
||||
for (ch = 0 ; ch < state->channels ; ch++)
|
||||
priv->last_value [ch] = data->data_in [ch] ;
|
||||
priv->dirty = true ;
|
||||
} ;
|
||||
|
||||
priv->in_count = data->input_frames * state->channels ;
|
||||
priv->out_count = data->output_frames * state->channels ;
|
||||
priv->in_used = priv->out_gen = 0 ;
|
||||
|
||||
src_ratio = state->last_ratio ;
|
||||
|
||||
if (is_bad_src_ratio (src_ratio))
|
||||
return SRC_ERR_BAD_INTERNAL_STATE ;
|
||||
|
||||
input_index = state->last_position ;
|
||||
|
||||
/* Calculate samples before first sample in input array. */
|
||||
while (input_index < 1.0 && priv->out_gen < priv->out_count)
|
||||
{
|
||||
if (priv->in_used + state->channels * (1.0 + input_index) >= priv->in_count)
|
||||
break ;
|
||||
|
||||
if (priv->out_count > 0 && fabs (state->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
|
||||
src_ratio = state->last_ratio + priv->out_gen * (data->src_ratio - state->last_ratio) / priv->out_count ;
|
||||
|
||||
for (ch = 0 ; ch < state->channels ; ch++)
|
||||
{ data->data_out [priv->out_gen] = (float) (priv->last_value [ch] + input_index *
|
||||
((double) data->data_in [ch] - priv->last_value [ch])) ;
|
||||
priv->out_gen ++ ;
|
||||
} ;
|
||||
|
||||
/* Figure out the next index. */
|
||||
input_index += 1.0 / src_ratio ;
|
||||
} ;
|
||||
|
||||
rem = fmod_one (input_index) ;
|
||||
priv->in_used += state->channels * psf_lrint (input_index - rem) ;
|
||||
input_index = rem ;
|
||||
|
||||
/* Main processing loop. */
|
||||
while (priv->out_gen < priv->out_count && priv->in_used + state->channels * input_index < priv->in_count)
|
||||
{
|
||||
if (priv->out_count > 0 && fabs (state->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
|
||||
src_ratio = state->last_ratio + priv->out_gen * (data->src_ratio - state->last_ratio) / priv->out_count ;
|
||||
|
||||
#if SRC_DEBUG
|
||||
if (priv->in_used < state->channels && input_index < 1.0)
|
||||
{ printf ("Whoops!!!! in_used : %ld channels : %d input_index : %f\n", priv->in_used, state->channels, input_index) ;
|
||||
exit (1) ;
|
||||
} ;
|
||||
#endif
|
||||
|
||||
for (ch = 0 ; ch < state->channels ; ch++)
|
||||
{ data->data_out [priv->out_gen] = (float) (data->data_in [priv->in_used - state->channels + ch] + input_index *
|
||||
((double) data->data_in [priv->in_used + ch] - data->data_in [priv->in_used - state->channels + ch])) ;
|
||||
priv->out_gen ++ ;
|
||||
} ;
|
||||
|
||||
/* Figure out the next index. */
|
||||
input_index += 1.0 / src_ratio ;
|
||||
rem = fmod_one (input_index) ;
|
||||
|
||||
priv->in_used += state->channels * psf_lrint (input_index - rem) ;
|
||||
input_index = rem ;
|
||||
} ;
|
||||
|
||||
if (priv->in_used > priv->in_count)
|
||||
{ input_index += (priv->in_used - priv->in_count) / state->channels ;
|
||||
priv->in_used = priv->in_count ;
|
||||
} ;
|
||||
|
||||
state->last_position = input_index ;
|
||||
|
||||
if (priv->in_used > 0)
|
||||
for (ch = 0 ; ch < state->channels ; ch++)
|
||||
priv->last_value [ch] = data->data_in [priv->in_used - state->channels + ch] ;
|
||||
|
||||
/* Save current ratio rather then target ratio. */
|
||||
state->last_ratio = src_ratio ;
|
||||
|
||||
data->input_frames_used = priv->in_used / state->channels ;
|
||||
data->output_frames_gen = priv->out_gen / state->channels ;
|
||||
|
||||
return SRC_ERR_NO_ERROR ;
|
||||
} /* linear_vari_process */
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
LIBSAMPLERATE_DLL_PRIVATE const char*
|
||||
linear_get_name (int src_enum)
|
||||
{
|
||||
if (src_enum == SRC_LINEAR)
|
||||
return "Linear Interpolator" ;
|
||||
|
||||
return NULL ;
|
||||
} /* linear_get_name */
|
||||
|
||||
LIBSAMPLERATE_DLL_PRIVATE const char*
|
||||
linear_get_description (int src_enum)
|
||||
{
|
||||
if (src_enum == SRC_LINEAR)
|
||||
return "Linear interpolator, very fast, poor quality." ;
|
||||
|
||||
return NULL ;
|
||||
} /* linear_get_descrition */
|
||||
|
||||
static LINEAR_DATA *
|
||||
linear_data_new (int channels)
|
||||
{
|
||||
assert (channels > 0) ;
|
||||
|
||||
LINEAR_DATA *priv = (LINEAR_DATA *) calloc (1, sizeof (LINEAR_DATA)) ;
|
||||
if (priv)
|
||||
{
|
||||
priv->linear_magic_marker = LINEAR_MAGIC_MARKER ;
|
||||
priv->last_value = (float *) calloc (channels, sizeof (float)) ;
|
||||
if (!priv->last_value)
|
||||
{
|
||||
free (priv) ;
|
||||
priv = NULL ;
|
||||
}
|
||||
}
|
||||
|
||||
return priv ;
|
||||
}
|
||||
|
||||
LIBSAMPLERATE_DLL_PRIVATE SRC_STATE *
|
||||
linear_state_new (int channels, SRC_ERROR *error)
|
||||
{
|
||||
assert (channels > 0) ;
|
||||
assert (error != NULL) ;
|
||||
|
||||
SRC_STATE *state = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
|
||||
if (!state)
|
||||
{
|
||||
*error = SRC_ERR_MALLOC_FAILED ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
state->channels = channels ;
|
||||
state->mode = SRC_MODE_PROCESS ;
|
||||
|
||||
state->private_data = linear_data_new (state->channels) ;
|
||||
if (!state->private_data)
|
||||
{
|
||||
free (state) ;
|
||||
*error = SRC_ERR_MALLOC_FAILED ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
state->vt = &linear_state_vt ;
|
||||
|
||||
linear_reset (state) ;
|
||||
|
||||
*error = SRC_ERR_NO_ERROR ;
|
||||
|
||||
return state ;
|
||||
}
|
||||
|
||||
/*===================================================================================
|
||||
*/
|
||||
|
||||
static void
|
||||
linear_reset (SRC_STATE *state)
|
||||
{ LINEAR_DATA *priv = NULL ;
|
||||
|
||||
priv = (LINEAR_DATA*) state->private_data ;
|
||||
if (priv == NULL)
|
||||
return ;
|
||||
|
||||
priv->dirty = false ;
|
||||
memset (priv->last_value, 0, sizeof (priv->last_value [0]) * state->channels) ;
|
||||
|
||||
return ;
|
||||
} /* linear_reset */
|
||||
|
||||
SRC_STATE *
|
||||
linear_copy (SRC_STATE *state)
|
||||
{
|
||||
assert (state != NULL) ;
|
||||
|
||||
if (state->private_data == NULL)
|
||||
return NULL ;
|
||||
|
||||
SRC_STATE *to = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
|
||||
if (!to)
|
||||
return NULL ;
|
||||
memcpy (to, state, sizeof (SRC_STATE)) ;
|
||||
|
||||
LINEAR_DATA* from_priv = (LINEAR_DATA*) state->private_data ;
|
||||
LINEAR_DATA *to_priv = (LINEAR_DATA *) calloc (1, sizeof (LINEAR_DATA)) ;
|
||||
if (!to_priv)
|
||||
{
|
||||
free (to) ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
memcpy (to_priv, from_priv, sizeof (LINEAR_DATA)) ;
|
||||
to_priv->last_value = (float *) malloc (sizeof (float) * state->channels) ;
|
||||
if (!to_priv->last_value)
|
||||
{
|
||||
free (to) ;
|
||||
free (to_priv) ;
|
||||
return NULL ;
|
||||
}
|
||||
memcpy (to_priv->last_value, from_priv->last_value, sizeof (float) * state->channels) ;
|
||||
|
||||
to->private_data = to_priv ;
|
||||
|
||||
return to ;
|
||||
} /* linear_copy */
|
||||
|
||||
static void
|
||||
linear_close (SRC_STATE *state)
|
||||
{
|
||||
if (state)
|
||||
{
|
||||
LINEAR_DATA *linear = (LINEAR_DATA *) state->private_data ;
|
||||
if (linear)
|
||||
{
|
||||
if (linear->last_value)
|
||||
{
|
||||
free (linear->last_value) ;
|
||||
linear->last_value = NULL ;
|
||||
}
|
||||
free (linear) ;
|
||||
linear = NULL ;
|
||||
}
|
||||
free (state) ;
|
||||
state = NULL ;
|
||||
}
|
||||
} /* linear_close */
|
||||
1252
src/src_sinc.c
Normal file
1252
src/src_sinc.c
Normal file
File diff suppressed because it is too large
Load Diff
291
src/src_zoh.c
Normal file
291
src/src_zoh.c
Normal file
@@ -0,0 +1,291 @@
|
||||
/*
|
||||
** Copyright (c) 2002-2021, Erik de Castro Lopo <erikd@mega-nerd.com>
|
||||
** All rights reserved.
|
||||
**
|
||||
** This code is released under 2-clause BSD license. Please see the
|
||||
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
static SRC_ERROR zoh_vari_process (SRC_STATE *state, SRC_DATA *data) ;
|
||||
static void zoh_reset (SRC_STATE *state) ;
|
||||
static SRC_STATE *zoh_copy (SRC_STATE *state) ;
|
||||
static void zoh_close (SRC_STATE *state) ;
|
||||
|
||||
/*========================================================================================
|
||||
*/
|
||||
|
||||
#define ZOH_MAGIC_MARKER MAKE_MAGIC ('s', 'r', 'c', 'z', 'o', 'h')
|
||||
|
||||
typedef struct
|
||||
{ int zoh_magic_marker ;
|
||||
bool dirty ;
|
||||
long in_count, in_used ;
|
||||
long out_count, out_gen ;
|
||||
float *last_value ;
|
||||
} ZOH_DATA ;
|
||||
|
||||
static SRC_STATE_VT zoh_state_vt =
|
||||
{
|
||||
zoh_vari_process,
|
||||
zoh_vari_process,
|
||||
zoh_reset,
|
||||
zoh_copy,
|
||||
zoh_close
|
||||
} ;
|
||||
|
||||
/*----------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static SRC_ERROR
|
||||
zoh_vari_process (SRC_STATE *state, SRC_DATA *data)
|
||||
{ ZOH_DATA *priv ;
|
||||
double src_ratio, input_index, rem ;
|
||||
int ch ;
|
||||
|
||||
if (data->input_frames <= 0)
|
||||
return SRC_ERR_NO_ERROR ;
|
||||
|
||||
if (state->private_data == NULL)
|
||||
return SRC_ERR_NO_PRIVATE ;
|
||||
|
||||
priv = (ZOH_DATA*) state->private_data ;
|
||||
|
||||
if (!priv->dirty)
|
||||
{ /* If we have just been reset, set the last_value data. */
|
||||
for (ch = 0 ; ch < state->channels ; ch++)
|
||||
priv->last_value [ch] = data->data_in [ch] ;
|
||||
priv->dirty = true ;
|
||||
} ;
|
||||
|
||||
priv->in_count = data->input_frames * state->channels ;
|
||||
priv->out_count = data->output_frames * state->channels ;
|
||||
priv->in_used = priv->out_gen = 0 ;
|
||||
|
||||
src_ratio = state->last_ratio ;
|
||||
|
||||
if (is_bad_src_ratio (src_ratio))
|
||||
return SRC_ERR_BAD_INTERNAL_STATE ;
|
||||
|
||||
input_index = state->last_position ;
|
||||
|
||||
/* Calculate samples before first sample in input array. */
|
||||
while (input_index < 1.0 && priv->out_gen < priv->out_count)
|
||||
{
|
||||
if (priv->in_used + state->channels * input_index >= priv->in_count)
|
||||
break ;
|
||||
|
||||
if (priv->out_count > 0 && fabs (state->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
|
||||
src_ratio = state->last_ratio + priv->out_gen * (data->src_ratio - state->last_ratio) / priv->out_count ;
|
||||
|
||||
for (ch = 0 ; ch < state->channels ; ch++)
|
||||
{ data->data_out [priv->out_gen] = priv->last_value [ch] ;
|
||||
priv->out_gen ++ ;
|
||||
} ;
|
||||
|
||||
/* Figure out the next index. */
|
||||
input_index += 1.0 / src_ratio ;
|
||||
} ;
|
||||
|
||||
rem = fmod_one (input_index) ;
|
||||
priv->in_used += state->channels * psf_lrint (input_index - rem) ;
|
||||
input_index = rem ;
|
||||
|
||||
/* Main processing loop. */
|
||||
while (priv->out_gen < priv->out_count && priv->in_used + state->channels * input_index <= priv->in_count)
|
||||
{
|
||||
if (priv->out_count > 0 && fabs (state->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
|
||||
src_ratio = state->last_ratio + priv->out_gen * (data->src_ratio - state->last_ratio) / priv->out_count ;
|
||||
|
||||
for (ch = 0 ; ch < state->channels ; ch++)
|
||||
{ data->data_out [priv->out_gen] = data->data_in [priv->in_used - state->channels + ch] ;
|
||||
priv->out_gen ++ ;
|
||||
} ;
|
||||
|
||||
/* Figure out the next index. */
|
||||
input_index += 1.0 / src_ratio ;
|
||||
rem = fmod_one (input_index) ;
|
||||
|
||||
priv->in_used += state->channels * psf_lrint (input_index - rem) ;
|
||||
input_index = rem ;
|
||||
} ;
|
||||
|
||||
if (priv->in_used > priv->in_count)
|
||||
{ input_index += (priv->in_used - priv->in_count) / state->channels ;
|
||||
priv->in_used = priv->in_count ;
|
||||
} ;
|
||||
|
||||
state->last_position = input_index ;
|
||||
|
||||
if (priv->in_used > 0)
|
||||
for (ch = 0 ; ch < state->channels ; ch++)
|
||||
priv->last_value [ch] = data->data_in [priv->in_used - state->channels + ch] ;
|
||||
|
||||
/* Save current ratio rather then target ratio. */
|
||||
state->last_ratio = src_ratio ;
|
||||
|
||||
data->input_frames_used = priv->in_used / state->channels ;
|
||||
data->output_frames_gen = priv->out_gen / state->channels ;
|
||||
|
||||
return SRC_ERR_NO_ERROR ;
|
||||
} /* zoh_vari_process */
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
LIBSAMPLERATE_DLL_PRIVATE const char*
|
||||
zoh_get_name (int src_enum)
|
||||
{
|
||||
if (src_enum == SRC_ZERO_ORDER_HOLD)
|
||||
return "ZOH Interpolator" ;
|
||||
|
||||
return NULL ;
|
||||
} /* zoh_get_name */
|
||||
|
||||
LIBSAMPLERATE_DLL_PRIVATE const char*
|
||||
zoh_get_description (int src_enum)
|
||||
{
|
||||
if (src_enum == SRC_ZERO_ORDER_HOLD)
|
||||
return "Zero order hold interpolator, very fast, poor quality." ;
|
||||
|
||||
return NULL ;
|
||||
} /* zoh_get_descrition */
|
||||
|
||||
static ZOH_DATA *
|
||||
zoh_data_new (int channels)
|
||||
{
|
||||
assert (channels > 0) ;
|
||||
|
||||
ZOH_DATA *priv = (ZOH_DATA *) calloc (1, sizeof (ZOH_DATA)) ;
|
||||
if (priv)
|
||||
{
|
||||
priv->zoh_magic_marker = ZOH_MAGIC_MARKER ;
|
||||
priv->last_value = (float *) calloc (channels, sizeof (float)) ;
|
||||
if (!priv->last_value)
|
||||
{
|
||||
free (priv) ;
|
||||
priv = NULL ;
|
||||
}
|
||||
}
|
||||
|
||||
return priv ;
|
||||
}
|
||||
|
||||
LIBSAMPLERATE_DLL_PRIVATE SRC_STATE *
|
||||
zoh_state_new (int channels, SRC_ERROR *error)
|
||||
{
|
||||
assert (channels > 0) ;
|
||||
assert (error != NULL) ;
|
||||
|
||||
SRC_STATE *state = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
|
||||
if (!state)
|
||||
{
|
||||
*error = SRC_ERR_MALLOC_FAILED ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
state->channels = channels ;
|
||||
state->mode = SRC_MODE_PROCESS ;
|
||||
|
||||
state->private_data = zoh_data_new (state->channels) ;
|
||||
if (!state->private_data)
|
||||
{
|
||||
free (state) ;
|
||||
*error = SRC_ERR_MALLOC_FAILED ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
state->vt = &zoh_state_vt ;
|
||||
|
||||
zoh_reset (state) ;
|
||||
|
||||
*error = SRC_ERR_NO_ERROR ;
|
||||
|
||||
return state ;
|
||||
}
|
||||
|
||||
/*===================================================================================
|
||||
*/
|
||||
|
||||
static void
|
||||
zoh_reset (SRC_STATE *state)
|
||||
{ ZOH_DATA *priv ;
|
||||
|
||||
priv = (ZOH_DATA*) state->private_data ;
|
||||
if (priv == NULL)
|
||||
return ;
|
||||
|
||||
priv->dirty = false ;
|
||||
memset (priv->last_value, 0, sizeof (float) * state->channels) ;
|
||||
|
||||
return ;
|
||||
} /* zoh_reset */
|
||||
|
||||
static SRC_STATE *
|
||||
zoh_copy (SRC_STATE *state)
|
||||
{
|
||||
assert (state != NULL) ;
|
||||
|
||||
if (state->private_data == NULL)
|
||||
return NULL ;
|
||||
|
||||
SRC_STATE *to = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
|
||||
if (!to)
|
||||
return NULL ;
|
||||
memcpy (to, state, sizeof (SRC_STATE)) ;
|
||||
|
||||
ZOH_DATA* from_priv = (ZOH_DATA*) state->private_data ;
|
||||
ZOH_DATA *to_priv = (ZOH_DATA *) calloc (1, sizeof (ZOH_DATA)) ;
|
||||
if (!to_priv)
|
||||
{
|
||||
free (to) ;
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
memcpy (to_priv, from_priv, sizeof (ZOH_DATA)) ;
|
||||
to_priv->last_value = (float *) malloc (sizeof (float) * state->channels) ;
|
||||
if (!to_priv->last_value)
|
||||
{
|
||||
free (to) ;
|
||||
free (to_priv) ;
|
||||
return NULL ;
|
||||
}
|
||||
memcpy (to_priv->last_value, from_priv->last_value, sizeof (float) * state->channels) ;
|
||||
|
||||
to->private_data = to_priv ;
|
||||
|
||||
return to ;
|
||||
} /* zoh_copy */
|
||||
|
||||
static void
|
||||
zoh_close (SRC_STATE *state)
|
||||
{
|
||||
if (state)
|
||||
{
|
||||
ZOH_DATA *zoh = (ZOH_DATA *) state->private_data ;
|
||||
if (zoh)
|
||||
{
|
||||
if (zoh->last_value)
|
||||
{
|
||||
free (zoh->last_value) ;
|
||||
zoh->last_value = NULL ;
|
||||
}
|
||||
free (zoh) ;
|
||||
zoh = NULL ;
|
||||
}
|
||||
free (state) ;
|
||||
state = NULL ;
|
||||
}
|
||||
} /* zoh_close */
|
||||
Reference in New Issue
Block a user