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;
42 Curve::Curve (double minv, double maxv, double canv, bool nostate)
43 : AutomationList (canv, nostate)
49 Curve::Curve (const Curve& other)
50 : AutomationList (other)
52 min_yval = other.min_yval;
53 max_yval = other.max_yval;
56 Curve::Curve (const Curve& other, double start, double end)
57 : AutomationList (other, start, end)
59 min_yval = other.min_yval;
60 max_yval = other.max_yval;
76 if ((npoints = events.size()) > 2) {
78 /* Compute coefficients needed to efficiently compute a constrained spline
79 curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
80 (www.korf.co.uk/spline.pdf) for more details.
86 AutomationEventList::iterator xx;
88 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
89 x[i] = (double) (*xx)->when;
90 y[i] = (double) (*xx)->value;
93 double lp0, lp1, fpone;
95 lp0 =(x[1] - x[0])/(y[1] - y[0]);
96 lp1 = (x[2] - x[1])/(y[2] - y[1]);
101 fpone = 2 / (lp1 + lp0);
106 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
108 CurvePoint* cp = dynamic_cast<CurvePoint*>(*xx);
111 fatal << _("programming error: ")
112 << X_("non-CurvePoint event found in event list for a Curve")
117 double xdelta; /* gcc is wrong about possible uninitialized use */
118 double xdelta2; /* ditto */
119 double ydelta; /* ditto */
124 xdelta = x[i] - x[i-1];
125 xdelta2 = xdelta * xdelta;
126 ydelta = y[i] - y[i-1];
129 /* compute (constrained) first derivatives */
135 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
137 /* we don't store coefficients for i = 0 */
141 } else if (i == npoints - 1) {
145 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
149 /* all other segments */
151 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
152 double slope_after = (xdelta / ydelta);
154 if (slope_after * slope_before < 0.0) {
155 /* slope changed sign */
158 fpi = 2 / (slope_before + slope_after);
163 /* compute second derivative for either side of control point `i' */
165 fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
166 ((6 * ydelta) / xdelta2);
168 fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
169 ((6 * ydelta) / xdelta2);
171 /* compute polynomial coefficients */
175 d = (fppR - fppL) / (6 * xdelta);
176 c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
181 xim12 = x[i-1] * x[i-1]; /* "x[i-1] squared" */
182 xim13 = xim12 * x[i-1]; /* "x[i-1] cubed" */
183 xi2 = x[i] * x[i]; /* "x[i] squared" */
184 xi3 = xi2 * x[i]; /* "x[i] cubed" */
186 b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
190 cp->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
204 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
206 Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
211 _get_vector (x0, x1, vec, veclen);
217 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
219 Glib::Mutex::Lock lm (lock);
220 _get_vector (x0, x1, vec, veclen);
224 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
226 double rx, dx, lx, hx, max_x, min_x;
228 int32_t original_veclen;
231 if ((npoints = events.size()) == 0) {
232 for (i = 0; i < veclen; ++i) {
233 vec[i] = default_value;
238 /* events is now known not to be empty */
240 max_x = events.back()->when;
241 min_x = events.front()->when;
243 lx = max (min_x, x0);
246 x1 = events.back()->when;
249 hx = min (max_x, x1);
251 original_veclen = veclen;
255 /* fill some beginning section of the array with the
256 initial (used to be default) value
259 double frac = (min_x - x0) / (x1 - x0);
260 int32_t subveclen = (int32_t) floor (veclen * frac);
262 subveclen = min (subveclen, veclen);
264 for (i = 0; i < subveclen; ++i) {
265 vec[i] = events.front()->value;
272 if (veclen && x1 > max_x) {
274 /* fill some end section of the array with the default or final value */
276 double frac = (x1 - max_x) / (x1 - x0);
278 int32_t subveclen = (int32_t) floor (original_veclen * frac);
282 subveclen = min (subveclen, veclen);
284 val = events.back()->value;
286 i = veclen - subveclen;
288 for (i = veclen - subveclen; i < veclen; ++i) {
301 for (i = 0; i < veclen; ++i) {
302 vec[i] = events.front()->value;
310 /* linear interpolation between 2 points */
312 /* XXX I'm not sure that this is the right thing to
313 do here. but its not a common case for the envisaged
318 dx = (hx - lx) / (veclen - 1) ;
323 double slope = (events.back()->value - events.front()->value)/
324 (events.back()->when - events.front()->when);
325 double yfrac = dx*slope;
327 vec[0] = events.front()->value + slope * (lx - events.front()->when);
329 for (i = 1; i < veclen; ++i) {
330 vec[i] = vec[i-1] + yfrac;
344 dx = (hx - lx) / veclen;
346 for (i = 0; i < veclen; ++i, rx += dx) {
347 vec[i] = multipoint_eval (rx);
353 Curve::unlocked_eval (double x)
359 return shared_eval (x);
363 Curve::multipoint_eval (double x)
365 pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
367 if ((lookup_cache.left < 0) ||
368 ((lookup_cache.left > x) ||
369 (lookup_cache.range.first == events.end()) ||
370 ((*lookup_cache.range.second)->when < x))) {
373 ControlEvent cp (x, 0.0);
375 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
378 range = lookup_cache.range;
382 a) x is an existing control point, so first == existing point, second == next point
386 b) x is between control points, so range is empty (first == second, points to where
391 if (range.first == range.second) {
393 /* x does not exist within the list as a control point */
395 lookup_cache.left = x;
397 if (range.first == events.begin()) {
398 /* we're before the first point */
399 // return default_value;
400 events.front()->value;
403 if (range.second == events.end()) {
404 /* we're after the last point */
405 return events.back()->value;
409 CurvePoint* cp = dynamic_cast<CurvePoint*> (*range.second);
411 return cp->coeff[0] + (cp->coeff[1] * x) + (cp->coeff[2] * x2) + (cp->coeff[3] * x2 * x);
414 /* x is a control point in the data */
415 /* invalidate the cached range because its not usable */
416 lookup_cache.left = -1;
417 return (*range.first)->value;
421 Curve::point_factory (double when, double val) const
423 return new CurvePoint (when, val);
427 Curve::point_factory (const ControlEvent& other) const
429 return new CurvePoint (other.when, other.value);
433 Curve::restore_state (StateManager::State& state)
436 return AutomationList::restore_state (state);
443 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
445 static_cast<Curve*>(arg)->get_vector (x0, x1, vec, vecsize);