Replace half-baked param metadata with descriptor.
[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 #include <vector>
26
27 #include <glibmm/threads.h>
28
29 #include "evoral/Curve.hpp"
30 #include "evoral/ControlList.hpp"
31
32 using namespace std;
33 using namespace sigc;
34
35 namespace Evoral {
36
37
38 Curve::Curve (const ControlList& cl)
39         : _dirty (true)
40         , _list (cl)
41 {
42 }
43
44 void
45 Curve::solve ()
46 {
47         uint32_t npoints;
48
49         if (!_dirty) {
50                 return;
51         }
52
53         if ((npoints = _list.events().size()) > 2) {
54
55                 /* Compute coefficients needed to efficiently compute a constrained spline
56                    curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
57                    (www.korf.co.uk/spline.pdf) for more details.
58                 */
59
60                 vector<double> x(npoints);
61                 vector<double> y(npoints);
62                 uint32_t i;
63                 ControlList::EventList::const_iterator xx;
64
65                 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
66                         x[i] = (double) (*xx)->when;
67                         y[i] = (double) (*xx)->value;
68                 }
69
70                 double lp0, lp1, fpone;
71
72                 lp0 = (x[1] - x[0])/(y[1] - y[0]);
73                 lp1 = (x[2] - x[1])/(y[2] - y[1]);
74
75                 if (lp0*lp1 < 0) {
76                         fpone = 0;
77                 } else {
78                         fpone = 2 / (lp1 + lp0);
79                 }
80
81                 double fplast = 0;
82
83                 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
84
85                         double xdelta;   /* gcc is wrong about possible uninitialized use */
86                         double xdelta2;  /* ditto */
87                         double ydelta;   /* ditto */
88                         double fppL, fppR;
89                         double fpi;
90
91                         if (i > 0) {
92                                 xdelta = x[i] - x[i-1];
93                                 xdelta2 = xdelta * xdelta;
94                                 ydelta = y[i] - y[i-1];
95                         }
96
97                         /* compute (constrained) first derivatives */
98
99                         if (i == 0) {
100
101                                 /* first segment */
102
103                                 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
104
105                                 /* we don't store coefficients for i = 0 */
106
107                                 continue;
108
109                         } else if (i == npoints - 1) {
110
111                                 /* last segment */
112
113                                 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
114
115                         } else {
116
117                                 /* all other segments */
118
119                                 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
120                                 double slope_after = (xdelta / ydelta);
121
122                                 if (slope_after * slope_before < 0.0) {
123                                         /* slope changed sign */
124                                         fpi = 0.0;
125                                 } else {
126                                         fpi = 2 / (slope_before + slope_after);
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::Threads::Mutex::Lock lm(_list.lock(), Glib::Threads::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::Threads::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, lx, hx, max_x, min_x;
195         int32_t i;
196         int32_t original_veclen;
197         int32_t npoints;
198
199         if (veclen == 0) {
200                 return;
201         }
202
203         if ((npoints = _list.events().size()) == 0) {
204                 /* no events in list, so just fill the entire array with the default value */
205                 for (int32_t i = 0; i < veclen; ++i) {
206                         vec[i] = _list.default_value();
207                 }
208                 return;
209         }
210
211         if (npoints == 1) {
212                 for (int32_t i = 0; i < veclen; ++i) {
213                         vec[i] = _list.events().front()->value;
214                 }
215                 return;
216         }
217
218         /* events is now known not to be empty */
219
220         max_x = _list.events().back()->when;
221         min_x = _list.events().front()->when;
222
223         if (x0 > max_x) {
224                 /* totally past the end - just fill the entire array with the final value */    
225                 for (int32_t i = 0; i < veclen; ++i) {
226                         vec[i] = _list.events().back()->value;
227                 }
228                 return;
229         }
230
231         if (x1 < min_x) {
232                 /* totally before the first event - fill the entire array with
233                  * the initial value.
234                  */
235                 for (int32_t i = 0; i < veclen; ++i) {
236                         vec[i] = _list.events().front()->value;
237                 }
238                 return;
239         }
240
241         original_veclen = veclen;
242
243         if (x0 < min_x) {
244
245                 /* fill some beginning section of the array with the
246                    initial (used to be default) value
247                 */
248
249                 double frac = (min_x - x0) / (x1 - x0);
250                 int64_t fill_len = (int64_t) floor (veclen * frac);
251
252                 fill_len = min (fill_len, (int64_t)veclen);
253
254                 for (i = 0; i < fill_len; ++i) {
255                         vec[i] = _list.events().front()->value;
256                 }
257
258                 veclen -= fill_len;
259                 vec += fill_len;
260         }
261
262         if (veclen && x1 > max_x) {
263
264                 /* fill some end section of the array with the default or final value */
265
266                 double frac = (x1 - max_x) / (x1 - x0);
267                 int64_t fill_len = (int64_t) floor (original_veclen * frac);
268                 float val;
269
270                 fill_len = min (fill_len, (int64_t)veclen);
271                 val = _list.events().back()->value;
272
273                 for (i = veclen - fill_len; i < veclen; ++i) {
274                         vec[i] = val;
275                 }
276
277                 veclen -= fill_len;
278         }
279
280         lx = max (min_x, x0);
281         hx = min (max_x, x1);
282
283         if (npoints == 2) {
284
285                 /* linear interpolation between 2 points */
286
287                 /* XXX: this numerator / denominator stuff is pretty grim, but it's the only
288                    way I could get the maths to be accurate; doing everything with pure doubles
289                    gives ~1e-17 errors in the vec[i] computation.
290                 */
291
292                 /* gradient of the line */
293                 double const m_num = _list.events().back()->value - _list.events().front()->value;
294                 double const m_den = _list.events().back()->when - _list.events().front()->when;
295
296                 /* y intercept of the line */
297                 double const c = double (_list.events().back()->value) - (m_num * _list.events().back()->when / m_den);
298
299                 /* dx that we are using */
300                 double dx_num = 0;
301                 double dx_den = 1;
302                 if (veclen > 1) {
303                         dx_num = hx - lx;
304                         dx_den = veclen - 1;
305                 }
306
307                 if (veclen > 1) {
308                         for (int i = 0; i < veclen; ++i) {
309                                 vec[i] = (lx * (m_num / m_den) + m_num * i * dx_num / (m_den * dx_den)) + c;
310                         }
311                 } else {
312                         vec[0] = lx;
313                 }
314
315                 return;
316         }
317
318         if (_dirty) {
319                 solve ();
320         }
321
322         rx = lx;
323
324         double dx = 0;
325         if (veclen > 1) {
326                 dx = (hx - lx) / (veclen - 1);
327         }
328
329         for (i = 0; i < veclen; ++i, rx += dx) {
330                 vec[i] = multipoint_eval (rx);
331         }
332 }
333
334 double
335 Curve::unlocked_eval (double x)
336 {
337         // I don't see the point of this...
338
339         if (_dirty) {
340                 solve ();
341         }
342
343         return _list.unlocked_eval (x);
344 }
345
346 double
347 Curve::multipoint_eval (double x)
348 {
349         pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
350
351         ControlList::LookupCache& lookup_cache = _list.lookup_cache();
352
353         if ((lookup_cache.left < 0) ||
354             ((lookup_cache.left > x) ||
355              (lookup_cache.range.first == _list.events().end()) ||
356              ((*lookup_cache.range.second)->when < x))) {
357
358                 ControlEvent cp (x, 0.0);
359
360                 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
361         }
362
363         range = lookup_cache.range;
364
365         /* EITHER
366
367            a) x is an existing control point, so first == existing point, second == next point
368
369            OR
370
371            b) x is between control points, so range is empty (first == second, points to where
372                to insert x)
373
374         */
375
376         if (range.first == range.second) {
377
378                 /* x does not exist within the list as a control point */
379
380                 lookup_cache.left = x;
381
382                 if (range.first == _list.events().begin()) {
383                         /* we're before the first point */
384                         // return default_value;
385                         return _list.events().front()->value;
386                 }
387
388                 if (range.second == _list.events().end()) {
389                         /* we're after the last point */
390                         return _list.events().back()->value;
391                 }
392
393                 ControlEvent* after = (*range.second);
394                 range.second--;
395                 ControlEvent* before = (*range.second);
396
397                 double vdelta = after->value - before->value;
398
399                 if (vdelta == 0.0) {
400                         return before->value;
401                 }
402
403                 double tdelta = x - before->when;
404                 double trange = after->when - before->when;
405
406                 if (_list.interpolation() == ControlList::Curved && after->coeff) {
407                                 ControlEvent* ev = after;
408                                 double x2 = x * x;
409                                 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
410                 } else {
411                         return before->value + (vdelta * (tdelta / trange));
412                 }
413         }
414
415         /* x is a control point in the data */
416         /* invalidate the cached range because its not usable */
417         lookup_cache.left = -1;
418         return (*range.first)->value;
419 }
420
421 } // namespace Evoral
422
423 extern "C" {
424
425 void
426 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
427 {
428         static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);
429 }
430
431 }