Improved realtime audio implementation

This commit is contained in:
OpenSauce04
2024-11-27 20:48:04 +00:00
parent 5b87ee3397
commit 1ff0dd780f
3 changed files with 14 additions and 10 deletions

View File

@@ -22,6 +22,7 @@ using DoubleSecs = std::chrono::duration<double, std::chrono::seconds::period>;
using std::chrono::duration_cast;
using std::chrono::microseconds;
constexpr double FRAME_LENGTH = 1.0 / SCREEN_REFRESH_RATE;
// Purposefully ignore the first five frames, as there's a significant amount of overhead in
// booting that we shouldn't account for
constexpr std::size_t IgnoreFrames = 5;
@@ -65,6 +66,9 @@ void PerfStats::EndSystemFrame() {
accumulated_frametime += frame_time;
system_frames += 1;
// TODO: Track previous frame times in a less stupid way. -OS
previous_previous_frame_length = previous_frame_length;
previous_frame_length = frame_end - previous_frame_end;
previous_frame_end = frame_end;
}
@@ -125,19 +129,17 @@ PerfStats::Results PerfStats::GetLastStats() {
double PerfStats::GetLastFrameTimeScale() const {
std::scoped_lock lock{object_mutex};
constexpr double FRAME_LENGTH = 1.0 / SCREEN_REFRESH_RATE;
return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;
}
double PerfStats::GetStableFrameTimeScale() const {
std::scoped_lock lock{object_mutex};
constexpr double FRAME_LENGTH_MILLIS = (1.0 / SCREEN_REFRESH_RATE) * 1000;
const size_t num_frames = std::min<size_t>(50UL, current_index + 1);
const double sum = std::accumulate(perf_history.begin() + current_index - num_frames,
perf_history.begin() + current_index, 0.0);
const double stable_frame_length = sum / num_frames;
return stable_frame_length / FRAME_LENGTH_MILLIS;
const double stable_previous_frame_length =
(duration_cast<DoubleSecs>(previous_frame_length).count() +
duration_cast<DoubleSecs>(previous_previous_frame_length).count()) /
2;
return stable_previous_frame_length / FRAME_LENGTH;
}
void FrameLimiter::WaitOnce() {

View File

@@ -85,7 +85,7 @@ public:
/**
* Has the same functionality as GetLastFrameTimeScale, but uses the mean frame time over the
* last 50 frames rather than only the frame time of the previous frame.
* last 2 frames rather than only the frame time of the previous frame.
*/
double GetStableFrameTimeScale() const;
@@ -137,6 +137,8 @@ private:
Clock::time_point frame_begin = reset_point;
/// Total visible duration (including frame-limiting, etc.) of the previous system frame
Clock::duration previous_frame_length = Clock::duration::zero();
/// Visible duration for the frame prior to previous_frame_length
Clock::duration previous_previous_frame_length = Clock::duration::zero();
/// Last recorded performance statistics.
Results last_stats;