1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David 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);
129 /* compute second derivative for either side of control point `i' */
131 fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
132 ((6 * ydelta) / xdelta2);
134 fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
135 ((6 * ydelta) / xdelta2);
137 /* compute polynomial coefficients */
141 d = (fppR - fppL) / (6 * xdelta);
142 c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
147 xim12 = x[i-1] * x[i-1]; /* "x[i-1] squared" */
148 xim13 = xim12 * x[i-1]; /* "x[i-1] cubed" */
149 xi2 = x[i] * x[i]; /* "x[i] squared" */
150 xi3 = xi2 * x[i]; /* "x[i] cubed" */
152 b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
156 (*xx)->create_coeffs();
157 (*xx)->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
171 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
173 Glib::Mutex::Lock lm(_list.lock(), Glib::TRY_LOCK);
178 _get_vector (x0, x1, vec, veclen);
184 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
186 Glib::Mutex::Lock lm(_list.lock());
187 _get_vector (x0, x1, vec, veclen);
191 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
193 double rx, dx, lx, hx, max_x, min_x;
195 int32_t original_veclen;
198 if ((npoints = _list.events().size()) == 0) {
199 for (i = 0; i < veclen; ++i) {
200 vec[i] = _list.default_value();
205 /* events is now known not to be empty */
207 max_x = _list.events().back()->when;
208 min_x = _list.events().front()->when;
210 lx = max (min_x, x0);
213 x1 = _list.events().back()->when;
216 hx = min (max_x, x1);
218 original_veclen = veclen;
222 /* fill some beginning section of the array with the
223 initial (used to be default) value
226 double frac = (min_x - x0) / (x1 - x0);
227 int32_t subveclen = (int32_t) floor (veclen * frac);
229 subveclen = min (subveclen, veclen);
231 for (i = 0; i < subveclen; ++i) {
232 vec[i] = _list.events().front()->value;
239 if (veclen && x1 > max_x) {
241 /* fill some end section of the array with the default or final value */
243 double frac = (x1 - max_x) / (x1 - x0);
245 int32_t subveclen = (int32_t) floor (original_veclen * frac);
249 subveclen = min (subveclen, veclen);
251 val = _list.events().back()->value;
253 i = veclen - subveclen;
255 for (i = veclen - subveclen; i < veclen; ++i) {
268 for (i = 0; i < veclen; ++i) {
269 vec[i] = _list.events().front()->value;
277 /* linear interpolation between 2 points */
279 /* XXX I'm not sure that this is the right thing to
280 do here. but its not a common case for the envisaged
285 dx = (hx - lx) / (veclen - 1) ;
290 double slope = (_list.events().back()->value - _list.events().front()->value)/
291 (_list.events().back()->when - _list.events().front()->when);
292 double yfrac = dx*slope;
294 vec[0] = _list.events().front()->value + slope * (lx - _list.events().front()->when);
296 for (i = 1; i < veclen; ++i) {
297 vec[i] = vec[i-1] + yfrac;
309 dx = (hx - lx) / (veclen - 1);
314 for (i = 0; i < veclen; ++i, rx += dx) {
315 vec[i] = multipoint_eval (rx);
320 Curve::unlocked_eval (double x)
322 // I don't see the point of this...
328 return _list.unlocked_eval (x);
332 Curve::multipoint_eval (double x)
334 pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
336 ControlList::LookupCache& lookup_cache = _list.lookup_cache();
338 if ((lookup_cache.left < 0) ||
339 ((lookup_cache.left > x) ||
340 (lookup_cache.range.first == _list.events().end()) ||
341 ((*lookup_cache.range.second)->when < x))) {
343 ControlEvent cp (x, 0.0);
345 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
348 range = lookup_cache.range;
352 a) x is an existing control point, so first == existing point, second == next point
356 b) x is between control points, so range is empty (first == second, points to where
361 if (range.first == range.second) {
363 /* x does not exist within the list as a control point */
365 lookup_cache.left = x;
367 if (range.first == _list.events().begin()) {
368 /* we're before the first point */
369 // return default_value;
370 _list.events().front()->value;
373 if (range.second == _list.events().end()) {
374 /* we're after the last point */
375 return _list.events().back()->value;
379 ControlEvent* ev = *range.second;
381 return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
384 /* x is a control point in the data */
385 /* invalidate the cached range because its not usable */
386 lookup_cache.left = -1;
387 return (*range.first)->value;
390 } // namespace Evoral
395 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
397 static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);