play: Convert com_quit() to lopsub.
[paraslash.git] / time.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 #include "para.h"
8 /** \file time.c Helper functions for dealing with time values. */
9
10 /**
11 * Convert struct timeval to milliseconds.
12 *
13 * \param tv The time value value to convert.
14 *
15 * \return The number of milliseconds in \a tv.
16 */
17 long unsigned tv2ms(const struct timeval *tv)
18 {
19 return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000;
20 }
21
22 /**
23 * Convert milliseconds to a struct timeval.
24 *
25 * \param n The number of milliseconds.
26 * \param tv Result pointer.
27 */
28 void ms2tv(long unsigned n, struct timeval *tv)
29 {
30 tv->tv_sec = n / 1000;
31 tv->tv_usec = (n % 1000) * 1000;
32 }
33
34 /**
35 * Compute the difference of two time values.
36 *
37 * \param b Minuend.
38 * \param a Subtrahend.
39 * \param diff Result pointer.
40 *
41 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
42 * return.
43 *
44 * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
45 */
46 int tv_diff(const struct timeval *b, const struct timeval *a,
47 struct timeval *diff)
48 {
49 int ret = 1;
50
51 if ((b->tv_sec < a->tv_sec) ||
52 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
53 const struct timeval *tmp = a;
54 a = b;
55 b = tmp;
56 ret = -1;
57 }
58 if (!diff)
59 return ret;
60 diff->tv_sec = b->tv_sec - a->tv_sec;
61 if (b->tv_usec < a->tv_usec) {
62 diff->tv_sec--;
63 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
64 } else
65 diff->tv_usec = b->tv_usec - a->tv_usec;
66 return ret;
67 }
68
69 /**
70 * Add two time values.
71 *
72 * \param a First addend.
73 * \param b Second addend.
74 * \param sum Contains the sum \a a + \a b on return.
75 */
76 void tv_add(const struct timeval *a, const struct timeval *b,
77 struct timeval *sum)
78 {
79 sum->tv_sec = a->tv_sec + b->tv_sec;
80 if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
81 sum->tv_sec++;
82 sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
83 } else
84 sum->tv_usec = a->tv_usec + b->tv_usec;
85 }
86
87 /**
88 * Compute integer multiple of given struct timeval.
89 *
90 * \param mult The integer value to multiply with.
91 * \param tv The timevalue to multiply.
92 *
93 * \param result Contains \a mult * \a tv on return.
94 */
95 void tv_scale(const unsigned long mult, const struct timeval *tv,
96 struct timeval *result)
97 {
98 uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult;
99
100 result->tv_sec = x / 1000 / 1000;
101 result->tv_usec = x % (1000 * 1000);
102 }
103
104 /**
105 * Compute a fraction of given struct timeval.
106 *
107 * \param divisor The integer value to divide by.
108 * \param tv The timevalue to divide.
109 * \param result Contains (1 / mult) * tv on return.
110 */
111 void tv_divide(const unsigned long divisor, const struct timeval *tv,
112 struct timeval *result)
113 {
114 uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
115
116 result->tv_sec = x / 1000 / 1000;
117 result->tv_usec = x % (1000 * 1000);
118 }
119
120 /**
121 * Compute a convex combination of two time values.
122 *
123 * \param a The first coefficient.
124 * \param tv1 The first time value.
125 * \param b The second coefficient.
126 * \param tv2 The second time value.
127 * \param result Contains the convex combination upon return.
128 *
129 * Compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result.
130 * Both \a a and \a b may be negative.
131 *
132 * \return Zero, 1 or -1, if \a x is zero, positive or negative, respectively.
133 */
134 int tv_convex_combination(const long a, const struct timeval *tv1,
135 const long b, const struct timeval *tv2,
136 struct timeval *result)
137 {
138 struct timeval tmp1, tmp2, tmp3;
139 int ret = 1;
140 unsigned long a1, b1;
141
142 if (a == 0 && b == 0) {
143 result->tv_sec = 0;
144 result->tv_usec = 0;
145 return 0;
146 }
147 a1 = PARA_ABS(a);
148 b1 = PARA_ABS(b);
149 tv_scale(a1, tv1, &tmp1);
150 tv_scale(b1, tv2, &tmp2);
151 if ((a > 0 && b < 0) || (a < 0 && b > 0)) /* subtract */
152 ret = tv_diff(&tmp1, &tmp2, &tmp3);
153 else
154 tv_add(&tmp1, &tmp2, &tmp3);
155 tv_divide(a1 + b1, &tmp3, result);
156 if (!a || !b) {
157 if (a + b < 0)
158 ret = -1;
159 } else if (a < 0)
160 ret = -ret;
161 return ret;
162 }
163
164 /**
165 * Compute when to send a chunk of an audio file.
166 *
167 * \param chunk_num The number of the chunk.
168 * \param chunk_tv The duration of one chunk.
169 * \param stream_start When the first chunk was sent.
170 * \param result The time when to send chunk number \a chunk_num.
171 *
172 * This function computes \a stream_start + \a chunk_num * \a chunk_time.
173 */
174 void compute_chunk_time(long unsigned chunk_num,
175 struct timeval *chunk_tv, struct timeval *stream_start,
176 struct timeval *result)
177 {
178 struct timeval tmp;
179
180 tv_scale(chunk_num, chunk_tv, &tmp);
181 tv_add(&tmp, stream_start, result);
182 }
183
184 /**
185 * Retrieve the time of the realtime clock.
186 *
187 * \param tv Where to store the result.
188 *
189 * Gets the current value of the system-wide real-time clock (identified by id
190 * \p CLOCK_REALTIME). If \a tv is \p NULL, the value is stored in a static
191 * buffer, otherwise it is stored at the location given by \a tv.
192 *
193 * \return This function aborts on errors. On success it returns a pointer to
194 * memory containing the current time.
195 *
196 * \sa clock_gettime(2), gettimeofday(2).
197 */
198 struct timeval *clock_get_realtime(struct timeval *tv)
199 {
200 static struct timeval user_friendly;
201
202 if (!tv)
203 tv = &user_friendly;
204 #ifdef HAVE_CLOCK_GETTIME
205 {
206 struct timespec t;
207 int ret;
208
209 ret = clock_gettime(CLOCK_REALTIME, &t);
210 assert(ret == 0);
211 tv->tv_sec = t.tv_sec;
212 tv->tv_usec = t.tv_nsec / 1000;
213 }
214 #else
215 #include <sys/time.h>
216 gettimeofday(tv, NULL);
217 #endif /* HAVE_CLOCK_GETTIME */
218 return tv;
219 }