comparison psg.c @ 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 6a14c5a95648
children 715475788e93
comparison
equal deleted inserted replaced
837:f2cd380adebe 838:9a5dc22297f2
6 #include "psg.h" 6 #include "psg.h"
7 #include "render.h" 7 #include "render.h"
8 #include "blastem.h" 8 #include "blastem.h"
9 #include <string.h> 9 #include <string.h>
10 #include <stdlib.h> 10 #include <stdlib.h>
11 #include <stdio.h>
12
13 FILE *blah;
11 14
12 void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame) 15 void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame)
13 { 16 {
14 memset(context, 0, sizeof(*context)); 17 memset(context, 0, sizeof(*context));
15 context->audio_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame); 18 context->audio_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame);
19 context->samples_frame = samples_frame; 22 context->samples_frame = samples_frame;
20 psg_adjust_master_clock(context, master_clock); 23 psg_adjust_master_clock(context, master_clock);
21 for (int i = 0; i < 4; i++) { 24 for (int i = 0; i < 4; i++) {
22 context->volume[i] = 0xF; 25 context->volume[i] = 0xF;
23 } 26 }
27 blah = fopen("psg.raw", "wb");
24 } 28 }
25 29
26 #define BUFFER_INC_RES 1000000000UL 30 #define BUFFER_INC_RES 1000000000UL
27 31
28 void psg_adjust_master_clock(psg_context * context, uint32_t master_clock) 32 void psg_adjust_master_clock(psg_context * context, uint32_t master_clock)
103 } 107 }
104 } 108 }
105 } 109 }
106 } 110 }
107 } 111 }
112
113 for (int i = 0; i < 3; i++) {
114 if (context->output_state[i]) {
115 context->accum += volume_table[context->volume[i]];
116 }
117 }
118 if (context->noise_out) {
119 context->accum += volume_table[context->volume[3]];
120 }
121 context->sample_count++;
122
108 context->buffer_fraction += context->buffer_inc; 123 context->buffer_fraction += context->buffer_inc;
109 if (context->buffer_fraction >= BUFFER_INC_RES) { 124 if (context->buffer_fraction >= BUFFER_INC_RES) {
110 context->buffer_fraction -= BUFFER_INC_RES; 125 context->buffer_fraction -= BUFFER_INC_RES;
111 int16_t acc = 0; 126
112 for (int i = 0; i < 3; i++) { 127 context->audio_buffer[context->buffer_pos++] = context->accum / context->sample_count;
113 if (context->output_state[i]) { 128 context->accum = context->sample_count = 0;
114 acc += volume_table[context->volume[i]];
115 }
116 }
117 if (context->noise_out) {
118 acc += volume_table[context->volume[3]];
119 }
120 context->audio_buffer[context->buffer_pos++] = acc;
121 if (context->buffer_pos == context->samples_frame) { 129 if (context->buffer_pos == context->samples_frame) {
130 fwrite(context->audio_buffer, 1, context->buffer_pos, blah);
122 if (!headless) { 131 if (!headless) {
123 render_wait_psg(context); 132 render_wait_psg(context);
124 } 133 }
125 } 134 }
126 } 135 }