Old 05-26-2023, 06:18 PM   #1
EricTbone
Human being with feelings
 
Join Date: Feb 2013
Posts: 279
Default How to get precise time? e.g. time_precise()

I'm trying to synchronize an event inside Reaper with an event outside it.

The external event occurs at a known system time, given in Epoch time with microsecond precision.

Lua provides os.time() which is in Epoch time, but only has 1 second precision, which is not good enough for my usage.

Lua also provides os.clock() which has millisecond precision, but it measures process runtime, not system time, so useless for this.

The Reaper API provides reaper.time_precise() which has microsecond precision and claims to be a "system timestamp", but it's not in Epoch time.

Here is example output of time_precise() compared to os.time():

time_precise: 709179.6048847; os.time(): 1685148400
time_precise: 709179.6335426; os.time(): 1685148400
time_precise: 709179.6653186; os.time(): 1685148400
time_precise: 709179.6973375; os.time(): 1685148400
time_precise: 709179.7284991; os.time(): 1685148400
time_precise: 709179.7615574; os.time(): 1685148400
time_precise: 709179.7903648; os.time(): 1685148400
time_precise: 709179.8204834; os.time(): 1685148400
time_precise: 709179.8554246; os.time(): 1685148400
time_precise: 709179.8863632; os.time(): 1685148400
time_precise: 709179.9166774; os.time(): 1685148400
time_precise: 709179.9473127; os.time(): 1685148400
time_precise: 709179.9811168; os.time(): 1685148401
time_precise: 709180.0095143; os.time(): 1685148401
time_precise: 709180.0403439; os.time(): 1685148401
time_precise: 709180.0703263; os.time(): 1685148401
time_precise: 709180.1036351; os.time(): 1685148401
time_precise: 709180.1335290; os.time(): 1685148401
time_precise: 709180.1683076; os.time(): 1685148401
time_precise: 709180.2003164; os.time(): 1685148401
time_precise: 709180.2352957; os.time(): 1685148401
time_precise: 709180.2643796; os.time(): 1685148401
time_precise: 709180.2960534; os.time(): 1685148401

os.time() is giving me the correct system time as Epoch time. WTF is that time_precise() number? Is there some way to correlate it with system time? Does that correlation work cross platform?
EricTbone is offline   Reply With Quote
Old 05-26-2023, 06:25 PM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,970
Default

On macOS time_precise gives a proper Unix timestamp. On Windows it seems to be seconds since boot?

Can combine the integer part of os.time with the fractional part of reaper.time_precise:

Code:
local ms_timestamp = os.time() + (reaper.time_precise() % 1)
...assuming Windows boot time doesn't start counting in the middle of a wall second.
cfillion is offline   Reply With Quote
Old 05-27-2023, 08:43 PM   #3
EricTbone
Human being with feelings
 
Join Date: Feb 2013
Posts: 279
Default

Quote:
Originally Posted by cfillion View Post
...assuming Windows boot time doesn't start counting in the middle of a wall second.
I tested, and yes, the boot time can start at any part of a second.

I wrote a Win32 app to compare the high precision (<1us) system time (GetSystemTimePrecise) with the high precision "time since boot" time (QueryPerformanceCounter). They differ 220ms and change.

So I guess I'll write a console app that spits out the difference, run that with reaper.ExecProcess, then use that to adjust `reaper.time_precise()`.

But, man, what a kludge. Par for the course with this API, I guess. *shrug*
EricTbone is offline   Reply With Quote
Old 05-29-2023, 04:39 AM   #4
jrk
Human being with feelings
 
Join Date: Aug 2015
Posts: 2,969
Default

Do it in python?
__________________
it's meant to sound like that...
jrk is offline   Reply With Quote
Old 05-29-2023, 06:48 AM   #5
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
Default

Beware that during playing or Rendering two kinds of clock need to be consider: the wall clock time and the project (audio/Midi) time (that can be considered starting starting with Bar 1 in the time line) Those don't have a fixed offset, but can widely differ between tracks and points in time.
Hence the "precise time" is only sensible usable in some dedicated situations.

-Michael
mschnell is offline   Reply With Quote
Old 05-29-2023, 09:25 PM   #6
EricTbone
Human being with feelings
 
Join Date: Feb 2013
Posts: 279
Default

Quote:
Originally Posted by mschnell View Post
Hence the "precise time" is only sensible usable in some dedicated situations.
Project time is irrelevant for my use, I just want the system time with precision. That's it. There's no reason `time_precise` can't give a high precision system time on all platforms, except that the implementation is not thoughtful. It's one of those things where if Reaper was open source, we could fix it in 5 minutes. *sigh*
EricTbone is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:52 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.