diff --git a/include/lmms_math.h b/include/lmms_math.h index b82e589a7..d7ca24561 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -120,5 +120,12 @@ static inline double fastPow( double a, double b ) return u.d; } +// sinc function +static inline double sinc( double _x ) +{ + return sin( F_PI * _x ) / ( F_PI * _x ); +} + + #endif diff --git a/src/core/BandLimitedWave.cpp b/src/core/BandLimitedWave.cpp index 65cc39119..cfa0a3c7d 100644 --- a/src/core/BandLimitedWave.cpp +++ b/src/core/BandLimitedWave.cpp @@ -44,13 +44,15 @@ void BandLimitedWave::generateWaves() { int harm = 1; double s = 0.0f; + double hlen; do { - const double amp = -1.0 / static_cast( harm ); + hlen = static_cast( len ) / static_cast( harm ); + const double amp = -1.0 / static_cast( harm ) * ( hlen <= 4 ? 0.5 : 1.0 ) * ( hlen < 8 ? 0.75 : 1.0 ); const double a2 = cos( om * harm * F_2PI ); s += amp * a2 * sin( static_cast( ph * harm ) / static_cast( len ) * F_2PI ); harm++; - } while( len/harm > 2 ); + } while( hlen >= 2.0 ); s_waveforms[ BandLimitedWave::BLSaw ].setSampleAt( i, ph, s ); max = qMax( max, qAbs( s ) ); } @@ -73,13 +75,15 @@ void BandLimitedWave::generateWaves() { int harm = 1; double s = 0.0f; + double hlen; do { - const double amp = 1.0 / static_cast( harm ); + hlen = static_cast( len ) / static_cast( harm ); + const double amp = 1.0 / static_cast( harm ) * ( hlen <= 4 ? 0.5 : 1.0 ) * ( hlen < 8 ? 0.75 : 1.0 ); const double a2 = cos( om * harm * F_2PI ); s += amp * a2 * sin( static_cast( ph * harm ) / static_cast( len ) * F_2PI ); harm += 2; - } while( len/harm > 2 ); + } while( hlen >= 2.0 ); s_waveforms[ BandLimitedWave::BLSquare ].setSampleAt( i, ph, s ); max = qMax( max, qAbs( s ) ); } @@ -103,14 +107,16 @@ void BandLimitedWave::generateWaves() { int harm = 1; double s = 0.0f; + double hlen; do { - const double amp = 1.0 / static_cast( harm * harm ); + hlen = static_cast( len ) / static_cast( harm ); + const double amp = 1.0 / static_cast( harm * harm ) * ( hlen <= 4 ? 0.5 : 1.0 ) * ( hlen < 8 ? 0.75 : 1.0 ); //const double a2 = cos( om * harm * F_2PI ); s += amp * /*a2 **/ sin( ( static_cast( ph * harm ) / static_cast( len ) + ( ( harm + 1 ) % 4 == 0 ? 0.5 : 0.0 ) ) * F_2PI ); harm += 2; - } while( len/harm > 2 ); + } while( hlen >= 2.0 ); s_waveforms[ BandLimitedWave::BLTriangle ].setSampleAt( i, ph, s ); max = qMax( max, qAbs( s ) ); }