(1) do not write sequential same-value automation data into a ControlList (2) thin...
[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                         /* 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::Mutex::Lock lm(_list.lock(), Glib::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::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, dx, 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                 int32_t subveclen = (int32_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                 int32_t subveclen = (int32_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 I'm not sure that this is the right thing to
280                    do here. but its not a common case for the envisaged
281                    uses.
282                 */
283
284                 if (veclen > 1) {
285                         dx = (hx - lx) / (veclen - 1) ;
286                 } else {
287                         dx = 0; // not used
288                 }
289
290                 double slope = (_list.events().back()->value - _list.events().front()->value)/
291                         (_list.events().back()->when - _list.events().front()->when);
292                 double yfrac = dx*slope;
293
294                 vec[0] = _list.events().front()->value + slope * (lx - _list.events().front()->when);
295
296                 for (i = 1; i < veclen; ++i) {
297                         vec[i] = vec[i-1] + yfrac;
298                 }
299                 return;
300         }
301
302         if (_dirty) {
303                 solve ();
304         }
305
306         rx = lx;
307
308         if (veclen > 1) {
309                 dx = (hx - lx) / (veclen - 1);
310         } else {
311                 dx = 0;
312         }
313
314         for (i = 0; i < veclen; ++i, rx += dx) {
315                 vec[i] = multipoint_eval (rx);
316         }
317 }
318
319 double
320 Curve::unlocked_eval (double x)
321 {
322         // I don't see the point of this...
323
324         if (_dirty) {
325                 solve ();
326         }
327
328         return _list.unlocked_eval (x);
329 }
330
331 double
332 Curve::multipoint_eval (double x)
333 {
334         pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
335
336         ControlList::LookupCache& lookup_cache = _list.lookup_cache();
337
338         if ((lookup_cache.left < 0) ||
339             ((lookup_cache.left > x) ||
340              (lookup_cache.range.first == _list.events().end()) ||
341              ((*lookup_cache.range.second)->when < x))) {
342
343                 ControlEvent cp (x, 0.0);
344
345                 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
346         }
347
348         range = lookup_cache.range;
349
350         /* EITHER
351
352            a) x is an existing control point, so first == existing point, second == next point
353
354            OR
355
356            b) x is between control points, so range is empty (first == second, points to where
357                to insert x)
358
359         */
360
361         if (range.first == range.second) {
362
363                 /* x does not exist within the list as a control point */
364
365                 lookup_cache.left = x;
366
367                 if (range.first == _list.events().begin()) {
368                         /* we're before the first point */
369                         // return default_value;
370                         _list.events().front()->value;
371                 }
372
373                 if (range.second == _list.events().end()) {
374                         /* we're after the last point */
375                         return _list.events().back()->value;
376                 }
377
378                 double x2 = x * x;
379                 ControlEvent* ev = *range.second;
380
381                 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
382         }
383
384         /* x is a control point in the data */
385         /* invalidate the cached range because its not usable */
386         lookup_cache.left = -1;
387         return (*range.first)->value;
388 }
389
390 } // namespace Evoral
391
392 extern "C" {
393
394 void
395 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
396 {
397         static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);
398 }
399
400 }