Also the stereo version needs to set need_expose
[ardour.git] / libs / evoral / src / Curve.cpp
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 "pbd/control_math.h"
30
31 #include "evoral/Curve.hpp"
32 #include "evoral/ControlList.hpp"
33
34 using namespace std;
35 using namespace sigc;
36
37 namespace Evoral {
38
39
40 Curve::Curve (const ControlList& cl)
41         : _dirty (true)
42         , _list (cl)
43 {
44 }
45
46 void
47 Curve::solve () const
48 {
49         uint32_t npoints;
50
51         if (!_dirty) {
52                 return;
53         }
54
55         if ((npoints = _list.events().size()) > 2) {
56
57                 /* Compute coefficients needed to efficiently compute a constrained spline
58                    curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
59                    (www.korf.co.uk/spline.pdf) for more details.
60                 */
61
62                 vector<double> x(npoints);
63                 vector<double> y(npoints);
64                 uint32_t i;
65                 ControlList::EventList::const_iterator xx;
66
67                 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
68                         x[i] = (double) (*xx)->when;
69                         y[i] = (double) (*xx)->value;
70                 }
71
72                 double lp0, lp1, fpone;
73
74                 lp0 = (x[1] - x[0])/(y[1] - y[0]);
75                 lp1 = (x[2] - x[1])/(y[2] - y[1]);
76
77                 if (lp0*lp1 < 0) {
78                         fpone = 0;
79                 } else {
80                         fpone = 2 / (lp1 + lp0);
81                 }
82
83                 double fplast = 0;
84
85                 for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
86
87                         double xdelta;   /* gcc is wrong about possible uninitialized use */
88                         double xdelta2;  /* ditto */
89                         double ydelta;   /* ditto */
90                         double fppL, fppR;
91                         double fpi;
92
93                         if (i > 0) {
94                                 xdelta = x[i] - x[i-1];
95                                 xdelta2 = xdelta * xdelta;
96                                 ydelta = y[i] - y[i-1];
97                         }
98
99                         /* compute (constrained) first derivatives */
100
101                         if (i == 0) {
102
103                                 /* first segment */
104
105                                 fplast = ((3 * (y[1] - y[0]) / (2 * (x[1] - x[0]))) - (fpone * 0.5));
106
107                                 /* we don't store coefficients for i = 0 */
108
109                                 continue;
110
111                         } else if (i == npoints - 1) {
112
113                                 /* last segment */
114
115                                 fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
116
117                         } else {
118
119                                 /* all other segments */
120
121                                 double slope_before = ((x[i+1] - x[i]) / (y[i+1] - y[i]));
122                                 double slope_after = (xdelta / ydelta);
123
124                                 if (slope_after * slope_before < 0.0) {
125                                         /* slope changed sign */
126                                         fpi = 0.0;
127                                 } else {
128                                         fpi = 2 / (slope_before + slope_after);
129                                 }
130                         }
131
132                         /* compute second derivative for either side of control point `i' */
133
134                         fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
135                                 ((6 * ydelta) / xdelta2);
136
137                         fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
138                                 ((6 * ydelta) / xdelta2);
139
140                         /* compute polynomial coefficients */
141
142                         double b, c, d;
143
144                         d = (fppR - fppL) / (6 * xdelta);
145                         c = ((x[i] * fppL) - (x[i-1] * fppR))/(2 * xdelta);
146
147                         double xim12, xim13;
148                         double xi2, xi3;
149
150                         xim12 = x[i-1] * x[i-1];  /* "x[i-1] squared" */
151                         xim13 = xim12 * x[i-1];   /* "x[i-1] cubed" */
152                         xi2 = x[i] * x[i];        /* "x[i] squared" */
153                         xi3 = xi2 * x[i];         /* "x[i] cubed" */
154
155                         b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
156
157                         /* store */
158
159                         (*xx)->create_coeffs();
160                         (*xx)->coeff[0] = y[i-1] - (b * x[i-1]) - (c * xim12) - (d * xim13);
161                         (*xx)->coeff[1] = b;
162                         (*xx)->coeff[2] = c;
163                         (*xx)->coeff[3] = d;
164
165                         fplast = fpi;
166                 }
167
168         }
169
170         _dirty = false;
171 }
172
173 bool
174 Curve::rt_safe_get_vector (double x0, double x1, float *vec, int32_t veclen) const
175 {
176         Glib::Threads::RWLock::ReaderLock lm(_list.lock(), Glib::Threads::TRY_LOCK);
177
178         if (!lm.locked()) {
179                 return false;
180         } else {
181                 _get_vector (x0, x1, vec, veclen);
182                 return true;
183         }
184 }
185
186 void
187 Curve::get_vector (double x0, double x1, float *vec, int32_t veclen) const
188 {
189         Glib::Threads::RWLock::ReaderLock lm(_list.lock());
190         _get_vector (x0, x1, vec, veclen);
191 }
192
193 void
194 Curve::_get_vector (double x0, double x1, float *vec, int32_t veclen) const
195 {
196         double rx, lx, hx, max_x, min_x;
197         int32_t i;
198         int32_t original_veclen;
199         int32_t npoints;
200
201         if (veclen == 0) {
202                 return;
203         }
204
205         if ((npoints = _list.events().size()) == 0) {
206                 /* no events in list, so just fill the entire array with the default value */
207                 for (int32_t i = 0; i < veclen; ++i) {
208                         vec[i] = _list.descriptor().normal;
209                 }
210                 return;
211         }
212
213         if (npoints == 1) {
214                 for (int32_t i = 0; i < veclen; ++i) {
215                         vec[i] = _list.events().front()->value;
216                 }
217                 return;
218         }
219
220         /* events is now known not to be empty */
221
222         max_x = _list.events().back()->when;
223         min_x = _list.events().front()->when;
224
225         if (x0 > max_x) {
226                 /* totally past the end - just fill the entire array with the final value */
227                 for (int32_t i = 0; i < veclen; ++i) {
228                         vec[i] = _list.events().back()->value;
229                 }
230                 return;
231         }
232
233         if (x1 < min_x) {
234                 /* totally before the first event - fill the entire array with
235                  * the initial value.
236                  */
237                 for (int32_t i = 0; i < veclen; ++i) {
238                         vec[i] = _list.events().front()->value;
239                 }
240                 return;
241         }
242
243         original_veclen = veclen;
244
245         if (x0 < min_x) {
246
247                 /* fill some beginning section of the array with the
248                    initial (used to be default) value
249                 */
250
251                 double frac = (min_x - x0) / (x1 - x0);
252                 int64_t fill_len = (int64_t) floor (veclen * frac);
253
254                 fill_len = min (fill_len, (int64_t)veclen);
255
256                 for (i = 0; i < fill_len; ++i) {
257                         vec[i] = _list.events().front()->value;
258                 }
259
260                 veclen -= fill_len;
261                 vec += fill_len;
262         }
263
264         if (veclen && x1 > max_x) {
265
266                 /* fill some end section of the array with the default or final value */
267
268                 double frac = (x1 - max_x) / (x1 - x0);
269                 int64_t fill_len = (int64_t) floor (original_veclen * frac);
270                 float val;
271
272                 fill_len = min (fill_len, (int64_t)veclen);
273                 val = _list.events().back()->value;
274
275                 for (i = veclen - fill_len; i < veclen; ++i) {
276                         vec[i] = val;
277                 }
278
279                 veclen -= fill_len;
280         }
281
282         lx = max (min_x, x0);
283         hx = min (max_x, x1);
284
285         if (npoints == 2) {
286
287                 const double lpos = _list.events().front()->when;
288                 const double lval = _list.events().front()->value;
289                 const double upos = _list.events().back()->when;
290                 const double uval = _list.events().back()->value;
291
292                 /* dx that we are using */
293                 if (veclen > 1) {
294                         const double dx_num = hx - lx;
295                         const double dx_den = veclen - 1;
296                         const double lower = _list.descriptor().lower;
297                         const double upper = _list.descriptor().upper;
298
299                         /* gradient of the line */
300                         const double m_num = uval - lval;
301                         const double m_den = upos - lpos;
302                         /* y intercept of the line */
303                         const double c = uval - (m_num * upos / m_den);
304
305                         switch (_list.interpolation()) {
306                                 case ControlList::Logarithmic:
307                                         for (int i = 0; i < veclen; ++i) {
308                                                 const double fraction = (lx - lpos + i * dx_num / dx_den) / m_den;
309                                                 vec[i] = interpolate_logarithmic (lval, uval, fraction, lower, upper);
310                                         }
311                                         break;
312                                 case ControlList::Exponential:
313                                         for (int i = 0; i < veclen; ++i) {
314                                                 const double fraction = (lx - lpos + i * dx_num / dx_den) / m_den;
315                                                 vec[i] = interpolate_gain (lval, uval, fraction, upper);
316                                         }
317                                         break;
318                                 case ControlList::Discrete:
319                                         // any discrete vector curves somewhere?
320                                         assert (0);
321                                 case ControlList::Curved:
322                                         // fallthrough, no 2 point spline
323                                 default: // Linear:
324                                         for (int i = 0; i < veclen; ++i) {
325                                                 vec[i] = (lx * (m_num / m_den) + m_num * i * dx_num / (m_den * dx_den)) + c;
326                                         }
327                                         break;
328                         }
329                 } else {
330                         double fraction = (lx - lpos) / (upos - lpos);
331                         switch (_list.interpolation()) {
332                                 case ControlList::Logarithmic:
333                                         vec[0] = interpolate_logarithmic (lval, uval, fraction, _list.descriptor().lower, _list.descriptor().upper);
334                                         break;
335                                 case ControlList::Exponential:
336                                         vec[0] = interpolate_gain (lval, uval, fraction, _list.descriptor().upper);
337                                         break;
338                                 case ControlList::Discrete:
339                                         // any discrete vector curves somewhere?
340                                         assert (0);
341                                 case ControlList::Curved:
342                                         // fallthrough, no 2 point spline
343                                 default: // Linear:
344                                         vec[0] = interpolate_linear (lval, uval, fraction);
345                                         break;
346                         }
347                 }
348
349                 return;
350         }
351
352         if (_dirty) {
353                 solve ();
354         }
355
356         rx = lx;
357
358         double dx = 0;
359         if (veclen > 1) {
360                 dx = (hx - lx) / (veclen - 1);
361         }
362
363         for (i = 0; i < veclen; ++i, rx += dx) {
364                 vec[i] = multipoint_eval (rx);
365         }
366 }
367
368 double
369 Curve::multipoint_eval (double x) const
370 {
371         pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
372
373         ControlList::LookupCache& lookup_cache = _list.lookup_cache();
374
375         if ((lookup_cache.left < 0) ||
376             ((lookup_cache.left > x) ||
377              (lookup_cache.range.first == _list.events().end()) ||
378              ((*lookup_cache.range.second)->when < x))) {
379
380                 ControlEvent cp (x, 0.0);
381
382                 lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
383         }
384
385         range = lookup_cache.range;
386
387         /* EITHER
388
389            a) x is an existing control point, so first == existing point, second == next point
390
391            OR
392
393            b) x is between control points, so range is empty (first == second, points to where
394                to insert x)
395
396         */
397
398         if (range.first == range.second) {
399
400                 /* x does not exist within the list as a control point */
401
402                 lookup_cache.left = x;
403
404                 if (range.first == _list.events().begin()) {
405                         /* we're before the first point */
406                         // return default_value;
407                         return _list.events().front()->value;
408                 }
409
410                 if (range.second == _list.events().end()) {
411                         /* we're after the last point */
412                         return _list.events().back()->value;
413                 }
414
415                 ControlEvent* after = (*range.second);
416                 range.second--;
417                 ControlEvent* before = (*range.second);
418
419                 double vdelta = after->value - before->value;
420
421                 if (vdelta == 0.0) {
422                         return before->value;
423                 }
424
425                 double tdelta = x - before->when;
426                 double trange = after->when - before->when;
427
428                 switch (_list.interpolation()) {
429                         case ControlList::Discrete:
430                                 return before->value;
431                         case ControlList::Logarithmic:
432                                 return interpolate_logarithmic (before->value, after->value, tdelta / trange, _list.descriptor().lower, _list.descriptor().upper);
433                         case ControlList::Exponential:
434                                 return interpolate_gain (before->value, after->value, tdelta / trange, _list.descriptor().upper);
435                         case ControlList::Curved:
436                                 if (after->coeff) {
437                                         ControlEvent* ev = after;
438                                         double x2 = x * x;
439                                         return ev->coeff[0] + (ev->coeff[1] * x) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x);
440                                 }
441                                 // no break, fallthru
442                         default: // Linear
443                                 return before->value + (vdelta * (tdelta / trange));
444                 }
445         }
446
447         /* x is a control point in the data */
448         /* invalidate the cached range because its not usable */
449         lookup_cache.left = -1;
450         return (*range.first)->value;
451 }
452
453 } // namespace Evoral