diff --git a/src/lib/time.c b/src/lib/time.c index 3748d7e8d..337456db3 100644 --- a/src/lib/time.c +++ b/src/lib/time.c @@ -22,6 +22,7 @@ #include #include "lib/time.h" #include "lib/tmpbuf.h" +#include /* GAVL_TIME_SCALE is the correct factor or dividend when using gavl_time_t for * units of whole seconds from gavl_time_t. Since we want to use milliseconds, @@ -58,8 +59,12 @@ lumiera_tmpbuf_print_time (gavl_time_t time) } gavl_time_t -lumiera_build_time(long millis, uint secs, uint mins, uint hours) +lumiera_build_time (long millis, uint secs, uint mins, uint hours) { + REQUIRE (millis >= 0 && millis <= 999); + REQUIRE (mins < 60); + REQUIRE (secs < 60); + gavl_time_t time = millis + 1000 * secs + 1000 * 60 * mins @@ -68,39 +73,55 @@ lumiera_build_time(long millis, uint secs, uint mins, uint hours) return time; } +gavl_time_t +lumiera_build_time_fps (float fps, uint frames, uint secs, uint mins, uint hours) +{ + REQUIRE (mins < 60); + REQUIRE (secs < 60); + REQUIRE (frames < fps); + + gavl_time_t time = frames * (1000.0 / fps) + + 1000 * secs + + 1000 * 60 * mins + + 1000 * 60 * 60 * hours; + time *= GAVL_TIME_SCALE_MS; + return time; +} + int -lumiera_time_hours(gavl_time_t time) +lumiera_time_hours (gavl_time_t time) { return time / GAVL_TIME_SCALE_MS / 1000 / 60 / 60; } int -lumiera_time_minutes(gavl_time_t time) +lumiera_time_minutes (gavl_time_t time) { return (time / GAVL_TIME_SCALE_MS / 1000 / 60) % 60; } int -lumiera_time_seconds(gavl_time_t time) +lumiera_time_seconds (gavl_time_t time) { return (time / GAVL_TIME_SCALE_MS / 1000) % 60; } int -lumiera_time_millis(gavl_time_t time) +lumiera_time_millis (gavl_time_t time) { return (time / GAVL_TIME_SCALE_MS) % 1000; } int -lumiera_time_frames(gavl_time_t time, float fps) +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) +lumiera_time_frame_count (gavl_time_t time, float fps) { - int ms = (time / GAVL_TIME_SCALE_MS); - return fps * ms / 1000; + REQUIRE (fps > 0); + + return roundf((time / GAVL_TIME_SCALE_MS / 1000.0f) * fps); } diff --git a/src/lib/time.h b/src/lib/time.h index 4a3812238..df7b1dfb0 100644 --- a/src/lib/time.h +++ b/src/lib/time.h @@ -36,43 +36,67 @@ lumiera_tmpbuf_print_time (gavl_time_t time); /** * Builds a time value by summing up the given components. + * @param millis number of milliseconds + * @param secs number of seconds + * @param mins number of minutes + * @param hours number of hours */ gavl_time_t lumiera_build_time (long millis, uint secs, uint mins, uint hours); /** - * Get the hour part of given time. + * Builds a time value by summing up the given components. + * @param fps the frame rate to be used + * @param frames number of frames + * @param secs number of seconds + * @param mins number of minutes + * @param hours number of hours */ -int lumiera_time_hours(gavl_time_t time); +gavl_time_t +lumiera_build_time_fps (float fps, uint frames, uint secs, uint mins, uint hours); + +/** + * Get the hour part of given time. + * @param time the time value to use + */ +int +lumiera_time_hours (gavl_time_t time); /** * Get the minute part of given time. + * @param time the time value to use */ -int lumiera_time_minutes(gavl_time_t time); +int +lumiera_time_minutes (gavl_time_t time); /** * Get the seconds part of given time. + * @param time the time value to use */ -int lumiera_time_seconds(gavl_time_t time); +int +lumiera_time_seconds (gavl_time_t time); /** * Get the milliseconds part of given time. + * @param time the time value to use */ -int lumiera_time_millis(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) + * Get the frame part of given time, using the given fps. + * @param time the time value to use + * @param fps frame rate (float for now, but should be a rational) */ -int lumiera_time_frames(gavl_time_t time, float fps); +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) + * Get the frame count for the given time, using the given fps. + * @param time the time value to use + * @param fps frame rate (float for now, but should be a rational) */ -int lumiera_time_frame_count(gavl_time_t time, float fps); +int +lumiera_time_frame_count (gavl_time_t time, float fps); #endif - diff --git a/tests/15time.tests b/tests/15time.tests index 0cb57ddcc..30ae5515c 100644 --- a/tests/15time.tests +++ b/tests/15time.tests @@ -4,3 +4,6 @@ TEST "basic functionality" basic <