2019-08-02 23:10:55 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008-2013 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2008-2016 David Robillard <d@drobilla.net>
|
|
|
|
* Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2012-2018 Robin Gareus <robin@gareus.org>
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2019-08-02 23:10:55 -04:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2019-08-02 23:10:55 -04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-10-14 12:10:01 -04:00
|
|
|
*
|
2008-09-18 20:47:49 -04:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2019-08-02 23:10:55 -04:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-09-18 20:47:49 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <float.h>
|
|
|
|
#include <cmath>
|
|
|
|
#include <climits>
|
|
|
|
#include <cfloat>
|
|
|
|
#include <cmath>
|
2013-07-31 20:08:43 -04:00
|
|
|
#include <vector>
|
2008-09-18 20:47:49 -04:00
|
|
|
|
2012-07-25 13:48:55 -04:00
|
|
|
#include <glibmm/threads.h>
|
2008-09-18 20:47:49 -04:00
|
|
|
|
2017-06-19 09:45:47 -04:00
|
|
|
#include "pbd/control_math.h"
|
|
|
|
|
2019-10-25 15:13:51 -04:00
|
|
|
#include "evoral/Curve.h"
|
|
|
|
#include "evoral/ControlList.h"
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace sigc;
|
|
|
|
|
|
|
|
namespace Evoral {
|
|
|
|
|
|
|
|
|
|
|
|
Curve::Curve (const ControlList& cl)
|
|
|
|
: _dirty (true)
|
|
|
|
, _list (cl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-06-03 06:30:26 -04:00
|
|
|
Curve::solve () const
|
2008-09-18 20:47:49 -04:00
|
|
|
{
|
|
|
|
uint32_t npoints;
|
|
|
|
|
|
|
|
if (!_dirty) {
|
|
|
|
return;
|
|
|
|
}
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
if ((npoints = _list.events().size()) > 2) {
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
/* Compute coefficients needed to efficiently compute a constrained spline
|
|
|
|
curve. See "Constrained Cubic Spline Interpolation" by CJC Kruger
|
|
|
|
(www.korf.co.uk/spline.pdf) for more details.
|
|
|
|
*/
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
vector<Temporal::timepos_t> x (npoints);
|
2013-07-23 02:53:57 -04:00
|
|
|
vector<double> y(npoints);
|
2008-09-18 20:47:49 -04:00
|
|
|
uint32_t i;
|
|
|
|
ControlList::EventList::const_iterator xx;
|
|
|
|
|
|
|
|
for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
|
2020-08-24 16:40:03 -04:00
|
|
|
x[i] = (*xx)->when;
|
|
|
|
y[i] = (*xx)->value;
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
double lp0, lp1, fpone;
|
2020-08-24 16:40:03 -04:00
|
|
|
double xd;
|
2008-09-18 20:47:49 -04:00
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
xd = x[0].distance (x[1]).magnitude();
|
|
|
|
lp0 = xd/(y[1] - y[0]);
|
|
|
|
xd = x[1].distance (x[2]).magnitude();
|
|
|
|
lp1 = xd/(y[2] - y[1]);
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
if (lp0*lp1 < 0) {
|
|
|
|
fpone = 0;
|
|
|
|
} else {
|
|
|
|
fpone = 2 / (lp1 + lp0);
|
|
|
|
}
|
|
|
|
|
|
|
|
double fplast = 0;
|
|
|
|
|
|
|
|
for (i = 0, xx = _list.events().begin(); xx != _list.events().end(); ++xx, ++i) {
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
double xdelta; /* gcc is wrong about possible uninitialized use */
|
|
|
|
double xdelta2; /* ditto */
|
|
|
|
double ydelta; /* ditto */
|
|
|
|
double fppL, fppR;
|
|
|
|
double fpi;
|
2020-08-24 16:40:03 -04:00
|
|
|
double xi = x[i].val();
|
|
|
|
double xim1;
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
if (i > 0) {
|
2020-08-24 16:40:03 -04:00
|
|
|
xim1 = x[i-1].val();
|
|
|
|
xdelta = xi - xim1;
|
2008-09-18 20:47:49 -04:00
|
|
|
xdelta2 = xdelta * xdelta;
|
|
|
|
ydelta = y[i] - y[i-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute (constrained) first derivatives */
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
if (i == 0) {
|
|
|
|
|
|
|
|
/* first segment */
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
fplast = ((3 * (y[1] - y[0]) / (2 * (xi - xim1))) - (fpone * 0.5));
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
/* we don't store coefficients for i = 0 */
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else if (i == npoints - 1) {
|
|
|
|
|
|
|
|
/* last segment */
|
|
|
|
|
|
|
|
fpi = ((3 * ydelta) / (2 * xdelta)) - (fplast * 0.5);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
} else {
|
|
|
|
|
|
|
|
/* all other segments */
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
double xip1 = x[i+1].val();
|
|
|
|
|
|
|
|
double slope_before = (xip1 - xi) / (y[i+1] - y[i]);
|
2008-09-18 20:47:49 -04:00
|
|
|
double slope_after = (xdelta / ydelta);
|
|
|
|
|
|
|
|
if (slope_after * slope_before < 0.0) {
|
|
|
|
/* slope changed sign */
|
|
|
|
fpi = 0.0;
|
|
|
|
} else {
|
|
|
|
fpi = 2 / (slope_before + slope_after);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute second derivative for either side of control point `i' */
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
fppL = (((-2 * (fpi + (2 * fplast))) / (xdelta))) +
|
|
|
|
((6 * ydelta) / xdelta2);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
fppR = (2 * ((2 * fpi) + fplast) / xdelta) -
|
|
|
|
((6 * ydelta) / xdelta2);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
/* compute polynomial coefficients */
|
|
|
|
|
|
|
|
double b, c, d;
|
|
|
|
|
2009-10-14 12:10:01 -04:00
|
|
|
d = (fppR - fppL) / (6 * xdelta);
|
2020-08-24 16:40:03 -04:00
|
|
|
c = ((xi * fppL) - (xim1 * fppR))/(2 * xdelta);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
double xim12, xim13;
|
|
|
|
double xi2, xi3;
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
xim12 = xim1 * xim1; /* "x[i-1] squared" */
|
|
|
|
xim13 = xim12 * xim1; /* "x[i-1] cubed" */
|
|
|
|
xi2 = xi * xi; /* "x[i] squared" */
|
|
|
|
xi3 = xi2 * xi; /* "x[i] cubed" */
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
b = (ydelta - (c * (xi2 - xim12)) - (d * (xi3 - xim13))) / xdelta;
|
|
|
|
|
|
|
|
/* store */
|
|
|
|
|
|
|
|
(*xx)->create_coeffs();
|
2020-08-24 16:40:03 -04:00
|
|
|
(*xx)->coeff[0] = y[i-1] - (b * xim1) - (c * xim12) - (d * xim13);
|
2008-09-18 20:47:49 -04:00
|
|
|
(*xx)->coeff[1] = b;
|
|
|
|
(*xx)->coeff[2] = c;
|
|
|
|
(*xx)->coeff[3] = d;
|
|
|
|
|
|
|
|
fplast = fpi;
|
|
|
|
}
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-08-24 16:40:03 -04:00
|
|
|
Curve::rt_safe_get_vector (Temporal::timepos_t const & x0, Temporal::timepos_t const & x1, float *vec, int32_t veclen) const
|
2008-09-18 20:47:49 -04:00
|
|
|
{
|
2015-04-15 12:13:56 -04:00
|
|
|
Glib::Threads::RWLock::ReaderLock lm(_list.lock(), Glib::Threads::TRY_LOCK);
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
if (!lm.locked()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
_get_vector (x0, x1, vec, veclen);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-08-24 16:40:03 -04:00
|
|
|
Curve::get_vector (Temporal::timepos_t const & x0, Temporal::timepos_t const & x1, float *vec, int32_t veclen) const
|
2008-09-18 20:47:49 -04:00
|
|
|
{
|
2015-04-15 12:13:56 -04:00
|
|
|
Glib::Threads::RWLock::ReaderLock lm(_list.lock());
|
2008-09-18 20:47:49 -04:00
|
|
|
_get_vector (x0, x1, vec, veclen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-08-24 16:40:03 -04:00
|
|
|
Curve::_get_vector (Temporal::timepos_t const & x0, Temporal::timepos_t const & x1, float *vec, int32_t veclen) const
|
2008-09-18 20:47:49 -04:00
|
|
|
{
|
2020-08-24 16:40:03 -04:00
|
|
|
double rx, lx, hx;
|
|
|
|
const double start = x0.val();
|
|
|
|
const double end = x1.val();
|
|
|
|
double max_x;
|
|
|
|
double min_x;
|
2008-09-18 20:47:49 -04:00
|
|
|
int32_t i;
|
|
|
|
int32_t original_veclen;
|
|
|
|
int32_t npoints;
|
|
|
|
|
2012-12-01 19:23:03 -05:00
|
|
|
if (veclen == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-30 18:21:12 -05:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
if ((npoints = _list.events().size()) == 0) {
|
2012-12-01 19:23:03 -05:00
|
|
|
/* no events in list, so just fill the entire array with the default value */
|
|
|
|
for (int32_t i = 0; i < veclen; ++i) {
|
2017-06-21 07:40:41 -04:00
|
|
|
vec[i] = _list.descriptor().normal;
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-01 19:23:03 -05:00
|
|
|
if (npoints == 1) {
|
|
|
|
for (int32_t i = 0; i < veclen; ++i) {
|
|
|
|
vec[i] = _list.events().front()->value;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-11-30 18:21:12 -05:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
/* events is now known not to be empty */
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
max_x = _list.events().back()->when.val();
|
|
|
|
min_x = _list.events().front()->when.val();
|
2008-09-18 20:47:49 -04:00
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
if (start > max_x) {
|
2015-10-05 10:17:49 -04:00
|
|
|
/* totally past the end - just fill the entire array with the final value */
|
2012-12-01 19:23:03 -05:00
|
|
|
for (int32_t i = 0; i < veclen; ++i) {
|
|
|
|
vec[i] = _list.events().back()->value;
|
|
|
|
}
|
|
|
|
return;
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
if (end < min_x) {
|
2012-12-01 19:23:03 -05:00
|
|
|
/* totally before the first event - fill the entire array with
|
|
|
|
* the initial value.
|
|
|
|
*/
|
|
|
|
for (int32_t i = 0; i < veclen; ++i) {
|
|
|
|
vec[i] = _list.events().front()->value;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
original_veclen = veclen;
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
if (start < min_x) {
|
2008-09-18 20:47:49 -04:00
|
|
|
|
2009-10-14 12:10:01 -04:00
|
|
|
/* fill some beginning section of the array with the
|
|
|
|
initial (used to be default) value
|
2008-09-18 20:47:49 -04:00
|
|
|
*/
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
double frac = (min_x - start) / (end - start);
|
2012-11-30 18:49:59 -05:00
|
|
|
int64_t fill_len = (int64_t) floor (veclen * frac);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2012-11-30 18:49:59 -05:00
|
|
|
fill_len = min (fill_len, (int64_t)veclen);
|
2012-12-09 12:24:58 -05:00
|
|
|
|
2012-11-30 18:49:59 -05:00
|
|
|
for (i = 0; i < fill_len; ++i) {
|
2008-09-18 20:47:49 -04:00
|
|
|
vec[i] = _list.events().front()->value;
|
|
|
|
}
|
|
|
|
|
2012-11-30 18:49:59 -05:00
|
|
|
veclen -= fill_len;
|
|
|
|
vec += fill_len;
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
if (veclen && end > max_x) {
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
/* fill some end section of the array with the default or final value */
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
double frac = (end - max_x) / (end - start);
|
2012-11-30 18:49:59 -05:00
|
|
|
int64_t fill_len = (int64_t) floor (original_veclen * frac);
|
2008-09-18 20:47:49 -04:00
|
|
|
float val;
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2012-11-30 18:49:59 -05:00
|
|
|
fill_len = min (fill_len, (int64_t)veclen);
|
2008-09-18 20:47:49 -04:00
|
|
|
val = _list.events().back()->value;
|
|
|
|
|
2012-11-30 18:49:59 -05:00
|
|
|
for (i = veclen - fill_len; i < veclen; ++i) {
|
2008-09-18 20:47:49 -04:00
|
|
|
vec[i] = val;
|
|
|
|
}
|
|
|
|
|
2012-11-30 18:49:59 -05:00
|
|
|
veclen -= fill_len;
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
lx = max (min_x, start);
|
|
|
|
hx = min (max_x, end);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2009-10-22 23:58:11 -04:00
|
|
|
if (npoints == 2) {
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
const double lpos = _list.events().front()->when.val();
|
2017-06-19 09:45:47 -04:00
|
|
|
const double lval = _list.events().front()->value;
|
2020-08-24 16:40:03 -04:00
|
|
|
const double upos = _list.events().back()->when.val();
|
2017-06-19 09:45:47 -04:00
|
|
|
const double uval = _list.events().back()->value;
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2012-04-16 12:32:22 -04:00
|
|
|
/* dx that we are using */
|
|
|
|
if (veclen > 1) {
|
2017-06-19 09:45:47 -04:00
|
|
|
const double dx_num = hx - lx;
|
|
|
|
const double dx_den = veclen - 1;
|
|
|
|
const double lower = _list.descriptor().lower;
|
|
|
|
const double upper = _list.descriptor().upper;
|
|
|
|
|
|
|
|
/* gradient of the line */
|
|
|
|
const double m_num = uval - lval;
|
|
|
|
const double m_den = upos - lpos;
|
|
|
|
/* y intercept of the line */
|
|
|
|
const double c = uval - (m_num * upos / m_den);
|
|
|
|
|
|
|
|
switch (_list.interpolation()) {
|
|
|
|
case ControlList::Logarithmic:
|
|
|
|
for (int i = 0; i < veclen; ++i) {
|
|
|
|
const double fraction = (lx - lpos + i * dx_num / dx_den) / m_den;
|
|
|
|
vec[i] = interpolate_logarithmic (lval, uval, fraction, lower, upper);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ControlList::Exponential:
|
|
|
|
for (int i = 0; i < veclen; ++i) {
|
|
|
|
const double fraction = (lx - lpos + i * dx_num / dx_den) / m_den;
|
|
|
|
vec[i] = interpolate_gain (lval, uval, fraction, upper);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ControlList::Discrete:
|
|
|
|
// any discrete vector curves somewhere?
|
|
|
|
assert (0);
|
|
|
|
case ControlList::Curved:
|
2019-09-18 11:27:09 -04:00
|
|
|
/* no 2 point spline */
|
|
|
|
/* fallthrough */
|
2017-06-19 09:45:47 -04:00
|
|
|
default: // Linear:
|
|
|
|
for (int i = 0; i < veclen; ++i) {
|
|
|
|
vec[i] = (lx * (m_num / m_den) + m_num * i * dx_num / (m_den * dx_den)) + c;
|
|
|
|
}
|
|
|
|
break;
|
2012-04-16 12:32:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-19 09:45:47 -04:00
|
|
|
double fraction = (lx - lpos) / (upos - lpos);
|
|
|
|
switch (_list.interpolation()) {
|
|
|
|
case ControlList::Logarithmic:
|
|
|
|
vec[0] = interpolate_logarithmic (lval, uval, fraction, _list.descriptor().lower, _list.descriptor().upper);
|
|
|
|
break;
|
|
|
|
case ControlList::Exponential:
|
|
|
|
vec[0] = interpolate_gain (lval, uval, fraction, _list.descriptor().upper);
|
|
|
|
break;
|
|
|
|
case ControlList::Discrete:
|
|
|
|
// any discrete vector curves somewhere?
|
|
|
|
assert (0);
|
|
|
|
case ControlList::Curved:
|
2019-09-18 11:27:09 -04:00
|
|
|
/* no 2 point spline */
|
|
|
|
/* fallthrough */
|
2017-06-19 09:45:47 -04:00
|
|
|
default: // Linear:
|
|
|
|
vec[0] = interpolate_linear (lval, uval, fraction);
|
|
|
|
break;
|
|
|
|
}
|
2009-10-22 23:58:11 -04:00
|
|
|
}
|
2012-04-16 12:32:22 -04:00
|
|
|
|
2009-10-22 23:58:11 -04:00
|
|
|
return;
|
|
|
|
}
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
if (_dirty) {
|
|
|
|
solve ();
|
|
|
|
}
|
|
|
|
|
|
|
|
rx = lx;
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
double dx = 0.;
|
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
if (veclen > 1) {
|
2010-09-19 20:57:44 -04:00
|
|
|
dx = (hx - lx) / (veclen - 1);
|
|
|
|
}
|
2008-09-18 20:47:49 -04:00
|
|
|
|
2010-09-19 20:57:44 -04:00
|
|
|
for (i = 0; i < veclen; ++i, rx += dx) {
|
2020-08-24 16:40:03 -04:00
|
|
|
vec[i] = multipoint_eval (x0.is_beats() ? Temporal::timepos_t::from_ticks (rx) : Temporal::timepos_t::from_superclock (rx));
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2020-08-24 16:40:03 -04:00
|
|
|
Curve::multipoint_eval (Temporal::timepos_t const & x) const
|
2009-10-14 12:10:01 -04:00
|
|
|
{
|
2008-09-18 20:47:49 -04:00
|
|
|
pair<ControlList::EventList::const_iterator,ControlList::EventList::const_iterator> range;
|
|
|
|
|
|
|
|
ControlList::LookupCache& lookup_cache = _list.lookup_cache();
|
|
|
|
|
2020-12-03 23:35:19 -05:00
|
|
|
if ((lookup_cache.left == Temporal::timepos_t::max (_list.time_domain())) ||
|
2009-10-14 12:10:01 -04:00
|
|
|
((lookup_cache.left > x) ||
|
|
|
|
(lookup_cache.range.first == _list.events().end()) ||
|
2008-09-18 20:47:49 -04:00
|
|
|
((*lookup_cache.range.second)->when < x))) {
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
ControlEvent cp (x, 0.0);
|
|
|
|
|
|
|
|
lookup_cache.range = equal_range (_list.events().begin(), _list.events().end(), &cp, ControlList::time_comparator);
|
|
|
|
}
|
|
|
|
|
|
|
|
range = lookup_cache.range;
|
|
|
|
|
2009-10-14 12:10:01 -04:00
|
|
|
/* EITHER
|
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
a) x is an existing control point, so first == existing point, second == next point
|
|
|
|
|
|
|
|
OR
|
|
|
|
|
|
|
|
b) x is between control points, so range is empty (first == second, points to where
|
|
|
|
to insert x)
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (range.first == range.second) {
|
|
|
|
|
|
|
|
/* x does not exist within the list as a control point */
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
lookup_cache.left = x;
|
|
|
|
|
|
|
|
if (range.first == _list.events().begin()) {
|
|
|
|
/* we're before the first point */
|
|
|
|
// return default_value;
|
2012-05-23 21:43:02 -04:00
|
|
|
return _list.events().front()->value;
|
2008-09-18 20:47:49 -04:00
|
|
|
}
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-09-18 20:47:49 -04:00
|
|
|
if (range.second == _list.events().end()) {
|
|
|
|
/* we're after the last point */
|
|
|
|
return _list.events().back()->value;
|
|
|
|
}
|
|
|
|
|
2012-12-09 12:24:58 -05:00
|
|
|
ControlEvent* after = (*range.second);
|
|
|
|
range.second--;
|
|
|
|
ControlEvent* before = (*range.second);
|
|
|
|
|
|
|
|
double vdelta = after->value - before->value;
|
|
|
|
|
|
|
|
if (vdelta == 0.0) {
|
|
|
|
return before->value;
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
double aw = after->when.val();
|
|
|
|
double bw = before->when.val();
|
|
|
|
|
|
|
|
double tdelta = x.val() - bw;
|
|
|
|
double trange = aw - bw;
|
2014-05-30 11:00:28 -04:00
|
|
|
|
2017-06-19 09:45:47 -04:00
|
|
|
switch (_list.interpolation()) {
|
|
|
|
case ControlList::Discrete:
|
|
|
|
return before->value;
|
|
|
|
case ControlList::Logarithmic:
|
|
|
|
return interpolate_logarithmic (before->value, after->value, tdelta / trange, _list.descriptor().lower, _list.descriptor().upper);
|
|
|
|
case ControlList::Exponential:
|
|
|
|
return interpolate_gain (before->value, after->value, tdelta / trange, _list.descriptor().upper);
|
|
|
|
case ControlList::Curved:
|
|
|
|
if (after->coeff) {
|
|
|
|
ControlEvent* ev = after;
|
2021-01-19 18:51:16 -05:00
|
|
|
|
|
|
|
/* As of Jan 2020, we only use Curved
|
|
|
|
* for fade in/out curves (of audio
|
|
|
|
* regions).
|
|
|
|
*
|
|
|
|
* This means that x is a relatively
|
2021-01-19 18:51:57 -05:00
|
|
|
* small value (an offset into the
|
2021-01-19 18:51:16 -05:00
|
|
|
* fade) amd we do not need to worry
|
|
|
|
* about the square overflowing.
|
|
|
|
*/
|
|
|
|
|
2020-08-24 16:40:03 -04:00
|
|
|
double x2 = x.val() * x.val();
|
|
|
|
return ev->coeff[0] + (ev->coeff[1] * x.val()) + (ev->coeff[2] * x2) + (ev->coeff[3] * x2 * x.val());
|
2017-06-19 09:45:47 -04:00
|
|
|
}
|
2019-09-18 11:27:09 -04:00
|
|
|
/* fallthrough */
|
2018-10-26 08:30:26 -04:00
|
|
|
case ControlList::Linear:
|
2017-06-19 09:45:47 -04:00
|
|
|
return before->value + (vdelta * (tdelta / trange));
|
2014-05-29 22:25:59 -04:00
|
|
|
}
|
2009-10-14 12:10:01 -04:00
|
|
|
}
|
2008-09-18 20:47:49 -04:00
|
|
|
|
|
|
|
/* x is a control point in the data */
|
|
|
|
/* invalidate the cached range because its not usable */
|
2020-12-03 23:35:19 -05:00
|
|
|
lookup_cache.left = Temporal::timepos_t::max (_list.time_domain());
|
2008-09-18 20:47:49 -04:00
|
|
|
return (*range.first)->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Evoral
|