Updated vio2sf with multiple configurable resampling modes

CQTexperiment
Chris Moeller 2014-03-31 20:38:24 -07:00
parent c4288a8e61
commit 52e8c8f07d
8 changed files with 985 additions and 363 deletions

View File

@ -9,8 +9,8 @@
/* Begin PBXBuildFile section */
833B1A3E180BAD0200414852 /* isqrt.c in Sources */ = {isa = PBXBuildFile; fileRef = 833B1A3A180BAD0200414852 /* isqrt.c */; };
833B1A3F180BAD0200414852 /* isqrt.h in Headers */ = {isa = PBXBuildFile; fileRef = 833B1A3B180BAD0200414852 /* isqrt.h */; };
833B1A40180BAD0200414852 /* lanczos_resampler.c in Sources */ = {isa = PBXBuildFile; fileRef = 833B1A3C180BAD0200414852 /* lanczos_resampler.c */; };
833B1A41180BAD0200414852 /* lanczos_resampler.h in Headers */ = {isa = PBXBuildFile; fileRef = 833B1A3D180BAD0200414852 /* lanczos_resampler.h */; };
83DD1A0318EA634F00DADA1A /* resampler.c in Sources */ = {isa = PBXBuildFile; fileRef = 83DD1A0118EA634F00DADA1A /* resampler.c */; };
83DD1A0418EA634F00DADA1A /* resampler.h in Headers */ = {isa = PBXBuildFile; fileRef = 83DD1A0218EA634F00DADA1A /* resampler.h */; };
83DE0C14180A9BD400269051 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 83DE0C12180A9BD400269051 /* InfoPlist.strings */; };
83DE0C81180A9CA400269051 /* ARM9.h in Headers */ = {isa = PBXBuildFile; fileRef = 83DE0C46180A9CA400269051 /* ARM9.h */; };
83DE0C82180A9CA400269051 /* arm_instructions.c in Sources */ = {isa = PBXBuildFile; fileRef = 83DE0C47180A9CA400269051 /* arm_instructions.c */; };
@ -55,8 +55,8 @@
/* Begin PBXFileReference section */
833B1A3A180BAD0200414852 /* isqrt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = isqrt.c; sourceTree = "<group>"; };
833B1A3B180BAD0200414852 /* isqrt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = isqrt.h; sourceTree = "<group>"; };
833B1A3C180BAD0200414852 /* lanczos_resampler.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lanczos_resampler.c; sourceTree = "<group>"; };
833B1A3D180BAD0200414852 /* lanczos_resampler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lanczos_resampler.h; sourceTree = "<group>"; };
83DD1A0118EA634F00DADA1A /* resampler.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = resampler.c; sourceTree = "<group>"; };
83DD1A0218EA634F00DADA1A /* resampler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = resampler.h; sourceTree = "<group>"; };
83DE0C06180A9BD400269051 /* vio2sf.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = vio2sf.framework; sourceTree = BUILT_PRODUCTS_DIR; };
83DE0C11180A9BD400269051 /* vio2sf-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "vio2sf-Info.plist"; sourceTree = "<group>"; };
83DE0C13180A9BD400269051 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
@ -173,10 +173,10 @@
83DE0C45180A9CA400269051 /* desmume */ = {
isa = PBXGroup;
children = (
83DD1A0118EA634F00DADA1A /* resampler.c */,
83DD1A0218EA634F00DADA1A /* resampler.h */,
833B1A3A180BAD0200414852 /* isqrt.c */,
833B1A3B180BAD0200414852 /* isqrt.h */,
833B1A3C180BAD0200414852 /* lanczos_resampler.c */,
833B1A3D180BAD0200414852 /* lanczos_resampler.h */,
83DE0C46180A9CA400269051 /* ARM9.h */,
83DE0C47180A9CA400269051 /* arm_instructions.c */,
83DE0C48180A9CA400269051 /* arm_instructions.h */,
@ -238,8 +238,8 @@
83DE0C8C180A9CA400269051 /* cp15.h in Headers */,
83DE0C95180A9CA400269051 /* matrix.h in Headers */,
83DE0C9C180A9CA400269051 /* NDSSystem.h in Headers */,
83DD1A0418EA634F00DADA1A /* resampler.h in Headers */,
83DE0C89180A9CA400269051 /* config.h in Headers */,
833B1A41180BAD0200414852 /* lanczos_resampler.h in Headers */,
83DE0C8D180A9CA400269051 /* debug.h in Headers */,
83DE0C98180A9CA400269051 /* mem.h in Headers */,
83DE0C90180A9CA400269051 /* FIFO.h in Headers */,
@ -326,13 +326,13 @@
83DE0C96180A9CA400269051 /* mc.c in Sources */,
83DE0C91180A9CA400269051 /* GPU.c in Sources */,
83DE0CA4180A9CA400269051 /* thumb_instructions.c in Sources */,
833B1A40180BAD0200414852 /* lanczos_resampler.c in Sources */,
83DE0C84180A9CA400269051 /* armcpu.c in Sources */,
83DE0C94180A9CA400269051 /* matrix.c in Sources */,
833B1A3E180BAD0200414852 /* isqrt.c in Sources */,
83DE0C93180A9CA400269051 /* instruction_tabdef.inc in Sources */,
83DE0C9B180A9CA400269051 /* NDSSystem.c in Sources */,
83DE0CB8180A9FD000269051 /* state.c in Sources */,
83DD1A0318EA634F00DADA1A /* resampler.c in Sources */,
83DE0C82180A9CA400269051 /* arm_instructions.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@ -308,11 +308,9 @@ void SPU_struct::KeyOn(int channel)
{
channel_struct &thischan = channels[channel];
if (spuInterpolationMode(state) == SPUInterpolation_Lanczos)
{
thischan.init_lanczos();
lanczos_resampler_clear(thischan.lanczos_resampler);
}
thischan.init_resampler();
resampler_clear(thischan.resampler);
resampler_set_quality(thischan.resampler, thischan.format == 3 ? RESAMPLER_QUALITY_BLEP : spuInterpolationMode(state));
adjust_channel_timer(&thischan);
@ -514,55 +512,19 @@ extern "C" void SPU_WriteLong(NDS_state *state, u32 addr, u32 val)
}
//////////////////////////////////////////////////////////////////////////////
static FORCEINLINE s32 Interpolate(s32 a, s32 b, double ratio)
{
//linear interpolation
ratio = ratio - sputrunc(ratio);
return s32floor((float)((1-ratio)*a + ratio*b));
}
//////////////////////////////////////////////////////////////////////////////
double round(double r)
{
return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
}
static FORCEINLINE void Fetch8BitDataInternal(SPUInterpolationMode INTERPOLATE_MODE, channel_struct *chan, s32 *data)
static FORCEINLINE void Fetch8BitDataInternal(channel_struct *chan, s32 *data)
{
u32 loc = sputrunc(chan->sampcnt);
if(INTERPOLATE_MODE == SPUInterpolation_Linear)
{
s32 a = (s32)(chan->buf8[loc] << 8);
if(loc < (chan->totlength << 2) - 1) {
s32 b = (s32)(chan->buf8[loc + 1] << 8);
a = Interpolate(a, b, chan->sampcnt);
}
*data = a;
}
else
*data = (s32)chan->buf8[loc] << 8;
*data = (s32)chan->buf8[loc] << 8;
}
static FORCEINLINE void Fetch16BitDataInternal(SPUInterpolationMode INTERPOLATE_MODE, const channel_struct * const chan, s32 *data)
static FORCEINLINE void Fetch16BitDataInternal(const channel_struct * const chan, s32 *data)
{
const s16* const buf16 = chan->buf16;
const int shift = 1;
if(INTERPOLATE_MODE == SPUInterpolation_Linear)
{
u32 loc = sputrunc(chan->sampcnt);
s32 a = (s32)buf16[loc], b;
if(loc < (chan->totlength << shift) - 1)
{
b = (s32)buf16[loc + 1];
a = Interpolate(a, b, chan->sampcnt);
}
*data = a;
}
else
*data = (s32)buf16[sputrunc(chan->sampcnt)];
*data = (s32)buf16[sputrunc(chan->sampcnt)];
}
static FORCEINLINE void FetchADPCMDataInternal(SPUInterpolationMode INTERPOLATE_MODE, channel_struct * const chan, s32 * const data)
static FORCEINLINE void FetchADPCMDataInternal(channel_struct * const chan, s32 * const data)
{
// No sense decoding, just return the last sample
if (chan->lastsampcnt != sputrunc(chan->sampcnt)){
@ -589,10 +551,7 @@ static FORCEINLINE void FetchADPCMDataInternal(SPUInterpolationMode INTERPOLATE_
chan->lastsampcnt = sputrunc(chan->sampcnt);
}
if(INTERPOLATE_MODE == SPUInterpolation_Linear)
*data = Interpolate((s32)chan->pcm16b_last,(s32)chan->pcm16b,chan->sampcnt);
else
*data = (s32)chan->pcm16b;
*data = (s32)chan->pcm16b;
}
static FORCEINLINE void FetchPSGDataInternal(channel_struct *chan, s32 *data)
@ -674,7 +633,7 @@ static FORCEINLINE void TestForLoop(NDS_state *state, int FORMAT, SPU_struct *SP
}
else
{
if (!chan->lanczos_resampler || !lanczos_resampler_get_sample_count(chan->lanczos_resampler))
if (!chan->resampler || !resampler_get_sample_count(chan->resampler))
{
chan->status = CHANSTAT_STOPPED;
@ -717,7 +676,7 @@ static FORCEINLINE void TestForLoop2(NDS_state *state, SPU_struct *SPU, channel_
}
else
{
if (!chan->lanczos_resampler || !lanczos_resampler_get_sample_count(chan->lanczos_resampler))
if (!chan->resampler || !resampler_get_sample_count(chan->resampler))
{
chan->status = CHANSTAT_STOPPED;
if(SPU == state->SPU_core)
@ -734,25 +693,22 @@ static FORCEINLINE void TestForLoop2(NDS_state *state, SPU_struct *SPU, channel_
static FORCEINLINE void Fetch8BitData(SPUInterpolationMode INTERPOLATE_MODE, NDS_state *state, SPU_struct* const SPU, channel_struct *chan, s32 *data)
{
if (INTERPOLATE_MODE != SPUInterpolation_Lanczos)
return Fetch8BitDataInternal(INTERPOLATE_MODE, chan, data);
double saved_inc = chan->sampinc;
chan->sampinc = 1.0;
lanczos_resampler_set_rate( chan->lanczos_resampler, saved_inc );
resampler_set_rate( chan->resampler, saved_inc );
while (chan->status != CHANSTAT_EMPTYBUFFER && lanczos_resampler_get_free_count(chan->lanczos_resampler))
while (chan->status != CHANSTAT_EMPTYBUFFER && resampler_get_free_count(chan->resampler))
{
s32 sample;
Fetch8BitDataInternal(SPUInterpolation_None, chan, &sample);
Fetch8BitDataInternal(chan, &sample);
TestForLoop(state, 0, SPU, chan);
lanczos_resampler_write_sample(chan->lanczos_resampler, sample);
resampler_write_sample(chan->resampler, sample);
}
chan->sampinc = saved_inc;
if (!lanczos_resampler_get_sample_count(chan->lanczos_resampler))
if (!resampler_get_sample_count(chan->resampler))
{
chan->status = CHANSTAT_STOPPED;
if(SPU == state->SPU_core)
@ -760,31 +716,28 @@ static FORCEINLINE void Fetch8BitData(SPUInterpolationMode INTERPOLATE_MODE, NDS
SPU->bufpos = SPU->buflength;
}
*data = lanczos_resampler_get_sample(chan->lanczos_resampler);
lanczos_resampler_remove_sample(chan->lanczos_resampler);
*data = resampler_get_sample(chan->resampler);
resampler_remove_sample(chan->resampler);
}
static FORCEINLINE void Fetch16BitData(SPUInterpolationMode INTERPOLATE_MODE, NDS_state *state, SPU_struct* const SPU, channel_struct *chan, s32 *data)
{
if (INTERPOLATE_MODE != SPUInterpolation_Lanczos)
return Fetch16BitDataInternal(INTERPOLATE_MODE, chan, data);
double saved_inc = chan->sampinc;
chan->sampinc = 1.0;
lanczos_resampler_set_rate( chan->lanczos_resampler, saved_inc );
resampler_set_rate( chan->resampler, saved_inc );
while (chan->status != CHANSTAT_EMPTYBUFFER && lanczos_resampler_get_free_count(chan->lanczos_resampler))
while (chan->status != CHANSTAT_EMPTYBUFFER && resampler_get_free_count(chan->resampler))
{
s32 sample;
Fetch16BitDataInternal(SPUInterpolation_None, chan, &sample);
Fetch16BitDataInternal(chan, &sample);
TestForLoop(state, 1, SPU, chan);
lanczos_resampler_write_sample(chan->lanczos_resampler, sample);
resampler_write_sample(chan->resampler, sample);
}
chan->sampinc = saved_inc;
if (!lanczos_resampler_get_sample_count(chan->lanczos_resampler))
if (!resampler_get_sample_count(chan->resampler))
{
chan->status = CHANSTAT_STOPPED;
if(SPU == state->SPU_core)
@ -792,31 +745,28 @@ static FORCEINLINE void Fetch16BitData(SPUInterpolationMode INTERPOLATE_MODE, ND
SPU->bufpos = SPU->buflength;
}
*data = lanczos_resampler_get_sample(chan->lanczos_resampler);
lanczos_resampler_remove_sample(chan->lanczos_resampler);
*data = resampler_get_sample(chan->resampler);
resampler_remove_sample(chan->resampler);
}
static FORCEINLINE void FetchADPCMData(SPUInterpolationMode INTERPOLATE_MODE, NDS_state *state, SPU_struct* const SPU, channel_struct *chan, s32 *data)
{
if (INTERPOLATE_MODE != SPUInterpolation_Lanczos)
return FetchADPCMDataInternal(INTERPOLATE_MODE, chan, data);
double saved_inc = chan->sampinc;
chan->sampinc = 1.0;
lanczos_resampler_set_rate( chan->lanczos_resampler, saved_inc );
resampler_set_rate( chan->resampler, saved_inc );
while (chan->status != CHANSTAT_EMPTYBUFFER && lanczos_resampler_get_free_count(chan->lanczos_resampler))
while (chan->status != CHANSTAT_EMPTYBUFFER && resampler_get_free_count(chan->resampler))
{
s32 sample;
FetchADPCMDataInternal(SPUInterpolation_None, chan, &sample);
FetchADPCMDataInternal(chan, &sample);
TestForLoop2(state, SPU, chan);
lanczos_resampler_write_sample(chan->lanczos_resampler, sample);
resampler_write_sample(chan->resampler, sample);
}
chan->sampinc = saved_inc;
if (!lanczos_resampler_get_sample_count(chan->lanczos_resampler))
if (!resampler_get_sample_count(chan->resampler))
{
chan->status = CHANSTAT_STOPPED;
if(SPU == state->SPU_core)
@ -824,34 +774,28 @@ static FORCEINLINE void FetchADPCMData(SPUInterpolationMode INTERPOLATE_MODE, ND
SPU->bufpos = SPU->buflength;
}
*data = lanczos_resampler_get_sample(chan->lanczos_resampler);
lanczos_resampler_remove_sample(chan->lanczos_resampler);
*data = resampler_get_sample(chan->resampler);
resampler_remove_sample(chan->resampler);
}
static FORCEINLINE void FetchPSGData(SPUInterpolationMode INTERPOLATE_MODE, channel_struct *chan, s32 *data)
{
const double PSG_RATIO = 32.0;
const double PSG_DIVIDER = 1.0 / PSG_RATIO;
resampler_set_rate( chan->resampler, chan->sampinc );
if (INTERPOLATE_MODE != SPUInterpolation_Lanczos)
return FetchPSGDataInternal(chan, data);
lanczos_resampler_set_rate( chan->lanczos_resampler, chan->sampinc * PSG_RATIO );
while (lanczos_resampler_get_free_count(chan->lanczos_resampler))
while (resampler_get_free_count(chan->resampler))
{
s32 sample;
FetchPSGDataInternal(chan, &sample);
chan->sampcnt += PSG_DIVIDER;
lanczos_resampler_write_sample(chan->lanczos_resampler, sample);
chan->sampcnt += 1.0;
resampler_write_sample(chan->resampler, sample);
}
/* No need to check if resampler is empty since we always fill it completely,
* and PSG channels never report terminating on their own.
*/
*data = lanczos_resampler_get_sample(chan->lanczos_resampler);
lanczos_resampler_remove_sample(chan->lanczos_resampler);
*data = resampler_get_sample(chan->resampler);
resampler_remove_sample(chan->resampler);
}
FORCEINLINE static void SPU_Mix(int CHANNELS, SPU_struct* SPU, channel_struct *chan, s32 data)
@ -880,15 +824,6 @@ FORCEINLINE static void ____SPU_ChanUpdate(NDS_state *state, int CHANNELS, int F
}
SPU_Mix(CHANNELS, SPU, chan, data);
}
if (INTERPOLATE_MODE != SPUInterpolation_Lanczos)
{
switch(FORMAT) {
case 0: case 1: TestForLoop(state, FORMAT, SPU, chan); break;
case 2: TestForLoop2(state, SPU, chan); break;
case 3: chan->sampcnt += chan->sampinc; break;
}
}
}
}

