Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions runtime/host/free_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(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<size_t>(room) : 0u;
if (n > allowed)
{
soundBudget_ += static_cast<double>(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<size_t>(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<double>(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<double>(n);
}
if (onAudio && n)
onAudio(stats_.frames, audio_.data(), n);
lastSoundNs_ = now;
return n;
}

namespace
Expand All @@ -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<double>(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);
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/include/bozo/host/free_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int16_t> 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).
Expand Down
Loading