changeset 963:bd549b25c362

Not so successful attempt at improved PSG resampling
author Michael Pavone <pavone@retrodev.com>
date Tue, 19 Apr 2016 00:38:44 -0700
parents f52cb02a1466
children e6dc30231b83
files psg.c psg.h
diffstat 2 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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);
--- 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];