# HG changeset patch # User Michael Pavone # Date 1461051524 25200 # Node ID bd549b25c362f3f0b076917cf9403f5a35653976 # Parent f52cb02a14665d0968788e9d11c032f4419efc0c Not so successful attempt at improved PSG resampling diff -r f52cb02a1466 -r bd549b25c362 psg.c --- a/psg.c Mon Apr 18 19:26:47 2016 -0700 +++ b/psg.c Tue Apr 19 00:38:44 2016 -0700 @@ -33,7 +33,7 @@ free(context); } -#define BUFFER_INC_RES 1000000000UL +#define BUFFER_INC_RES 0x40000000UL void psg_adjust_master_clock(psg_context * context, uint32_t master_clock) { @@ -116,6 +116,9 @@ } } + context->last_sample = context->accum; + context->accum = 0; + for (int i = 0; i < 3; i++) { if (context->output_state[i]) { context->accum += volume_table[context->volume[i]]; @@ -129,9 +132,13 @@ context->buffer_fraction += context->buffer_inc; if (context->buffer_fraction >= BUFFER_INC_RES) { context->buffer_fraction -= BUFFER_INC_RES; + uint32_t tmp = context->last_sample * (0x10000 - ((context->buffer_fraction << 16) / context->buffer_inc)); + tmp += context->accum * ((context->buffer_fraction << 16) / context->buffer_inc); + printf("Last: %d, Cur: %d, Fraction: %d, Inc: %d, Result: %d, Samples: %d, Float: %f, Fixed: %X\n", + context->last_sample, context->accum, (int)context->buffer_fraction, (int)context->buffer_inc, tmp >> 16, context->sample_count, (float)context->buffer_fraction/(float)context->buffer_inc, (int)((context->buffer_fraction << 16) / context->buffer_inc)); - context->audio_buffer[context->buffer_pos++] = context->accum / context->sample_count; - context->accum = context->sample_count = 0; + context->audio_buffer[context->buffer_pos++] = tmp >> 16; + context->sample_count = 0; if (context->buffer_pos == context->samples_frame) { if (!headless) { render_wait_psg(context); diff -r f52cb02a1466 -r bd549b25c362 psg.h --- a/psg.h Mon Apr 18 19:26:47 2016 -0700 +++ b/psg.h Tue Apr 19 00:38:44 2016 -0700 @@ -22,6 +22,7 @@ uint16_t counter_load[4]; uint16_t counters[4]; int16_t accum; + int16_t last_sample; int16_t sample_count; uint8_t volume[4]; uint8_t output_state[4];