2 Copyright (C) 2001-2007 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));
59 if ((npoints = _list.events().size()) > 2) {
61 /* Compute coefficients needed to efficiently compute a constrained spline
62 curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
63 (www.korf.co.uk/spline.pdf) for more details.
69 AutomationList::EventList::const_iterator xx;
71 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
72 x[i] = (double) (*xx)->when;
73 y[i] = (double) (*xx)->value;
76 double lp0, lp1, fpone;
78 lp0 =(x[1] - x[0])/(y[1] - y[0]);
79 lp1 = (x[2] - x[1])/(y[2] - y[1]);
84 fpone = 2 / (lp1 + lp0);
89 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
91 double xdelta; /* gcc is wrong about possible uninitialized use */
92 double xdelta2; /* ditto */
93 double ydelta; /* ditto */
98 xdelta = x[i] - x[i-1];
99 xdelta2 = xdelta * xdelta;
100 ydelta = y[i] - y[i-1];
103 /* compute (constrained) first derivatives */
109 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
111 /* we don't store coefficients for i = 0 */
115 } else if (i == npoints - 1) {
119 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
123 /* all other segments */
125 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
126 double slope_after = (xdelta / ydelta);
128 if (slope_after * slope_before < 0.0) {
129 /* slope changed sign */
132 fpi = 2 / (slope_before + slope_after);
137 /* compute second derivative for either side of control point `i' */
139 fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
140 ((6 * ydelta) / xdelta2);
142 fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
143 ((6 * ydelta) / xdelta2);
145 /* compute polynomial coefficients */
149 d = (fppR - fppL) / (6 * xdelta);
150 c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
155 xim12 = x[i-1] * x[i-1]; /* "x[i-1] squared" */
156 xim13 = xim12 * x[i-1]; /* "x[i-1] cubed" */
157 xi2 = x[i] * x[i]; /* "x[i] squared" */
158 xi3 = xi2 * x[i]; /* "x[i] cubed" */
160 b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
164 (*xx)->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
178 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
180 Glib::Mutex::Lock lm(_list.lock(), Glib::TRY_LOCK);
185 _get_vector (x0, x1, vec, veclen);
191 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
193 Glib::Mutex::Lock lm(_list.lock());
194 _get_vector (x0, x1, vec, veclen);
198 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
200 double rx, dx, lx, hx, max_x, min_x;
202 int32_t original_veclen;
205 if ((npoints = _list.events().size()) == 0) {
206 for (i = 0; i < veclen; ++i) {
207 vec[i] = _list.default_value();
212 /* events is now known not to be empty */
214 max_x = _list.events().back()->when;
215 min_x = _list.events().front()->when;
217 lx = max (min_x, x0);
220 x1 = _list.events().back()->when;
223 hx = min (max_x, x1);
225 original_veclen = veclen;
229 /* fill some beginning section of the array with the
230 initial (used to be default) value
233 double frac = (min_x - x0) / (x1 - x0);
234 int32_t subveclen = (int32_t) floor (veclen * frac);
236 subveclen = min (subveclen, veclen);
238 for (i = 0; i < subveclen; ++i) {
239 vec[i] = _list.events().front()->value;
246 if (veclen && x1 > max_x) {
248 /* fill some end section of the array with the default or final value */
250 double frac = (x1 - max_x) / (x1 - x0);
252 int32_t subveclen = (int32_t) floor (original_veclen * frac);
256 subveclen = min (subveclen, veclen);
258 val = _list.events().back()->value;
260 i = veclen - subveclen;
262 for (i = veclen - subveclen; i < veclen; ++i) {
275 for (i = 0; i < veclen; ++i) {
276 vec[i] = _list.events().front()->value;
284 /* linear interpolation between 2 points */
286 /* XXX I'm not sure that this is the right thing to
287 do here. but its not a common case for the envisaged
292 dx = (hx - lx) / (veclen - 1) ;
297 double slope = (_list.events().back()->value - _list.events().front()->value)/
298 (_list.events().back()->when - _list.events().front()->when);
299 double yfrac = dx*slope;
301 vec[0] = _list.events().front()->value + slope * (lx - _list.events().front()->when);
303 for (i = 1; i < veclen; ++i) {
304 vec[i] = vec[i-1] + yfrac;
318 dx = (hx - lx) / veclen;
320 for (i = 0; i < veclen; ++i, rx += dx) {
321 vec[i] = multipoint_eval (rx);
327 Curve::unlocked_eval (double x)
329 // I don't see the point of this...
335 return _list.unlocked_eval (x);
339 Curve::multipoint_eval (double x)
341 pair<AutomationList::EventList::const_iterator,AutomationList::EventList::const_iterator> range;
343 AutomationList::LookupCache& lookup_cache = _list.lookup_cache();
345 if ((lookup_cache.left < 0) ||
346 ((lookup_cache.left > x) ||
347 (lookup_cache.range.first == _list.events().end()) ||
348 ((*lookup_cache.range.second)->when < x))) {
350 ControlEvent cp (x, 0.0);
352 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, AutomationList::time_comparator);
355 range = lookup_cache.range;
359 a) x is an existing control point, so first == existing point, second == next point
363 b) x is between control points, so range is empty (first == second, points to where
368 if (range.first == range.second) {
370 /* x does not exist within the list as a control point */
372 lookup_cache.left = x;
374 if (range.first == _list.events().begin()) {
375 /* we're before the first point */
376 // return default_value;
377 _list.events().front()->value;
380 if (range.second == _list.events().end()) {
381 /* we're after the last point */
382 return _list.events().back()->value;
386 ControlEvent* ev = *range.second;
388 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
391 /* x is a control point in the data */
392 /* invalidate the cached range because its not usable */
393 lookup_cache.left = -1;
394 return (*range.first)->value;
400 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
402 static_cast<Curve*>(arg)->get_vector (x0, x1, vec, vecsize);