2 Copyright (C) 2001-2003 Paul Davis
4 Contains ideas derived from "Constrained Cubic Spline Interpolation"
5 by CJC Kruger (www.korf.co.uk/spline.pdf).
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.
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.
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.
30 #include <glibmm/thread.h>
31 #include <sigc++/bind.h>
33 #include "ardour/curve.h"
34 #include "ardour/automation_event.h"
39 using namespace ARDOUR;
43 Curve::Curve (const AutomationList& al)
47 _list.Dirty.connect(mem_fun(*this, &Curve::on_list_dirty));
50 Curve::Curve (const Curve& other)
54 _list.Dirty.connect(mem_fun(*this, &Curve::on_list_dirty));
57 Curve::Curve (const Curve& other, double start, double end)
60 _min_yval = other._min_yval;
61 _max_yval = other._max_yval;
64 /** \a id is used for legacy sessions where the type is not present
65 * in or below the <AutomationList> node. It is used if \a id is non-null.
67 Curve::Curve (const XMLNode& node, Parameter id)
68 : AutomationList (node, id)
86 if ((npoints = _list.events().size()) > 2) {
88 /* Compute coefficients needed to efficiently compute a constrained spline
89 curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
90 (www.korf.co.uk/spline.pdf) for more details.
96 AutomationList::EventList::const_iterator xx;
98 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
99 x[i] = (double) (*xx)->when;
100 y[i] = (double) (*xx)->value;
103 double lp0, lp1, fpone;
105 lp0 =(x[1] - x[0])/(y[1] - y[0]);
106 lp1 = (x[2] - x[1])/(y[2] - y[1]);
111 fpone = 2 / (lp1 + lp0);
116 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
118 double xdelta; /* gcc is wrong about possible uninitialized use */
119 double xdelta2; /* ditto */
120 double ydelta; /* ditto */
125 xdelta = x[i] - x[i-1];
126 xdelta2 = xdelta * xdelta;
127 ydelta = y[i] - y[i-1];
130 /* compute (constrained) first derivatives */
136 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
138 /* we don't store coefficients for i = 0 */
142 } else if (i == npoints - 1) {
146 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
150 /* all other segments */
152 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
153 double slope_after = (xdelta / ydelta);
155 if (slope_after * slope_before < 0.0) {
156 /* slope changed sign */
159 fpi = 2 / (slope_before + slope_after);
164 /* compute second derivative for either side of control point `i' */
166 fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
167 ((6 * ydelta) / xdelta2);
169 fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
170 ((6 * ydelta) / xdelta2);
172 /* compute polynomial coefficients */
176 d = (fppR - fppL) / (6 * xdelta);
177 c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
182 xim12 = x[i-1] * x[i-1]; /* "x[i-1] squared" */
183 xim13 = xim12 * x[i-1]; /* "x[i-1] cubed" */
184 xi2 = x[i] * x[i]; /* "x[i] squared" */
185 xi3 = xi2 * x[i]; /* "x[i] cubed" */
187 b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
191 (*xx)->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
205 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
207 Glib::Mutex::Lock lm(_list.lock(), Glib::TRY_LOCK);
212 _get_vector (x0, x1, vec, veclen);
218 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
220 Glib::Mutex::Lock lm(_list.lock());
221 _get_vector (x0, x1, vec, veclen);
225 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
227 double rx, dx, lx, hx, max_x, min_x;
229 int32_t original_veclen;
232 if ((npoints = _list.events().size()) == 0) {
233 for (i = 0; i < veclen; ++i) {
234 vec[i] = _list.default_value();
239 /* events is now known not to be empty */
241 max_x = _list.events().back()->when;
242 min_x = _list.events().front()->when;
244 lx = max (min_x, x0);
247 x1 = _list.events().back()->when;
250 hx = min (max_x, x1);
252 original_veclen = veclen;
256 /* fill some beginning section of the array with the
257 initial (used to be default) value
260 double frac = (min_x - x0) / (x1 - x0);
261 int32_t subveclen = (int32_t) floor (veclen * frac);
263 subveclen = min (subveclen, veclen);
265 for (i = 0; i < subveclen; ++i) {
266 vec[i] = _list.events().front()->value;
273 if (veclen && x1 > max_x) {
275 /* fill some end section of the array with the default or final value */
277 double frac = (x1 - max_x) / (x1 - x0);
279 int32_t subveclen = (int32_t) floor (original_veclen * frac);
283 subveclen = min (subveclen, veclen);
285 val = _list.events().back()->value;
287 i = veclen - subveclen;
289 for (i = veclen - subveclen; i < veclen; ++i) {
302 for (i = 0; i < veclen; ++i) {
303 vec[i] = _list.events().front()->value;
311 /* linear interpolation between 2 points */
313 /* XXX I'm not sure that this is the right thing to
314 do here. but its not a common case for the envisaged
319 dx = (hx - lx) / (veclen - 1) ;
324 double slope = (_list.events().back()->value - _list.events().front()->value)/
325 (_list.events().back()->when - _list.events().front()->when);
326 double yfrac = dx*slope;
328 vec[0] = _list.events().front()->value + slope * (lx - _list.events().front()->when);
330 for (i = 1; i < veclen; ++i) {
331 vec[i] = vec[i-1] + yfrac;
345 dx = (hx - lx) / veclen;
347 for (i = 0; i < veclen; ++i, rx += dx) {
348 vec[i] = multipoint_eval (rx);
354 Curve::unlocked_eval (double x)
356 // I don't see the point of this...
362 return _list.unlocked_eval (x);
366 Curve::multipoint_eval (double x)
368 pair<AutomationList::EventList::const_iterator,AutomationList::EventList::const_iterator> range;
370 AutomationList::LookupCache& lookup_cache = _list.lookup_cache();
372 if ((lookup_cache.left < 0) ||
373 ((lookup_cache.left > x) ||
374 (lookup_cache.range.first == _list.events().end()) ||
375 ((*lookup_cache.range.second)->when < x))) {
377 AutomationList::TimeComparator cmp;
378 ControlEvent cp (x, 0.0);
380 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, cmp);
383 range = lookup_cache.range;
387 a) x is an existing control point, so first == existing point, second == next point
391 b) x is between control points, so range is empty (first == second, points to where
396 if (range.first == range.second) {
398 /* x does not exist within the list as a control point */
400 lookup_cache.left = x;
402 if (range.first == _list.events().begin()) {
403 /* we're before the first point */
404 // return default_value;
405 _list.events().front()->value;
408 if (range.second == _list.events().end()) {
409 /* we're after the last point */
410 return _list.events().back()->value;
414 ControlEvent* ev = *range.second;
416 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
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;
428 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
430 static_cast<Curve*>(arg)->get_vector (x0, x1, vec, vecsize);