summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--psg.c13
-rw-r--r--psg.h1
2 files changed, 11 insertions, 3 deletions
diff --git a/psg.c b/psg.c
index a33aa7c..8d43190 100644
--- a/psg.c
+++ b/psg.c
@@ -33,7 +33,7 @@ void psg_free(psg_context *context)
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 @@ void psg_run(psg_context * context, uint32_t cycles)
}
}
+ 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 @@ void psg_run(psg_context * context, uint32_t cycles)
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 --git a/psg.h b/psg.h
index 03768aa..ca3f1f0 100644
--- a/psg.h
+++ b/psg.h
@@ -22,6 +22,7 @@ typedef struct {
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];