1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
5 * Evoral is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <glibmm/thread.h>
28 #include "evoral/Curve.hpp"
29 #include "evoral/ControlList.hpp"
37 Curve::Curve (const ControlList& cl)
52 if ((npoints = _list.events().size()) > 2) {
54 /* Compute coefficients needed to efficiently compute a constrained spline
55 curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
56 (www.korf.co.uk/spline.pdf) for more details.
62 ControlList::EventList::const_iterator xx;
64 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
65 x[i] = (double) (*xx)->when;
66 y[i] = (double) (*xx)->value;
69 double lp0, lp1, fpone;
71 lp0 = (x[1] - x[0])/(y[1] - y[0]);
72 lp1 = (x[2] - x[1])/(y[2] - y[1]);
77 fpone = 2 / (lp1 + lp0);
82 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
84 double xdelta; /* gcc is wrong about possible uninitialized use */
85 double xdelta2; /* ditto */
86 double ydelta; /* ditto */
91 xdelta = x[i] - x[i-1];
92 xdelta2 = xdelta * xdelta;
93 ydelta = y[i] - y[i-1];
96 /* compute (constrained) first derivatives */
102 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
104 /* we don't store coefficients for i = 0 */
108 } else if (i == npoints - 1) {
112 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
116 /* all other segments */
118 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
119 double slope_after = (xdelta / ydelta);
121 if (slope_after * slope_before < 0.0) {
122 /* slope changed sign */
125 fpi = 2 / (slope_before + slope_after);
130 /* compute second derivative for either side of control point `i' */
132 fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
133 ((6 * ydelta) / xdelta2);
135 fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
136 ((6 * ydelta) / xdelta2);
138 /* compute polynomial coefficients */
142 d = (fppR - fppL) / (6 * xdelta);
143 c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
148 xim12 = x[i-1] * x[i-1]; /* "x[i-1] squared" */
149 xim13 = xim12 * x[i-1]; /* "x[i-1] cubed" */
150 xi2 = x[i] * x[i]; /* "x[i] squared" */
151 xi3 = xi2 * x[i]; /* "x[i] cubed" */
153 b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
157 (*xx)->create_coeffs();
158 (*xx)->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
172 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
174 Glib::Mutex::Lock lm(_list.lock(), Glib::TRY_LOCK);
179 _get_vector (x0, x1, vec, veclen);
185 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
187 Glib::Mutex::Lock lm(_list.lock());
188 _get_vector (x0, x1, vec, veclen);
192 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
194 double rx, dx, lx, hx, max_x, min_x;
196 int32_t original_veclen;
199 if ((npoints = _list.events().size()) == 0) {
200 for (i = 0; i < veclen; ++i) {
201 vec[i] = _list.default_value();
206 /* events is now known not to be empty */
208 max_x = _list.events().back()->when;
209 min_x = _list.events().front()->when;
211 lx = max (min_x, x0);
214 x1 = _list.events().back()->when;
217 hx = min (max_x, x1);
219 original_veclen = veclen;
223 /* fill some beginning section of the array with the
224 initial (used to be default) value
227 double frac = (min_x - x0) / (x1 - x0);
228 int32_t subveclen = (int32_t) floor (veclen * frac);
230 subveclen = min (subveclen, veclen);
232 for (i = 0; i < subveclen; ++i) {
233 vec[i] = _list.events().front()->value;
240 if (veclen && x1 > max_x) {
242 /* fill some end section of the array with the default or final value */
244 double frac = (x1 - max_x) / (x1 - x0);
246 int32_t subveclen = (int32_t) floor (original_veclen * frac);
250 subveclen = min (subveclen, veclen);
252 val = _list.events().back()->value;
254 i = veclen - subveclen;
256 for (i = veclen - subveclen; i < veclen; ++i) {
269 for (i = 0; i < veclen; ++i) {
270 vec[i] = _list.events().front()->value;
278 /* linear interpolation between 2 points */
280 /* XXX I'm not sure that this is the right thing to
281 do here. but its not a common case for the envisaged
286 dx = (hx - lx) / (veclen - 1) ;
291 double slope = (_list.events().back()->value - _list.events().front()->value)/
292 (_list.events().back()->when - _list.events().front()->when);
293 double yfrac = dx*slope;
295 vec[0] = _list.events().front()->value + slope * (lx - _list.events().front()->when);
297 for (i = 1; i < veclen; ++i) {
298 vec[i] = vec[i-1] + yfrac;
312 dx = (hx - lx) / (veclen-1);
314 for (i = 0; i < veclen; ++i, rx += dx) {
315 vec[i] = multipoint_eval (rx);
321 Curve::unlocked_eval (double x)
323 // I don't see the point of this...
329 return _list.unlocked_eval (x);
333 Curve::multipoint_eval (double x)
335 pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
337 ControlList::LookupCache& lookup_cache = _list.lookup_cache();
339 if ((lookup_cache.left < 0) ||
340 ((lookup_cache.left > x) ||
341 (lookup_cache.range.first == _list.events().end()) ||
342 ((*lookup_cache.range.second)->when < x))) {
344 ControlEvent cp (x, 0.0);
346 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
349 range = lookup_cache.range;
353 a) x is an existing control point, so first == existing point, second == next point
357 b) x is between control points, so range is empty (first == second, points to where
362 if (range.first == range.second) {
364 /* x does not exist within the list as a control point */
366 lookup_cache.left = x;
368 if (range.first == _list.events().begin()) {
369 /* we're before the first point */
370 // return default_value;
371 _list.events().front()->value;
374 if (range.second == _list.events().end()) {
375 /* we're after the last point */
376 return _list.events().back()->value;
380 ControlEvent* ev = *range.second;
382 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
385 /* x is a control point in the data */
386 /* invalidate the cached range because its not usable */
387 lookup_cache.left = -1;
388 return (*range.first)->value;
391 } // namespace Evoral
396 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
398 static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);