diff --git a/Core/apu.c b/Core/apu.c index 127d34c..147ea63 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -457,10 +457,9 @@ void GB_apu_run(GB_gameboy_t *gb) if (gb->apu_output.sample_rate) { gb->apu_output.cycles_since_render += cycles; - double cycles_per_sample = 2 * GB_get_clock_rate(gb) / (double)gb->apu_output.sample_rate; /* 2 * because we use 8MHz units */ - if (gb->apu_output.sample_cycles > cycles_per_sample) { - gb->apu_output.sample_cycles -= cycles_per_sample; + if (gb->apu_output.sample_cycles > gb->apu_output.cycles_per_sample) { + gb->apu_output.sample_cycles -= gb->apu_output.cycles_per_sample; render(gb, false, NULL); } } @@ -976,9 +975,17 @@ void GB_set_sample_rate(GB_gameboy_t *gb, unsigned int sample_rate) if (sample_rate) { gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate); } + GB_apu_update_cycles_per_sample(gb); } void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode) { gb->apu_output.highpass_mode = mode; } + +void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb) +{ + 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 */ + } +} diff --git a/Core/apu.h b/Core/apu.h index ab42055..bfa3598 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -140,6 +140,7 @@ typedef struct { volatile bool lock; double sample_cycles; // In 8 MHz units + double cycles_per_sample; // Samples are NOT normalized to MAX_CH_AMP * 4 at this stage! unsigned cycles_since_render; @@ -164,6 +165,7 @@ uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg); void GB_apu_div_event(GB_gameboy_t *gb); void GB_apu_init(GB_gameboy_t *gb); void GB_apu_run(GB_gameboy_t *gb); +void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb); #endif #endif /* apu_h */ diff --git a/Core/gb.c b/Core/gb.c index edc0a56..fc3bdc4 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -638,6 +638,8 @@ void GB_reset(GB_gameboy_t *gb) /* Todo: Ugly, fixme, see comment in the timer state machine */ gb->div_state = 3; + GB_apu_update_cycles_per_sample(gb); + gb->magic = (uintptr_t)'SAME'; } @@ -725,6 +727,7 @@ void *GB_get_direct_access(GB_gameboy_t *gb, GB_direct_access_t access, size_t * void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier) { gb->clock_multiplier = multiplier; + GB_apu_update_cycles_per_sample(gb); } uint32_t GB_get_clock_rate(GB_gameboy_t *gb)