View File

@ -28,7 +28,7 @@
#include <math.h>
#include <assert.h>
#include "lanczos_resampler.h"
#include "resampler.h"
#ifdef _MSC_VER
#define FORCEINLINE __forceinline
@ -84,8 +84,10 @@ static FORCEINLINE s32 spumuldiv7(s32 val, u8 multiplier) {
enum SPUInterpolationMode
{
SPUInterpolation_None = 0,
SPUInterpolation_Linear = 1,
SPUInterpolation_Lanczos = 2
SPUInterpolation_Blep = 1,
SPUInterpolation_Linear = 2,
SPUInterpolation_Cubic = 3,
SPUInterpolation_Sinc = 4
};
typedef struct NDS_state NDS_state;
@ -106,29 +108,29 @@ typedef struct SoundInterface_struct
extern SoundInterface_struct SNDDummy;
extern SoundInterface_struct SNDFile;
static bool lanczos_initialized = false;
static bool resampler_initialized = false;
struct channel_struct
{
channel_struct()
{
lanczos_resampler = 0;
resampler = 0;
}
~channel_struct()
{
if (lanczos_resampler)
lanczos_resampler_delete(lanczos_resampler);
if (resampler)
resampler_delete(resampler);
}
void init_lanczos()
void init_resampler()
{
if (!lanczos_resampler)
if (!resampler)
{
if (!lanczos_initialized)
if (!resampler_initialized)
{
lanczos_init();
lanczos_initialized = true;
resampler_init();
resampler_initialized = true;
}
lanczos_resampler = lanczos_resampler_create();
resampler = resampler_create();
}
}
u32 num;
@ -160,7 +162,7 @@ struct channel_struct
int loop_index;
u16 x;
s16 psgnoise_last;
void *lanczos_resampler;
void *resampler;
} ;
struct SPU_struct

View File

@ -1,211 +0,0 @@
#include <stdlib.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "lanczos_resampler.h"
enum { LANCZOS_RESOLUTION = 8192 };
enum { LANCZOS_WIDTH = 8 };
enum { LANCZOS_SAMPLES = LANCZOS_RESOLUTION * LANCZOS_WIDTH };
static double lanczos_lut[LANCZOS_SAMPLES + 1];
enum { lanczos_buffer_size = LANCZOS_WIDTH * 4 };
int fEqual(const double b, const double a)
{
return fabs(a - b) < 1.0e-6;
}
static double sinc(double x)
{
return fEqual(x, 0.0) ? 1.0 : sin(x * M_PI) / (x * M_PI);
}
void lanczos_init()
{
unsigned i;
double dx = (double)(LANCZOS_WIDTH) / LANCZOS_SAMPLES, x = 0.0;
for (i = 0; i < LANCZOS_SAMPLES + 1; ++i, x += dx)
lanczos_lut[i] = fabs(x) < LANCZOS_WIDTH ? sinc(x) * sinc(x / LANCZOS_WIDTH) : 0.0;
}
typedef struct lanczos_resampler
{
long write_pos, write_filled;
long read_pos, read_filled;
unsigned short phase;
unsigned long phase_inc;
float buffer_in[lanczos_buffer_size * 2];
int buffer_out[lanczos_buffer_size];
} lanczos_resampler;
void * lanczos_resampler_create()
{
lanczos_resampler * r = ( lanczos_resampler * ) malloc( sizeof(lanczos_resampler) );
if ( !r ) return 0;
r->write_pos = 0;
r->write_filled = 0;
r->read_pos = 0;
r->read_filled = 0;
r->phase = 0;
r->phase_inc = 0;
memset( r->buffer_in, 0, sizeof(r->buffer_in) );
memset( r->buffer_out, 0, sizeof(r->buffer_out) );
return r;
}
void lanczos_resampler_delete(void * _r)
{
free( _r );
}
long lanczos_resampler_get_free_count(void *_r)
{
lanczos_resampler * r = ( lanczos_resampler * ) _r;
return lanczos_buffer_size - r->write_filled;
}
long lanczos_resampler_ready(void *_r)
{
lanczos_resampler * r = ( lanczos_resampler * ) _r;
return r->write_filled > (LANCZOS_WIDTH * 2);
}
void lanczos_resampler_clear(void *_r)
{
lanczos_resampler * r = ( lanczos_resampler * ) _r;
r->write_pos = 0;
r->write_filled = 0;
r->read_pos = 0;
r->read_filled = 0;
r->phase = 0;
}
void lanczos_resampler_set_rate(void *_r, double new_factor)
{
lanczos_resampler * r = ( lanczos_resampler * ) _r;
r->phase_inc = (long)( new_factor * LANCZOS_RESOLUTION );
}
void lanczos_resampler_write_sample(void *_r, int s)
{
lanczos_resampler * r = ( lanczos_resampler * ) _r;
if ( r->write_filled < lanczos_buffer_size )
{
float s32 = (float)s;
r->buffer_in[ r->write_pos ] = s32;
r->buffer_in[ r->write_pos + lanczos_buffer_size ] = s32;
++r->write_filled;
r->write_pos = ( r->write_pos + 1 ) % lanczos_buffer_size;
}
}
static long lanczos_resampler_run(lanczos_resampler * r, int ** out_, int * out_end)
{
long in_size = r->write_filled;
float const* in_ = r->buffer_in + lanczos_buffer_size + r->write_pos - r->write_filled;
long used = 0;
in_size -= LANCZOS_WIDTH * 2;
if ( in_size > 0 )
{
int* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
long phase = r->phase;
long phase_inc = r->phase_inc;
long step = phase_inc > LANCZOS_RESOLUTION ? LANCZOS_RESOLUTION * LANCZOS_RESOLUTION / phase_inc : LANCZOS_RESOLUTION;
do
{
// accumulate in extended precision
double kernel[LANCZOS_WIDTH * 2], kernel_sum = 0.0;
long i = LANCZOS_WIDTH;
long phase_adj = phase * step / LANCZOS_RESOLUTION;
double sample;
if ( out >= out_end )
break;
for (; i >= -LANCZOS_WIDTH + 1; --i)
{
long pos = i * step;
kernel_sum += kernel[i + LANCZOS_WIDTH - 1] = lanczos_lut[labs(phase_adj - pos)];
}
for (sample = 0, i = 0; i < LANCZOS_WIDTH * 2; ++i)
sample += in[i] * kernel[i];
*out++ = (int) (sample / kernel_sum);
phase += phase_inc;
in += phase >> 13;
phase &= 8191;
}
while ( in < in_end );
r->phase = phase;
*out_ = out;
used = in - in_;
r->write_filled -= used;
}
return used;
}
static void lanczos_resampler_fill(lanczos_resampler * r)
{
while ( r->write_filled > (LANCZOS_WIDTH * 2) &&
r->read_filled < lanczos_buffer_size )
{
long write_pos = ( r->read_pos + r->read_filled ) % lanczos_buffer_size;
long write_size = lanczos_buffer_size - write_pos;
int * out = r->buffer_out + write_pos;
if ( write_size > ( lanczos_buffer_size - r->read_filled ) )
write_size = lanczos_buffer_size - r->read_filled;
lanczos_resampler_run( r, &out, out + write_size );
r->read_filled += out - r->buffer_out - write_pos;
}
}
long lanczos_resampler_get_sample_count(void *_r)
{
lanczos_resampler * r = ( lanczos_resampler * ) _r;
if ( r->read_filled < 1 )
lanczos_resampler_fill( r );
return r->read_filled;
}
int lanczos_resampler_get_sample(void *_r)
{
lanczos_resampler * r = ( lanczos_resampler * ) _r;
if ( r->read_filled < 1 )
lanczos_resampler_fill( r );
if ( r->read_filled < 1 )
return 0;
return r->buffer_out[ r->read_pos ];
}
void lanczos_resampler_remove_sample(void *_r)
{
lanczos_resampler * r = ( lanczos_resampler * ) _r;
if ( r->read_filled > 0 )
{
--r->read_filled;
r->read_pos = ( r->read_pos + 1 ) % lanczos_buffer_size;
}
}

View File

@ -1,26 +0,0 @@
#ifndef _LANCZOS_RESAMPLER_H_
#define _LANCZOS_RESAMPLER_H_
#ifdef __cplusplus
extern "C" {
#endif
void lanczos_init();
void * lanczos_resampler_create();
void lanczos_resampler_delete(void *);
long lanczos_resampler_get_free_count(void *);
void lanczos_resampler_write_sample(void *, int sample);
void lanczos_resampler_set_rate( void *, double new_factor );
long lanczos_resampler_ready(void *);
void lanczos_resampler_clear(void *);
long lanczos_resampler_get_sample_count(void *);
int lanczos_resampler_get_sample(void *);
void lanczos_resampler_remove_sample(void *);
#ifdef __cplusplus
};
#endif
#endif

View File

@ -0,0 +1,848 @@
#include <stdlib.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#if (defined(_M_IX86) || defined(__i386__) || defined(_M_X64) || defined(__amd64__))
#include <xmmintrin.h>
#define RESAMPLER_SSE
#endif
#ifdef _MSC_VER
#define ALIGNED _declspec(align(16))
#else
#define ALIGNED __attribute__((aligned(16)))
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "resampler.h"
enum { RESAMPLER_SHIFT = 13 };
enum { RESAMPLER_RESOLUTION = 1 << RESAMPLER_SHIFT };
enum { SINC_WIDTH = 16 };
enum { SINC_SAMPLES = RESAMPLER_RESOLUTION * SINC_WIDTH };
enum { CUBIC_SAMPLES = RESAMPLER_RESOLUTION * 4 };
ALIGNED static float cubic_lut[CUBIC_SAMPLES];
static float sinc_lut[SINC_SAMPLES + 1];
enum { resampler_buffer_size = SINC_WIDTH * 4 };
static int fEqual(const float b, const float a)
{
return fabs(a - b) < 1.0e-6;
}
static float sinc(float x)
{
return fEqual(x, 0.0) ? 1.0 : sin(x * M_PI) / (x * M_PI);
}
#ifdef RESAMPLER_SSE
#ifdef _MSC_VER
#include <intrin.h>
#elif defined(__clang__) || defined(__GNUC__)
static inline void
__cpuid(int *data, int selector)
{
asm("cpuid"
: "=a" (data[0]),
"=b" (data[1]),
"=c" (data[2]),
"=d" (data[3])
: "a"(selector));
}
#else
#define __cpuid(a,b) memset((a), 0, sizeof(int) * 4)
#endif
static int query_cpu_feature_sse() {
int buffer[4];
__cpuid(buffer,1);
if ((buffer[3]&(1<<25)) == 0) return 0;
return 1;
}
static int resampler_has_sse = 0;
#endif
void resampler_init(void)
{
unsigned i;
double dx = (float)(SINC_WIDTH) / SINC_SAMPLES, x = 0.0;
for (i = 0; i < SINC_SAMPLES + 1; ++i, x += dx)
{
float y = x / SINC_WIDTH;
#if 0
// Blackman
float window = 0.42659 - 0.49656 * cos(M_PI + M_PI * y) + 0.076849 * cos(2.0 * M_PI * y);
#elif 1
// Nuttal 3 term
float window = 0.40897 + 0.5 * cos(M_PI * y) + 0.09103 * cos(2.0 * M_PI * y);
#elif 0
// C.R.Helmrich's 2 term window
float window = 0.79445 * cos(0.5 * M_PI * y) + 0.20555 * cos(1.5 * M_PI * y);
#elif 0
// Lanczos
float window = sinc(y);
#endif
sinc_lut[i] = fabs(x) < SINC_WIDTH ? sinc(x) * window : 0.0;
}
dx = 1.0 / (float)(RESAMPLER_RESOLUTION);
x = 0.0;
for (i = 0; i < RESAMPLER_RESOLUTION; ++i, x += dx)
{
cubic_lut[i*4] = (float)(-0.5 * x * x * x + x * x - 0.5 * x);
cubic_lut[i*4+1] = (float)( 1.5 * x * x * x - 2.5 * x * x + 1.0);
cubic_lut[i*4+2] = (float)(-1.5 * x * x * x + 2.0 * x * x + 0.5 * x);
cubic_lut[i*4+3] = (float)( 0.5 * x * x * x - 0.5 * x * x);
}
#ifdef RESAMPLER_SSE
resampler_has_sse = query_cpu_feature_sse();
#endif
}
typedef struct resampler
{
int write_pos, write_filled;
int read_pos, read_filled;
unsigned int phase;
unsigned int phase_inc;
unsigned int inv_phase;
unsigned int inv_phase_inc;
unsigned char quality;
float last_amp;
float accumulator;
float buffer_in[resampler_buffer_size * 2];
float buffer_out[resampler_buffer_size + SINC_WIDTH * 2 - 1];
} resampler;
void * resampler_create(void)
{
resampler * r = ( resampler * ) malloc( sizeof(resampler) );
if ( !r ) return 0;
r->write_pos = SINC_WIDTH - 1;
r->write_filled = SINC_WIDTH - 1;
r->read_pos = 0;
r->read_filled = 0;
r->phase = 0;
r->phase_inc = 0;
r->inv_phase = 0;
r->inv_phase_inc = 0;
r->quality = RESAMPLER_QUALITY_MAX;
r->last_amp = 0;
r->accumulator = 0;
memset( r->buffer_in, 0, sizeof(r->buffer_in) );
memset( r->buffer_out, 0, sizeof(r->buffer_out) );
return r;
}
void resampler_delete(void * _r)
{
free( _r );
}
void * resampler_dup(const void * _r)
{
const resampler * r_in = ( const resampler * ) _r;
resampler * r_out = ( resampler * ) malloc( sizeof(resampler) );
if ( !r_out ) return 0;
r_out->write_pos = r_in->write_pos;
r_out->write_filled = r_in->write_filled;
r_out->read_pos = r_in->read_pos;
r_out->read_filled = r_in->read_filled;
r_out->phase = r_in->phase;
r_out->phase_inc = r_in->phase_inc;
r_out->inv_phase = r_in->inv_phase;
r_out->inv_phase_inc = r_in->inv_phase_inc;
r_out->quality = r_in->quality;
r_out->last_amp = r_in->last_amp;
r_out->accumulator = r_in->accumulator;
memcpy( r_out->buffer_in, r_in->buffer_in, sizeof(r_in->buffer_in) );
memcpy( r_out->buffer_out, r_in->buffer_out, sizeof(r_in->buffer_out) );
return r_out;
}
void resampler_dup_inplace(void *_d, const void *_s)
{
const resampler * r_in = ( const resampler * ) _s;
resampler * r_out = ( resampler * ) _d;
r_out->write_pos = r_in->write_pos;
r_out->write_filled = r_in->write_filled;
r_out->read_pos = r_in->read_pos;
r_out->read_filled = r_in->read_filled;
r_out->phase = r_in->phase;
r_out->phase_inc = r_in->phase_inc;
r_out->inv_phase = r_in->inv_phase;
r_out->inv_phase_inc = r_in->inv_phase_inc;
r_out->quality = r_in->quality;
r_out->last_amp = r_in->last_amp;
r_out->accumulator = r_in->accumulator;
memcpy( r_out->buffer_in, r_in->buffer_in, sizeof(r_in->buffer_in) );
memcpy( r_out->buffer_out, r_in->buffer_out, sizeof(r_in->buffer_out) );
}
void resampler_set_quality(void *_r, int quality)
{
resampler * r = ( resampler * ) _r;
if (quality < RESAMPLER_QUALITY_MIN)
quality = RESAMPLER_QUALITY_MIN;
else if (quality > RESAMPLER_QUALITY_MAX)
quality = RESAMPLER_QUALITY_MAX;
if ( r->quality != quality )
{
if ( quality == RESAMPLER_QUALITY_BLEP || r->quality == RESAMPLER_QUALITY_BLEP )
{
r->read_pos = 0;
r->read_filled = 0;
r->last_amp = 0;
r->accumulator = 0;
memset( r->buffer_out, 0, sizeof(r->buffer_out) );
}
}
r->quality = (unsigned char)quality;
}
int resampler_get_free_count(void *_r)
{
resampler * r = ( resampler * ) _r;
return resampler_buffer_size - r->write_filled;
}
static int resampler_min_filled(resampler *r)
{
switch (r->quality)
{
default:
case RESAMPLER_QUALITY_ZOH:
case RESAMPLER_QUALITY_BLEP:
return 1;
case RESAMPLER_QUALITY_LINEAR:
return 2;
case RESAMPLER_QUALITY_CUBIC:
return 4;
case RESAMPLER_QUALITY_SINC:
return SINC_WIDTH * 2;
}
}
int resampler_ready(void *_r)
{
resampler * r = ( resampler * ) _r;
return r->write_filled > resampler_min_filled(r);
}
void resampler_clear(void *_r)
{
resampler * r = ( resampler * ) _r;
r->write_pos = SINC_WIDTH - 1;
r->write_filled = SINC_WIDTH - 1;
r->read_pos = 0;
r->read_filled = 0;
r->phase = 0;
memset(r->buffer_in, 0, (SINC_WIDTH - 1) * sizeof(r->buffer_in[0]));
memset(r->buffer_in + resampler_buffer_size, 0, (SINC_WIDTH - 1) * sizeof(r->buffer_in[0]));
if (r->quality == RESAMPLER_QUALITY_BLEP)
memset(r->buffer_out, 0, sizeof(r->buffer_out));
}
void resampler_set_rate(void *_r, double new_factor)
{
resampler * r = ( resampler * ) _r;
r->phase_inc = (int)( new_factor * RESAMPLER_RESOLUTION );
new_factor = 1.0 / new_factor;
r->inv_phase_inc = (int)( new_factor * RESAMPLER_RESOLUTION );
}
void resampler_write_sample(void *_r, int s)
{
resampler * r = ( resampler * ) _r;
if ( r->write_filled < resampler_buffer_size )
{
float s32 = s;
r->buffer_in[ r->write_pos ] = s32;
r->buffer_in[ r->write_pos + resampler_buffer_size ] = s32;
++r->write_filled;
r->write_pos = ( r->write_pos + 1 ) % resampler_buffer_size;
}
}
void resampler_write_sample_fixed(void *_r, int s, unsigned char depth)
{
resampler * r = ( resampler * ) _r;
if ( r->write_filled < resampler_buffer_size )
{
float s32 = s;
s32 /= (double)(1 << (depth - 1));
r->buffer_in[ r->write_pos ] = s32;
r->buffer_in[ r->write_pos + resampler_buffer_size ] = s32;
++r->write_filled;
r->write_pos = ( r->write_pos + 1 ) % resampler_buffer_size;
}
}
static int resampler_run_zoh(resampler * r, float ** out_, float * out_end)
{
int in_size = r->write_filled;
float const* in_ = r->buffer_in + resampler_buffer_size + r->write_pos - r->write_filled;
int used = 0;
in_size -= 1;
if ( in_size > 0 )
{
float* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
int phase = r->phase;
int phase_inc = r->phase_inc;
do
{
float sample;
if ( out >= out_end )
break;
sample = *in;
*out++ = sample;
phase += phase_inc;
in += phase >> RESAMPLER_SHIFT;
phase &= RESAMPLER_RESOLUTION-1;
}
while ( in < in_end );
r->phase = (unsigned short) phase;
*out_ = out;
used = (int)(in - in_);
r->write_filled -= used;
}
return used;
}
static int resampler_run_blep(resampler * r, float ** out_, float * out_end)
{
int in_size = r->write_filled;
float const* in_ = r->buffer_in + resampler_buffer_size + r->write_pos - r->write_filled;
int used = 0;
in_size -= 1;
if ( in_size > 0 )
{
float* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
float last_amp = r->last_amp;
int inv_phase = r->inv_phase;
int inv_phase_inc = r->inv_phase_inc;
const int step = RESAMPLER_RESOLUTION;
do
{
float kernel[SINC_WIDTH * 2], kernel_sum = 0.0;
int i = SINC_WIDTH;
float sample;
if ( out + SINC_WIDTH * 2 > out_end )
break;
for (; i >= -SINC_WIDTH + 1; --i)
{
int pos = i * step;
kernel_sum += kernel[i + SINC_WIDTH - 1] = sinc_lut[abs(inv_phase - pos)];
}
sample = *in++ - last_amp;
last_amp += sample;
sample /= kernel_sum;
for (sample = 0, i = 0; i < SINC_WIDTH * 2; ++i)
out[i] += sample * kernel[i];
inv_phase += inv_phase_inc;
out += inv_phase >> RESAMPLER_SHIFT;
inv_phase &= RESAMPLER_RESOLUTION-1;
}
while ( in < in_end );
r->inv_phase = inv_phase;
r->last_amp = last_amp;
*out_ = out;
used = (int)(in - in_);
r->write_filled -= used;
}
return used;
}
#ifdef RESAMPLER_SSE
static int resampler_run_blep_sse(resampler * r, float ** out_, float * out_end)
{
int in_size = r->write_filled;
float const* in_ = r->buffer_in + resampler_buffer_size + r->write_pos - r->write_filled;
int used = 0;
in_size -= 1;
if ( in_size > 0 )
{
float* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
float last_amp = r->last_amp;
int inv_phase = r->inv_phase;
int inv_phase_inc = r->inv_phase_inc;
const int step = RESAMPLER_RESOLUTION;
do
{
// accumulate in extended precision
float kernel_sum = 0.0;
__m128 kernel[SINC_WIDTH / 2];
__m128 temp1, temp2;
__m128 samplex;
float sample;
float *kernelf = (float*)(&kernel);
int i = SINC_WIDTH;
if ( out + SINC_WIDTH * 2 > out_end )
break;
for (; i >= -SINC_WIDTH + 1; --i)
{
int pos = i * step;
kernel_sum += kernelf[i + SINC_WIDTH - 1] = sinc_lut[abs(inv_phase - pos)];
}
sample = *in++ - last_amp;
last_amp += sample;
sample /= kernel_sum;
samplex = _mm_set1_ps( sample );
for (i = 0; i < SINC_WIDTH / 2; ++i)
{
temp1 = _mm_load_ps( (const float *)( kernel + i ) );
temp1 = _mm_mul_ps( temp1, samplex );
temp2 = _mm_loadu_ps( (const float *) out + i * 4 );
temp1 = _mm_add_ps( temp1, temp2 );
_mm_storeu_ps( (float *) out + i * 4, temp1 );
}
inv_phase += inv_phase_inc;
out += inv_phase >> RESAMPLER_SHIFT;
inv_phase &= RESAMPLER_RESOLUTION - 1;
}
while ( in < in_end );
r->inv_phase = inv_phase;
r->last_amp = last_amp;
*out_ = out;
used = (int)(in - in_);
r->write_filled -= used;
}
return used;
}
#endif
static int resampler_run_linear(resampler * r, float ** out_, float * out_end)
{
int in_size = r->write_filled;
float const* in_ = r->buffer_in + resampler_buffer_size + r->write_pos - r->write_filled;
int used = 0;
in_size -= 2;
if ( in_size > 0 )
{
float* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
int phase = r->phase;
int phase_inc = r->phase_inc;
do
{
float sample;
if ( out >= out_end )
break;
sample = in[0] + (in[1] - in[0]) * ((float)phase / RESAMPLER_RESOLUTION);
*out++ = sample;
phase += phase_inc;
in += phase >> RESAMPLER_SHIFT;
phase &= RESAMPLER_RESOLUTION-1;
}
while ( in < in_end );
r->phase = phase;
*out_ = out;
used = (int)(in - in_);
r->write_filled -= used;
}
return used;
}
static int resampler_run_cubic(resampler * r, float ** out_, float * out_end)
{
int in_size = r->write_filled;
float const* in_ = r->buffer_in + resampler_buffer_size + r->write_pos - r->write_filled;
int used = 0;
in_size -= 4;
if ( in_size > 0 )
{
float* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
int phase = r->phase;
int phase_inc = r->phase_inc;
do
{
float * kernel;
int i;
float sample;
if ( out >= out_end )
break;
kernel = cubic_lut + phase * 4;
for (sample = 0, i = 0; i < 4; ++i)
sample += in[i] * kernel[i];
*out++ = sample;
phase += phase_inc;
in += phase >> RESAMPLER_SHIFT;
phase &= RESAMPLER_RESOLUTION-1;
}
while ( in < in_end );
r->phase = phase;
*out_ = out;
used = (int)(in - in_);
r->write_filled -= used;
}
return used;
}
#ifdef RESAMPLER_SSE
static int resampler_run_cubic_sse(resampler * r, float ** out_, float * out_end)
{
int in_size = r->write_filled;
float const* in_ = r->buffer_in + resampler_buffer_size + r->write_pos - r->write_filled;
int used = 0;
in_size -= 4;
if ( in_size > 0 )
{
float* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
int phase = r->phase;
int phase_inc = r->phase_inc;
do
{
__m128 temp1, temp2;
__m128 samplex = _mm_setzero_ps();
if ( out >= out_end )
break;
temp1 = _mm_loadu_ps( (const float *)( in ) );
temp2 = _mm_load_ps( (const float *)( cubic_lut + phase * 4 ) );
temp1 = _mm_mul_ps( temp1, temp2 );
samplex = _mm_add_ps( samplex, temp1 );
temp1 = _mm_movehl_ps( temp1, samplex );
samplex = _mm_add_ps( samplex, temp1 );
temp1 = samplex;
temp1 = _mm_shuffle_ps( temp1, samplex, _MM_SHUFFLE(0, 0, 0, 1) );
samplex = _mm_add_ps( samplex, temp1 );
_mm_store_ss( out, samplex );
++out;
phase += phase_inc;
in += phase >> RESAMPLER_SHIFT;
phase &= RESAMPLER_RESOLUTION - 1;
}
while ( in < in_end );
r->phase = phase;
*out_ = out;
used = (int)(in - in_);
r->write_filled -= used;
}
return used;
}
#endif
static int resampler_run_sinc(resampler * r, float ** out_, float * out_end)
{
int in_size = r->write_filled;
float const* in_ = r->buffer_in + resampler_buffer_size + r->write_pos - r->write_filled;
int used = 0;
in_size -= SINC_WIDTH * 2;
if ( in_size > 0 )
{
float* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
int phase = r->phase;
int phase_inc = r->phase_inc;
int step = phase_inc > RESAMPLER_RESOLUTION ? RESAMPLER_RESOLUTION * RESAMPLER_RESOLUTION / phase_inc : RESAMPLER_RESOLUTION;
do
{
float kernel[SINC_WIDTH * 2], kernel_sum = 0.0;
int i = SINC_WIDTH;
int phase_adj = phase * step / RESAMPLER_RESOLUTION;
float sample;
if ( out >= out_end )
break;
for (; i >= -SINC_WIDTH + 1; --i)
{
int pos = i * step;
kernel_sum += kernel[i + SINC_WIDTH - 1] = sinc_lut[abs(phase_adj - pos)];
}
for (sample = 0, i = 0; i < SINC_WIDTH * 2; ++i)
sample += in[i] * kernel[i];
*out++ = (float)(sample / kernel_sum);
phase += phase_inc;
in += phase >> RESAMPLER_SHIFT;
phase &= RESAMPLER_RESOLUTION-1;
}
while ( in < in_end );
r->phase = phase;
*out_ = out;
used = (int)(in - in_);
r->write_filled -= used;
}
return used;
}
#ifdef RESAMPLER_SSE
static int resampler_run_sinc_sse(resampler * r, float ** out_, float * out_end)
{
int in_size = r->write_filled;
float const* in_ = r->buffer_in + resampler_buffer_size + r->write_pos - r->write_filled;
int used = 0;
in_size -= SINC_WIDTH * 2;
if ( in_size > 0 )
{
float* out = *out_;
float const* in = in_;
float const* const in_end = in + in_size;
int phase = r->phase;
int phase_inc = r->phase_inc;
int step = phase_inc > RESAMPLER_RESOLUTION ? RESAMPLER_RESOLUTION * RESAMPLER_RESOLUTION / phase_inc : RESAMPLER_RESOLUTION;
do
{
// accumulate in extended precision
float kernel_sum = 0.0;
__m128 kernel[SINC_WIDTH / 2];
__m128 temp1, temp2;
__m128 samplex = _mm_setzero_ps();
float *kernelf = (float*)(&kernel);
int i = SINC_WIDTH;
int phase_adj = phase * step / RESAMPLER_RESOLUTION;
if ( out >= out_end )
break;
for (; i >= -SINC_WIDTH + 1; --i)
{
int pos = i * step;
kernel_sum += kernelf[i + SINC_WIDTH - 1] = sinc_lut[abs(phase_adj - pos)];
}
for (i = 0; i < SINC_WIDTH / 2; ++i)
{
temp1 = _mm_loadu_ps( (const float *)( in + i * 4 ) );
temp2 = _mm_load_ps( (const float *)( kernel + i ) );
temp1 = _mm_mul_ps( temp1, temp2 );
samplex = _mm_add_ps( samplex, temp1 );
}
kernel_sum = 1.0 / kernel_sum;
temp1 = _mm_movehl_ps( temp1, samplex );
samplex = _mm_add_ps( samplex, temp1 );
temp1 = samplex;
temp1 = _mm_shuffle_ps( temp1, samplex, _MM_SHUFFLE(0, 0, 0, 1) );
samplex = _mm_add_ps( samplex, temp1 );
temp1 = _mm_set_ss( kernel_sum );
samplex = _mm_mul_ps( samplex, temp1 );
_mm_store_ss( out, samplex );
++out;
phase += phase_inc;
in += phase >> RESAMPLER_SHIFT;
phase &= RESAMPLER_RESOLUTION - 1;
}
while ( in < in_end );
r->phase = phase;
*out_ = out;
used = (int)(in - in_);
r->write_filled -= used;
}
return used;
}
#endif
static void resampler_fill(resampler * r)
{
int min_filled = resampler_min_filled(r);
int quality = r->quality;
while ( r->write_filled > min_filled &&
r->read_filled < resampler_buffer_size )
{
int write_pos = ( r->read_pos + r->read_filled ) % resampler_buffer_size;
int write_size = resampler_buffer_size - write_pos;
float * out = r->buffer_out + write_pos;
if ( write_size > ( resampler_buffer_size - r->read_filled ) )
write_size = resampler_buffer_size - r->read_filled;
switch (quality)
{
case RESAMPLER_QUALITY_ZOH:
resampler_run_zoh( r, &out, out + write_size );
break;
case RESAMPLER_QUALITY_BLEP:
{
int used;
int write_extra = 0;
if ( write_pos >= r->read_pos )
write_extra = r->read_pos;
if ( write_extra > SINC_WIDTH * 2 - 1 )
write_extra = SINC_WIDTH * 2 - 1;
memcpy( r->buffer_out + resampler_buffer_size, r->buffer_out, write_extra * sizeof(r->buffer_out[0]) );
if ( resampler_has_sse )
used = resampler_run_blep_sse( r, &out, out + write_size + write_extra );
else
used = resampler_run_blep( r, &out, out + write_size + write_extra );
memcpy( r->buffer_out, r->buffer_out + resampler_buffer_size, write_extra * sizeof(r->buffer_out[0]) );
if (!used)
return;
break;
}
case RESAMPLER_QUALITY_LINEAR:
resampler_run_linear( r, &out, out + write_size );
break;
case RESAMPLER_QUALITY_CUBIC:
#ifdef RESAMPLER_SSE
if ( resampler_has_sse )
resampler_run_cubic_sse( r, &out, out + write_size );
else
#endif
resampler_run_cubic( r, &out, out + write_size );
break;
case RESAMPLER_QUALITY_SINC:
#ifdef RESAMPLER_SSE
if ( resampler_has_sse )
resampler_run_sinc_sse( r, &out, out + write_size );
else
#endif
resampler_run_sinc( r, &out, out + write_size );
break;
}
r->read_filled += out - r->buffer_out - write_pos;
}
}
int resampler_get_sample_count(void *_r)
{
resampler * r = ( resampler * ) _r;
if ( r->read_filled < 1 && (r->quality != RESAMPLER_QUALITY_BLEP || r->inv_phase_inc))
resampler_fill( r );
return r->read_filled;
}
int resampler_get_sample(void *_r)
{
resampler * r = ( resampler * ) _r;
if ( r->read_filled < 1 && r->phase_inc)
resampler_fill( r );
if ( r->read_filled < 1 )
return 0;
if ( r->quality == RESAMPLER_QUALITY_BLEP )
return (int)(r->buffer_out[ r->read_pos ] + r->accumulator);
else
return (int)r->buffer_out[ r->read_pos ];
}
void resampler_remove_sample(void *_r)
{
resampler * r = ( resampler * ) _r;
if ( r->read_filled > 0 )
{
if ( r->quality == RESAMPLER_QUALITY_BLEP )
{
r->accumulator += r->buffer_out[ r->read_pos ];
r->buffer_out[ r->read_pos ] = 0;
r->accumulator -= r->accumulator * (1.0 / 8192.0);
if (fabs(r->accumulator) < 1e-20)
r->accumulator = 0;
}
--r->read_filled;
r->read_pos = ( r->read_pos + 1 ) % resampler_buffer_size;
}
}

View File

@ -0,0 +1,61 @@
#ifndef _RESAMPLER_H_
#define _RESAMPLER_H_
// Ugglay
#ifdef RESAMPLER_DECORATE
#define PASTE(a,b) a ## b
#define EVALUATE(a,b) PASTE(a,b)
#define resampler_init EVALUATE(RESAMPLER_DECORATE,_resampler_init)
#define resampler_create EVALUATE(RESAMPLER_DECORATE,_resampler_create)
#define resampler_delete EVALUATE(RESAMPLER_DECORATE,_resampler_delete)
#define resampler_dup EVALUATE(RESAMPLER_DECORATE,_resampler_dup)
#define resampler_dup_inplace EVALUATE(RESAMPLER_DECORATE,_resampler_dup_inplace)
#define resampler_set_quality EVALUATE(RESAMPLER_DECORATE,_resampler_set_quality)
#define resampler_get_free_count EVALUATE(RESAMPLER_DECORATE,_resampler_get_free_count)
#define resampler_write_sample EVALUATE(RESAMPLER_DECORATE,_resampler_write_sample)
#define resampler_set_rate EVALUATE(RESAMPLER_DECORATE,_resampler_set_rate)
#define resampler_ready EVALUATE(RESAMPLER_DECORATE,_resampler_ready)
#define resampler_clear EVALUATE(RESAMPLER_DECORATE,_resampler_clear)
#define resampler_get_sample_count EVALUATE(RESAMPLER_DECORATE,_resampler_get_sample_count)
#define resampler_get_sample EVALUATE(RESAMPLER_DECORATE,_resampler_get_sample)
#define resampler_remove_sample EVALUATE(RESAMPLER_DECORATE,_resampler_remove_sample)
#endif
#ifdef __cplusplus
extern "C" {
#endif
void resampler_init(void);
void * resampler_create(void);
void resampler_delete(void *);
void * resampler_dup(const void *);
void resampler_dup_inplace(void *, const void *);
enum
{
RESAMPLER_QUALITY_MIN = 0,
RESAMPLER_QUALITY_ZOH = 0,
RESAMPLER_QUALITY_BLEP = 1,
RESAMPLER_QUALITY_LINEAR = 2,
RESAMPLER_QUALITY_CUBIC = 3,
RESAMPLER_QUALITY_SINC = 4,
RESAMPLER_QUALITY_MAX = 4
};
void resampler_set_quality(void *, int quality);
int resampler_get_free_count(void *);
void resampler_write_sample(void *, int sample);
void resampler_set_rate( void *, double new_factor );
int resampler_ready(void *);
void resampler_clear(void *);
int resampler_get_sample_count(void *);
int resampler_get_sample(void *);
void resampler_remove_sample(void *);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1098,7 +1098,20 @@ static int usf_info(void * context, const char * name, const char * value)
return NO;
}
core->dwInterpolation = 2;
int resampling_int = -1;
NSString * resampling = [[NSUserDefaults standardUserDefaults] stringForKey:@"resampling"];
if ([resampling isEqualToString:@"zoh"])
resampling_int = 0;
else if ([resampling isEqualToString:@"blep"])
resampling_int = 1;
else if ([resampling isEqualToString:@"linear"])
resampling_int = 2;
else if ([resampling isEqualToString:@"cubic"])
resampling_int = 3;
else if ([resampling isEqualToString:@"sinc"])
resampling_int = 4;
core->dwInterpolation = resampling_int;
core->dwChannelMute = 0;
if (!state.arm7_clockdown_level)