Merge pull request #564 from diizy/master

Optimize linear interpolation function
This commit is contained in:
Tobias Doerffel
2014-04-05 07:43:11 +02:00
9 changed files with 58 additions and 67 deletions

View File

@@ -232,23 +232,13 @@ public:
inline sample_t userWaveSample( const float _sample ) const
{
// Precise implementation
// const float frame = fraction( _sample ) * m_frames;
// const f_cnt_t f1 = static_cast<f_cnt_t>( frame );
// const f_cnt_t f2 = ( f1 + 1 ) % m_frames;
// sample_t waveSample = linearInterpolate( m_data[f1][0],
// m_data[f2][0],
// fraction( frame ) );
// return waveSample;
// Fast implementation
const float frame = _sample * m_frames;
f_cnt_t f1 = static_cast<f_cnt_t>( frame ) % m_frames;
if( f1 < 0 )
{
f1 += m_frames;
}
return m_data[f1][0];
return linearInterpolate( m_data[f1][0], m_data[ (f1 + 1) % m_frames ][0], fraction( frame ) );
}
static QString tryToMakeRelative( const QString & _file );

View File

@@ -23,8 +23,8 @@
*/
#ifndef _INTERPOLATION_H
#define _INTERPOLATION_H
#ifndef INTERPOLATION_H
#define INTERPOLATION_H
#ifndef __USE_XOPEN
#define __USE_XOPEN
@@ -80,14 +80,21 @@ inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x )
inline float cosinusInterpolate( float v0, float v1, float x )
{
float f = cosf( x * ( F_PI_2 ) );
return( v0*f + v1*( 1.0f-f ) );
return( v1 - f * (v1-v0) );
// return( v0*f + v1*( 1.0f-f ) );
}
inline float linearInterpolate( float v0, float v1, float x )
{
return( v0*( 1.0f-x ) + v1*x );
// take advantage of fma function if present in hardware
#ifdef FP_FAST_FMAF
return fmaf( x, v1-v0, v0 );
#else
return x * (v1-v0) + v0;
#endif
}