gui: Avoid unnecessary strdup's and replace newlines by spaces in curses output.
[paraslash.git] / time.c
1 /*
2  * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
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 off 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  * Convert a double to a struct timeval.
36  *
37  * \param x The value to convert.
38  * \param tv Result pointer.
39  */
40 void d2tv(double x, struct timeval *tv)
41 {
42         tv->tv_sec = x;
43         tv->tv_usec = (x - (double)tv->tv_sec) * 1000.0 * 1000.0 + 0.5;
44 }
45
46 /**
47  * Compute the difference of two time values.
48  *
49  * \param b Minuend.
50  * \param a Subtrahend.
51  * \param diff Result pointer.
52  *
53  * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
54  * return.
55  *
56  * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
57  */
58 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff)
59 {
60         int ret = 1;
61
62         if ((b->tv_sec < a->tv_sec) ||
63                 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
64                 const struct timeval *tmp = a;
65                 a = b;
66                 b = tmp;
67                 ret = -1;
68         }
69         if (!diff)
70                 return ret;
71         diff->tv_sec = b->tv_sec - a->tv_sec;
72         if (b->tv_usec < a->tv_usec) {
73                 diff->tv_sec--;
74                 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
75         } else
76                 diff->tv_usec = b->tv_usec - a->tv_usec;
77         return ret;
78 }
79
80 /**
81  * Add two time values.
82  *
83  * \param a First addend.
84  * \param b Second addend.
85  * \param sum Contains the sum \a + \a b on return.
86  */
87 void tv_add(const struct timeval *a, const struct timeval *b,
88         struct timeval *sum)
89 {
90         sum->tv_sec = a->tv_sec + b->tv_sec;
91         if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
92                 sum->tv_sec++;
93                 sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
94         } else
95                 sum->tv_usec = a->tv_usec + b->tv_usec;
96 }
97
98 /**
99  * Compute integer multiple of given struct timeval.
100  *
101  * \param mult The integer value to multiply with.
102  * \param tv The timevalue to multiply.
103  *
104  * \param result Contains \a mult * \a tv on return.
105  */
106 void tv_scale(const unsigned long mult, const struct timeval *tv,
107         struct timeval *result)
108 {
109         result->tv_sec = mult * tv->tv_sec;
110         result->tv_sec += tv->tv_usec * mult / 1000 / 1000;
111         result->tv_usec = tv->tv_usec * mult % (1000 * 1000);
112 }
113
114 /**
115  * Compute a fraction of given struct timeval.
116  *
117  * \param divisor The integer value to divide by.
118  * \param tv The timevalue to divide.
119  * \param result Contains (1 / mult) * tv on return.
120  */
121 void tv_divide(const unsigned long divisor, const struct timeval *tv,
122         struct timeval *result)
123 {
124         uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
125
126         result->tv_sec = x / 1000 / 1000;
127         result->tv_usec = x % (1000 * 1000);
128 }
129
130 /**
131  * Compute a convex combination of two time values.
132  *
133  * \param a The first coefiicent.
134  * \param tv1 The first time value.
135  * \param b The second coefiicent.
136  * \param tv2 The second time value.
137  * \param result Contains the convex combination upon return.
138  *
139  * compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result.
140  * Both \a a and \a b may be negative.
141  *
142  * \return One if \a x is positive, -1 otherwise.
143  */
144 int tv_convex_combination(const long a, const struct timeval *tv1,
145                 const long b, const struct timeval *tv2,
146                 struct timeval *result)
147 {
148         struct timeval tmp1, tmp2, tmp3;
149         int ret = 1, subtract = ((a > 0 && b < 0) || (a < 0 && b > 0));
150         unsigned long a1 = PARA_ABS(a), b1 = PARA_ABS(b);
151
152         tv_scale(a1, tv1, &tmp1);
153         tv_scale(b1, tv2, &tmp2);
154         if (subtract)
155                 ret = tv_diff(&tmp1, &tmp2, &tmp3);
156         else
157                 tv_add(&tmp1, &tmp2, &tmp3);
158         if (a1 + b1)
159                 tv_divide(a1 + b1, &tmp3, result);
160         else {
161                 result->tv_sec = 0;
162                 result->tv_usec = 0;
163         }
164         if (!a || !b) {
165                 if (a + b < 0)
166                         ret = -1;
167         } else
168                 if (a < 0)
169                         ret = -ret;
170         return ret;
171 }
172
173 /**
174  * Compute when to send a chunk of an audio file.
175  *
176  * \param chunk_num The number of the chunk.
177  * \param chunk_tv The duration of one chunk.
178  * \param stream_start When the first chunk was sent.
179  * \param result The time when to send chunk number \a chunk_num.
180  *
181  * This function computes stream_start + chunk_num * chunk_time.
182  */
183 void compute_chunk_time(long unsigned chunk_num,
184                 struct timeval *chunk_tv, struct timeval *stream_start,
185                 struct timeval *result)
186 {
187         struct timeval tmp;
188
189         tv_scale(chunk_num, chunk_tv, &tmp);
190         tv_add(&tmp, stream_start, result);
191 }