Optimize fade calculations.

This commit is contained in:
Jonathan Moore Liles 2008-04-16 00:50:32 -05:00
parent 14330f607d
commit fc2ed291aa
2 changed files with 47 additions and 38 deletions

View File

@ -515,10 +515,16 @@ Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool line, int X, in
fl_vertex( 0.0, 0.0 ); fl_vertex( 0.0, 0.0 );
fl_vertex( 0.0, 1.0 ); fl_vertex( 0.0, 1.0 );
// if ( draw_real_fade_curve )
{
nframes_t tsx = timeline->x_to_ts( 1 ); nframes_t tsx = timeline->x_to_ts( 1 );
nframes_t ts = 0; nframes_t ts = 0;
for ( int i = 0; i < width; ++i, ts += tsx ) for ( int i = 0; i < width; ++i, ts += tsx )
fl_vertex( i / (float)width, 1.0f - fade.gain( ts ) ); fl_vertex( i / (float)width, 1.0f - fade.gain( ts / (float)fade.length ) );
}
fl_vertex( 1.0, 0.0 ); fl_vertex( 1.0, 0.0 );
@ -695,17 +701,27 @@ Region::normalize ( void )
void void
Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
{ {
printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end ); // printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
nframes_t i = start > 0 ? start : 0; const nframes_t i = start > 0 ? start : 0;
nframes_t e = end > nframes ? nframes : end; const nframes_t e = end > nframes ? nframes : end;
const float inc = increment();
float fi = ( i - start ) / (float)length;
buf += i;
nframes_t n = e - i;
if ( dir == Fade::Out ) if ( dir == Fade::Out )
for ( ; i < e; ++i ) {
buf[ i ] *= gain( (length - 1) - (i - start) ); fi = 1.0f - fi;
for ( ; n--; fi -= inc )
*(buf++) *= gain( fi );
}
else else
for ( ; i < e; ++i ) for ( ; n--; fi += inc )
buf[ i ] *= gain( i - start ); *(buf++) *= gain( fi );
} }

View File

@ -59,38 +59,31 @@ public:
return length < rhs.length; return length < rhs.length;
} }
float increment ( void ) const
{
return 1.0f / (float)length;
}
/** Return gain for frame /index/ of /nframes/ on a gain curve /** Return gain for frame /index/ of /nframes/ on a gain curve
* of type /type/.*/ * of type /type/.*/
/* FIXME: calling a function per sample is bad, switching on /* FIXME: calling a function per sample is bad, switching on
* type mid fade is bad. */ * type mid fade is bad. */
inline float inline float
gain ( nframes_t index ) const gain ( const float fi ) const
{ {
float g;
const float fi = index / (float)length;
switch ( type ) switch ( type )
{ {
case Linear: case Linear:
g = fi; return fi;
break;
case Sigmoid: case Sigmoid:
g = (1.0f - cos( fi * M_PI )) / 2.0f; return (1.0f - cos( fi * M_PI )) / 2.0f;
break;
case Logarithmic: case Logarithmic:
/* FIXME: this is wrong */ return pow( 0.1f, (1.0f - fi) * 3.0f );
g = pow( 0.1f, (1.0f - fi) * 3.0f );
break;
case Parabolic: case Parabolic:
g = 1.0f - (1.0f - fi) * (1.0f - fi); return 1.0f - (1.0f - fi) * (1.0f - fi);
break;
default: default:
g = 1.0f; return 1.0f;
} }
return g;
} }
void apply ( sample_t *buf, fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const; void apply ( sample_t *buf, fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const;