1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-12-12 15:46:25 +01:00

Rewrite Envelope::InsertSpace

This commit is contained in:
Paul Licameli
2017-05-28 12:31:35 -04:00
parent 0126918fb5
commit bd4d2dc31d
2 changed files with 46 additions and 16 deletions

View File

@@ -999,39 +999,66 @@ void Envelope::RemoveUnneededPoints(double time, double tolerence)
} }
} }
void Envelope::InsertSpace( double t0, double tlen ) std::pair< int, int > Envelope::ExpandRegion
( double t0, double tlen, double *pLeftVal, double *pRightVal )
// NOFAIL-GUARANTEE // NOFAIL-GUARANTEE
{ {
t0 -= mOffset; // t0 is relative time
// Preserve the left-side limit at the split. double val;
auto val = GetValueRelative( t0 ); const auto range = EqualRange( t0, 0 );
auto range = EqualRange( t0, 0 );
size_t index; // Preserve the left-side limit.
if ( range.first < range.second ) int index = 1 + range.first;
if ( index <= range.second )
// There is already a control point. // There is already a control point.
index = 1 + range.first; ;
else else {
// Make a control point. // Make a control point.
index = 1 + InsertOrReplaceRelative( t0, val ); val = GetValueRelative( t0 );
Insert( range.first, EnvPoint{ t0, val } );
}
// Shift points. // Shift points.
auto len = mEnv.size(); auto len = mEnv.size();
for ( ; index < len; ++index ) { for ( int ii = index; ii < len; ++ii ) {
auto &point = mEnv[ index ]; auto &point = mEnv[ ii ];
point.SetT( point.GetT() + tlen ); point.SetT( point.GetT() + tlen );
} }
// increase track len, before insert or replace,
// since it range chacks the values.
mTrackLen += tlen; mTrackLen += tlen;
// Preserve the right-side limit. // Preserve the right-side limit.
if ( 1 + range.first < range.second ) if ( index < range.second )
// There was a control point already. // There was a control point already.
; ;
else else
InsertOrReplaceRelative( t0 + tlen, val ); // Make a control point.
Insert( index, EnvPoint{ t0 + tlen, val } );
// Make discontinuities at ends, maybe:
if ( pLeftVal )
// Make a discontinuity at the left side of the expansion
Insert( index++, EnvPoint{ t0, *pLeftVal } );
if ( pRightVal )
// Make a discontinuity at the right side of the expansion
Insert( index++, EnvPoint{ t0 + tlen, *pRightVal } );
// Return the range of indices that includes the inside limiting points,
// none, one, or two
return { 1 + range.first, index };
}
void Envelope::InsertSpace( double t0, double tlen )
// NOFAIL-GUARANTEE
{
auto range = ExpandRegion( t0 - mOffset, tlen, nullptr, nullptr );
// Simplify the boundaries if possible
RemoveUnneededPoints( range.second, true );
RemoveUnneededPoints( range.first - 1, false );
} }
int Envelope::Reassign(double when, double value) int Envelope::Reassign(double when, double value)

View File

@@ -154,6 +154,9 @@ public:
void Cap( double sampleDur ); void Cap( double sampleDur );
private: private:
std::pair< int, int > ExpandRegion
( double t0, double tlen, double *pLeftVal, double *pRightVal );
void RemoveUnneededPoints( size_t startAt, bool rightward ); void RemoveUnneededPoints( size_t startAt, bool rightward );
double GetValueRelative(double t) const; double GetValueRelative(double t) const;