comparison psg.c @ 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 252dfd29831d
children e6dc30231b83
comparison
equal deleted inserted replaced
962:f52cb02a1466 963:bd549b25c362
31 //audio thread could still be using this 31 //audio thread could still be using this
32 free(context->back_buffer); 32 free(context->back_buffer);
33 free(context); 33 free(context);
34 } 34 }
35 35
36 #define BUFFER_INC_RES 1000000000UL 36 #define BUFFER_INC_RES 0x40000000UL
37 37
38 void psg_adjust_master_clock(psg_context * context, uint32_t master_clock) 38 void psg_adjust_master_clock(psg_context * context, uint32_t master_clock)
39 { 39 {
40 uint64_t old_inc = context->buffer_inc; 40 uint64_t old_inc = context->buffer_inc;
41 context->buffer_inc = ((BUFFER_INC_RES * (uint64_t)context->sample_rate) / (uint64_t)master_clock) * (uint64_t)context->clock_inc; 41 context->buffer_inc = ((BUFFER_INC_RES * (uint64_t)context->sample_rate) / (uint64_t)master_clock) * (uint64_t)context->clock_inc;
114 } 114 }
115 } 115 }
116 } 116 }
117 } 117 }
118 118
119 context->last_sample = context->accum;
120 context->accum = 0;
121
119 for (int i = 0; i < 3; i++) { 122 for (int i = 0; i < 3; i++) {
120 if (context->output_state[i]) { 123 if (context->output_state[i]) {
121 context->accum += volume_table[context->volume[i]]; 124 context->accum += volume_table[context->volume[i]];
122 } 125 }
123 } 126 }
127 context->sample_count++; 130 context->sample_count++;
128 131
129 context->buffer_fraction += context->buffer_inc; 132 context->buffer_fraction += context->buffer_inc;
130 if (context->buffer_fraction >= BUFFER_INC_RES) { 133 if (context->buffer_fraction >= BUFFER_INC_RES) {
131 context->buffer_fraction -= BUFFER_INC_RES; 134 context->buffer_fraction -= BUFFER_INC_RES;
135 uint32_t tmp = context->last_sample * (0x10000 - ((context->buffer_fraction << 16) / context->buffer_inc));
136 tmp += context->accum * ((context->buffer_fraction << 16) / context->buffer_inc);
137 printf("Last: %d, Cur: %d, Fraction: %d, Inc: %d, Result: %d, Samples: %d, Float: %f, Fixed: %X\n",
138 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));
132 139
133 context->audio_buffer[context->buffer_pos++] = context->accum / context->sample_count; 140 context->audio_buffer[context->buffer_pos++] = tmp >> 16;
134 context->accum = context->sample_count = 0; 141 context->sample_count = 0;
135 if (context->buffer_pos == context->samples_frame) { 142 if (context->buffer_pos == context->samples_frame) {
136 if (!headless) { 143 if (!headless) {
137 render_wait_psg(context); 144 render_wait_psg(context);
138 } 145 }
139 } 146 }