From 89aa56f25116fc642928f352c14fe2d485532749 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 26 Feb 2019 13:21:29 +0000 Subject: working sync and audio --- sync.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 sync.c (limited to 'sync.c') diff --git a/sync.c b/sync.c new file mode 100644 index 0000000..1b6b6bc --- /dev/null +++ b/sync.c @@ -0,0 +1,54 @@ +#include "project.h" + +#define SLEEP_MIN 100000 +#define THRESH 1000 + +void sync_to_second (struct timeval *ret) +{ + struct timeval tv1, tv2; + ret->tv_usec = 0; + + for (;;) { + tv2.tv_sec = 0; + gettimeofday (&tv1, NULL); + tv2.tv_usec = 1000000 - tv1.tv_usec; + + if (tv1.tv_usec < THRESH) { + ret->tv_sec = tv1.tv_sec; + return; + } else if (tv2.tv_usec < THRESH) { + ret->tv_sec = tv1.tv_sec + 1; + return; + } + + if (tv2.tv_usec > SLEEP_MIN) + select (0, NULL, NULL, NULL, &tv2); + else + usleep (tv2.tv_usec / 2); + } +} + + + +void sync_to_minute (struct timeval *ret) +{ + struct timeval tv1, tv2; + ret->tv_usec = 0; + + for (;;) { + tv2.tv_sec = 0; + gettimeofday (&tv1, NULL); + tv2.tv_sec = 60 - (tv1.tv_sec % 60); + tv2.tv_usec = 1000; + + printf ("%d seconds left in min\n", (int) tv2.tv_sec); + + if (tv2.tv_sec > 1) { + tv2.tv_sec /= 2; + select (0, NULL, NULL, NULL, &tv2); + } else { + sync_to_second (ret); + return; + } + } +} -- cgit v1.2.3