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"
38 using namespace ARDOUR;
42 Curve::Curve (double minv, double maxv, double canv, bool nostate)
43 : AutomationList (canv)
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;
63 Curve::Curve (const XMLNode& node)
64 : AutomationList (node)
81 if ((npoints = events.size()) > 2) {
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.
91 AutomationEventList::iterator xx;
93 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
94 x[i] = (double) (*xx)->when;
95 y[i] = (double) (*xx)->value;
98 double lp0, lp1, fpone;
100 lp0 =(x[1] - x[0])/(y[1] - y[0]);
101 lp1 = (x[2] - x[1])/(y[2] - y[1]);
106 fpone = 2 / (lp1 + lp0);
111 for (i = 0, xx = events.begin(); xx != events.end(); ++xx, ++i) {
113 CurvePoint* cp = dynamic_cast<CurvePoint*>(*xx);
116 fatal << _("programming error: ")
117 << X_("non-CurvePoint event found in event list for a Curve")
122 double xdelta; /* gcc is wrong about possible uninitialized use */
123 double xdelta2; /* ditto */
124 double ydelta; /* ditto */
129 xdelta = x[i] - x[i-1];
130 xdelta2 = xdelta * xdelta;
131 ydelta = y[i] - y[i-1];
134 /* compute (constrained) first derivatives */
140 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
142 /* we don't store coefficients for i = 0 */
146 } else if (i == npoints - 1) {
150 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
154 /* all other segments */
156 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
157 double slope_after = (xdelta / ydelta);
159 if (slope_after * slope_before < 0.0) {
160 /* slope changed sign */
163 fpi = 2 / (slope_before + slope_after);
168 /* compute second derivative for either side of control point `i' */
170 fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
171 ((6 * ydelta) / xdelta2);
173 fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
174 ((6 * ydelta) / xdelta2);
176 /* compute polynomial coefficients */
180 d = (fppR - fppL) / (6 * xdelta);
181 c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
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" */
191 b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
195 cp->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
209 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
211 Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
216 _get_vector (x0, x1, vec, veclen);
222 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
224 Glib::Mutex::Lock lm (lock);
225 _get_vector (x0, x1, vec, veclen);
229 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
231 double rx, dx, lx, hx, max_x, min_x;
233 int32_t original_veclen;
236 if ((npoints = events.size()) == 0) {
237 for (i = 0; i < veclen; ++i) {
238 vec[i] = default_value;
243 /* events is now known not to be empty */
245 max_x = events.back()->when;
246 min_x = events.front()->when;
248 lx = max (min_x, x0);
251 x1 = events.back()->when;
254 hx = min (max_x, x1);
256 original_veclen = veclen;
260 /* fill some beginning section of the array with the
261 initial (used to be default) value
264 double frac = (min_x - x0) / (x1 - x0);
265 int32_t subveclen = (int32_t) floor (veclen * frac);
267 subveclen = min (subveclen, veclen);
269 for (i = 0; i < subveclen; ++i) {
270 vec[i] = events.front()->value;
277 if (veclen && x1 > max_x) {
279 /* fill some end section of the array with the default or final value */
281 double frac = (x1 - max_x) / (x1 - x0);
283 int32_t subveclen = (int32_t) floor (original_veclen * frac);
287 subveclen = min (subveclen, veclen);
289 val = events.back()->value;
291 i = veclen - subveclen;
293 for (i = veclen - subveclen; i < veclen; ++i) {
306 for (i = 0; i < veclen; ++i) {
307 vec[i] = events.front()->value;
315 /* linear interpolation between 2 points */
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
323 dx = (hx - lx) / (veclen - 1) ;
328 double slope = (events.back()->value - events.front()->value)/
329 (events.back()->when - events.front()->when);
330 double yfrac = dx*slope;
332 vec[0] = events.front()->value + slope * (lx - events.front()->when);
334 for (i = 1; i < veclen; ++i) {
335 vec[i] = vec[i-1] + yfrac;
349 dx = (hx - lx) / veclen;
351 for (i = 0; i < veclen; ++i, rx += dx) {
352 vec[i] = multipoint_eval (rx);
358 Curve::unlocked_eval (double x)
364 return shared_eval (x);
368 Curve::multipoint_eval (double x)
370 pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
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))) {
378 ControlEvent cp (x, 0.0);
380 lookup_cache.range = equal_range (events.begin(), 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 == events.begin()) {
403 /* we're before the first point */
404 // return default_value;
405 events.front()->value;
408 if (range.second == events.end()) {
409 /* we're after the last point */
410 return events.back()->value;
414 CurvePoint* cp = dynamic_cast<CurvePoint*> (*range.second);
416 return cp->coeff[0] + (cp->coeff[1] * x) + (cp->coeff[2] * x2) + (cp->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;
426 Curve::point_factory (double when, double val) const
428 return new CurvePoint (when, val);
432 Curve::point_factory (const ControlEvent& other) const
434 return new CurvePoint (other.when, other.value);
440 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
442 static_cast<Curve*>(arg)->get_vector (x0, x1, vec, vecsize);