Skip to content

Commit 4bd7158

Browse files
committed
update time duration calculation to avoid drift due to rounding error.
1 parent 7c436da commit 4bd7158

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_RecordKeyboardController.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ JsonValue RecordKeyboardController::controller_history_to_json(Logger& logger, C
529529

530530
JsonArray json_array;
531531
ControllerStateSnapshot* prev_snapshot = &m_controller_history[0]; // the previous non-duplicate snapshot
532+
WallClock initial_time_stamp = m_controller_history[0].time_stamp;
532533

533534
for (size_t i = 1; i < m_controller_history.size(); i++){ // start at index i = 1, since prev_snapshot starts at i=0 and we continue when current == previous.
534535
ControllerStateSnapshot& snapshot = m_controller_history[i];
@@ -542,10 +543,42 @@ JsonValue RecordKeyboardController::controller_history_to_json(Logger& logger, C
542543
continue;
543544
}
544545

545-
// cout << time_stamp << endl;
546-
// cout << prev_time_stamp << endl;
547-
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(time_stamp - prev_time_stamp); // might need to figure out how rounding works.
546+
// Normalize each time stamp relative to the initial time stamp, and round to milliseconds.
547+
// When only considering the distance between adjacent timestamps, you end up with drift due to rounding from nanoseconds to milliseconds,
548+
549+
// example:
550+
// Timestamps Diff only comparing adjacent Total time since start
551+
// 12:00 1ns 1.4ns -> 1ms 0ms
552+
// 12:00 1401ns 1.4ns -> 1ms 1ms
553+
// 12:00 2801ns 1.4ns -> 1ms 2ms
554+
// 12:00 4201ns 1.4ns -> 1ms 3ms
555+
// 12:00 5601ns 1.4ns -> 1ms 4ms
556+
// 12:00 7001ns 1.4ns -> 1ms 5ms
557+
// 12:00 8401ns 1.4ns -> 1ms 6ms
558+
// 12:00 9801ns 1.4ns -> 1ms 7ms
559+
// 12:00 11201ns 8ms
560+
// total time elapsed: 11.201ms vs 8ms
561+
562+
// Normalized timestamps Diff using normalized timestamps Total time since start
563+
// 0ns 1ms 0ms
564+
// 1.4ms -> 1ms 2ms 1ms
565+
// 2.8ms -> 3ms 1ms 3ms
566+
// 4.2ms -> 4ms 2ms 4ms
567+
// 5.6ms -> 6ms 1ms 6ms
568+
// 7.0ms -> 7ms 1ms 7ms
569+
// 8.4ms -> 8ms 2ms 8ms
570+
// 9.8ms -> 10ms 1ms 10ms
571+
// 11.2ms -> 11ms 11ms
572+
// total time elapsed: 11.201ms vs 11ms
573+
574+
575+
Milliseconds current_timestamp_time_since_start =
576+
std::chrono::round<Milliseconds>(std::chrono::duration_cast<std::chrono::nanoseconds>(time_stamp - initial_time_stamp)); // find the time difference as nanoseconds, then round to milliseconds
577+
Milliseconds prev_timestamp_time_since_start =
578+
std::chrono::round<Milliseconds>(std::chrono::duration_cast<std::chrono::nanoseconds>(prev_time_stamp - initial_time_stamp));
579+
Milliseconds elapsed_time = current_timestamp_time_since_start - prev_timestamp_time_since_start;
548580
int64_t duration = elapsed_time.count();
581+
549582
// cout << std::to_string(duration) << endl;
550583
// cout << prev_controller_state.dump() << endl;
551584
JsonObject recording = prev_controller_state.clone();

0 commit comments

Comments
 (0)