Changed USF player to resample using lazyusf2

CQTexperiment
Chris Moeller 2015-03-01 23:04:22 -08:00
parent 4d8ec1960f
commit b73c20985c
6 changed files with 64 additions and 141 deletions

View File

@ -176,6 +176,15 @@ m64p_error main_start(usf_state_t * state)
state->g_ai.regs[AI_STATUS_REG] |= 0x40000000;
}
// We want to leave in all the necessary code so that these can one day be enabled for the trimmed sets
if (state->enable_trimming_mode)
{
state->g_delay_si = 1;
state->g_delay_ai = 1;
state->g_delay_pi = 1;
state->g_delay_dp = 1;
}
return M64ERR_SUCCESS;
}

View File

@ -108,7 +108,7 @@ int write_rdram_dram_tracked(void* opaque, uint32_t address, uint32_t value, uin
struct ri_controller* ri = &state->g_ri;
uint32_t addr = rdram_dram_address(address);
if (mask && !bit_array_test(state->barray_ram_read, addr / 4))
if (mask == 0xFFFFFFFFU && !bit_array_test(state->barray_ram_read, addr / 4))
bit_array_set(state->barray_ram_written_first, addr / 4);
masked_write(&ri->rdram.dram[addr], value, mask);

View File

@ -9,65 +9,23 @@
#include "resampler.h"
enum { RESAMPLER_SHIFT = 10 };
#include "rsp_hle/audio.h"
enum { RESAMPLER_SHIFT = 16 };
enum { RESAMPLER_RESOLUTION = 1 << RESAMPLER_SHIFT };
enum { SINC_WIDTH = 16 };
enum { SINC_SAMPLES = RESAMPLER_RESOLUTION * SINC_WIDTH };
static const float RESAMPLER_BLEP_CUTOFF = 0.90f;
static const float RESAMPLER_BLAM_CUTOFF = 0.93f;
static const float RESAMPLER_SINC_CUTOFF = 0.999f;
static float sinc_lut[SINC_SAMPLES + 1];
static float window_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);
}
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) : 0.0;
window_lut[i] = window;
}
}
enum { resampler_buffer_size = 64 * 4 };
typedef struct resampler
{
int write_pos, write_filled;
int read_pos, read_filled;
float phase;
float phase_inc;
int phase;
int phase_inc;
signed char delay_added;
signed char delay_removed;
float buffer_in[2][resampler_buffer_size * 2];
float buffer_out[resampler_buffer_size * 2];
short buffer_in[2][resampler_buffer_size * 2];
short buffer_out[resampler_buffer_size * 2];
} resampler;
void * resampler_create(void)
@ -75,7 +33,7 @@ void * resampler_create(void)
resampler * r = ( resampler * ) malloc( sizeof(resampler) );
if ( !r ) return 0;
r->write_pos = SINC_WIDTH - 1;
r->write_pos = 1;
r->write_filled = 0;
r->read_pos = 0;
r->read_filled = 0;
@ -129,12 +87,12 @@ int resampler_get_free_count(void *_r)
static int resampler_min_filled(resampler *r)
{
return SINC_WIDTH * 2;
return 4;
}
static int resampler_input_delay(resampler *r)
{
return SINC_WIDTH - 1;
return 1;
}
static int resampler_output_delay(resampler *r)
@ -151,7 +109,7 @@ int resampler_ready(void *_r)
void resampler_clear(void *_r)
{
resampler * r = ( resampler * ) _r;
r->write_pos = SINC_WIDTH - 1;
r->write_pos = 1;
r->write_filled = 0;
r->read_pos = 0;
r->read_filled = 0;
@ -163,7 +121,7 @@ void resampler_clear(void *_r)
void resampler_set_rate(void *_r, double new_factor)
{
resampler * r = ( resampler * ) _r;
r->phase_inc = new_factor;
r->phase_inc = new_factor * RESAMPLER_RESOLUTION;
}
void resampler_write_sample(void *_r, short ls, short rs)
@ -178,15 +136,11 @@ void resampler_write_sample(void *_r, short ls, short rs)
if ( r->write_filled < resampler_buffer_size )
{
float s32 = ls;
r->buffer_in[ 0 ][ r->write_pos ] = ls;
r->buffer_in[ 0 ][ r->write_pos + resampler_buffer_size ] = ls;
r->buffer_in[ 0 ][ r->write_pos ] = s32;
r->buffer_in[ 0 ][ r->write_pos + resampler_buffer_size ] = s32;
s32 = rs;
r->buffer_in[ 1 ][ r->write_pos ] = s32;
r->buffer_in[ 1 ][ r->write_pos + resampler_buffer_size ] = s32;
r->buffer_in[ 1 ][ r->write_pos ] = rs;
r->buffer_in[ 1 ][ r->write_pos + resampler_buffer_size ] = rs;
++r->write_filled;
@ -194,58 +148,49 @@ void resampler_write_sample(void *_r, short ls, short rs)
}
}
static int resampler_run_sinc(resampler * r, float ** out_, float * out_end)
static int resampler_run_cubic(resampler * r, short ** out_, short * out_end)
{
int in_size = r->write_filled;
int in_offset = resampler_buffer_size + r->write_pos - r->write_filled;
float const* inl_ = r->buffer_in[0] + in_offset;
float const* inr_ = r->buffer_in[1] + in_offset;
short const* inl_ = r->buffer_in[0] + in_offset;
short const* inr_ = r->buffer_in[1] + in_offset;
int used = 0;
in_size -= SINC_WIDTH * 2;
in_size -= 4;
if ( in_size > 0 )
{
float* out = *out_;
float const* inl = inl_;
float const* inr = inr_;
float const* const in_end = inl + in_size;
float phase = r->phase;
float phase_inc = r->phase_inc;
int step = phase_inc > 1.0f ? (int)(RESAMPLER_RESOLUTION / phase_inc * RESAMPLER_SINC_CUTOFF) : (int)(RESAMPLER_RESOLUTION * RESAMPLER_SINC_CUTOFF);
int window_step = RESAMPLER_RESOLUTION;
short* out = *out_;
short const* inl = inl_;
short const* inr = inr_;
short const* const in_end = inl + in_size;
int phase = r->phase;
int phase_inc = r->phase_inc;
do
{
float kernel[SINC_WIDTH * 2], kernel_sum = 0.0;
int i = SINC_WIDTH;
int phase_reduced = (int)(phase * RESAMPLER_RESOLUTION);
int phase_adj = phase_reduced * step / RESAMPLER_RESOLUTION;
float samplel, sampler;
int samplel, sampler;
if ( out >= out_end )
break;
for (; i >= -SINC_WIDTH + 1; --i)
{
int pos = i * step;
int window_pos = i * window_step;
kernel_sum += kernel[i + SINC_WIDTH - 1] = sinc_lut[abs(phase_adj - pos)] * window_lut[abs(phase_reduced - window_pos)];
}
for (samplel = 0, sampler = 0, i = 0; i < SINC_WIDTH * 2; ++i)
{
samplel += inl[i] * kernel[i];
sampler += inr[i] * kernel[i];
}
kernel_sum = 1.0f / kernel_sum;
*out++ = (float)(samplel * kernel_sum);
*out++ = (float)(sampler * kernel_sum);
const int16_t* lut = RESAMPLE_LUT + ((phase & 0xfc00) >> 8);
samplel = ((inl[0] * lut[0]) >> 15) + ((inl[1] * lut[1]) >> 15)
+ ((inl[2] * lut[2]) >> 15) + ((inl[3] * lut[3]) >> 15);
sampler = ((inr[0] * lut[0]) >> 15) + ((inr[1] * lut[1]) >> 15)
+ ((inr[2] * lut[2]) >> 15) + ((inr[3] * lut[3]) >> 15);
if ((samplel + 0x8000) & 0xffff0000) samplel = 0x7fff ^ (samplel >> 31);
if ((sampler + 0x8000) & 0xffff0000) sampler = 0x7fff ^ (sampler >> 31);
*out++ = (short)samplel;
*out++ = (short)sampler;
phase += phase_inc;
inl += (int)phase;
inr += (int)phase;
inl += (phase >> 16);
inr += (phase >> 16);
phase = fmod(phase, 1.0f);
phase &= 0xFFFF;
}
while ( inl < in_end );
@ -268,10 +213,10 @@ static void resampler_fill(resampler * r)
{
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 * 2;
short * out = r->buffer_out + write_pos * 2;
if ( write_size > ( resampler_buffer_size - r->read_filled ) )
write_size = resampler_buffer_size - r->read_filled;
resampler_run_sinc( r, &out, out + write_size * 2 );
resampler_run_cubic( r, &out, out + write_size * 2 );
r->read_filled += ( out - r->buffer_out - write_pos * 2 ) / 2;
}
}
@ -299,7 +244,7 @@ int resampler_get_sample_count(void *_r)
void resampler_get_sample(void *_r, short * ls, short * rs)
{
resampler * r = ( resampler * ) _r;
if ( r->read_filled < 1 && r->phase_inc)
if ( r->read_filled < 1 && r->phase_inc )
resampler_fill_and_remove_delay( r );
if ( r->read_filled < 1 )
{
@ -308,12 +253,8 @@ void resampler_get_sample(void *_r, short * ls, short * rs)
}
else
{
int samplel = (int)r->buffer_out[ r->read_pos * 2 + 0 ];
int sampler = (int)r->buffer_out[ r->read_pos * 2 + 1 ];
if ( ( samplel + 0x8000 ) & 0xffff0000 ) samplel = ( samplel >> 31 ) ^ 0x7fff;
if ( ( sampler + 0x8000 ) & 0xffff0000 ) sampler = ( sampler >> 31 ) ^ 0x7fff;
*ls = (short)samplel;
*rs = (short)sampler;
*ls = r->buffer_out[ r->read_pos * 2 + 0 ];
*rs = r->buffer_out[ r->read_pos * 2 + 1 ];
}
}

View File

@ -1,11 +1,9 @@
#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)
@ -23,8 +21,6 @@
#define resampler_remove_sample EVALUATE(RESAMPLER_DECORATE,_resampler_remove_sample)
#endif
void resampler_init(void);
void * resampler_create(void);
void resampler_delete(void *);
void * resampler_dup(const void *);

View File

@ -55,8 +55,6 @@ void usf_clear(void * state)
USF_STATE->EmptySpace[offset / 4] = (uint32_t)((offset << 16) | offset);
}
resampler_init();
USF_STATE->resampler = resampler_create();
#ifdef DEBUG_INFO

