remove debugging output; properly handle cases in Curve::_get_vector() where the...
[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 (veclen == 0) {
199                 return;
200         }
201
202         if ((npoints = _list.events().size()) == 0) {
203                 /* no events in list, so just fill the entire array with the default value */
204                 for (int32_t i = 0; i < veclen; ++i) {
205                         vec[i] = _list.default_value();
206                 }
207                 return;
208         }
209
210         if (npoints == 1) {
211                 for (int32_t i = 0; i < veclen; ++i) {
212                         vec[i] = _list.events().front()->value;
213                 }
214                 return;
215         }
216
217         /* events is now known not to be empty */
218
219         max_x = _list.events().back()->when;
220         min_x = _list.events().front()->when;
221
222         if (x0 > max_x) {
223                 /* totally past the end - just fill the entire array with the final value */    
224                 for (int32_t i = 0; i < veclen; ++i) {
225                         vec[i] = _list.events().back()->value;
226                 }
227                 return;
228         }
229
230         if (x1 < min_x) {
231                 /* totally before the first event - fill the entire array with
232                  * the initial value.
233                  */
234                 for (int32_t i = 0; i < veclen; ++i) {
235                         vec[i] = _list.events().front()->value;
236                 }
237                 return;
238         }
239
240         original_veclen = veclen;
241
242         if (x0 < min_x) {
243
244                 /* fill some beginning section of the array with the
245                    initial (used to be default) value
246                 */
247
248                 double frac = (min_x - x0) / (x1 - x0);
249                 int64_t fill_len = (int64_t) floor (veclen * frac);
250
251                 fill_len = min (fill_len, (int64_t)veclen);
252                 
253                 for (i = 0; i < fill_len; ++i) {
254                         vec[i] = _list.events().front()->value;
255                 }
256
257                 veclen -= fill_len;
258                 vec += fill_len;
259         }
260
261         if (veclen && x1 > max_x) {
262
263                 /* fill some end section of the array with the default or final value */
264
265                 double frac = (x1 - max_x) / (x1 - x0);
266                 int64_t fill_len = (int64_t) floor (original_veclen * frac);
267                 float val;
268
269                 fill_len = min (fill_len, (int64_t)veclen);
270                 val = _list.events().back()->value;
271
272                 for (i = veclen - fill_len; i < veclen; ++i) {
273                         vec[i] = val;
274                 }
275
276                 veclen -= fill_len;
277         }
278
279         lx = max (min_x, x0);
280         hx = min (max_x, x1);
281
282         if (npoints == 2) {
283
284                 /* linear interpolation between 2 points */
285
286                 /* XXX: this numerator / denominator stuff is pretty grim, but it's the only
287                    way I could get the maths to be accurate; doing everything with pure doubles
288                    gives ~1e-17 errors in the vec[i] computation.
289                 */
290
291                 /* gradient of the line */
292                 double const m_num = _list.events().back()->value - _list.events().front()->value;
293                 double const m_den = _list.events().back()->when - _list.events().front()->when;
294
295                 /* y intercept of the line */
296                 double const c = double (_list.events().back()->value) - (m_num * _list.events().back()->when / m_den);
297
298                 /* dx that we are using */
299                 double dx_num = 0;
300                 double dx_den = 1;
301                 if (veclen > 1) {
302                         dx_num = hx - lx;
303                         dx_den = veclen - 1;
304                 }
305
306                 if (veclen > 1) {
307                         for (int i = 0; i < veclen; ++i) {
308                                 vec[i] = (lx * (m_num / m_den) + m_num * i * dx_num / (m_den * dx_den)) + c;
309                         }
310                 } else {
311                         vec[0] = lx;
312                 }
313
314                 return;
315         }
316
317         if (_dirty) {
318                 solve ();
319         }
320
321         rx = lx;
322
323         double dx = 0;
324         if (veclen > 1) {
325                 dx = (hx - lx) / (veclen - 1);
326         }
327
328         for (i = 0; i < veclen; ++i, rx += dx) {
329                 vec[i] = multipoint_eval (rx);
330         }
331 }
332
333 double
334 Curve::unlocked_eval (double x)
335 {
336         // I don't see the point of this...
337
338         if (_dirty) {
339                 solve ();
340         }
341
342         return _list.unlocked_eval (x);
343 }
344
345 double
346 Curve::multipoint_eval (double x)
347 {
348         pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
349
350         ControlList::LookupCache& lookup_cache = _list.lookup_cache();
351
352         if ((lookup_cache.left < 0) ||
353             ((lookup_cache.left > x) ||
354              (lookup_cache.range.first == _list.events().end()) ||
355              ((*lookup_cache.range.second)->when < x))) {
356
357                 ControlEvent cp (x, 0.0);
358
359                 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
360         }
361
362         range = lookup_cache.range;
363
364         /* EITHER
365
366            a) x is an existing control point, so first == existing point, second == next point
367
368            OR
369
370            b) x is between control points, so range is empty (first == second, points to where
371                to insert x)
372
373         */
374
375         if (range.first == range.second) {
376
377                 /* x does not exist within the list as a control point */
378
379                 lookup_cache.left = x;
380
381                 if (range.first == _list.events().begin()) {
382                         /* we're before the first point */
383                         // return default_value;
384                         return _list.events().front()->value;
385                 }
386
387                 if (range.second == _list.events().end()) {
388                         /* we're after the last point */
389                         return _list.events().back()->value;
390                 }
391
392                 double x2 = x * x;
393                 ControlEvent* ev = *range.second;
394
395                 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
396         }
397
398         /* x is a control point in the data */
399         /* invalidate the cached range because its not usable */
400         lookup_cache.left = -1;
401         return (*range.first)->value;
402 }
403
404 } // namespace Evoral
405
406 extern "C" {
407
408 void
409 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
410 {
411         static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);
412 }
413
414 }