Timeline: Simplify fade application/avoid cast to long type.

pull/3/head
Jonathan Moore Liles 2012-06-06 23:43:36 -07:00
parent c6f6c57c05
commit f3505a84de
2 changed files with 20 additions and 41 deletions

View File

@ -60,17 +60,17 @@ public:
return length < rhs.length; return length < rhs.length;
} }
float increment ( void ) const double increment ( void ) const
{ {
return 1.0f / (float)length; return 1.0f / 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 double
gain ( const float fi ) const gain ( const double fi ) const
{ {
switch ( type ) switch ( type )
{ {
@ -87,7 +87,7 @@ public:
} }
} }
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, nframes_t start, nframes_t nframes ) const;
}; };
/* struct Fade_In : public Fade; */ /* struct Fade_In : public Fade; */

View File

@ -33,31 +33,18 @@
/** Apply a (portion of) fade from /start/ to /end/ assuming a /** Apply a (portion of) fade from /start/ to a buffer up to size /nframes/. */
* buffer size of /nframes/. /start/ and /end/ are relative to the
* given buffer, and /start/ may be negative. */
void void
Audio_Region::Fade::apply ( sample_t *buf, Audio_Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const Audio_Region::Fade::apply ( sample_t *buf, Audio_Region::Fade::fade_dir_e dir, nframes_t start, 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 );
if ( ! nframes ) if ( ! nframes )
return; return;
const nframes_t i = start > 0 ? start : 0; nframes_t n = nframes;
const nframes_t e = end > nframes ? nframes : end;
assert( i < nframes ); const double inc = increment();
double fi = start / (double)length;
const float inc = increment();
float fi = ( i - start ) / (float)length;
// buf += i;
buf = &buf[ i ];
nframes_t n = e - i;
assert( i + n <= nframes );
if ( dir == Fade::Out ) if ( dir == Fade::Out )
{ {
@ -94,18 +81,22 @@ Audio_Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channe
/* calculate offsets into file and sample buffer */ /* calculate offsets into file and sample buffer */
nframes_t sofs, ofs, cnt; nframes_t sofs, /* offset into source */
ofs, /* offset into buffer */
cnt;
cnt = nframes; cnt = nframes;
if ( pos < r.start ) if ( pos < r.start )
{ {
/* region starts somewhere after the beginning of this buffer */
sofs = 0; sofs = 0;
ofs = r.start - pos; ofs = r.start - pos;
cnt -= ofs; cnt -= ofs;
} }
else else
{ {
/* region started before this buffer */
ofs = 0; ofs = 0;
sofs = pos - r.start; sofs = pos - r.start;
} }
@ -161,34 +152,22 @@ Audio_Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channe
declick.type = Fade::Linear; declick.type = Fade::Linear;
{ {
assert( cnt <= nframes );
Fade fade; Fade fade;
fade = declick < _fade_in ? _fade_in : declick; fade = declick < _fade_in ? _fade_in : declick;
/* do fade in if necessary */ /* do fade in if necessary */
if ( sofs < fade.length ) if ( sofs < fade.length )
{ fade.apply( buf + ofs, Fade::In, sofs , cnt );
const long d = 0L - (long)sofs;
assert( cnt <= nframes );
fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt );
}
fade = declick < _fade_out ? _fade_out : declick; fade = declick < _fade_out ? _fade_out : declick;
/* do fade out if necessary */ /* do fade out if necessary */
// if ( start + cnt + fade.length > r.end ) if ( start + fade.length > r.offset + r.length )
if ( start + fade.length > ( r.offset + r.length ) ) fade.apply( buf, Fade::Out, ( start + fade.length ) - ( r.offset + r.length ), cnt );
{
const nframes_t d = ( r.offset + r.length ) - start;
assert( cnt <= nframes );
fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
} }
}
// printf( "read %lu frames\n", cnt );
return cnt; return cnt;
} }