View File

@ -1001,8 +1001,6 @@ static int usf_info(void * context, const char * name, const char * value)
}
else if ( type == 0x21 )
{
int32_t samplerate;
struct usf_loader_state state;
memset( &state, 0, sizeof(state) );
@ -1022,15 +1020,6 @@ static int usf_info(void * context, const char * name, const char * value)
usf_set_compare( state.emu_state, state.enablecompare );
usf_set_fifo_full( state.emu_state, state.enablefifofull );
const char * err;
if ( (err = usf_render( state.emu_state, 0, 0, &samplerate ) ) != 0 )
{
fprintf(stderr, "%s\n", err);
return NO;
}
sampleRate = samplerate;
usfRemoveSilence = YES;
silence_seconds = 10;
@ -1200,13 +1189,10 @@ static int usf_info(void * context, const char * name, const char * value)
silence_test_buffer.resize( sampleRate * silence_seconds * 2 );
if ( type != 0x21 )
{
if (![self fillBuffer])
return NO;
if (![self fillBuffer])
return NO;
silence_test_buffer.remove_leading_silence();
}
silence_test_buffer.remove_leading_silence();
return YES;
}
@ -1248,8 +1234,6 @@ static int usf_info(void * context, const char * name, const char * value)
if ( type == 2 )
sampleRate = 48000;
else if ( type == 0x21 )
[self initializeDecoder];
tagLengthMs = info.tag_length_ms;
tagFadeMs = info.tag_fade_ms;
@ -1312,16 +1296,12 @@ static int usf_info(void * context, const char * name, const char * value)
}
else if ( type == 0x21 )
{
int32_t samplerate;
const char * err;
if ( (err = usf_render( emulatorCore, (int16_t*) buf, frames, &samplerate )) != 0 )
if ( (err = usf_render_resampled( emulatorCore, (int16_t*) buf, frames, sampleRate )) != 0 )
{
fprintf(stderr, "%s\n", err);
return 0;
}
sampleRate = samplerate;
}
else if ( type == 0x22 )
{
@ -1543,12 +1523,11 @@ static int usf_info(void * context, const char * name, const char * value)
}
else if ( type == 0x21 )
{
int16_t temp[2048];
do
{
ssize_t howmany = frame - framesRead;
if (howmany > 1024) howmany = 1024;
if ( usf_render(emulatorCore, temp, howmany, NULL) != 0 )
if ( usf_render_resampled(emulatorCore, NULL, howmany, sampleRate) != 0 )
return -1;
framesRead += howmany;
} while (framesRead < frame);