Abolished slow double use

This commit is contained in:
Lior Halphon 2021-12-26 15:20:46 +02:00
parent c5f6be1e64
commit c53d99dbc4
4 changed files with 5 additions and 22 deletions

View File

@ -765,8 +765,9 @@ void GB_apu_run(GB_gameboy_t *gb)
if (gb->apu_output.sample_rate) { if (gb->apu_output.sample_rate) {
gb->apu_output.cycles_since_render += cycles; gb->apu_output.cycles_since_render += cycles;
if (gb->apu_output.sample_cycles >= gb->apu_output.cycles_per_sample) { uint32_t clock_rate = GB_get_clock_rate(gb) * 2;
gb->apu_output.sample_cycles -= gb->apu_output.cycles_per_sample; if (gb->apu_output.sample_cycles >= clock_rate) {
gb->apu_output.sample_cycles -= clock_rate;
render(gb); render(gb);
} }
} }
@ -1462,8 +1463,6 @@ void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate)
if (sample_rate) { if (sample_rate) {
gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate); gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate);
} }
gb->apu_output.rate_set_in_clocks = false;
GB_apu_update_cycles_per_sample(gb);
} }
void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, double cycles_per_sample) void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, double cycles_per_sample)
@ -1473,10 +1472,8 @@ void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, double cycles_per_sample)
GB_set_sample_rate(gb, 0); GB_set_sample_rate(gb, 0);
return; return;
} }
gb->apu_output.cycles_per_sample = cycles_per_sample;
gb->apu_output.sample_rate = GB_get_clock_rate(gb) / cycles_per_sample * 2; gb->apu_output.sample_rate = GB_get_clock_rate(gb) / cycles_per_sample * 2;
gb->apu_output.highpass_rate = pow(0.999958, cycles_per_sample); gb->apu_output.highpass_rate = pow(0.999958, cycles_per_sample);
gb->apu_output.rate_set_in_clocks = true;
} }
void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback) void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback)
@ -1489,14 +1486,6 @@ void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode)
gb->apu_output.highpass_mode = mode; gb->apu_output.highpass_mode = mode;
} }
void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb)
{
if (gb->apu_output.rate_set_in_clocks) return;
if (gb->apu_output.sample_rate) {
gb->apu_output.cycles_per_sample = 2 * GB_get_clock_rate(gb) / (double)gb->apu_output.sample_rate; /* 2 * because we use 8MHz units */
}
}
void GB_set_interference_volume(GB_gameboy_t *gb, double volume) void GB_set_interference_volume(GB_gameboy_t *gb, double volume)
{ {
gb->apu_output.interference_volume = volume; gb->apu_output.interference_volume = volume;

View File

@ -143,8 +143,7 @@ typedef enum {
typedef struct { typedef struct {
unsigned sample_rate; unsigned sample_rate;
double sample_cycles; // In 8 MHz units unsigned sample_cycles; // Counts by sample_rate until it reaches the clock frequency
double cycles_per_sample;
// Samples are NOT normalized to MAX_CH_AMP * 4 at this stage! // Samples are NOT normalized to MAX_CH_AMP * 4 at this stage!
unsigned cycles_since_render; unsigned cycles_since_render;
@ -159,7 +158,6 @@ typedef struct {
GB_sample_callback_t sample_callback; GB_sample_callback_t sample_callback;
bool rate_set_in_clocks;
double interference_volume; double interference_volume;
double interference_highpass; double interference_highpass;
} GB_apu_output_t; } GB_apu_output_t;
@ -178,7 +176,6 @@ internal void GB_apu_div_event(GB_gameboy_t *gb);
internal void GB_apu_div_secondary_event(GB_gameboy_t *gb); internal void GB_apu_div_secondary_event(GB_gameboy_t *gb);
internal void GB_apu_init(GB_gameboy_t *gb); internal void GB_apu_init(GB_gameboy_t *gb);
internal void GB_apu_run(GB_gameboy_t *gb); internal void GB_apu_run(GB_gameboy_t *gb);
internal void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb);
#endif #endif
#endif /* apu_h */ #endif /* apu_h */

View File

@ -1653,8 +1653,6 @@ void GB_reset(GB_gameboy_t *gb)
GB_set_internal_div_counter(gb, 8); GB_set_internal_div_counter(gb, 8);
GB_apu_update_cycles_per_sample(gb);
if (gb->nontrivial_jump_state) { if (gb->nontrivial_jump_state) {
free(gb->nontrivial_jump_state); free(gb->nontrivial_jump_state);
gb->nontrivial_jump_state = NULL; gb->nontrivial_jump_state = NULL;
@ -1758,7 +1756,6 @@ GB_registers_t *GB_get_registers(GB_gameboy_t *gb)
void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier) void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier)
{ {
gb->clock_multiplier = multiplier; gb->clock_multiplier = multiplier;
GB_apu_update_cycles_per_sample(gb);
} }
uint32_t GB_get_clock_rate(GB_gameboy_t *gb) uint32_t GB_get_clock_rate(GB_gameboy_t *gb)

View File

@ -423,7 +423,7 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles)
gb->double_speed_alignment += cycles; gb->double_speed_alignment += cycles;
} }
gb->hdma_cycles += cycles; gb->hdma_cycles += cycles;
gb->apu_output.sample_cycles += cycles; gb->apu_output.sample_cycles += cycles * gb->apu_output.sample_rate;
gb->cycles_since_last_sync += cycles; gb->cycles_since_last_sync += cycles;
gb->cycles_since_run += cycles; gb->cycles_since_run += cycles;