changeset 1002:8d032a368dd5

Made low pass filter frequency configurable
author Michael Pavone <pavone@retrodev.com>
date Sun, 01 May 2016 13:36:14 -0700
parents 1dc749c9c0d9
children 534f522a1162
files blastem.c default.cfg psg.c psg.h vgmplay.c ym2612.c ym2612.h
diffstat 7 files changed, 18 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/blastem.c	Sat Apr 30 20:57:29 2016 -0700
+++ b/blastem.c	Sun May 01 13:36:14 2016 -0700
@@ -32,6 +32,7 @@
 #define MCLKS_PER_Z80 15
 #define MCLKS_PER_PSG (MCLKS_PER_Z80*16)
 #define DEFAULT_SYNC_INTERVAL MCLKS_LINE
+#define DEFAULT_LOWPASS_CUTOFF 3390
 
 //TODO: Figure out the exact value for this
 #define LINES_NTSC 262
@@ -898,11 +899,14 @@
 	char * config_cycles = tern_find_path(config, "clocks\0max_cycles\0").ptrval;
 	gen->max_cycles = config_cycles ? atoi(config_cycles) : DEFAULT_SYNC_INTERVAL;
 
+	char * lowpass_cutoff_str = tern_find_path(config, "audio\0lowpass_cutoff\0").ptrval;
+	uint32_t lowpass_cutoff = lowpass_cutoff_str ? atoi(lowpass_cutoff_str) : DEFAULT_LOWPASS_CUTOFF;
+	
 	gen->ym = malloc(sizeof(ym2612_context));
-	ym_init(gen->ym, render_sample_rate(), gen->master_clock, MCLKS_PER_YM, render_audio_buffer(), ym_opts);
+	ym_init(gen->ym, render_sample_rate(), gen->master_clock, MCLKS_PER_YM, render_audio_buffer(), ym_opts, lowpass_cutoff);
 
 	gen->psg = malloc(sizeof(psg_context));
-	psg_init(gen->psg, render_sample_rate(), gen->master_clock, MCLKS_PER_PSG, render_audio_buffer());
+	psg_init(gen->psg, render_sample_rate(), gen->master_clock, MCLKS_PER_PSG, render_audio_buffer(), lowpass_cutoff);
 
 	gen->z80 = calloc(1, sizeof(z80_context));
 #ifndef NO_Z80
--- a/default.cfg	Sat Apr 30 20:57:29 2016 -0700
+++ b/default.cfg	Sun May 01 13:36:14 2016 -0700
@@ -113,6 +113,7 @@
 audio {
 	rate 48000
 	buffer 512
+	lowpass_cutoff 3390
 }
 
 clocks {
--- a/psg.c	Sat Apr 30 20:57:29 2016 -0700
+++ b/psg.c	Sun May 01 13:36:14 2016 -0700
@@ -10,9 +10,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <math.h>
-#define LOWPASS_CUTOFF 3390
-
-void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame)
+void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame, uint32_t lowpass_cutoff)
 {
 	memset(context, 0, sizeof(*context));
 	context->audio_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame);
@@ -20,7 +18,7 @@
 	context->clock_inc = clock_div;
 	context->sample_rate = sample_rate;
 	context->samples_frame = samples_frame;
-	double rc = (1.0 / (double)LOWPASS_CUTOFF) / (2.0 * M_PI);
+	double rc = (1.0 / (double)lowpass_cutoff) / (2.0 * M_PI);
 	double dt = 1.0 / ((double)master_clock / (double)clock_div);
 	double alpha = dt / (dt + rc);
 	context->lowpass_alpha = (int32_t)(((double)0x10000) * alpha);
--- a/psg.h	Sat Apr 30 20:57:29 2016 -0700
+++ b/psg.h	Sun May 01 13:36:14 2016 -0700
@@ -33,7 +33,7 @@
 } psg_context;
 
 
-void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame);
+void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame, uint32_t lowpass_cutoff);
 void psg_free(psg_context *context);
 void psg_adjust_master_clock(psg_context * context, uint32_t master_clock);
 void psg_write(psg_context * context, uint8_t value);
--- a/vgmplay.c	Sat Apr 30 20:57:29 2016 -0700
+++ b/vgmplay.c	Sun May 01 13:36:14 2016 -0700
@@ -95,12 +95,15 @@
 	if (argc >= 3 && !strcmp(argv[2], "-y")) {
 		opts |= YM_OPT_WAVE_LOG;
 	}
+	
+	char * lowpass_cutoff_str = tern_find_path(config, "audio\0lowpass_cutoff\0").ptrval;
+	uint32_t lowpass_cutoff = lowpass_cutoff_str ? atoi(lowpass_cutoff_str) : 3390;
 
 	ym2612_context y_context;
-	ym_init(&y_context, render_sample_rate(), MCLKS_NTSC, MCLKS_PER_YM, render_audio_buffer(), opts);
+	ym_init(&y_context, render_sample_rate(), MCLKS_NTSC, MCLKS_PER_YM, render_audio_buffer(), opts, lowpass_cutoff);
 
 	psg_context p_context;
-	psg_init(&p_context, render_sample_rate(), MCLKS_NTSC, MCLKS_PER_PSG, render_audio_buffer());
+	psg_init(&p_context, render_sample_rate(), MCLKS_NTSC, MCLKS_PER_PSG, render_audio_buffer(), lowpass_cutoff);
 
 	FILE * f = fopen(argv[1], "rb");
 	vgm_header header;
--- a/ym2612.c	Sat Apr 30 20:57:29 2016 -0700
+++ b/ym2612.c	Sun May 01 13:36:14 2016 -0700
@@ -128,9 +128,7 @@
 #define log2(x) (log(x)/log(2))
 #endif
 
-#define LOWPASS_CUTOFF 3390
-
-void ym_init(ym2612_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t sample_limit, uint32_t options)
+void ym_init(ym2612_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t sample_limit, uint32_t options, uint32_t lowpass_cutoff)
 {
 	static uint8_t registered_finalize;
 	dfopen(debug_file, "ym_debug.txt", "w");
@@ -141,7 +139,7 @@
 	context->clock_inc = clock_div * 6;
 	ym_adjust_master_clock(context, master_clock);
 	
-	double rc = (1.0 / (double)LOWPASS_CUTOFF) / (2.0 * M_PI);
+	double rc = (1.0 / (double)lowpass_cutoff) / (2.0 * M_PI);
 	double dt = 1.0 / ((double)master_clock / (double)(context->clock_inc * NUM_OPERATORS));
 	double alpha = dt / (dt + rc);
 	context->lowpass_alpha = (int32_t)(((double)0x10000) * alpha);
--- a/ym2612.h	Sat Apr 30 20:57:29 2016 -0700
+++ b/ym2612.h	Sun May 01 13:36:14 2016 -0700
@@ -125,7 +125,7 @@
 	REG_LR_AMS_PMS   = 0xB4
 };
 
-void ym_init(ym2612_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t sample_limit, uint32_t options);
+void ym_init(ym2612_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t sample_limit, uint32_t options, uint32_t lowpass_cutoff);
 void ym_free(ym2612_context *context);
 void ym_adjust_master_clock(ym2612_context * context, uint32_t master_clock);
 void ym_run(ym2612_context * context, uint32_t to_cycle);