From bca53d29a58408dd9de2265ed08a271b55063627 Mon Sep 17 00:00:00 2001 From: Thomas Martin <304581853+boubou666@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:59:08 +0100 Subject: [PATCH] Stall sound held to the host's time, as a frame's is In real time a stall's sound (the watcher's, while no frame ends: a load) was the engine brought up to the EE clock, its disc waits counted where the PC had none: The Third Age's first movie after the title gave about 1.3 s of extra sound in a second (bursts up to 8613 samples), and the output queue dropped 1.2 s of it. stallSound now goes through the same budget as frameSound (holdToHostTime): 52K samples in that second, the queue at most 82 ms, nothing dropped. Co-Authored-By: Claude Opus 5.5 --- runtime/host/free_host.cpp | 42 +++++++++++++++------------ runtime/include/bozo/host/free_host.h | 3 ++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/runtime/host/free_host.cpp b/runtime/host/free_host.cpp index 4236c82..75f69fe 100644 --- a/runtime/host/free_host.cpp +++ b/runtime/host/free_host.cpp @@ -516,26 +516,30 @@ namespace bozo::host // where it was made. The budget is kept within a few fields, so neither a stall nor a run of short frames // stores up a burst. if (opt_.realTime) + n = holdToHostTime(n); + if (onAudio && n) + onAudio(stats_.frames, audio_.data(), n); + } + + size_t FreeHost::holdToHostTime(size_t n) + { + const int64_t now = steadyNs(); + if (lastSoundNs_ != 0) { - const int64_t now = steadyNs(); - if (lastSoundNs_ != 0) + soundBudget_ += static_cast(now - lastSoundNs_) * 48e-6; + soundBudget_ = std::clamp(soundBudget_, -double(kFieldFrames), 3.0 * kFieldFrames); + const double room = soundBudget_ + kFieldFrames / 4.0; + const size_t allowed = room > 0.0 ? static_cast(room) : 0u; + if (n > allowed) { - soundBudget_ += static_cast(now - lastSoundNs_) * 48e-6; - soundBudget_ = std::clamp(soundBudget_, -double(kFieldFrames), 3.0 * kFieldFrames); - const double room = soundBudget_ + kFieldFrames / 4.0; - const size_t allowed = room > 0.0 ? static_cast(room) : 0u; - if (n > allowed) - { - std::memmove(audio_.data(), audio_.data() + 2u * (n - allowed), 2u * allowed * sizeof(int16_t)); - leapFrames_ += n - allowed; - n = allowed; - } - soundBudget_ -= static_cast(n); + std::memmove(audio_.data(), audio_.data() + 2u * (n - allowed), 2u * allowed * sizeof(int16_t)); + leapFrames_ += n - allowed; + n = allowed; } - lastSoundNs_ = now; + soundBudget_ -= static_cast(n); } - if (onAudio && n) - onAudio(stats_.frames, audio_.data(), n); + lastSoundNs_ = now; + return n; } namespace @@ -558,8 +562,10 @@ namespace bozo::host if (!mixer || !onAudio) return; mixer->setPortStream(&libs_.portStream); audio_.resize(2u * audio::Mixer::kQueueFrames); - const size_t n = mixer->take(audio_.data(), audio::Mixer::kQueueFrames); - soundBudget_ -= static_cast(n); // the host's time it fills (frameSound) + // Held to the host's time as a frame's sound is: brought up to the EE clock, a stall during a load (its disc + // waits counted, the PC's not) gave the leap's sound in a few bursts, up to 8613 samples each, and the output + // queue dropped about 1.2 s of it (The Third Age's first movie after the title). + const size_t n = holdToHostTime(mixer->take(audio_.data(), audio::Mixer::kQueueFrames)); if (n) onAudio(stats_.frames, audio_.data(), n); } diff --git a/runtime/include/bozo/host/free_host.h b/runtime/include/bozo/host/free_host.h index ad3a3cb..8374474 100644 --- a/runtime/include/bozo/host/free_host.h +++ b/runtime/include/bozo/host/free_host.h @@ -159,6 +159,9 @@ namespace bozo::host uint64_t lastStallAdvance_ = 0; // advanceLibrariesWhileStalled: the EE cycle of the last advance void endFrame(R5900Context *ctx); void frameSound(uint64_t &hash, uint32_t &count); + // Real time: the n samples in audio_ held to the host's time since the last output (the budget below), the + // newest kept; returns how many are given. + size_t holdToHostTime(size_t n); std::vector audio_; // A field's sound at the mixer's 48 kHz (60 fields a second), and the samples skipped after EE clock leaps in // real time (frameSound).