Skip to content

Check it yourself

stepmaniay-verification Public

Tests anyone can run against StepManiaY's published builds and ITGmania 1.3.0, the release it grew from.

Clone

Clone with HTTPS

Plain git over HTTPS: no account, no app, nothing else to install.

Download the source tarball 3.0 MB

1010 lines · 37.1 KB

Raw
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
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.
+ */