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