ardour
Curve.cpp
Go to the documentation of this file.
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
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
8  * version.
9  *
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.
13  *
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
17  */
18 
19 #include <iostream>
20 #include <float.h>
21 #include <cmath>
22 #include <climits>
23 #include <cfloat>
24 #include <cmath>
25 #include <vector>
26 
27 #include <glibmm/threads.h>
28 
29 #include "evoral/Curve.hpp"
30 #include "evoral/ControlList.hpp"
31 
32 using namespace std;
33 using namespace sigc;
34 
35 namespace Evoral {
36 
37 
38 Curve::Curve (const ControlList& cl)
39  : _dirty (true)
40  , _list (cl)
41 {
42 }
43 
44 void
46 {
47  uint32_t npoints;
48 
49  if (!_dirty) {
50  return;
51  }
52 
53  if ((npoints = _list.events().size()) > 2) {
54 
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.
58  */
59 
60  vector<double> x(npoints);
61  vector<double> y(npoints);
62  uint32_t i;
63  ControlList::EventList::const_iterator xx;
64 
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;
68  }
69 
70  double lp0, lp1, fpone;
71 
72  lp0 = (x[1] - x[0])/(y[1] - y[0]);
73  lp1 = (x[2] - x[1])/(y[2] - y[1]);
74 
75  if (lp0*lp1 < 0) {
76  fpone = 0;
77  } else {
78  fpone = 2 / (lp1 + lp0);
79  }
80 
81  double fplast = 0;
82 
83  for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
84 
85  double xdelta; /* gcc is wrong about possible uninitialized use */
86  double xdelta2; /* ditto */
87  double ydelta; /* ditto */
88  double fppL, fppR;
89  double fpi;
90 
91  if (i > 0) {
92  xdelta = x[i] - x[i-1];
93  xdelta2 = xdelta * xdelta;
94  ydelta = y[i] - y[i-1];
95  }
96 
97  /* compute (constrained) first derivatives */
98 
99  if (i == 0) {
100 
101  /* first segment */
102 
103  fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
104 
105  /* we don't store coefficients for i = 0 */
106 
107  continue;
108 
109  } else if (i == npoints - 1) {
110 
111  /* last segment */
112 
113  fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
114 
115  } else {
116 
117  /* all other segments */
118 
119  double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
120  double slope_after = (xdelta / ydelta);
121 
122  if (slope_after * slope_before < 0.0) {
123  /* slope changed sign */
124  fpi = 0.0;
125  } else {
126  fpi = 2 / (slope_before + slope_after);
127  }
128  }
129 
130  /* compute second derivative for either side of control point `i' */
131 
132  fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
133  ((6 * ydelta) / xdelta2);
134 
135  fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
136  ((6 * ydelta) / xdelta2);
137 
138  /* compute polynomial coefficients */
139 
140  double b, c, d;
141 
142  d = (fppR - fppL) / (6 * xdelta);
143  c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
144 
145  double xim12, xim13;
146  double xi2, xi3;
147 
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" */
152 
153  b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
154 
155  /* store */
156 
157  (*xx)->create_coeffs();
158  (*xx)->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
159  (*xx)->coeff[1] = b;
160  (*xx)->coeff[2] = c;
161  (*xx)->coeff[3] = d;
162 
163  fplast = fpi;
164  }
165 
166  }
167 
168  _dirty = false;
169 }
170 
171 bool
172 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen)
173 {
174  Glib::Threads::RWLock::ReaderLock lm(_list.lock(), Glib::Threads::TRY_LOCK);
175 
176  if (!lm.locked()) {
177  return false;
178  } else {
179  _get_vector (x0, x1, vec, veclen);
180  return true;
181  }
182 }
183 
184 void
185 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen)
186 {
187  Glib::Threads::RWLock::ReaderLock lm(_list.lock());
188  _get_vector (x0, x1, vec, veclen);
189 }
190 
191 void
192 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen)
193 {
194  double rx, lx, hx, max_x, min_x;
195  int32_t i;
196  int32_t original_veclen;
197  int32_t npoints;
198 
199  if (veclen == 0) {
200  return;
201  }
202 
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();
207  }
208  return;
209  }
210 
211  if (npoints == 1) {
212  for (int32_t i = 0; i < veclen; ++i) {
213  vec[i] = _list.events().front()->value;
214  }
215  return;
216  }
217 
218  /* events is now known not to be empty */
219 
220  max_x = _list.events().back()->when;
221  min_x = _list.events().front()->when;
222 
223  if (x0 > max_x) {
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;
227  }
228  return;
229  }
230 
231  if (x1 < min_x) {
232  /* totally before the first event - fill the entire array with
233  * the initial value.
234  */
235  for (int32_t i = 0; i < veclen; ++i) {
236  vec[i] = _list.events().front()->value;
237  }
238  return;
239  }
240 
241  original_veclen = veclen;
242 
243  if (x0 < min_x) {
244 
245  /* fill some beginning section of the array with the
246  initial (used to be default) value
247  */
248 
249  double frac = (min_x - x0) / (x1 - x0);
250  int64_t fill_len = (int64_t) floor (veclen * frac);
251 
252  fill_len = min (fill_len, (int64_t)veclen);
253 
254  for (i = 0; i < fill_len; ++i) {
255  vec[i] = _list.events().front()->value;
256  }
257 
258  veclen -= fill_len;
259  vec += fill_len;
260  }
261 
262  if (veclen && x1 > max_x) {
263 
264  /* fill some end section of the array with the default or final value */
265 
266  double frac = (x1 - max_x) / (x1 - x0);
267  int64_t fill_len = (int64_t) floor (original_veclen * frac);
268  float val;
269 
270  fill_len = min (fill_len, (int64_t)veclen);
271  val = _list.events().back()->value;
272 
273  for (i = veclen - fill_len; i < veclen; ++i) {
274  vec[i] = val;
275  }
276 
277  veclen -= fill_len;
278  }
279 
280  lx = max (min_x, x0);
281  hx = min (max_x, x1);
282 
283  if (npoints == 2) {
284 
285  /* linear interpolation between 2 points */
286 
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.
290  */
291 
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;
295 
296  /* y intercept of the line */
297  double const c = double (_list.events().back()->value) - (m_num * _list.events().back()->when / m_den);
298 
299  /* dx that we are using */
300  double dx_num = 0;
301  double dx_den = 1;
302  if (veclen > 1) {
303  dx_num = hx - lx;
304  dx_den = veclen - 1;
305  for (int i = 0; i < veclen; ++i) {
306  vec[i] = (lx * (m_num / m_den) + m_num * i * dx_num / (m_den * dx_den)) + c;
307  }
308  } else {
309  vec[0] = lx * (m_num / m_den) + c;
310  }
311 
312  return;
313  }
314 
315  if (_dirty) {
316  solve ();
317  }
318 
319  rx = lx;
320 
321  double dx = 0;
322  if (veclen > 1) {
323  dx = (hx - lx) / (veclen - 1);
324  }
325 
326  for (i = 0; i < veclen; ++i, rx += dx) {
327  vec[i] = multipoint_eval (rx);
328  }
329 }
330 
331 double
333 {
334  // I don't see the point of this...
335 
336  if (_dirty) {
337  solve ();
338  }
339 
340  return _list.unlocked_eval (x);
341 }
342 
343 double
345 {
346  pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
347 
348  ControlList::LookupCache& lookup_cache = _list.lookup_cache();
349 
350  if ((lookup_cache.left < 0) ||
351  ((lookup_cache.left > x) ||
352  (lookup_cache.range.first == _list.events().end()) ||
353  ((*lookup_cache.range.second)->when < x))) {
354 
355  ControlEvent cp (x, 0.0);
356 
357  lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
358  }
359 
360  range = lookup_cache.range;
361 
362  /* EITHER
363 
364  a) x is an existing control point, so first == existing point, second == next point
365 
366  OR
367 
368  b) x is between control points, so range is empty (first == second, points to where
369  to insert x)
370 
371  */
372 
373  if (range.first == range.second) {
374 
375  /* x does not exist within the list as a control point */
376 
377  lookup_cache.left = x;
378 
379  if (range.first == _list.events().begin()) {
380  /* we're before the first point */
381  // return default_value;
382  return _list.events().front()->value;
383  }
384 
385  if (range.second == _list.events().end()) {
386  /* we're after the last point */
387  return _list.events().back()->value;
388  }
389 
390  ControlEvent* after = (*range.second);
391  range.second--;
392  ControlEvent* before = (*range.second);
393 
394  double vdelta = after->value - before->value;
395 
396  if (vdelta == 0.0) {
397  return before->value;
398  }
399 
400  double tdelta = x - before->when;
401  double trange = after->when - before->when;
402 
403  if (_list.interpolation() == ControlList::Curved && after->coeff) {
404  ControlEvent* ev = after;
405  double x2 = x * x;
406  return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
407  } else {
408  return before->value + (vdelta * (tdelta / trange));
409  }
410  }
411 
412  /* x is a control point in the data */
413  /* invalidate the cached range because its not usable */
414  lookup_cache.left = -1;
415  return (*range.first)->value;
416 }
417 
418 } // namespace Evoral
419 
420 extern "C" {
421 
422 void
423 curve_get_vector_from_c (void *arg, double x0, double x1, float* vec, int32_t vecsize)
424 {
425  static_cast<Evoral::Curve*>(arg)->get_vector (x0, x1, vec, vecsize);
426 }
427 
428 }
double unlocked_eval(double where)
Definition: Curve.cpp:332
bool rt_safe_get_vector(double x0, double x1, float *arg, int32_t veclen)
Definition: Curve.cpp:172
double default_value() const
Definition: Beats.hpp:239
void solve()
Definition: Curve.cpp:45
const ControlList & _list
Definition: Curve.hpp:50
void get_vector(double x0, double x1, float *arg, int32_t veclen)
Definition: Curve.cpp:185
LookupCache & lookup_cache() const
bool _dirty
Definition: Curve.hpp:49
double unlocked_eval(double x) const
double multipoint_eval(double x)
Definition: Curve.cpp:344
InterpolationStyle interpolation() const
std::pair< ControlList::const_iterator, ControlList::const_iterator > range
void curve_get_vector_from_c(void *arg, double x0, double x1, float *vec, int32_t vecsize)
Definition: Curve.cpp:423
Glib::Threads::RWLock & lock() const
void _get_vector(double x0, double x1, float *arg, int32_t veclen)
Definition: Curve.cpp:192
static bool time_comparator(const ControlEvent *a, const ControlEvent *b)
double * coeff
double[4] allocated by Curve as needed
Definition: ControlList.hpp:73
const EventList & events() const