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
27 #include <glibmm/threads.h>
29 #include "evoral/Curve.hpp"
30 #include "evoral/ControlList.hpp"
38 Curve::Curve (const ControlList& cl)
53 if ((npoints = _list.events().size()) > 2) {
55 /* Compute coefficients needed to efficiently compute a constrained spline
56 curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
57 (www.korf.co.uk/spline.pdf) for more details.
60 vector<double> x(npoints);
61 vector<double> y(npoints);
63 ControlList::EventList::const_iterator xx;
65 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
66 x[i] = (double) (*xx)->when;
67 y[i] = (double) (*xx)->value;
70 double lp0, lp1, fpone;
72 lp0 = (x[1] - x[0])/(y[1] - y[0]);
73 lp1 = (x[2] - x[1])/(y[2] - y[1]);
78 fpone = 2 / (lp1 + lp0);
83 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
85 double xdelta; /* gcc is wrong about possible uninitialized use */
86 double xdelta2; /* ditto */
87 double ydelta; /* ditto */
92 xdelta = x[i] - x[i-1];
93 xdelta2 = xdelta * xdelta;
94 ydelta = y[i] - y[i-1];
97 /* compute (constrained) first derivatives */
103 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
105 /* we don't store coefficients for i = 0 */
109 } else if (i == npoints - 1) {
113 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
117 /* all other segments */
119 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
120 double slope_after = (xdelta / ydelta);
122 if (slope_after * slope_before < 0.0) {
123 /* slope changed sign */
126 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::Threads::Mutex::Lock lm(_list.lock(), Glib::Threads::TRY_LOCK);
179 _get_vector (x0, x1, vec, veclen);
185 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
187 Glib::Threads::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, lx, hx, max_x, min_x;
196 int32_t original_veclen;
203 if ((npoints = _list.events().size()) == 0) {
204 /* no events in list, so just fill the entire array with the default value */
205 for (int32_t i = 0; i < veclen; ++i) {
206 vec[i] = _list.default_value();
212 for (int32_t i = 0; i < veclen; ++i) {
213 vec[i] = _list.events().front()->value;
218 /* events is now known not to be empty */
220 max_x = _list.events().back()->when;
221 min_x = _list.events().front()->when;
224 /* totally past the end - just fill the entire array with the final value */
225 for (int32_t i = 0; i < veclen; ++i) {
226 vec[i] = _list.events().back()->value;
232 /* totally before the first event - fill the entire array with
235 for (int32_t i = 0; i < veclen; ++i) {
236 vec[i] = _list.events().front()->value;
241 original_veclen = veclen;
245 /* fill some beginning section of the array with the
246 initial (used to be default) value
249 double frac = (min_x - x0) / (x1 - x0);
250 int64_t fill_len = (int64_t) floor (veclen * frac);
252 fill_len = min (fill_len, (int64_t)veclen);
254 for (i = 0; i < fill_len; ++i) {
255 vec[i] = _list.events().front()->value;
262 if (veclen && x1 > max_x) {
264 /* fill some end section of the array with the default or final value */
266 double frac = (x1 - max_x) / (x1 - x0);
267 int64_t fill_len = (int64_t) floor (original_veclen * frac);
270 fill_len = min (fill_len, (int64_t)veclen);
271 val = _list.events().back()->value;
273 for (i = veclen - fill_len; i < veclen; ++i) {
280 lx = max (min_x, x0);
281 hx = min (max_x, x1);
285 /* linear interpolation between 2 points */
287 /* XXX: this numerator / denominator stuff is pretty grim, but it's the only
288 way I could get the maths to be accurate; doing everything with pure doubles
289 gives ~1e-17 errors in the vec[i] computation.
292 /* gradient of the line */
293 double const m_num = _list.events().back()->value - _list.events().front()->value;
294 double const m_den = _list.events().back()->when - _list.events().front()->when;
296 /* y intercept of the line */
297 double const c = double (_list.events().back()->value) - (m_num * _list.events().back()->when / m_den);
299 /* dx that we are using */
308 for (int i = 0; i < veclen; ++i) {
309 vec[i] = (lx * (m_num / m_den) + m_num * i * dx_num / (m_den * dx_den)) + c;
326 dx = (hx - lx) / (veclen - 1);
329 for (i = 0; i < veclen; ++i, rx += dx) {
330 vec[i] = multipoint_eval (rx);
335 Curve::unlocked_eval (double x)
337 // I don't see the point of this...
343 return _list.unlocked_eval (x);
347 Curve::multipoint_eval (double x)
349 pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
351 ControlList::LookupCache& lookup_cache = _list.lookup_cache();
353 if ((lookup_cache.left < 0) ||
354 ((lookup_cache.left > x) ||
355 (lookup_cache.range.first == _list.events().end()) ||
356 ((*lookup_cache.range.second)->when < x))) {
358 ControlEvent cp (x, 0.0);
360 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
363 range = lookup_cache.range;
367 a) x is an existing control point, so first == existing point, second == next point
371 b) x is between control points, so range is empty (first == second, points to where
376 if (range.first == range.second) {
378 /* x does not exist within the list as a control point */
380 lookup_cache.left = x;
382 if (range.first == _list.events().begin()) {
383 /* we're before the first point */
384 // return default_value;
385 return _list.events().front()->value;
388 if (range.second == _list.events().end()) {
389 /* we're after the last point */
390 return _list.events().back()->value;
393 ControlEvent* after = (*range.second);
395 ControlEvent* before = (*range.second);
397 double vdelta = after->value - before->value;
400 return before->value;
403 double tdelta = x - before->when;
404 double trange = after->when - before->when;
406 return before->value + (vdelta * (tdelta / trange));
410 ControlEvent* ev = *range.second;
412 return = ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
417 /* x is a control point in the data */
418 /* invalidate the cached range because its not usable */
419 lookup_cache.left = -1;
420 return (*range.first)->value;
423 } // namespace Evoral
428 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
430 static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);