This commit is contained in:
2025-10-13 00:39:34 +02:00
parent e37f8e3dc8
commit 1bc5b75dba

View File

@@ -435,12 +435,12 @@ public:
GameboyApp* owner = nullptr; GameboyApp* owner = nullptr;
std::array<uint8_t, kRegisterCount> regs{}; std::array<uint8_t, kRegisterCount> regs{};
bool enabled = true; bool enabled = true;
mutable uint8_t squareAlternate = 0; mutable uint8_t squareAlternate = 0;
mutable uint8_t lastChannel = 0xFF; mutable uint8_t lastChannel = 0xFF;
mutable double filteredFreqHz = 0.0; mutable double filteredFreqHz = 0.0;
mutable std::array<uint16_t, 2> lastSquareRaw{}; mutable std::array<uint16_t, 2> lastSquareRaw{};
mutable std::array<uint8_t, 2> squareStable{}; mutable std::array<uint8_t, 2> squareStable{};
static constexpr bool inRange(uint16_t addr) { static constexpr bool inRange(uint16_t addr) {
return addr >= kBaseAddr && addr <= (kBaseAddr + static_cast<uint16_t>(kRegisterCount) - 1); return addr >= kBaseAddr && addr <= (kBaseAddr + static_cast<uint16_t>(kRegisterCount) - 1);
@@ -510,7 +510,7 @@ public:
// Track how stable each square channel's raw frequency is so we can bias selection. // Track how stable each square channel's raw frequency is so we can bias selection.
auto updateSquareHistory = [&](int idx, uint16_t raw) { auto updateSquareHistory = [&](int idx, uint16_t raw) {
if (raw == 0 || raw >= 2048U) { if (raw == 0 || raw >= 2048U) {
squareStable[static_cast<std::size_t>(idx)] = 0; squareStable[static_cast<std::size_t>(idx)] = 0;
lastSquareRaw[static_cast<std::size_t>(idx)] = 0; lastSquareRaw[static_cast<std::size_t>(idx)] = 0;
return; return;
} }
@@ -520,7 +520,7 @@ public:
squareStable[static_cast<std::size_t>(idx)] = static_cast<uint8_t>(current + 1); squareStable[static_cast<std::size_t>(idx)] = static_cast<uint8_t>(current + 1);
} else { } else {
lastSquareRaw[static_cast<std::size_t>(idx)] = raw; lastSquareRaw[static_cast<std::size_t>(idx)] = raw;
squareStable[static_cast<std::size_t>(idx)] = 0; squareStable[static_cast<std::size_t>(idx)] = 0;
} }
}; };
@@ -548,7 +548,7 @@ public:
updateSquareHistory(0, raw); updateSquareHistory(0, raw);
pushCandidate(freq, loud, 3, Channel::Square1); pushCandidate(freq, loud, 3, Channel::Square1);
} else { } else {
squareStable[0] = 0; squareStable[0] = 0;
lastSquareRaw[0] = 0; lastSquareRaw[0] = 0;
} }
} }
@@ -568,7 +568,7 @@ public:
updateSquareHistory(1, raw); updateSquareHistory(1, raw);
pushCandidate(freq, loud, 3, Channel::Square2); pushCandidate(freq, loud, 3, Channel::Square2);
} else { } else {
squareStable[1] = 0; squareStable[1] = 0;
lastSquareRaw[1] = 0; lastSquareRaw[1] = 0;
} }
} }
@@ -625,7 +625,7 @@ public:
for (int idx = 0; idx < 2; ++idx) { for (int idx = 0; idx < 2; ++idx) {
if (!squareActive[idx]) { if (!squareActive[idx]) {
squareStable[static_cast<std::size_t>(idx)] = 0; squareStable[static_cast<std::size_t>(idx)] = 0;
lastSquareRaw[static_cast<std::size_t>(idx)] = 0; lastSquareRaw[static_cast<std::size_t>(idx)] = 0;
} }
} }
@@ -691,9 +691,8 @@ public:
bestSquare = squareCandidates[1]; bestSquare = squareCandidates[1];
} }
if (!bestSquare) { if (!bestSquare) {
const Candidate* preferred = const Candidate* preferred = (squareAlternate & 1U) ? squareCandidates[1] : squareCandidates[0];
(squareAlternate & 1U) ? squareCandidates[1] : squareCandidates[0]; bestSquare = preferred;
bestSquare = preferred;
squareAlternate ^= 1U; squareAlternate ^= 1U;
} }
} }
@@ -705,7 +704,7 @@ public:
if (!best) if (!best)
best = bestOther; best = bestOther;
else if (bestOther) { else if (bestOther) {
int bestScore = static_cast<int>(best->loud); int bestScore = static_cast<int>(best->loud);
int otherScore = static_cast<int>(bestOther->loud); int otherScore = static_cast<int>(bestOther->loud);
if (waveBass) { if (waveBass) {
if (best->channel == Channel::Wave) if (best->channel == Channel::Wave)