mirror of
https://github.com/LMMS/lmms.git
synced 2026-01-21 21:07:56 -05:00
* use c++20 concepts and numbers for lmms_constants.h * replace lmms::numbers::sqrt2 with std::numbers::sqrt2 * replace lmms::numbers::e with std::numbers::e Also replace the only use of lmms::numbers::inv_e with a local constexpr instead * remove lmms::numbers::pi_half and lmms::numbers::pi_sqr They were only used in one or two places each * replace lmms::numbers::pi with std::numbers::pi * add #include <numbers> to every file touched so far This is probably not needed for some of these files. I'll remove those later * Remove lmms::numbers Rest in peace lmms::numbers::tau, my beloved * Add missing #include <numbers> * replace stray use of F_EPSILON with approximatelyEqual() * make many constants inline constexpr A lot of the remaining constants in lmms_constants.h are specific to SaProcessor. If they are only used there, shouldn't they be in SaProcessor.h? * ok then, it's allowed to be signed * remove #include "lmms_constants.h" for files that don't need it - And also move F_EPSILON into lmms_math.h - And also add an overload for fast_rand() to specify a higher and lower bound - And a bunch of other nonsense * ok then, it's allowed to be inferred * ok then, it can accept an integral * fix typo * appease msvc * appease msvc again * Replace linearInterpolate with std::lerp() As well as time travel to undo several foolish decisions and squash tiny commits together * Fix msvc constexpr warnings * Fix msvc float to double truncation warning * Apply two suggestions from code review Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com> * Apply suggestions from code review Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com> * fix silly mistake * Remove SlicerT's dependence on lmms_math.h * Allow more type inference on fastRand() and fastPow10f() * Apply suggestions from code review Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com> * Clean up fastRand() a little bit more --------- Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>
132 lines
3.5 KiB
C++
132 lines
3.5 KiB
C++
/*
|
|
* interpolation.h - fast implementations of several interpolation-algorithms
|
|
*
|
|
* Copyright (c) 2004-2005 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
|
*
|
|
* This file is part of LMMS - https://lmms.io
|
|
*
|
|
* 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.
|
|
*
|
|
* 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.
|
|
*
|
|
* You should have received a copy of the GNU General Public
|
|
* License along with this program (see COPYING); if not, write to the
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
* Boston, MA 02110-1301 USA.
|
|
*
|
|
*/
|
|
|
|
#ifndef LMMS_INTERPOLATION_H
|
|
#define LMMS_INTERPOLATION_H
|
|
|
|
#include <cmath>
|
|
#include <numbers>
|
|
|
|
namespace lmms
|
|
{
|
|
|
|
inline float hermiteInterpolate( float x0, float x1, float x2, float x3,
|
|
float frac_pos )
|
|
{
|
|
const float frsq = frac_pos*frac_pos;
|
|
const float frsq2 = 2*frsq;
|
|
return( ( (x2-x0) *0.5f ) * ( frac_pos * (frsq+1) -frsq2 ) +
|
|
( frsq2*frac_pos - 3*frsq ) * ( x1-x2 ) +
|
|
frsq2 * (frac_pos-1) * ( ( x3-x1 ) * 0.25f ) + x1 );
|
|
|
|
/*
|
|
const float frsq = frac_pos*frac_pos;
|
|
//const float frsq2 = 2*frsq;
|
|
frac_pos *= 0.5;
|
|
const float frcu = frsq*frac_pos;
|
|
return (
|
|
|
|
(frcu - frsq + frac_pos) * ((x2 - x0)) +
|
|
|
|
(4*frcu - 3*frsq) * (x1 - x2)
|
|
//frsq*(2*frac_pos-3) * (x1 - x2)
|
|
|
|
+ (frcu - 0.5*frsq)*((x3 - x1))
|
|
|
|
+ x1
|
|
|
|
);
|
|
*/
|
|
}
|
|
|
|
|
|
|
|
inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x )
|
|
{
|
|
float frsq = x * x;
|
|
float frcu = frsq * v0;
|
|
float t1 = v1 * 3.f + v3;
|
|
|
|
return v1 + (0.5f * frcu + x) * (v2 - frcu * (1.0f / 6.0f) -
|
|
(t1 * (1.0f / 6.0f) - v0) * (1.0f / 3.0f)) + frsq * x * (t1 *
|
|
(1.0f / 6.0f) - 0.5f * v2) + frsq * (0.5f * v2 - v1);
|
|
}
|
|
|
|
|
|
|
|
inline float cosinusInterpolate( float v0, float v1, float x )
|
|
{
|
|
const float f = (1.0f - std::cos(x * std::numbers::pi_v<float>)) * 0.5f;
|
|
return f * (v1 - v0) + v0;
|
|
}
|
|
|
|
|
|
inline float optimalInterpolate( float v0, float v1, float x )
|
|
{
|
|
const float z = x - 0.5f;
|
|
const float even = v1 + v0;
|
|
const float odd = v1 - v0;
|
|
|
|
const float c0 = even * 0.50037842517188658;
|
|
const float c1 = odd * 1.00621089801788210;
|
|
const float c2 = even * -0.004541102062639801;
|
|
const float c3 = odd * -1.57015627178718420;
|
|
|
|
return ((c3 * z + c2) * z + c1) * z + c0;
|
|
}
|
|
|
|
|
|
inline float optimal4pInterpolate( float v0, float v1, float v2, float v3, float x )
|
|
{
|
|
const float z = x - 0.5f;
|
|
const float even1 = v2 + v1;
|
|
const float odd1 = v2 - v1;
|
|
const float even2 = v3 + v0;
|
|
const float odd2 = v3 - v0;
|
|
|
|
const float c0 = even1 * 0.45868970870461956 + even2 * 0.04131401926395584;
|
|
const float c1 = odd1 * 0.48068024766578432 + odd2 * 0.17577925564495955;
|
|
const float c2 = even1 * -0.246185007019907091 + even2 * 0.24614027139700284;
|
|
const float c3 = odd1 * -0.36030925263849456 + odd2 * 0.10174985775982505;
|
|
|
|
return ((c3 * z + c2) * z + c1) * z + c0;
|
|
}
|
|
|
|
|
|
|
|
inline float lagrangeInterpolate( float v0, float v1, float v2, float v3, float x )
|
|
{
|
|
const float c0 = v1;
|
|
const float c1 = v2 - v0 * ( 1.0f / 3.0f ) - v1 * 0.5f - v3 * ( 1.0f / 6.0f );
|
|
const float c2 = 0.5f * (v0 + v2) - v1;
|
|
const float c3 = ( 1.0f/6.0f ) * ( v3 - v0 ) + 0.5f * ( v1 - v2 );
|
|
return ((c3 * x + c2) * x + c1) * x + c0;
|
|
}
|
|
|
|
|
|
|
|
} // namespace lmms
|
|
|
|
#endif // LMMS_INTERPOLATION_H
|