SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 8a234a25a3c34cf98581152c474ec62711861605 parent 5b883465370bc7ad5a3576d26e7cee2ec553d3a0 Author: Lior Halphon <LIJI32@gmail.com> Date: Mon, 18 Aug 2025 21:27:01 +0300 Add silence detection to eliminate rounding artifacts when playing nothing but frequencies above Nyquist. Fixes #394 Diffstat:
| M | Core/apu.c | 24 | ++++++++++++++++++++++++ |
| M | Core/apu.h | 2 | ++ |
2 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/Core/apu.c b/Core/apu.c @@ -103,6 +103,30 @@ static void band_limited_read(GB_band_limited_t *band_limited, GB_sample_t *outp output->left = band_limited->output.left * multiplier / GB_BAND_LIMITED_ONE; output->right = band_limited->output.right * multiplier / GB_BAND_LIMITED_ONE; + + /* This hueristic will mute the channel if it's only playing an amplitude of 1 or 2 units, usually + caused by rounding errors when the channel is playing a single frequency above Nyquist. */ + + unsigned diff = abs(output->left - band_limited->last_output.left); + if (diff > 4) { + band_limited->silence_detection = 0; + band_limited->last_output.packed = output->packed; + return; + } + + diff = abs(output->right - band_limited->last_output.right); + if (diff > 4) { + band_limited->silence_detection = 0; + band_limited->last_output.packed = output->packed; + return; + } + + if (band_limited->silence_detection == 4000) { + output->packed = band_limited->last_output.packed; + } + else { + band_limited->silence_detection++; + } } static inline uint32_t sample_fraction_multiply(GB_gameboy_t *gb, unsigned multiplier) diff --git a/Core/apu.h b/Core/apu.h @@ -173,6 +173,8 @@ typedef struct { } buffer[GB_BAND_LIMITED_WIDTH * 2], output; uint8_t pos; GB_sample_t input; + GB_sample_t last_output; + unsigned silence_detection; } GB_band_limited_t; typedef struct {
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.