diff --git a/src/CMakeData-actor.cmake b/src/CMakeData-actor.cmake
index 697c371..cf60160 100644
--- a/src/CMakeData-actor.cmake
+++ b/src/CMakeData-actor.cmake
@@ -66,6 +66,8 @@ list(APPEND SMDATA_ACTOR_GAMEPLAY_SRC
"NoteField.cpp"
"PercentageDisplay.cpp"
"Player.cpp"
+ "TimingRecorder.cpp"
+ "TimingSelfTest.cpp"
"ReceptorArrow.cpp"
"ReceptorArrowRow.cpp"
"ScoreDisplay.cpp"
diff --git a/src/Player.cpp b/src/Player.cpp
index f8b5e70..5b4182a 100644
--- a/src/Player.cpp
+++ b/src/Player.cpp
@@ -1,4 +1,5 @@
#include "Player.h"
+#include "TimingRecorder.h"
#include <algorithm>
#include <climits>
@@ -2218,6 +2219,33 @@ void Player::PlayKeysound(const TapNote& tn, TapNoteScore score) {
}
}
+// The flight recorder's line for a judged step (TimingRecorder).
+static void RecordStep(const PlayerState* ps, int col, int row, int iSearchRow, bool bRowGiven,
+ bool bHeld, bool bRelease, const RageTimer& tm,
+ float fStepMusicSeconds, float fNoteSeconds, float fOffset,
+ TapNoteScore tns) {
+ if (!TimingRecorder::Enabled()) {
+ return;
+ }
+ TimingRecorder::StepRecord r;
+ r.pn = ps->m_PlayerNumber;
+ r.iCol = col;
+ r.iRow = row;
+ r.iSearchRow = iSearchRow;
+ r.bRowGiven = bRowGiven;
+ r.bHeld = bHeld;
+ r.bRelease = bRelease;
+ r.iInputUs = TimingRecorder::Microseconds(tm);
+ r.iNowUs = TimingRecorder::Microseconds(RageTimer());
+ r.iUpdateUs = TimingRecorder::Microseconds(ps->m_Position.m_LastBeatUpdate);
+ r.fMusicSecondsAtUpdate = ps->m_Position.m_fMusicSeconds;
+ r.fStepMusicSeconds = fStepMusicSeconds;
+ r.fNoteSeconds = fNoteSeconds;
+ r.fOffset = fOffset;
+ r.tns = tns;
+ TimingRecorder::Step(r);
+}
+
void Player::Step(
int col, int row, const RageTimer& tm, bool bHeld, bool bRelease) {
if (IsOniDead()) {
@@ -2428,6 +2456,7 @@ void Player::Step(
// we need this later if we are autosyncing
const float fStepBeat = NoteRowToBeat(iRowOfOverlappingNoteOrRow);
const float fStepSeconds = m_Timing->GetElapsedTimeFromBeat(fStepBeat);
+ float fStepMusicSeconds = fStepSeconds;
if (row == -1) {
// We actually stepped on the note this long ago:
@@ -2446,6 +2475,7 @@ void Player::Step(
fCurrentMusicSeconds -
fTimeSinceStep * GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate;
+ fStepMusicSeconds = fMusicSeconds;
// The offset from the actual step in seconds:
fNoteOffset = (fStepSeconds - fMusicSeconds) /
GAMESTATE->m_SongOptions.GetCurrent()
@@ -2766,6 +2796,8 @@ void Player::Step(
}
m_LastTapNoteScore = score;
+ RecordStep(m_pPlayerState, col, iRowOfOverlappingNoteOrRow, iSongRow, row != -1, bHeld,
+ bRelease, tm, fStepMusicSeconds, fStepSeconds, fNoteOffset, score);
if (GAMESTATE->GetCurrentGame()->m_bCountNotesSeparately) {
if (pTN->type != TapNoteType_Mine) {
const bool bBlind =
@@ -2790,6 +2822,9 @@ void Player::Step(
m_NoteData, iRowOfOverlappingNoteOrRow)) {
FlashGhostRow(iRowOfOverlappingNoteOrRow);
}
+ } else if (!bRelease) {
+ RecordStep(m_pPlayerState, col, -1, iSongRow, row != -1, bHeld, bRelease, tm, 0, 0, 0,
+ TNS_None);
}
if (score == TNS_None) {
@@ -2894,6 +2929,11 @@ void Player::UpdateTapNotesMissedOlderThan(float fMissIfOlderThanSeconds) {
} else {
tn.result.tns = TNS_Miss;
}
+ TimingRecorder::Miss(
+ m_pPlayerState->m_PlayerNumber, iter.Track(), iter.Row(),
+ m_Timing->GetElapsedTimeFromBeat(NoteRowToBeat(iter.Row())),
+ m_pPlayerState->m_Position.m_fMusicSeconds,
+ TimingRecorder::Microseconds(RageTimer()));
}
}
}
diff --git a/src/ScreenGameplay.cpp b/src/ScreenGameplay.cpp
index 54da385..1936518 100644
--- a/src/ScreenGameplay.cpp
+++ b/src/ScreenGameplay.cpp
@@ -78,6 +78,9 @@
#include "ScreenSaveSync.h"
#include "ScreenWithMenuElements.h"
#include "Song.h"
+#include "StepMania.h"
+#include "TimingRecorder.h"
+#include "TimingSelfTest.h"
#include "SongManager.h"
#include "SongOptions.h"
#include "SongUtil.h"
@@ -394,6 +397,26 @@ ScreenGameplay::ScreenGameplay() {
}
void ScreenGameplay::Init() {
+ // --smytest=timing: the timing self-test plays this stage (TimingSelfTest).
+ std::string sTest;
+ if (GetCommandlineArgument("smytest", &sTest) && sTest == "timing") {
+ std::string sOffsets, sRate, sStutter;
+ GetCommandlineArgument("timingoffsets", &sOffsets);
+ GetCommandlineArgument("timingrate", &sRate);
+ GetCommandlineArgument("timingstutter", &sStutter);
+ std::vector<std::string> vsOffsets;
+ split(sOffsets, ",", vsOffsets);
+ std::vector<float> vOffsets;
+ for (const std::string& s : vsOffsets) {
+ vOffsets.push_back(StringToFloat(s));
+ }
+ if (!sRate.empty()) {
+ SongOptions so = GAMESTATE->m_SongOptions.GetPreferred();
+ so.m_fMusicRate = StringToFloat(sRate);
+ GAMESTATE->m_SongOptions.Assign(ModsLevel_Preferred, so);
+ }
+ TimingSelfTest::Start(vOffsets, sStutter.empty() ? 0.0f : StringToFloat(sStutter));
+ }
SubscribeToMessage("Judgment");
PLAYER_TYPE.Load(m_sName, "PlayerType");
@@ -1088,6 +1111,13 @@ ScreenGameplay::~ScreenGameplay() {
}
LOG->Trace("ScreenGameplay::~ScreenGameplay()");
+ if (TimingRecorder::Enabled()) {
+ const Song* pSong = GAMESTATE->m_pCurSong;
+ TimingRecorder::Flush(ssprintf(
+ "%s, rate %.2f", pSong != nullptr ? pSong->GetDisplayFullTitle().c_str() : "no song",
+ GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate));
+ }
+ TimingSelfTest::Stop();
const Style* pMasterStyle =
GAMESTATE->GetCurrentStyle(GAMESTATE->GetMasterPlayerNumber());
@@ -1730,6 +1760,7 @@ void ScreenGameplay::UpdateSongPosition(float fDeltaTime) {
RageTimer tm;
const float fSeconds = m_pSoundMusic->GetPositionSeconds(&tm);
+ TimingRecorder::PositionUpdate(TimingRecorder::Microseconds(tm), fSeconds);
GAMESTATE->UpdateSongPosition(
fSeconds, GAMESTATE->m_pCurSong->m_SongTiming, tm);
}
@@ -1829,6 +1860,14 @@ void ScreenGameplay::Update(float fDeltaTime) {
}
UpdateSongPosition(fDeltaTime);
+ if (TimingSelfTest::Active() && m_pSoundMusic->IsPlaying() &&
+ GAMESTATE->m_pCurSteps[PLAYER_1] != nullptr) {
+ RageTimer tm;
+ const float fSeconds = m_pSoundMusic->GetPositionSeconds(&tm);
+ TimingSelfTest::Update(PLAYER_1, m_vPlayerInfo[PLAYER_1].m_pPlayer->GetNoteData(),
+ *GAMESTATE->m_pCurSteps[PLAYER_1]->GetTimingData(), fSeconds, tm,
+ GAMESTATE->m_SongOptions.GetCurrent().m_fMusicRate);
+ }
if (m_bZeroDeltaOnNextUpdate) {
Screen::Update(0);
diff --git a/src/TimingRecorder.cpp b/src/TimingRecorder.cpp
new file mode 100644
index 0000000..c8f188a
--- /dev/null
+++ b/src/TimingRecorder.cpp
@@ -0,0 +1,422 @@
+#include "global.h"
+
+#include "TimingRecorder.h"
+
+#include <algorithm>
+#include <cmath>
+#include <ctime>
+#include <map>
+#include <utility>
+#include <vector>
+
+#include "Preference.h"
+#include "RageFile.h"
+#include "RageLog.h"
+#include "RageTimer.h"
+#include "RageUtil.h"
+
+static Preference<bool> g_bTimingRecorder("TimingRecorder", false);
+
+namespace {
+enum Kind { Kind_Position, Kind_Step, Kind_Miss };
+
+struct Record {
+ Kind kind = Kind_Position;
+ int64_t iUs = 0;
+ float fMusicSeconds = 0; // position updates and misses
+ TimingRecorder::StepRecord step;
+};
+
+struct Intent {
+ int iRow;
+ float fOffset;
+};
+
+// Everything runs on the game thread: Player and ScreenGameplay record, and
+// ScreenGameplay flushes.
+bool g_bForced = false;
+std::vector<Record> g_Records;
+std::vector<TimingRecorder::FrameRecord> g_Frames;
+std::map<std::pair<int, int64_t>, Intent> g_Intents;
+int64_t g_iDropped = 0;
+const size_t kMaxRecords = 500000;
+const size_t kMaxFrames = 500000;
+
+void Add(const Record& r) {
+ if (g_Records.size() >= kMaxRecords) {
+ ++g_iDropped;
+ return;
+ }
+ g_Records.push_back(r);
+}
+
+const Intent* FindIntent(int iCol, int64_t iInputUs) {
+ auto it = g_Intents.find(std::make_pair(iCol, iInputUs));
+ return it == g_Intents.end() ? nullptr : &it->second;
+}
+
+struct Stats {
+ int64_t n = 0;
+ double fSum = 0;
+ double fSumSquares = 0;
+ double fWorst = 0;
+ void Add(double f) {
+ ++n;
+ fSum += f;
+ fSumSquares += f * f;
+ if (std::abs(f) > std::abs(fWorst)) {
+ fWorst = f;
+ }
+ }
+ double Mean() const { return n != 0 ? fSum / n : 0; }
+ double Deviation() const {
+ if (n < 2) {
+ return 0;
+ }
+ const double fMean = Mean();
+ return std::sqrt(std::max(0.0, fSumSquares / n - fMean * fMean));
+ }
+};
+
+std::string Counts(const std::map<TapNoteScore, int>& counts) {
+ std::vector<std::string> parts;
+ for (const auto& count : counts) {
+ parts.push_back(ssprintf("%s %d", TapNoteScoreToString(count.first).c_str(), count.second));
+ }
+ return parts.empty() ? std::string("none") : join(", ", parts);
+}
+
+// A frame more than half a refresh late missed the refresh it was drawn for.
+void WriteFrames(const char* szTime, const std::string& sWhat) {
+ if (g_Frames.empty()) {
+ return;
+ }
+ const std::string sPath = ssprintf("/Logs/frames-%s.csv", szTime);
+ RageFile f;
+ const bool bFile = f.Open(sPath, RageFile::WRITE);
+ if (bFile) {
+ f.PutLine("# " + sWhat);
+ f.PutLine("update_us,update_ms,draw_ms,swap_ms,interval_ms,draw_calls,verts,texture_uploads,"
+ "depth_clears,depth_clears_skipped,depth_layers,refresh_hz,position_us,"
+ "predicted_us,lead_ms,shown_us");
+ }
+ Stats update, draw, swap, interval, calls, clears, skipped, layers;
+ // From the song position's moment to the frame's showing, as drawn and as carried: to the
+ // display's refresh for the frames it's known for, else to the end of the swap.
+ Stats drawnAt, carriedTo, drawnToSwap, carriedToSwap;
+ // Frames whose refresh was predicted and is known, and those the prediction got right.
+ int iPredicted = 0;
+ int iPredictedRight = 0;
+ std::vector<double> intervals;
+ int iLate = 0;
+ int iMostCalls = 0;
+ int iUploads = 0;
+ int iRefreshHz = 0;
+ for (const TimingRecorder::FrameRecord& r : g_Frames) {
+ const double fUpdateMs = (r.iDrawBeganUs - r.iUpdateBeganUs) / 1000.0;
+ const double fDrawMs = (r.iSwapBeganUs - r.iDrawBeganUs) / 1000.0;
+ const double fSwapMs = (r.iSwapEndedUs - r.iSwapBeganUs) / 1000.0;
+ const double fIntervalMs =
+ r.iPreviousSwapEndedUs != 0 ? (r.iSwapEndedUs - r.iPreviousSwapEndedUs) / 1000.0 : 0;
+ update.Add(fUpdateMs);
+ draw.Add(fDrawMs);
+ swap.Add(fSwapMs);
+ calls.Add(r.iDrawCalls);
+ clears.Add(r.iDepthClears);
+ skipped.Add(r.iDepthClearsSkipped);
+ layers.Add(r.iDepthLayers);
+ if (r.iPositionUs != 0) {
+ const bool bShown = r.iShownUs != 0;
+ const double fLeadMs = ((bShown ? r.iShownUs : r.iSwapEndedUs) - r.iPositionUs) / 1000.0;
+ (bShown ? drawnAt : drawnToSwap).Add(fLeadMs);
+ (bShown ? carriedTo : carriedToSwap).Add(fLeadMs - r.fPresentLeadMs);
+ if (bShown && r.iPredictedPresentUs != 0 && r.iRefreshHz > 0) {
+ ++iPredicted;
+ iPredictedRight += std::abs(r.iPredictedPresentUs - r.iShownUs) < 500000 / r.iRefreshHz;
+ }
+ }
+ iMostCalls = std::max(iMostCalls, r.iDrawCalls);
+ iUploads += r.iTextureUploads;
+ iRefreshHz = r.iRefreshHz;
+ if (fIntervalMs > 0) {
+ interval.Add(fIntervalMs);
+ intervals.push_back(fIntervalMs);
+ if (r.iRefreshHz > 0 && fIntervalMs > 1500.0 / r.iRefreshHz) {
+ ++iLate;
+ }
+ }
+ if (bFile) {
+ f.PutLine(ssprintf("%lld,%.3f,%.3f,%.3f,%.3f,%d,%d,%d,%d,%d,%d,%d,%lld,%lld,%.3f,%lld",
+ (long long)r.iUpdateBeganUs, fUpdateMs, fDrawMs, fSwapMs, fIntervalMs,
+ r.iDrawCalls, r.iVerts, r.iTextureUploads, r.iDepthClears,
+ r.iDepthClearsSkipped, r.iDepthLayers, r.iRefreshHz,
+ (long long)r.iPositionUs, (long long)r.iPredictedPresentUs,
+ r.fPresentLeadMs, (long long)r.iShownUs));
+ }
+ }
+ if (bFile) {
+ f.Close();
+ }
+ double fPercentile99 = 0;
+ if (!intervals.empty()) {
+ const size_t k = std::min(intervals.size() - 1, size_t(intervals.size() * 0.99));
+ std::nth_element(intervals.begin(), intervals.begin() + k, intervals.end());
+ fPercentile99 = intervals[k];
+ }
+ LOG->Info(
+ "Frames (%s): %lld at %d Hz, %.2f ms apart on average (99th percentile %.2f, longest "
+ "%.2f), %d late by a refresh or more; update %.2f ms (longest %.2f), drawing %.2f ms "
+ "(longest %.2f), swap %.2f ms; %.0f draw calls a frame (most %d), %.1f depth clears "
+ "(%.1f skipped, nothing drawn into depth; %.1f layers instead), %d texture uploads; "
+ "written to %s%s",
+ sWhat.c_str(), (long long)g_Frames.size(), iRefreshHz, interval.Mean(), fPercentile99,
+ interval.fWorst, iLate, update.Mean(), update.fWorst, draw.Mean(), draw.fWorst, swap.Mean(),
+ calls.Mean(), iMostCalls, clears.Mean(), skipped.Mean(), layers.Mean(), iUploads,
+ sPath.c_str(), bFile ? "" : " (couldn't write it)");
+ const bool bShownKnown = drawnAt.n > 0;
+ if (!bShownKnown) {
+ drawnAt = drawnToSwap;
+ carriedTo = carriedToSwap;
+ }
+ if (drawnAt.n > 0) {
+ const std::string sTo =
+ !bShownKnown ? "end of the swap"
+ : drawnToSwap.n == 0
+ ? "display's refresh"
+ : ssprintf("display's refresh, known for %lld of %lld", (long long)drawnAt.n,
+ (long long)(drawnAt.n + drawnToSwap.n));
+ const std::string sPredicted =
+ iPredicted == 0 ? std::string()
+ : ssprintf("; the refresh predicted right for %.1f%%",
+ 100.0 * iPredictedRight / iPredicted);
+ LOG->Info(
+ "Frames (%s): shown %.2f ms after their song position on average (to the %s), %.2f ms "
+ "after the moment drawn for as carried to the expected showing; that varies by %.2f ms "
+ "frame to frame as drawn, %.2f ms as carried%s",
+ sWhat.c_str(), drawnAt.Mean(), sTo.c_str(), carriedTo.Mean(), drawnAt.Deviation(),
+ carriedTo.Deviation(), sPredicted.c_str());
+ }
+ g_Frames.clear();
+}
+} // namespace
+
+bool TimingRecorder::Enabled() { return g_bForced || g_bTimingRecorder.Get(); }
+
+void TimingRecorder::SetForced(bool bForced) { g_bForced = bForced; }
+
+int64_t TimingRecorder::Microseconds(const RageTimer& timer) {
+ return int64_t(timer.m_secs) * 1000000 + int64_t(timer.m_us);
+}
+
+void TimingRecorder::PositionUpdate(int64_t iTimestampUs, float fMusicSeconds) {
+ if (!Enabled()) {
+ return;
+ }
+ Record r;
+ r.kind = Kind_Position;
+ r.iUs = iTimestampUs;
+ r.fMusicSeconds = fMusicSeconds;
+ Add(r);
+}
+
+void TimingRecorder::Step(const StepRecord& step) {
+ if (!Enabled()) {
+ return;
+ }
+ Record r;
+ r.kind = Kind_Step;
+ r.iUs = step.iNowUs;
+ r.step = step;
+ Add(r);
+}
+
+void TimingRecorder::Miss(PlayerNumber pn, int iCol, int iRow, float fNoteSeconds,
+ float fMusicSeconds, int64_t iNowUs) {
+ if (!Enabled()) {
+ return;
+ }
+ Record r;
+ r.kind = Kind_Miss;
+ r.iUs = iNowUs;
+ r.fMusicSeconds = fMusicSeconds;
+ r.step.pn = pn;
+ r.step.iCol = iCol;
+ r.step.iRow = iRow;
+ r.step.fNoteSeconds = fNoteSeconds;
+ r.step.tns = TNS_Miss;
+ Add(r);
+}
+
+void TimingRecorder::Intended(int iCol, int64_t iInputUs, int iRow, float fOffset) {
+ if (!Enabled()) {
+ return;
+ }
+ g_Intents[std::make_pair(iCol, iInputUs)] = Intent{iRow, fOffset};
+}
+
+void TimingRecorder::Frame(const FrameRecord& r) {
+ if (!Enabled()) {
+ return;
+ }
+ if (g_Frames.size() >= kMaxFrames) {
+ ++g_iDropped;
+ return;
+ }
+ g_Frames.push_back(r);
+}
+
+void TimingRecorder::FrameShown(int64_t iSwap, int64_t iShownUs) {
+ // The display says within a few frames.
+ const size_t iLook = std::min<size_t>(g_Frames.size(), 16);
+ for (size_t i = 0; i < iLook; ++i) {
+ FrameRecord& r = g_Frames[g_Frames.size() - 1 - i];
+ if (r.iSwap == iSwap) {
+ r.iShownUs = iShownUs;
+ return;
+ }
+ }
+}
+
+void TimingRecorder::Flush(const std::string& sWhat) {
+ char szTime[32];
+ const time_t now = time(nullptr);
+ strftime(szTime, sizeof(szTime), "%Y%m%d-%H%M%S", localtime(&now));
+ WriteFrames(szTime, sWhat);
+ if (g_Records.empty()) {
+ g_Intents.clear();
+ return;
+ }
+
+ const std::string sPath = ssprintf("/Logs/timing-%s.csv", szTime);
+
+ Stats offsets; // human steps that found a note, ms late
+ Stats errors; // self-test steps against what they meant, ms
+ Stats updateIntervals; // between position updates, ms
+ std::map<TapNoteScore, int> counts;
+ std::map<int, std::map<TapNoteScore, int>> countsByIntent; // by meant offset, 0.1 ms
+ int iIntended = 0;
+ int iWrongNote = 0;
+ int iNoNote = 0;
+ int iMissed = 0;
+ int64_t iLastUpdateUs = 0;
+
+ RageFile f;
+ const bool bFile = f.Open(sPath, RageFile::WRITE);
+ if (bFile) {
+ f.PutLine("# " + sWhat);
+ f.PutLine(
+ "kind,us,player,col,row,search_row,row_given,held,release,input_us,update_us,"
+ "music_at_update,step_music,note_seconds,offset_ms,tns,intended_row,intended_ms,"
+ "error_ms");
+ }
+ for (const Record& r : g_Records) {
+ switch (r.kind) {
+ case Kind_Position:
+ if (iLastUpdateUs != 0) {
+ updateIntervals.Add((r.iUs - iLastUpdateUs) / 1000.0);
+ }
+ iLastUpdateUs = r.iUs;
+ if (bFile) {
+ f.PutLine(ssprintf("position,%lld,,,,,,,,,,%.6f,,,,,,,", (long long)r.iUs,
+ r.fMusicSeconds));
+ }
+ break;
+ case Kind_Miss:
+ ++iMissed;
+ ++counts[TNS_Miss];
+ if (bFile) {
+ f.PutLine(ssprintf("miss,%lld,%d,%d,%d,,,,,,,%.6f,,%.6f,,Miss,,,", (long long)r.iUs,
+ int(r.step.pn) + 1, r.step.iCol, r.step.iRow, r.fMusicSeconds,
+ r.step.fNoteSeconds));
+ }
+ break;
+ case Kind_Step: {
+ const TimingRecorder::StepRecord& s = r.step;
+ const double fLateMs = -s.fOffset * 1000.0;
+ const Intent* pIntent = FindIntent(s.iCol, s.iInputUs);
+ std::string sIntent = ",,";
+ if (pIntent != nullptr) {
+ ++iIntended;
+ const double fMeantMs = pIntent->fOffset * 1000.0;
+ if (s.iRow == -1) {
+ ++iNoNote;
+ } else if (s.iRow != pIntent->iRow) {
+ ++iWrongNote;
+ } else {
+ errors.Add(fLateMs - fMeantMs);
+ }
+ ++countsByIntent[int(std::lround(fMeantMs * 10))][s.tns];
+ sIntent = ssprintf("%d,%.3f,%.3f", pIntent->iRow, fMeantMs,
+ s.iRow == pIntent->iRow ? fLateMs - fMeantMs : 0.0);
+ }
+ if (s.iRow != -1 && !s.bRowGiven && !s.bRelease && s.tns >= TNS_W5 &&
+ s.tns <= TNS_W1) {
+ offsets.Add(fLateMs);
+ ++counts[s.tns];
+ }
+ if (bFile) {
+ f.PutLine(ssprintf(
+ "step,%lld,%d,%d,%d,%d,%d,%d,%d,%lld,%lld,%.6f,%.6f,%.6f,%.3f,%s,%s",
+ (long long)r.iUs, int(s.pn) + 1, s.iCol, s.iRow, s.iSearchRow, int(s.bRowGiven),
+ int(s.bHeld), int(s.bRelease), (long long)s.iInputUs, (long long)s.iUpdateUs,
+ s.fMusicSecondsAtUpdate, s.fStepMusicSeconds, s.fNoteSeconds, fLateMs,
+ TapNoteScoreToString(s.tns).c_str(), sIntent.c_str()));
+ }
+ break;
+ }
+ }
+ }
+ if (bFile) {
+ f.Close();
+ }
+
+ LOG->Info(
+ "Timing recorder (%s): %lld steps judged against a note (%s), %+.2f ms late on average, "
+ "%.2f ms deviation; position updates %.2f ms apart (longest %.2f); written to %s%s",
+ sWhat.c_str(), (long long)offsets.n, Counts(counts).c_str(), offsets.Mean(),
+ offsets.Deviation(), updateIntervals.Mean(), updateIntervals.fWorst, sPath.c_str(),
+ bFile ? "" : " (couldn't write it)");
+ if (iIntended > 0) {
+ std::vector<std::string> byIntent;
+ for (const auto& entry : countsByIntent) {
+ byIntent.push_back(ssprintf("%+.1f ms: %s", entry.first / 10.0, Counts(entry.second).c_str()));
+ }
+ LOG->Info(
+ "Timing self-test: %d steps; %d judged against another note, %d against none; error "
+ "against what was meant %+.3f ms on average, %.3f ms deviation, worst %+.3f ms; %s",
+ iIntended, iWrongNote, iNoNote, errors.Mean(), errors.Deviation(), errors.fWorst,
+ join("; ", byIntent).c_str());
+ }
+ if (g_iDropped > 0) {
+ LOG->Warn("Timing recorder: %lld records didn't fit", (long long)g_iDropped);
+ }
+
+ g_Records.clear();
+ g_Intents.clear();
+ g_iDropped = 0;
+}
+
+/*
+ * (c) 2026 ITGmania contributors
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
diff --git a/src/TimingRecorder.h b/src/TimingRecorder.h
new file mode 100644
index 0000000..c1318dc
--- /dev/null
+++ b/src/TimingRecorder.h
@@ -0,0 +1,112 @@
+/* TimingRecorder - A flight recorder for judgment timing.
+ *
+ * Every song position update and every judged step or miss, with the clocks
+ * that went into it, kept during a stage and written to
+ * Logs/timing-<time>.csv when the stage ends, with a summary in the log; the
+ * stage's frames (FrameClock) go to Logs/frames-<time>.csv. On with the
+ * TimingRecorder preference; the timing self-test (TimingSelfTest) turns it on
+ * too. The columns are explained in Docs/StepManiaY/TIMING.md. */
+
+#ifndef TIMING_RECORDER_H
+#define TIMING_RECORDER_H
+
+#include <cstdint>
+#include <string>
+
+#include "GameConstantsAndTypes.h"
+#include "PlayerNumber.h"
+
+class RageTimer;
+
+namespace TimingRecorder {
+bool Enabled();
+void SetForced(bool bForced);
+
+int64_t Microseconds(const RageTimer& timer);
+
+void PositionUpdate(int64_t iTimestampUs, float fMusicSeconds);
+
+struct StepRecord {
+ PlayerNumber pn = PLAYER_INVALID;
+ int iCol = -1;
+ int iRow = -1; // the note judged; -1 when the step found none
+ int iSearchRow = -1; // where the step looked for one
+ bool bRowGiven = false; // judged as the row was crossed (autoplay, holds held through)
+ bool bHeld = false;
+ bool bRelease = false;
+ int64_t iInputUs = 0; // the step's own timestamp
+ int64_t iNowUs = 0; // when it was judged
+ int64_t iUpdateUs = 0; // the position update it was judged against
+ float fMusicSecondsAtUpdate = 0;
+ float fStepMusicSeconds = 0; // where in the song the step was
+ float fNoteSeconds = 0;
+ float fOffset = 0; // seconds, early positive (as Player computes it)
+ TapNoteScore tns = TNS_None;
+};
+void Step(const StepRecord& r);
+void Miss(PlayerNumber pn, int iCol, int iRow, float fNoteSeconds,
+ float fMusicSeconds, int64_t iNowUs);
+
+// A step the self-test made: meant for this note, this far from it (seconds,
+// late positive). Steps carrying the same timestamp are compared with it.
+void Intended(int iCol, int64_t iInputUs, int iRow, float fOffset);
+
+// A frame while a song played: when its update, drawing and swap began, when
+// the swap (and the frame before's) ended, and what it drew.
+struct FrameRecord {
+ int64_t iUpdateBeganUs = 0;
+ int64_t iDrawBeganUs = 0;
+ int64_t iSwapBeganUs = 0;
+ int64_t iSwapEndedUs = 0;
+ int64_t iPreviousSwapEndedUs = 0;
+ int iDrawCalls = 0;
+ int iVerts = 0;
+ int iTextureUploads = 0;
+ int iDepthClears = 0;
+ int iDepthClearsSkipped = 0;
+ int iDepthLayers = 0;
+ int iRefreshHz = 0;
+ // The song position's moment, the refresh the frame was expected at, how far
+ // what it drew was carried for that (ms), and the refresh it was shown at
+ // where the display's refreshes are known (else 0).
+ int64_t iPositionUs = 0;
+ int64_t iPredictedPresentUs = 0;
+ float fPresentLeadMs = 0;
+ int64_t iShownUs = 0;
+ // The window's count of swaps at this frame's, where the display says later when it showed them.
+ int64_t iSwap = 0;
+};
+void Frame(const FrameRecord& r);
+// The display showed the frame of that swap at that refresh.
+void FrameShown(int64_t iSwap, int64_t iShownUs);
+
+// Writes what was recorded since the last flush and starts over.
+void Flush(const std::string& sWhat);
+} // namespace TimingRecorder
+
+#endif // TIMING_RECORDER_H
+
+/*
+ * (c) 2026 ITGmania contributors
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
diff --git a/src/TimingSelfTest.cpp b/src/TimingSelfTest.cpp
new file mode 100644
index 0000000..bfd746e
--- /dev/null
+++ b/src/TimingSelfTest.cpp
@@ -0,0 +1,211 @@
+#include "global.h"
+
+#include "TimingSelfTest.h"
+
+#include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <thread>
+#include <vector>
+
+#include "GameState.h"
+#include "InputFilter.h"
+#include "InputMapper.h"
+#include "NoteData.h"
+#include "NoteTypes.h"
+#include "RageInputDevice.h"
+#include "RageLog.h"
+#include "RageTimer.h"
+#include "Style.h"
+#include "TimingData.h"
+#include "TimingRecorder.h"
+#include "arch/ArchHooks/ArchHooks.h"
+
+namespace {
+struct Event {
+ int64_t iUs;
+ int iCol;
+ bool bPress;
+};
+
+bool g_bActive = false;
+std::vector<float> g_vOffsetsMs;
+float g_fStutterMs = 0;
+int64_t g_iLastStutterUs = 0;
+size_t g_iNextOffset = 0;
+int g_iNextRow = 0;
+std::vector<Event> g_Pending; // releases and roll re-presses, by time
+int g_iSkipped = 0;
+
+const int64_t kTapLengthUs = 30000;
+const int64_t kRollRepressUs = 120000;
+const int64_t kRollPressLengthUs = 40000;
+
+RageTimer TimerAt(int64_t iUs) {
+ return RageTimer(uint64_t(iUs / 1000000), uint64_t(iUs % 1000000));
+}
+
+bool DeviceFor(PlayerNumber pn, int iCol, DeviceInput& di) {
+ std::vector<GameInput> GameI;
+ GAMESTATE->GetCurrentStyle(pn)->StyleInputToGameInput(iCol, pn, GameI);
+ for (const GameInput& gi : GameI) {
+ for (int slot = 0; slot < NUM_GAME_TO_DEVICE_SLOTS; ++slot) {
+ if (INPUTMAPPER->GameToDevice(gi, slot, di) && di.IsValid()) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+void Send(PlayerNumber pn, int iCol, bool bPress, int64_t iUs) {
+ DeviceInput di;
+ if (!DeviceFor(pn, iCol, di)) {
+ return;
+ }
+ INPUTFILTER->ButtonPressed(DeviceInput(di.device, di.button, bPress ? 1.0f : 0.0f, TimerAt(iUs)));
+}
+
+void Schedule(int64_t iUs, int iCol, bool bPress) {
+ g_Pending.push_back(Event{iUs, iCol, bPress});
+}
+
+// When the next note in this column starts, or a long way off.
+int64_t NextInColumnUs(const NoteData& nd, const TimingData& timing, int iCol, int iRow,
+ int64_t iPositionUs, float fMusicSeconds, float fMusicRate) {
+ int iNext = iRow;
+ if (!nd.GetNextTapNoteRowForTrack(iCol, iNext)) {
+ return INT64_MAX;
+ }
+ const float fSeconds = timing.GetElapsedTimeFromBeat(NoteRowToBeat(iNext));
+ return iPositionUs + int64_t((fSeconds - fMusicSeconds) / fMusicRate * 1e6);
+}
+} // namespace
+
+bool TimingSelfTest::Active() { return g_bActive; }
+
+void TimingSelfTest::Start(const std::vector<float>& vOffsetsMs, float fStutterMs) {
+ g_bActive = true;
+ g_vOffsetsMs = vOffsetsMs.empty() ? std::vector<float>{0.0f} : vOffsetsMs;
+ g_fStutterMs = fStutterMs;
+ g_iLastStutterUs = 0;
+ g_iNextOffset = 0;
+ g_iNextRow = 0;
+ g_Pending.clear();
+ g_iSkipped = 0;
+ TimingRecorder::SetForced(true);
+}
+
+void TimingSelfTest::Stop() {
+ if (g_bActive && g_iSkipped > 0) {
+ LOG->Info("Timing self-test: left %d mines, lifts and fakes alone", g_iSkipped);
+ }
+ g_bActive = false;
+ g_Pending.clear();
+}
+
+void TimingSelfTest::Update(PlayerNumber pn, const NoteData& nd, const TimingData& timing,
+ float fMusicSeconds, const RageTimer& positionTime,
+ float fMusicRate) {
+ if (!g_bActive || fMusicRate <= 0) {
+ return;
+ }
+ const int64_t iNowUs = ArchHooks::GetSystemTimeInMicroseconds();
+ const int64_t iPositionUs = TimingRecorder::Microseconds(positionTime);
+ auto SystemUs = [&](float fSeconds) {
+ return iPositionUs + int64_t(std::llround((fSeconds - fMusicSeconds) / fMusicRate * 1e6));
+ };
+
+ // Rows whose moment (with this row's offset) has come: their panels are
+ // pressed, stamped with that moment.
+ for (int iRow = g_iNextRow - 1; nd.GetNextTapNoteRowForAllTracks(iRow);) {
+ if (!timing.IsJudgableAtRow(iRow)) {
+ g_iNextRow = iRow + 1;
+ continue;
+ }
+ const float fOffsetMs = g_vOffsetsMs[g_iNextOffset % g_vOffsetsMs.size()];
+ const float fNoteSeconds = timing.GetElapsedTimeFromBeat(NoteRowToBeat(iRow));
+ const int64_t iPressUs = SystemUs(fNoteSeconds) + int64_t(std::llround(fOffsetMs * 1000.0));
+ if (iPressUs > iNowUs) {
+ break;
+ }
+ for (int iCol = 0; iCol < nd.GetNumTracks(); ++iCol) {
+ const TapNote& tn = nd.GetTapNote(iCol, iRow);
+ switch (tn.type) {
+ case TapNoteType_Tap:
+ case TapNoteType_HoldHead:
+ break;
+ case TapNoteType_Empty:
+ continue;
+ default:
+ ++g_iSkipped;
+ continue;
+ }
+ TimingRecorder::Intended(iCol, iPressUs, iRow, fOffsetMs / 1000.0f);
+ Schedule(iPressUs, iCol, true);
+ if (tn.type == TapNoteType_HoldHead) {
+ const float fEndSeconds =
+ timing.GetElapsedTimeFromBeat(NoteRowToBeat(iRow + tn.iDuration));
+ const int64_t iEndUs = SystemUs(fEndSeconds) + kTapLengthUs;
+ if (tn.subType == TapNoteSubType_Roll) {
+ for (int64_t iUs = iPressUs + kRollRepressUs; iUs < iEndUs; iUs += kRollRepressUs) {
+ Schedule(iUs - kRollRepressUs + kRollPressLengthUs, iCol, false);
+ Schedule(iUs, iCol, true);
+ }
+ }
+ Schedule(iEndUs, iCol, false);
+ } else {
+ // Let go before the next note in the column, however close.
+ const int64_t iNextUs =
+ NextInColumnUs(nd, timing, iCol, iRow, iPositionUs, fMusicSeconds, fMusicRate);
+ const int64_t iGapUs = iNextUs == INT64_MAX ? kTapLengthUs : (iNextUs - iPressUs) / 2;
+ Schedule(iPressUs + std::max<int64_t>(1000, std::min(kTapLengthUs, iGapUs)), iCol, false);
+ }
+ }
+ ++g_iNextOffset;
+ g_iNextRow = iRow + 1;
+ }
+
+ // Everything due, in the order it happened, as a controller would send it:
+ // after a long frame a press, its release and the next press in the column
+ // can all be due at once.
+ std::stable_sort(g_Pending.begin(), g_Pending.end(),
+ [](const Event& a, const Event& b) { return a.iUs < b.iUs; });
+ size_t iDue = 0;
+ while (iDue < g_Pending.size() && g_Pending[iDue].iUs <= iNowUs) {
+ const Event& e = g_Pending[iDue];
+ Send(pn, e.iCol, e.bPress, e.iUs);
+ ++iDue;
+ }
+ g_Pending.erase(g_Pending.begin(), g_Pending.begin() + iDue);
+
+ if (g_fStutterMs > 0 && iNowUs - g_iLastStutterUs >= 1000000) {
+ g_iLastStutterUs = iNowUs;
+ std::this_thread::sleep_for(std::chrono::microseconds(int64_t(g_fStutterMs * 1000)));
+ }
+}
+
+/*
+ * (c) 2026 ITGmania contributors
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
diff --git a/src/TimingSelfTest.h b/src/TimingSelfTest.h
new file mode 100644
index 0000000..b43e46e
--- /dev/null
+++ b/src/TimingSelfTest.h
@@ -0,0 +1,59 @@
+/* TimingSelfTest - Steps at exact moments, for the flight recorder.
+ *
+ * Development switch: --smytest=timing (with --mpsong) plays the song for
+ * player 1 and presses their panels as the notes come, through the input
+ * system with each press's exact time: on the note, or off it by the offsets
+ * in --timingoffsets (milliseconds, late positive, taken in turn row by row).
+ * --timingrate sets the music rate; --timingstutter makes one frame that many
+ * milliseconds long every second, since a frame's length mustn't move a
+ * judgment. The flight recorder (TimingRecorder) compares every judgment with
+ * what was meant. Taps, holds and rolls are stepped; mines, lifts and fakes
+ * are left alone. */
+
+#ifndef TIMING_SELF_TEST_H
+#define TIMING_SELF_TEST_H
+
+#include <vector>
+
+#include "PlayerNumber.h"
+
+class NoteData;
+class RageTimer;
+class TimingData;
+
+namespace TimingSelfTest {
+bool Active();
+void Start(const std::vector<float>& vOffsetsMs, float fStutterMs);
+void Stop();
+
+// Each frame of gameplay, with the music's position read just now.
+void Update(PlayerNumber pn, const NoteData& nd, const TimingData& timing,
+ float fMusicSeconds, const RageTimer& positionTime, float fMusicRate);
+} // namespace TimingSelfTest
+
+#endif // TIMING_SELF_TEST_H
+
+/*
+ * (c) 2026 ITGmania contributors
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */