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.
31 #include <glibmm/thread.h>
32 #include <sigc++/bind.h>
34 #include "ardour/curve.h"
39 using namespace ARDOUR;
43 sigc::signal<void, Curve*> Curve::CurveCreated;
45 Curve::Curve (double minv, double maxv, double canv, bool nostate)
46 : AutomationList (canv, nostate)
53 Curve::Curve (const Curve& other)
54 : AutomationList (other)
56 min_yval = other.min_yval;
57 max_yval = other.max_yval;
61 Curve::Curve (const Curve& other, double start, double end)
62 : AutomationList (other, start, end)
64 min_yval = other.min_yval;
65 max_yval = other.max_yval;
82 if ((npoints = events.size()) > 2) {
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.
92 AutomationEventList::iterator xx;
94 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
95 x[i] = (double) (*xx)->when;
96 y[i] = (double) (*xx)->value;
99 double lp0, lp1, fpone;
101 lp0 =(x[1] - x[0])/(y[1] - y[0]);
102 lp1 = (x[2] - x[1])/(y[2] - y[1]);
107 fpone = 2 / (lp1 + lp0);
112 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
114 CurvePoint* cp = dynamic_cast<CurvePoint*>(*xx);
117 fatal << _("programming error: ")
118 << X_("non-CurvePoint event found in event list for a Curve")
123 double xdelta; /* gcc is wrong about possible uninitialized use */
124 double xdelta2; /* ditto */
125 double ydelta; /* ditto */
130 xdelta = x[i] - x[i-1];
131 xdelta2 = xdelta * xdelta;
132 ydelta = y[i] - y[i-1];
135 /* compute (constrained) first derivatives */
141 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
143 /* we don't store coefficients for i = 0 */
147 } else if (i == npoints - 1) {
151 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
155 /* all other segments */
157 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
158 double slope_after = (xdelta / ydelta);
160 if (slope_after * slope_before < 0.0) {
161 /* slope changed sign */
164 fpi = 2 / (slope_before + slope_after);
169 /* compute second derivative for either side of control point `i' */
171 fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
172 ((6 * ydelta) / xdelta2);
174 fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
175 ((6 * ydelta) / xdelta2);
177 /* compute polynomial coefficients */
181 d = (fppR - fppL) / (6 * xdelta);
182 c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
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" */
192 b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
196 cp->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
210 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
212 Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
217 _get_vector (x0, x1, vec, veclen);
223 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
225 Glib::Mutex::Lock lm (lock);
226 _get_vector (x0, x1, vec, veclen);
230 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
232 double rx, dx, lx, hx, max_x, min_x;
234 int32_t original_veclen;
237 if ((npoints = events.size()) == 0) {
238 for (i = 0; i < veclen; ++i) {
239 vec[i] = default_value;
244 /* events is now known not to be empty */
246 max_x = events.back()->when;
247 min_x = events.front()->when;
249 lx = max (min_x, x0);
252 x1 = events.back()->when;
255 hx = min (max_x, x1);
257 original_veclen = veclen;
261 /* fill some beginning section of the array with the
262 initial (used to be default) value
265 double frac = (min_x - x0) / (x1 - x0);
266 int32_t subveclen = (int32_t) floor (veclen * frac);
268 subveclen = min (subveclen, veclen);
270 for (i = 0; i < subveclen; ++i) {
271 vec[i] = events.front()->value;
278 if (veclen && x1 > max_x) {
280 /* fill some end section of the array with the default or final value */
282 double frac = (x1 - max_x) / (x1 - x0);
284 int32_t subveclen = (int32_t) floor (original_veclen * frac);
288 subveclen = min (subveclen, veclen);
290 val = events.back()->value;
292 i = veclen - subveclen;
294 for (i = veclen - subveclen; i < veclen; ++i) {
307 for (i = 0; i < veclen; ++i) {
308 vec[i] = events.front()->value;
316 /* linear interpolation between 2 points */
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
324 dx = (hx - lx) / (veclen - 1) ;
329 double slope = (events.back()->value - events.front()->value)/
330 (events.back()->when - events.front()->when);
331 double yfrac = dx*slope;
333 vec[0] = events.front()->value + slope * (lx - events.front()->when);
335 for (i = 1; i < veclen; ++i) {
336 vec[i] = vec[i-1] + yfrac;
350 dx = (hx - lx) / veclen;
352 for (i = 0; i < veclen; ++i, rx += dx) {
353 vec[i] = multipoint_eval (rx);
359 Curve::unlocked_eval (double x)
365 return shared_eval (x);
369 Curve::multipoint_eval (double x)
371 pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
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))) {
379 ControlEvent cp (x, 0.0);
381 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
384 range = lookup_cache.range;
388 a) x is an existing control point, so first == existing point, second == next point
392 b) x is between control points, so range is empty (first == second, points to where
397 if (range.first == range.second) {
399 /* x does not exist within the list as a control point */
401 lookup_cache.left = x;
403 if (range.first == events.begin()) {
404 /* we're before the first point */
405 // return default_value;
406 events.front()->value;
409 if (range.second == events.end()) {
410 /* we're after the last point */
411 return events.back()->value;
415 CurvePoint* cp = dynamic_cast<CurvePoint*> (*range.second);
417 return cp->coeff[0] + (cp->coeff[1] * x) + (cp->coeff[2] * x2) + (cp->coeff[3] * x2 * x);
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;
427 Curve::point_factory (double when, double val) const
429 return new CurvePoint (when, val);
433 Curve::point_factory (const ControlEvent& other) const
435 return new CurvePoint (other.when, other.value);
441 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
443 static_cast<Curve*>(arg)->get_vector (x0, x1, vec, vecsize);