I think you’re on the right track! Some remarks:
The server side uses
AudioGetCurrentHostTime()
for “system time,” while the language side useshigh_resolution_clock::now().time_since_epoch()
.
These two functions actually measure monotonic time. System time (= wall clock time) is measured by gettimeofday
.
(AudioGetCurrentHostTime
uses the same clock as the timestamps that are passed to the audio callback. That’s how we are actually able to synchronize the audio time to the system time.)
Perhaps sometimes, in Mac, these values differ more than expected?
It is expected that these two clocks may differ significantly. The very point of syncOSCOffsetWithTimeOfDay
is to synchronize them. The important part is that the monotonic clocks are steady (no jumps), sufficiently precise and do not drift too much.
Now here’s the big catch: std::chrono::high_resolution_clock
is not necessarily monotonic!
Class
std::chrono::high_resolution_clock
represents the clock with the smallest tick period provided by the implementation. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.
https://en.cppreference.com/w/cpp/chrono/high_resolution_clock
In particular:
It is often just an alias for std::chrono::steady_clock or std::chrono::system_clock, but which one it is depends on the library or configuration. When it is a
system_clock
, it is not monotonic (e.g., the time can go backwards). For example, as of 2023, libstdc++ has it aliased tosystem_clock
“until higher-than-nanosecond definitions become feasible”[1], MSVC has it assteady_clock
[2], and libc++ usessteady_clock
when the C++ standard library implementation supports a monotonic clock andsystem_clock
otherwise[3].
I doubt that AudioGetCurrentHostTime()
resp. the CoreAudio callback timestamps are broken, so I suspect the problem is more with std::chrono::high_resolution_clock
.
It would be interesting to see both the OSC timestamps of the outgoing bundles on the client and the OSC timestamp for each audio callback on the server. Then we could see if there’s a problem with one particular clock. However, for that we would need to insert some printf
statements. Unfortunately, I can’t really spend much time on this issue at the moment…