Fix my name :)
[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/thread.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
130                         /* compute second derivative for either side of control point `i' */
131
132                         fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
133                                 ((6 * ydelta) / xdelta2);
134
135                         fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
136                                 ((6 * ydelta) / xdelta2);
137
138                         /* compute polynomial coefficients */
139
140                         double b, c, d;
141
142                         d = (fppR - fppL) / (6 * xdelta);
143                         c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
144
145                         double xim12, xim13;
146                         double xi2, xi3;
147
148                         xim12 = x[i-1] * x[i-1];  /* "x[i-1] squared" */
149                         xim13 = xim12 * x[i-1];   /* "x[i-1] cubed" */
150                         xi2 = x[i] * x[i];        /* "x[i] squared" */
151                         xi3 = xi2 * x[i];         /* "x[i] cubed" */
152
153                         b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
154
155                         /* store */
156
157                         (*xx)->create_coeffs();
158                         (*xx)->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
159                         (*xx)->coeff[1] = b;
160                         (*xx)->coeff[2] = c;
161                         (*xx)->coeff[3] = d;
162
163                         fplast = fpi;
164                 }
165
166         }
167
168         _dirty = false;
169 }
170
171 bool
172 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
173 {
174         Glib::Mutex::Lock lm(_list.lock(), Glib::TRY_LOCK);
175
176         if (!lm.locked()) {
177                 return false;
178         } else {
179                 _get_vector (x0, x1, vec, veclen);
180                 return true;
181         }
182 }
183
184 void
185 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
186 {
187         Glib::Mutex::Lock lm(_list.lock());
188         _get_vector (x0, x1, vec, veclen);
189 }
190
191 void
192 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
193 {
194         double rx, dx, lx, hx, max_x, min_x;
195         int32_t i;
196         int32_t original_veclen;
197         int32_t npoints;
198
199         if ((npoints = _list.events().size()) == 0) {
200                 for (i = 0; i < veclen; ++i) {
201                         vec[i] = _list.default_value();
202                 }
203                 return;
204         }
205
206         /* events is now known not to be empty */
207
208         max_x = _list.events().back()->when;
209         min_x = _list.events().front()->when;
210
211         lx = max (min_x, x0);
212
213         if (x1 < 0) {
214                 x1 = _list.events().back()->when;
215         }
216
217         hx = min (max_x, x1);
218
219         original_veclen = veclen;
220
221         if (x0 < min_x) {
222
223                 /* fill some beginning section of the array with the
224                    initial (used to be default) value
225                 */
226
227                 double frac = (min_x - x0) / (x1 - x0);
228                 int32_t subveclen = (int32_t) floor (veclen * frac);
229
230                 subveclen = min (subveclen, veclen);
231
232                 for (i = 0; i < subveclen; ++i) {
233                         vec[i] = _list.events().front()->value;
234                 }
235
236                 veclen -= subveclen;
237                 vec += subveclen;
238         }
239
240         if (veclen && x1 > max_x) {
241
242                 /* fill some end section of the array with the default or final value */
243
244                 double frac = (x1 - max_x) / (x1 - x0);
245
246                 int32_t subveclen = (int32_t) floor (original_veclen * frac);
247
248                 float val;
249
250                 subveclen = min (subveclen, veclen);
251
252                 val = _list.events().back()->value;
253
254                 i = veclen - subveclen;
255
256                 for (i = veclen - subveclen; i < veclen; ++i) {
257                         vec[i] = val;
258                 }
259
260                 veclen -= subveclen;
261         }
262
263         if (veclen == 0) {
264                 return;
265         }
266
267         if (npoints == 1) {
268
269                 for (i = 0; i < veclen; ++i) {
270                         vec[i] = _list.events().front()->value;
271                 }
272                 return;
273         }
274
275
276         if (npoints == 2) {
277
278                 /* linear interpolation between 2 points */
279
280                 /* XXX I'm not sure that this is the right thing to
281                    do here. but its not a common case for the envisaged
282                    uses.
283                 */
284
285                 if (veclen > 1) {
286                         dx = (hx - lx) / (veclen - 1) ;
287                 } else {
288                         dx = 0; // not used
289                 }
290
291                 double slope = (_list.events().back()->value - _list.events().front()->value)/
292                         (_list.events().back()->when - _list.events().front()->when);
293                 double yfrac = dx*slope;
294
295                 vec[0] = _list.events().front()->value + slope * (lx - _list.events().front()->when);
296
297                 for (i = 1; i < veclen; ++i) {
298                         vec[i] = vec[i-1] + yfrac;
299                 }
300
301                 return;
302         }
303
304         if (_dirty) {
305                 solve ();
306         }
307
308         rx = lx;
309
310         if (veclen > 1) {
311                 dx = (hx - lx) / (veclen - 1);
312         } else {
313                 dx = 0;
314         }
315
316         for (i = 0; i < veclen; ++i, rx += dx) {
317                 vec[i] = multipoint_eval (rx);
318         }
319 }
320
321 double
322 Curve::unlocked_eval (double x)
323 {
324         // I don't see the point of this...
325
326         if (_dirty) {
327                 solve ();
328         }
329
330         return _list.unlocked_eval (x);
331 }
332
333 double
334 Curve::multipoint_eval (double x)
335 {
336         pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
337
338         ControlList::LookupCache& lookup_cache = _list.lookup_cache();
339
340         if ((lookup_cache.left < 0) ||
341             ((lookup_cache.left > x) ||
342              (lookup_cache.range.first == _list.events().end()) ||
343              ((*lookup_cache.range.second)->when < x))) {
344
345                 ControlEvent cp (x, 0.0);
346
347                 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
348         }
349
350         range = lookup_cache.range;
351
352         /* EITHER
353
354            a) x is an existing control point, so first == existing point, second == next point
355
356            OR
357
358            b) x is between control points, so range is empty (first == second, points to where
359                to insert x)
360
361         */
362
363         if (range.first == range.second) {
364
365                 /* x does not exist within the list as a control point */
366
367                 lookup_cache.left = x;
368
369                 if (range.first == _list.events().begin()) {
370                         /* we're before the first point */
371                         // return default_value;
372                         _list.events().front()->value;
373                 }
374
375                 if (range.second == _list.events().end()) {
376                         /* we're after the last point */
377                         return _list.events().back()->value;
378                 }
379
380                 double x2 = x * x;
381                 ControlEvent* ev = *range.second;
382
383                 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
384         }
385
386         /* x is a control point in the data */
387         /* invalidate the cached range because its not usable */
388         lookup_cache.left = -1;
389         return (*range.first)->value;
390 }
391
392 } // namespace Evoral
393
394 extern "C" {
395
396 void
397 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
398 {
399         static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);
400 }
401
402 }