From 3b6d4d642df6d562e1067e91d59f6fb8e5bd7343 Mon Sep 17 00:00:00 2001 From: klemek Date: Sat, 16 May 2026 17:56:51 +0200 Subject: [PATCH] fix: use clock instead of timeofday --- configure.ac | 1 - src/tempo.c | 18 ++++++------------ src/timer.c | 12 +++++------- src/types.h | 2 +- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/configure.ac b/configure.ac index 5cdbd83..34a19d8 100644 --- a/configure.ac +++ b/configure.ac @@ -11,7 +11,6 @@ AC_CHECK_HEADERS([stdlib.h]) AC_CHECK_HEADERS([sys/ioctl.h]) AC_CHECK_HEADERS([sys/mman.h]) AC_CHECK_HEADERS([sys/stat.h]) -AC_CHECK_HEADERS([sys/time.h]) AC_CHECK_HEADERS([sys/types.h]) AC_CHECK_HEADERS([sys/wait.h]) diff --git a/src/tempo.c b/src/tempo.c index a919334..5c040a4 100644 --- a/src/tempo.c +++ b/src/tempo.c @@ -1,19 +1,13 @@ #include #include -#include +#include #include "types.h" #include "config.h" #include "tempo.h" -static long now() { - struct timeval now; - - gettimeofday(&now, NULL); - - return now.tv_sec * 1000 + now.tv_usec / 1000; -} +static long now_ms() { return 1000 * clock() / CLOCKS_PER_SEC; } static void reset_tap_chain(Tempo *tempo, long t) { tempo->last_reset = t; @@ -93,7 +87,7 @@ static void add_tap_to_chain(Tempo *tempo, long t) { void tempo_init(Tempo *tempo, float value) { long t; - t = now(); + t = now_ms(); reset_tap_chain(tempo, t); @@ -105,7 +99,7 @@ void tempo_set(Tempo *tempo, float value) { long t; long progress; - t = now(); + t = now_ms(); progress = (t - tempo->last_reset) % tempo->beat_length; @@ -118,7 +112,7 @@ void tempo_set(Tempo *tempo, float value) { void tempo_tap(Tempo *tempo) { long t; - t = now(); + t = now_ms(); if (!is_chain_active(*tempo, t)) { reset_tap_chain(tempo, t); @@ -130,7 +124,7 @@ void tempo_tap(Tempo *tempo) { double tempo_total(const Tempo *tempo) { long t; - t = now(); + t = now_ms(); return (double)(t - tempo->last_reset) / (double)tempo->beat_length; } diff --git a/src/timer.c b/src/timer.c index 666ab8c..3016dc9 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,4 +1,4 @@ -#include +#include #include "types.h" @@ -7,8 +7,7 @@ void timer_init(Timer *timer, const unsigned int target) { timer->counter = 0; timer->target = target; - - gettimeofday(&timer->start, NULL); + timer->start = clock(); } bool timer_inc(Timer *timer) { @@ -17,14 +16,13 @@ bool timer_inc(Timer *timer) { } double timer_reset(Timer *timer) { - struct timeval stop; + clock_t stop; double secs; double per_secs; - gettimeofday(&stop, NULL); + stop = clock(); - secs = (double)(stop.tv_usec - timer->start.tv_usec) / 1000000 + - (double)(stop.tv_sec - timer->start.tv_sec); + secs = (double)(stop - timer->start) / CLOCKS_PER_SEC; per_secs = (double)timer->counter / secs; timer->start = stop; diff --git a/src/types.h b/src/types.h index 225ff64..b0fa496 100644 --- a/src/types.h +++ b/src/types.h @@ -279,7 +279,7 @@ typedef struct StateBackgroundWriteArgs { // timer.c typedef struct Timer { - struct timeval start; + clock_t start; unsigned int counter; unsigned int target; } Timer;