X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=time.c;h=0cb9babe2ae8de65be0fdfdc4e65ee6a626688e8;hp=b77138295c2ded13d8890cf3e626a7a8a0df3494;hb=749779fab22f802e41472a31db5a84b9b6314996;hpb=755bbd9fa36e16f266467bed60b158e5737bd838 diff --git a/time.c b/time.c index b7713829..0cb9babe 100644 --- a/time.c +++ b/time.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2007 Andre Noll + * Copyright (C) 2005-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -106,9 +106,10 @@ void tv_add(const struct timeval *a, const struct timeval *b, void tv_scale(const unsigned long mult, const struct timeval *tv, struct timeval *result) { - result->tv_sec = mult * tv->tv_sec; - result->tv_sec += tv->tv_usec * mult / 1000 / 1000; - result->tv_usec = tv->tv_usec * mult % (1000 * 1000); + uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult; + + result->tv_sec = x / 1000 / 1000; + result->tv_usec = x % (1000 * 1000); } /** @@ -121,17 +122,10 @@ void tv_scale(const unsigned long mult, const struct timeval *tv, void tv_divide(const unsigned long divisor, const struct timeval *tv, struct timeval *result) { - long unsigned q; + uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor; - q = tv->tv_usec / divisor; - result->tv_sec = tv->tv_sec / divisor; - result->tv_usec = (tv->tv_sec - result->tv_sec * divisor) - * 1000 * 1000 / divisor; - if (result->tv_usec + q >= 1000 * 1000) { - result->tv_sec++; - result->tv_usec = 1000 * 1000 - result->tv_usec - q; - } else - result->tv_usec += q; + result->tv_sec = x / 1000 / 1000; + result->tv_usec = x % (1000 * 1000); } /** @@ -176,3 +170,23 @@ int tv_convex_combination(const long a, const struct timeval *tv1, ret = -ret; return ret; } + +/** + * Compute when to send a chunk of an audio file. + * + * \param chunk_num The number of the chunk. + * \param chunk_tv The duration of one chunk. + * \param stream_start When the first chunk was sent. + * \param result The time when to send chunk number \a chunk_num. + * + * This function computes stream_start + chunk_num * chunk_time. + */ +void compute_chunk_time(long unsigned chunk_num, + struct timeval *chunk_tv, struct timeval *stream_start, + struct timeval *result) +{ + struct timeval tmp; + + tv_scale(chunk_num, chunk_tv, &tmp); + tv_add(&tmp, stream_start, result); +}