diff --git a/src/lib/time.c b/src/lib/time.c index 5af40d640..3748d7e8d 100644 --- a/src/lib/time.c +++ b/src/lib/time.c @@ -91,3 +91,16 @@ lumiera_time_millis(gavl_time_t time) { return (time / GAVL_TIME_SCALE_MS) % 1000; } + +int +lumiera_time_frames(gavl_time_t time, float fps) +{ + return (fps * (lumiera_time_millis(time))) / 1000; +} + +int +lumiera_time_frame_count(gavl_time_t time, float fps) +{ + int ms = (time / GAVL_TIME_SCALE_MS); + return fps * ms / 1000; +} diff --git a/src/lib/time.h b/src/lib/time.h index f812c1b9d..4a3812238 100644 --- a/src/lib/time.h +++ b/src/lib/time.h @@ -60,5 +60,19 @@ int lumiera_time_seconds(gavl_time_t time); */ int lumiera_time_millis(gavl_time_t time); +/** + * Get the frame part of given time, using the given number of frames. + * @param gavl_time_t the time we are interested in converting + * @param fps Frame rate (float for now, but should be a rational) + */ +int lumiera_time_frames(gavl_time_t time, float fps); + +/** + * Get the frame count for the given time. + * @param gavl_time_t the time we are interested in converting + * @param fps Frame rate (float for now, but should be a rational) + */ +int lumiera_time_frame_count(gavl_time_t time, float fps); + #endif diff --git a/tests/library/test-time.c b/tests/library/test-time.c index 4ce8b63bb..fc0f4f343 100644 --- a/tests/library/test-time.c +++ b/tests/library/test-time.c @@ -32,6 +32,7 @@ const int MILLIS = 700; const int SECONDS = 20; const int MINUTES = 55; const int HOURS = 3; +const float FPS = 24.0; /* * 1. Basic functionality @@ -46,6 +47,10 @@ TEST (basic) { CHECK (lumiera_time_seconds(t) == 0); CHECK (lumiera_time_minutes(t) == 0); CHECK (lumiera_time_hours(t) == 0); + CHECK (lumiera_time_frames(t, FPS) == 0); + CHECK (lumiera_time_frames(t, FPS+5) == 0); + CHECK (lumiera_time_frame_count(t, FPS) == 0); + CHECK (lumiera_time_frame_count(t, FPS+5) == 0); ECHO ("%s", lumiera_tmpbuf_print_time(t)); @@ -56,6 +61,10 @@ TEST (basic) { CHECK (lumiera_time_seconds(t) == SECONDS); CHECK (lumiera_time_minutes(t) == MINUTES); CHECK (lumiera_time_hours(t) == HOURS); + CHECK (lumiera_time_frames(t, FPS) == (int)((FPS * MILLIS) / 1000)); + CHECK (lumiera_time_frames(t, FPS+5) == (int)(((FPS+5) * MILLIS) / 1000)); + CHECK (lumiera_time_frame_count(t, FPS) == 338896); + CHECK (lumiera_time_frame_count(t, FPS+5) == 409500); ECHO ("%s", lumiera_tmpbuf_print_time(t)); }