prevent subveclen to wrap at 2^31
[ardour.git] / libs / evoral / src / Curve.cpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <iostream>
20 #include <float.h>
21 #include <cmath>
22 #include <climits>
23 #include <cfloat>
24 #include <cmath>
25
26 #include <glibmm/threads.h>
27
28 #include "evoral/Curve.hpp"
29 #include "evoral/ControlList.hpp"
30
31 using namespace std;
32 using namespace sigc;
33
34 namespace Evoral {
35
36
37 Curve::Curve (const ControlList& cl)
38         : _dirty (true)
39         , _list (cl)
40 {
41 }
42
43 void
44 Curve::solve ()
45 {
46         uint32_t npoints;
47
48         if (!_dirty) {
49                 return;
50         }
51
52         if ((npoints = _list.events().size()) > 2) {
53
54                 /* Compute coefficients needed to efficiently compute a constrained spline
55                    curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
56                    (www.korf.co.uk/spline.pdf) for more details.
57                 */
58
59                 double x[npoints];
60                 double y[npoints];
61                 uint32_t i;
62                 ControlList::EventList::const_iterator xx;
63
64                 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
65                         x[i] = (double) (*xx)->when;
66                         y[i] = (double) (*xx)->value;
67                 }
68
69                 double lp0, lp1, fpone;
70
71                 lp0 = (x[1] - x[0])/(y[1] - y[0]);
72                 lp1 = (x[2] - x[1])/(y[2] - y[1]);
73
74                 if (lp0*lp1 < 0) {
75                         fpone = 0;
76                 } else {
77                         fpone = 2 / (lp1 + lp0);
78                 }
79
80                 double fplast = 0;
81
82                 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
83
84                         double xdelta;   /* gcc is wrong about possible uninitialized use */
85                         double xdelta2;  /* ditto */
86                         double ydelta;   /* ditto */
87                         double fppL, fppR;
88                         double fpi;
89
90                         if (i > 0) {
91                                 xdelta = x[i] - x[i-1];
92                                 xdelta2 = xdelta * xdelta;
93                                 ydelta = y[i] - y[i-1];
94                         }
95
96                         /* compute (constrained) first derivatives */
97
98                         if (i == 0) {
99
100                                 /* first segment */
101
102                                 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
103
104                                 /* we don't store coefficients for i = 0 */
105
106                                 continue;
107
108                         } else if (i == npoints - 1) {
109
110                                 /* last segment */
111
112                                 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
113
114                         } else {
115
116                                 /* all other segments */
117
118                                 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
119                                 double slope_after = (xdelta / ydelta);
120
121                                 if (slope_after * slope_before < 0.0) {
122                                         /* slope changed sign */
123                                         fpi = 0.0;
124                                 } else {
125                                         fpi = 2 / (slope_before + slope_after);
126                                 }
127                         }
128
129                         /* compute second derivative for either side of control point `i' */
130
131                         fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
132                                 ((6 * ydelta) / xdelta2);
133
134                         fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
135                                 ((6 * ydelta) / xdelta2);
136
137                         /* compute polynomial coefficients */
138
139                         double b, c, d;
140
141                         d = (fppR - fppL) / (6 * xdelta);
142                         c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
143
144                         double xim12, xim13;
145                         double xi2, xi3;
146
147                         xim12 = x[i-1] * x[i-1];  /* "x[i-1] squared" */
148                         xim13 = xim12 * x[i-1];   /* "x[i-1] cubed" */
149                         xi2 = x[i] * x[i];        /* "x[i] squared" */
150                         xi3 = xi2 * x[i];         /* "x[i] cubed" */
151
152                         b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
153
154                         /* store */
155
156                         (*xx)->create_coeffs();
157                         (*xx)->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
158                         (*xx)->coeff[1] = b;
159                         (*xx)->coeff[2] = c;
160                         (*xx)->coeff[3] = d;
161
162                         fplast = fpi;
163                 }
164
165         }
166
167         _dirty = false;
168 }
169
170 bool
171 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
172 {
173         Glib::Threads::Mutex::Lock lm(_list.lock(), Glib::Threads::TRY_LOCK);
174
175         if (!lm.locked()) {
176                 return false;
177         } else {
178                 _get_vector (x0, x1, vec, veclen);
179                 return true;
180         }
181 }
182
183 void
184 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
185 {
186         Glib::Threads::Mutex::Lock lm(_list.lock());
187         _get_vector (x0, x1, vec, veclen);
188 }
189
190 void
191 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
192 {
193         double rx, lx, hx, max_x, min_x;
194         int32_t i;
195         int32_t original_veclen;
196         int32_t npoints;
197
198         if ((npoints = _list.events().size()) == 0) {
199                 for (i = 0; i < veclen; ++i) {
200                         vec[i] = _list.default_value();
201                 }
202                 return;
203         }
204
205         /* events is now known not to be empty */
206
207         max_x = _list.events().back()->when;
208         min_x = _list.events().front()->when;
209
210         lx = max (min_x, x0);
211
212         if (x1 < 0) {
213                 x1 = _list.events().back()->when;
214         }
215
216         hx = min (max_x, x1);
217
218         original_veclen = veclen;
219
220         if (x0 < min_x) {
221
222                 /* fill some beginning section of the array with the
223                    initial (used to be default) value
224                 */
225
226                 double frac = (min_x - x0) / (x1 - x0);
227                 int64_t subveclen = (int64_t) floor (veclen * frac);
228
229                 subveclen = min (subveclen, veclen);
230
231                 for (i = 0; i < subveclen; ++i) {
232                         vec[i] = _list.events().front()->value;
233                 }
234
235                 veclen -= subveclen;
236                 vec += subveclen;
237         }
238
239         if (veclen && x1 > max_x) {
240
241                 /* fill some end section of the array with the default or final value */
242
243                 double frac = (x1 - max_x) / (x1 - x0);
244
245                 int64_t subveclen = (int64_t) floor (original_veclen * frac);
246
247                 float val;
248
249                 subveclen = min (subveclen, veclen);
250
251                 val = _list.events().back()->value;
252
253                 i = veclen - subveclen;
254
255                 for (i = veclen - subveclen; i < veclen; ++i) {
256                         vec[i] = val;
257                 }
258
259                 veclen -= subveclen;
260         }
261
262         if (veclen == 0) {
263                 return;
264         }
265
266         if (npoints == 1) {
267
268                 for (i = 0; i < veclen; ++i) {
269                         vec[i] = _list.events().front()->value;
270                 }
271                 return;
272         }
273
274
275         if (npoints == 2) {
276
277                 /* linear interpolation between 2 points */
278
279                 /* XXX: this numerator / denominator stuff is pretty grim, but it's the only
280                    way I could get the maths to be accurate; doing everything with pure doubles
281                    gives ~1e-17 errors in the vec[i] computation.
282                 */
283
284                 /* gradient of the line */
285                 double const m_num = _list.events().back()->value - _list.events().front()->value;
286                 double const m_den = _list.events().back()->when - _list.events().front()->when;
287
288                 /* y intercept of the line */
289                 double const c = double (_list.events().back()->value) - (m_num * _list.events().back()->when / m_den);
290
291                 /* dx that we are using */
292                 double dx_num = 0;
293                 double dx_den = 1;
294                 if (veclen > 1) {
295                         dx_num = hx - lx;
296                         dx_den = veclen - 1;
297                 }
298
299                 if (veclen > 1) {
300                         for (int i = 0; i < veclen; ++i) {
301                                 vec[i] = (lx * (m_num / m_den) + m_num * i * dx_num / (m_den * dx_den)) + c;
302                         }
303                 } else {
304                         vec[0] = lx;
305                 }
306
307                 return;
308         }
309
310         if (_dirty) {
311                 solve ();
312         }
313
314         rx = lx;
315
316         double dx = 0;
317         if (veclen > 1) {
318                 dx = (hx - lx) / (veclen - 1);
319         }
320
321         for (i = 0; i < veclen; ++i, rx += dx) {
322                 vec[i] = multipoint_eval (rx);
323         }
324 }
325
326 double
327 Curve::unlocked_eval (double x)
328 {
329         // I don't see the point of this...
330
331         if (_dirty) {
332                 solve ();
333         }
334
335         return _list.unlocked_eval (x);
336 }
337
338 double
339 Curve::multipoint_eval (double x)
340 {
341         pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
342
343         ControlList::LookupCache& lookup_cache = _list.lookup_cache();
344
345         if ((lookup_cache.left < 0) ||
346             ((lookup_cache.left > x) ||
347              (lookup_cache.range.first == _list.events().end()) ||
348              ((*lookup_cache.range.second)->when < x))) {
349
350                 ControlEvent cp (x, 0.0);
351
352                 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
353         }
354
355         range = lookup_cache.range;
356
357         /* EITHER
358
359            a) x is an existing control point, so first == existing point, second == next point
360
361            OR
362
363            b) x is between control points, so range is empty (first == second, points to where
364                to insert x)
365
366         */
367
368         if (range.first == range.second) {
369
370                 /* x does not exist within the list as a control point */
371
372                 lookup_cache.left = x;
373
374                 if (range.first == _list.events().begin()) {
375                         /* we're before the first point */
376                         // return default_value;
377                         return _list.events().front()->value;
378                 }
379
380                 if (range.second == _list.events().end()) {
381                         /* we're after the last point */
382                         return _list.events().back()->value;
383                 }
384
385                 double x2 = x * x;
386                 ControlEvent* ev = *range.second;
387
388                 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
389         }
390
391         /* x is a control point in the data */
392         /* invalidate the cached range because its not usable */
393         lookup_cache.left = -1;
394         return (*range.first)->value;
395 }
396
397 } // namespace Evoral
398
399 extern "C" {
400
401 void
402 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
403 {
404         static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);
405 }
406
407 }