merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[ardour.git] / libs / ardour / curve.cc
1 /*
2     Copyright (C) 2001-2003 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
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace sigc;
40 using namespace PBD;
41
42 Curve::Curve (double minv, double maxv, double canv, bool nostate)
43         : AutomationList (canv)
44 {
45         min_yval = minv;
46         max_yval = maxv;
47 }
48
49 Curve::Curve (const Curve& other)
50         : AutomationList (other)
51 {
52         min_yval = other.min_yval;
53         max_yval = other.max_yval;
54 }
55
56 Curve::Curve (const Curve& other, double start, double end)
57         : AutomationList (other, start, end)
58 {
59         min_yval = other.min_yval;
60         max_yval = other.max_yval;
61 }
62
63 Curve::Curve (const XMLNode& node)
64         : AutomationList (node)
65 {
66 }
67
68 Curve::~Curve ()
69 {
70 }
71
72 void
73 Curve::solve ()
74 {
75         uint32_t npoints;
76
77         if (!_dirty) {
78                 return;
79         }
80         
81         if ((npoints = events.size()) > 2) {
82                 
83                 /* Compute coefficients needed to efficiently compute a constrained spline
84                    curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
85                    (www.korf.co.uk/spline.pdf) for more details.
86                 */
87
88                 double x[npoints];
89                 double y[npoints];
90                 uint32_t i;
91                 AutomationEventList::iterator xx;
92
93                 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
94                         x[i] = (double) (*xx)->when;
95                         y[i] = (double) (*xx)->value;
96                 }
97
98                 double lp0, lp1, fpone;
99
100                 lp0 =(x[1] - x[0])/(y[1] - y[0]);
101                 lp1 = (x[2] - x[1])/(y[2] - y[1]);
102
103                 if (lp0*lp1 < 0) {
104                         fpone = 0;
105                 } else {
106                         fpone = 2 / (lp1 + lp0);
107                 }
108
109                 double fplast = 0;
110
111                 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
112                         
113                         CurvePoint* cp = dynamic_cast<CurvePoint*>(*xx);
114
115                         if (cp == 0) {
116                                 fatal  << _("programming error: ")
117                                        << X_("non-CurvePoint event found in event list for a Curve")
118                                        << endmsg;
119                                 /*NOTREACHED*/
120                         }
121                         
122                         double xdelta;   /* gcc is wrong about possible uninitialized use */
123                         double xdelta2;  /* ditto */
124                         double ydelta;   /* ditto */
125                         double fppL, fppR;
126                         double fpi;
127
128                         if (i > 0) {
129                                 xdelta = x[i] - x[i-1];
130                                 xdelta2 = xdelta * xdelta;
131                                 ydelta = y[i] - y[i-1];
132                         }
133
134                         /* compute (constrained) first derivatives */
135                         
136                         if (i == 0) {
137
138                                 /* first segment */
139                                 
140                                 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
141
142                                 /* we don't store coefficients for i = 0 */
143
144                                 continue;
145
146                         } else if (i == npoints - 1) {
147
148                                 /* last segment */
149
150                                 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
151                                 
152                         } else {
153
154                                 /* all other segments */
155
156                                 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
157                                 double slope_after = (xdelta / ydelta);
158
159                                 if (slope_after * slope_before < 0.0) {
160                                         /* slope changed sign */
161                                         fpi = 0.0;
162                                 } else {
163                                         fpi = 2 / (slope_before + slope_after);
164                                 }
165                                 
166                         }
167
168                         /* compute second derivative for either side of control point `i' */
169                         
170                         fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
171                                 ((6 * ydelta) / xdelta2);
172                         
173                         fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
174                                 ((6 * ydelta) / xdelta2);
175                         
176                         /* compute polynomial coefficients */
177
178                         double b, c, d;
179
180                         d = (fppR - fppL) / (6 * xdelta);   
181                         c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
182                         
183                         double xim12, xim13;
184                         double xi2, xi3;
185                         
186                         xim12 = x[i-1] * x[i-1];  /* "x[i-1] squared" */
187                         xim13 = xim12 * x[i-1];   /* "x[i-1] cubed" */
188                         xi2 = x[i] * x[i];        /* "x[i] squared" */
189                         xi3 = xi2 * x[i];         /* "x[i] cubed" */
190                         
191                         b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
192
193                         /* store */
194
195                         cp->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
196                         cp->coeff[1] = b;
197                         cp->coeff[2] = c;
198                         cp->coeff[3] = d;
199
200                         fplast = fpi;
201                 }
202                 
203         }
204
205         _dirty = false;
206 }
207
208 bool
209 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
210 {
211         Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
212
213         if (!lm.locked()) {
214                 return false;
215         } else {
216                 _get_vector (x0, x1, vec, veclen);
217                 return true;
218         }
219 }
220
221 void
222 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
223 {
224         Glib::Mutex::Lock lm (lock);
225         _get_vector (x0, x1, vec, veclen);
226 }
227
228 void
229 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
230 {
231         double rx, dx, lx, hx, max_x, min_x;
232         int32_t i;
233         int32_t original_veclen;
234         int32_t npoints;
235
236         if ((npoints = events.size()) == 0) {
237                  for (i = 0; i < veclen; ++i) {
238                          vec[i] = default_value;
239                  }
240                  return;
241         }
242
243         /* events is now known not to be empty */
244
245         max_x = events.back()->when;
246         min_x = events.front()->when;
247
248         lx = max (min_x, x0);
249
250         if (x1 < 0) {
251                 x1 = events.back()->when;
252         }
253
254         hx = min (max_x, x1);
255
256         original_veclen = veclen;
257
258         if (x0 < min_x) {
259
260                 /* fill some beginning section of the array with the 
261                    initial (used to be default) value 
262                 */
263
264                 double frac = (min_x - x0) / (x1 - x0);
265                 int32_t subveclen = (int32_t) floor (veclen * frac);
266                 
267                 subveclen = min (subveclen, veclen);
268
269                 for (i = 0; i < subveclen; ++i) {
270                         vec[i] = events.front()->value;
271                 }
272
273                 veclen -= subveclen;
274                 vec += subveclen;
275         }
276
277         if (veclen && x1 > max_x) {
278
279                 /* fill some end section of the array with the default or final value */
280
281                 double frac = (x1 - max_x) / (x1 - x0);
282
283                 int32_t subveclen = (int32_t) floor (original_veclen * frac);
284
285                 float val;
286                 
287                 subveclen = min (subveclen, veclen);
288
289                 val = events.back()->value;
290
291                 i = veclen - subveclen;
292
293                 for (i = veclen - subveclen; i < veclen; ++i) {
294                         vec[i] = val;
295                 }
296
297                 veclen -= subveclen;
298         }
299
300         if (veclen == 0) {
301                 return;
302         }
303
304         if (npoints == 1 ) {
305         
306                 for (i = 0; i < veclen; ++i) {
307                         vec[i] = events.front()->value;
308                 }
309                 return;
310         }
311  
312  
313         if (npoints == 2) {
314  
315                 /* linear interpolation between 2 points */
316  
317                 /* XXX I'm not sure that this is the right thing to
318                    do here. but its not a common case for the envisaged
319                    uses.
320                 */
321         
322                 if (veclen > 1) {
323                         dx = (hx - lx) / (veclen - 1) ;
324                 } else {
325                         dx = 0; // not used
326                 }
327         
328                 double slope = (events.back()->value - events.front()->value)/  
329                         (events.back()->when - events.front()->when);
330                 double yfrac = dx*slope;
331  
332                 vec[0] = events.front()->value + slope * (lx - events.front()->when);
333  
334                 for (i = 1; i < veclen; ++i) {
335                         vec[i] = vec[i-1] + yfrac;
336                 }
337  
338                 return;
339         }
340  
341         if (_dirty) {
342                 solve ();
343         }
344
345         rx = lx;
346
347         if (veclen > 1) {
348
349                 dx = (hx - lx) / veclen;
350
351                 for (i = 0; i < veclen; ++i, rx += dx) {
352                         vec[i] = multipoint_eval (rx);
353                 }
354         }
355 }
356
357 double
358 Curve::unlocked_eval (double x)
359 {
360         if (_dirty) {
361                 solve ();
362         }
363
364         return shared_eval (x);
365 }
366
367 double
368 Curve::multipoint_eval (double x)
369 {       
370         pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
371
372         if ((lookup_cache.left < 0) ||
373             ((lookup_cache.left > x) || 
374              (lookup_cache.range.first == events.end()) || 
375              ((*lookup_cache.range.second)->when < x))) {
376                 
377                 TimeComparator cmp;
378                 ControlEvent cp (x, 0.0);
379
380                 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
381         }
382
383         range = lookup_cache.range;
384
385         /* EITHER 
386            
387            a) x is an existing control point, so first == existing point, second == next point
388
389            OR
390
391            b) x is between control points, so range is empty (first == second, points to where
392                to insert x)
393            
394         */
395
396         if (range.first == range.second) {
397
398                 /* x does not exist within the list as a control point */
399                 
400                 lookup_cache.left = x;
401
402                 if (range.first == events.begin()) {
403                         /* we're before the first point */
404                         // return default_value;
405                         events.front()->value;
406                 }
407                 
408                 if (range.second == events.end()) {
409                         /* we're after the last point */
410                         return events.back()->value;
411                 }
412
413                 double x2 = x * x;
414                 CurvePoint* cp = dynamic_cast<CurvePoint*> (*range.second);
415
416                 return cp->coeff[0] + (cp->coeff[1] * x) + (cp->coeff[2] * x2) + (cp->coeff[3] * x2 * x);
417         } 
418
419         /* x is a control point in the data */
420         /* invalidate the cached range because its not usable */
421         lookup_cache.left = -1;
422         return (*range.first)->value;
423 }
424
425 ControlEvent*
426 Curve::point_factory (double when, double val) const
427 {
428         return new CurvePoint (when, val);
429 }
430
431 ControlEvent*
432 Curve::point_factory (const ControlEvent& other) const
433 {
434         return new CurvePoint (other.when, other.value);
435 }
436
437 extern "C" {
438
439 void 
440 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
441 {
442         static_cast<Curve*>(arg)->get_vector (x0, x1, vec, vecsize);
443 }
444
445 }