Further accuracy improvements to the audio envelope

This commit is contained in:
Lior Halphon 2021-02-22 01:58:43 +02:00
parent 6d2d88648e
commit d50fdc52ea
1 changed files with 44 additions and 46 deletions

View File

@ -333,7 +333,6 @@ static void tick_square_envelope(GB_gameboy_t *gb, enum GB_CHANNELS index)
{ {
uint8_t nrx2 = gb->io_registers[index == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22]; uint8_t nrx2 = gb->io_registers[index == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22];
if (!gb->apu.square_channels[index].volume_countdown || !--gb->apu.square_channels[index].volume_countdown) {
gb->apu.square_channels[index].volume_countdown = nrx2 & 7; gb->apu.square_channels[index].volume_countdown = nrx2 & 7;
if (!(nrx2 & 7)) return; if (!(nrx2 & 7)) return;
if (gb->cgb_double_speed) { if (gb->cgb_double_speed) {
@ -358,14 +357,11 @@ static void tick_square_envelope(GB_gameboy_t *gb, enum GB_CHANNELS index)
update_square_sample(gb, index); update_square_sample(gb, index);
} }
} }
}
static void tick_noise_envelope(GB_gameboy_t *gb) static void tick_noise_envelope(GB_gameboy_t *gb)
{ {
uint8_t nr42 = gb->io_registers[GB_IO_NR42]; uint8_t nr42 = gb->io_registers[GB_IO_NR42];
if (!gb->apu.noise_channel.volume_countdown || !--gb->apu.noise_channel.volume_countdown) {
gb->apu.noise_channel.volume_countdown = nr42 & 7;
if (!(nr42 & 7)) return; if (!(nr42 & 7)) return;
if (gb->cgb_double_speed) { if (gb->cgb_double_speed) {
@ -387,7 +383,6 @@ static void tick_noise_envelope(GB_gameboy_t *gb)
0); 0);
} }
} }
}
static void trigger_sweep_calculation(GB_gameboy_t *gb) static void trigger_sweep_calculation(GB_gameboy_t *gb)
{ {
@ -428,28 +423,31 @@ void GB_apu_div_event(GB_gameboy_t *gb)
} }
if ((gb->apu.div_divider & 1) == 0) { if ((gb->apu.div_divider & 1) == 0) {
for (unsigned i = GB_SQUARE_2 + 1; i--;) { UNROLL for (unsigned i = GB_SQUARE_2 + 1; i--;) {
uint8_t nrx2 = gb->io_registers[i == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22]; uint8_t nrx2 = gb->io_registers[i == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22];
if (gb->apu.is_active[i] && gb->apu.square_channels[i].volume_countdown == 0 && (nrx2 & 7)) { if (gb->apu.is_active[i] && gb->apu.square_channels[i].volume_countdown == 0) {
tick_square_envelope(gb, i); tick_square_envelope(gb, i);
gb->apu.square_channels[i].volume_countdown = nrx2 & 7;
} }
} }
if (gb->apu.is_active[GB_NOISE] && gb->apu.noise_channel.volume_countdown == 0 && (gb->io_registers[GB_IO_NR42] & 7)) { if (gb->apu.is_active[GB_NOISE] && gb->apu.noise_channel.volume_countdown == 0) {
tick_noise_envelope(gb); tick_noise_envelope(gb);
gb->apu.noise_channel.volume_countdown = gb->io_registers[GB_IO_NR42] & 7;
} }
} }
if ((gb->apu.div_divider & 7) == 0) { if ((gb->apu.div_divider & 7) == 7) {
for (unsigned i = GB_SQUARE_2 + 1; i--;) { UNROLL for (unsigned i = GB_SQUARE_2 + 1; i--;) {
tick_square_envelope(gb, i); gb->apu.square_channels[i].volume_countdown--;
gb->apu.square_channels[i].volume_countdown &= 7;
} }
gb->apu.noise_channel.volume_countdown--;
tick_noise_envelope(gb); gb->apu.noise_channel.volume_countdown &= 7;
} }
if ((gb->apu.div_divider & 1) == 1) { if ((gb->apu.div_divider & 1) == 1) {
for (unsigned i = GB_SQUARE_2 + 1; i--;) { UNROLL for (unsigned i = GB_SQUARE_2 + 1; i--;) {
if (gb->apu.square_channels[i].length_enabled) { if (gb->apu.square_channels[i].length_enabled) {
if (gb->apu.square_channels[i].pulse_length) { if (gb->apu.square_channels[i].pulse_length) {
if (!--gb->apu.square_channels[i].pulse_length) { if (!--gb->apu.square_channels[i].pulse_length) {