changeset 838:9a5dc22297f2

Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
author Michael Pavone <pavone@retrodev.com>
date Thu, 29 Oct 2015 01:06:48 -0700
parents f2cd380adebe
children 4556818b6847
files psg.c psg.h
diffstat 2 files changed, 21 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/psg.c	Wed Oct 28 19:45:24 2015 -0700
+++ b/psg.c	Thu Oct 29 01:06:48 2015 -0700
@@ -8,6 +8,9 @@
 #include "blastem.h"
 #include <string.h>
 #include <stdlib.h>
+#include <stdio.h>
+
+FILE *blah;
 
 void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame)
 {
@@ -21,6 +24,7 @@
 	for (int i = 0; i < 4; i++) {
 		context->volume[i] = 0xF;
 	}
+	blah = fopen("psg.raw", "wb");
 }
 
 #define BUFFER_INC_RES 1000000000UL
@@ -105,20 +109,25 @@
 				}
 			}
 		}
+
+		for (int i = 0; i < 3; i++) {
+			if (context->output_state[i]) {
+				context->accum += volume_table[context->volume[i]];
+			}
+		}
+		if (context->noise_out) {
+			context->accum += volume_table[context->volume[3]];
+		}
+		context->sample_count++;
+
 		context->buffer_fraction += context->buffer_inc;
 		if (context->buffer_fraction >= BUFFER_INC_RES) {
 			context->buffer_fraction -= BUFFER_INC_RES;
-			int16_t acc = 0;
-			for (int i = 0; i < 3; i++) {
-				if (context->output_state[i]) {
-					acc += volume_table[context->volume[i]];
-				}
-			}
-			if (context->noise_out) {
-				acc += volume_table[context->volume[3]];
-			}
-			context->audio_buffer[context->buffer_pos++] = acc;
+
+			context->audio_buffer[context->buffer_pos++] = context->accum / context->sample_count;
+			context->accum = context->sample_count = 0;
 			if (context->buffer_pos == context->samples_frame) {
+				fwrite(context->audio_buffer, 1, context->buffer_pos, blah);
 				if (!headless) {
 					render_wait_psg(context);
 				}
--- a/psg.h	Wed Oct 28 19:45:24 2015 -0700
+++ b/psg.h	Thu Oct 29 01:06:48 2015 -0700
@@ -21,6 +21,8 @@
 	uint16_t lsfr;
 	uint16_t counter_load[4];
 	uint16_t counters[4];
+	int16_t  accum;
+	int16_t  sample_count;
 	uint8_t  volume[4];
 	uint8_t  output_state[4];
 	uint8_t  noise_out;