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