From 507cee7ee52823f72824d20086a370b99dc8203b Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Mon, 1 Feb 2016 16:01:14 -0500 Subject: [PATCH] One less indirection accessing Region --- src/Project.cpp | 65 ++++++++++++----------------- src/WaveTrack.cpp | 13 +++--- src/WaveTrack.h | 15 ++++--- src/effects/TruncSilence.cpp | 80 +++++++++++++++--------------------- src/effects/TruncSilence.h | 5 +-- 5 files changed, 77 insertions(+), 101 deletions(-) diff --git a/src/Project.cpp b/src/Project.cpp index abf5ecb23..9df141fe3 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -4476,32 +4476,26 @@ void AudacityProject::GetRegionsByLabel( Regions ®ions ) const LabelStruct *ls = lt->GetLabel( i ); if( ls->selectedRegion.t0() >= mViewInfo.selectedRegion.t0() && ls->selectedRegion.t1() <= mViewInfo.selectedRegion.t1() ) - { - Region *region = new Region; - region->start = ls->getT0(); - region->end = ls->getT1(); - regions.Add( region ); - } + regions.push_back(Region(ls->getT0(), ls->getT1())); } } //anything to do ? - if( regions.GetCount() == 0 ) + if( regions.size() == 0 ) return; //sort and remove unnecessary regions - regions.Sort( Region::cmp ); + std::sort(regions.begin(), regions.end()); unsigned int selected = 1; - while( selected < regions.GetCount() ) + while( selected < regions.size() ) { - Region *cur = regions.Item( selected ); - Region *last = regions.Item( selected - 1 ); - if( cur->start < last->end ) + const Region &cur = regions.at( selected ); + Region &last = regions.at( selected - 1 ); + if( cur.start < last.end ) { - if( cur->end > last->end ) - last->end = cur->end; - delete cur; - regions.RemoveAt( selected ); + if( cur.end > last.end ) + last.end = cur.end; + regions.erase( regions.begin() + selected ); } else selected++; @@ -4520,7 +4514,7 @@ void AudacityProject::EditByLabel( EditFunction action, Regions regions; GetRegionsByLabel( regions ); - if( regions.GetCount() == 0 ) + if( regions.size() == 0 ) return; TrackListIterator iter( mTracks ); @@ -4546,15 +4540,13 @@ void AudacityProject::EditByLabel( EditFunction action, (allTracks || n->GetSelected() || (bSyncLockedTracks && n->IsSyncLockSelected()))) { WaveTrack *wt = ( WaveTrack* )n; - for( int i = ( int )regions.GetCount() - 1; i >= 0; i-- ) - ( wt->*action )( regions.Item( i )->start, regions.Item( i )->end ); + for (int i = (int)regions.size() - 1; i >= 0; i--) { + const Region ®ion = regions.at(i); + (wt->*action)(region.start, region.end); + } } n = iter.Next(); } - - //delete label regions - for( unsigned int i = 0; i < regions.GetCount(); i++ ) - delete regions.Item( i ); } //Executes the edit function on all selected wave tracks with @@ -4568,7 +4560,7 @@ void AudacityProject::EditClipboardByLabel( EditDestFunction action ) Regions regions; GetRegionsByLabel( regions ); - if( regions.GetCount() == 0 ) + if( regions.size() == 0 ) return; TrackListIterator iter( mTracks ); @@ -4594,10 +4586,11 @@ void AudacityProject::EditClipboardByLabel( EditDestFunction action ) { WaveTrack *wt = ( WaveTrack* )n; WaveTrack *merged = NULL; - for( int i = ( int )regions.GetCount() - 1; i >= 0; i-- ) + for( int i = ( int )regions.size() - 1; i >= 0; i-- ) { Track *dest = NULL; - ( wt->*action )( regions.Item( i )->start, regions.Item( i )->end, + const Region ®ion = regions.at(i); + ( wt->*action )( region.start, region.end, &dest ); if( dest ) { @@ -4610,10 +4603,9 @@ void AudacityProject::EditClipboardByLabel( EditDestFunction action ) { // Paste to the beginning; unless this is the first region, // offset the track to account for time between the regions - if (i < (int)regions.GetCount() - 1) { + if (i < (int)regions.size() - 1) merged->Offset( - regions.Item(i+1)->start - regions.Item(i)->end); - } + regions.at(i + 1).start - region.end); bool bResult = merged->Paste( 0.0 , dest ); wxASSERT(bResult); // TO DO: Actually handle this. @@ -4621,21 +4613,18 @@ void AudacityProject::EditClipboardByLabel( EditDestFunction action ) } } else // nothing copied but there is a 'region', so the 'region' must be a 'point label' so offset - if (i < (int)regions.GetCount() - 1) - if( merged ) - merged->Offset(regions.Item(i+1)->start - regions.Item(i)->end); + if (i < (int)regions.size() - 1) + if (merged) + merged->Offset( + regions.at(i + 1).start - region.end); } if( merged ) msClipboard->Add( merged ); } } - msClipT0 = regions.Item(0)->start; - msClipT1 = regions.Item(regions.GetCount() - 1)->end; - - //delete label regions - for( unsigned int i = 0; i < regions.GetCount(); i++ ) - delete regions.Item( i ); + msClipT0 = regions.front().start; + msClipT1 = regions.back().end; } diff --git a/src/WaveTrack.cpp b/src/WaveTrack.cpp index 3f1465e4a..4b71c9954 100644 --- a/src/WaveTrack.cpp +++ b/src/WaveTrack.cpp @@ -1481,10 +1481,9 @@ bool WaveTrack::Disjoin(double t0, double t1) seqEnd = curSamplePos; if( seqEnd - seqStart + 1 > minSamples ) { - Region *region = new Region; - region->start = seqStart / GetRate() + clip->GetStartTime(); - region->end = seqEnd / GetRate() + clip->GetStartTime(); - regions.Add( region ); + regions.push_back(Region( + seqStart / GetRate() + clip->GetStartTime(), + seqEnd / GetRate() + clip->GetStartTime())); } seqStart = -1; } @@ -1493,10 +1492,10 @@ bool WaveTrack::Disjoin(double t0, double t1) } } - for( unsigned int i = 0; i < regions.GetCount(); i++ ) + for( unsigned int i = 0; i < regions.size(); i++ ) { - SplitDelete( regions.Item( i )->start, regions.Item( i )->end ); - delete regions.Item( i ); + const Region ®ion = regions.at(i); + SplitDelete(region.start, region.end ); } delete[] buffer; diff --git a/src/WaveTrack.h b/src/WaveTrack.h index 7c4b6d298..8261206ba 100644 --- a/src/WaveTrack.h +++ b/src/WaveTrack.h @@ -17,6 +17,7 @@ #include "Experimental.h" #include "widgets/ProgressDialog.h" +#include #include #include #include @@ -40,17 +41,21 @@ class TimeWarper; /// \brief Structure to hold region of a wavetrack and a comparison function /// for sortability. -typedef struct REGION +struct Region { + Region() : start(0), end(0) {} + Region(double start_, double end_) : start(start_), end(end_) {} + double start, end; //used for sorting - static int cmp( REGION **a, REGION **b ) + bool operator < (const Region &b) const { - return ( ( *a )->start < ( *b )->start ) ? -1 : 1; + return this->start < b.start; } -}Region; -WX_DEFINE_ARRAY( Region*, Regions ); +}; + +class Regions : public std::vector < Region > {}; class Envelope; diff --git a/src/effects/TruncSilence.cpp b/src/effects/TruncSilence.cpp index db45b71e7..9d4126ec0 100644 --- a/src/effects/TruncSilence.cpp +++ b/src/effects/TruncSilence.cpp @@ -19,7 +19,7 @@ #include "TruncSilence.h" #include - +#include #include #include @@ -33,6 +33,9 @@ #include "../WaveTrack.h" #include "../widgets/valnum.h" +// Declaration of RegionList +class RegionList : public std::list < Region > {}; + enum kActions { kTruncate, @@ -71,9 +74,6 @@ static const double DEF_MinTruncMs = 0.001; // Typical fraction of total time taken by detection (better to guess low) const double detectFrac = 0.4; -#include -WX_DEFINE_LIST(RegionList); - BEGIN_EVENT_TABLE(EffectTruncSilence, wxEvtHandler) EVT_CHOICE(wxID_ANY, EffectTruncSilence::OnControlChange) EVT_TEXT(wxID_ANY, EffectTruncSilence::OnControlChange) @@ -178,13 +178,9 @@ double EffectTruncSilence::CalcPreviewInputLength(double /* previewLength */) // Master list of silent regions RegionList silences; - silences.DeleteContents(true); // Start with the whole selection silent - Region *sel = new Region; - sel->start = mT0; - sel->end = mT1; - silences.push_back(sel); + silences.push_back(Region(mT0, mT1)); SelectedTrackListOfKindIterator iter(Track::Wave, mTracks); int whichTrack = 0; @@ -193,7 +189,6 @@ double EffectTruncSilence::CalcPreviewInputLength(double /* previewLength */) WaveTrack *const wt = static_cast(t); RegionList trackSilences; - trackSilences.DeleteContents(true); sampleCount index = wt->TimeToLongSamples(mT0); sampleCount silentFrame = 0; // length of the current silence @@ -318,7 +313,6 @@ bool EffectTruncSilence::ProcessIndependently() Track *const last = link ? link : track; RegionList silences; - silences.DeleteContents(true); if (!FindSilences(silences, track, last)) return false; @@ -350,10 +344,9 @@ bool EffectTruncSilence::ProcessAll() // Copy tracks CopyInputTracks(Track::All); - // Master list of silent regions; it is responsible for deleting them. + // Master list of silent regions. // This list should always be kept in order. RegionList silences; - silences.DeleteContents(true); SelectedTrackListOfKindIterator iter(Track::Wave, mTracks); if (FindSilences(silences, iter.First(), iter.Last())) { @@ -373,10 +366,7 @@ bool EffectTruncSilence::FindSilences (RegionList &silences, Track *firstTrack, Track *lastTrack) { // Start with the whole selection silent - Region *sel = new Region; - sel->start = mT0; - sel->end = mT1; - silences.push_back(sel); + silences.push_back(Region(mT0, mT1)); // Remove non-silent regions in each track SelectedTrackListOfKindIterator iter(Track::Wave, mTracks); @@ -395,7 +385,6 @@ bool EffectTruncSilence::FindSilences // Scan the track for silences // RegionList trackSilences; - trackSilences.DeleteContents(true); sampleCount index = wt->TimeToLongSamples(mT0); sampleCount silentFrame = 0; @@ -413,10 +402,10 @@ bool EffectTruncSilence::FindSilences if (silentFrame >= minSilenceFrames) { // Track ended in silence -- record region - Region *r = new Region; - r->start = wt->LongSamplesToTime(index - silentFrame); - r->end = wt->LongSamplesToTime(index); - trackSilences.push_back(r); + trackSilences.push_back(Region( + wt->LongSamplesToTime(index - silentFrame), + wt->LongSamplesToTime(index) + )); } // Intersect with the overall silent region list @@ -441,7 +430,8 @@ bool EffectTruncSilence::DoRemoval RegionList::const_reverse_iterator rit; for (rit = silences.rbegin(); rit != silences.rend(); ++rit) { - Region *r = *rit; + const Region ®ion = *rit; + const Region *const r = ®ion; // Progress dialog and cancellation. Do additional cleanup before return. const double frac = detectFrac + @@ -597,7 +587,7 @@ bool EffectTruncSilence::Analyze(RegionList& silenceList, double curTime = wt->LongSamplesToTime(*index); for ( ; rit != silenceList.end(); ++rit) { // Find the first silent region ending after current time - if ((*rit)->end >= curTime) { + if (rit->end >= curTime) { break; } } @@ -613,16 +603,16 @@ bool EffectTruncSilence::Analyze(RegionList& silenceList, break; } - else if ((*rit)->start > curTime) { + else if (rit->start > curTime) { // End current silent region, skip ahead if (*silentFrame >= minSilenceFrames) { - Region *r = new Region; - r->start = wt->LongSamplesToTime(*index - *silentFrame); - r->end = wt->LongSamplesToTime(*index); - trackSilences.push_back(r); + trackSilences.push_back(Region( + wt->LongSamplesToTime(*index - *silentFrame), + wt->LongSamplesToTime(*index) + )); } *silentFrame = 0; - sampleCount newIndex = wt->TimeToLongSamples((*rit)->start); + sampleCount newIndex = wt->TimeToLongSamples(rit->start); if (inputLength) { sampleCount requiredTrackSamples = previewLen - outLength; // Add non-silent sample to outLength @@ -673,7 +663,10 @@ bool EffectTruncSilence::Analyze(RegionList& silenceList, Region *r = new Region; r->start = wt->LongSamplesToTime(*index + i - *silentFrame); r->end = wt->LongSamplesToTime(*index + i); - trackSilences.push_back(r); + trackSilences.push_back(Region( + wt->LongSamplesToTime(*index + i - *silentFrame), + wt->LongSamplesToTime(*index + i) + )); } else if (inputLength) { // included as part of non-silence outLength += *silentFrame; @@ -805,7 +798,7 @@ void EffectTruncSilence::Intersect(RegionList &dest, const RegionList &src) // Any time we reach the end of the dest list we're finished if (destIter == dest.end()) return; - Region *curDest = *destIter; + RegionList::iterator curDest = destIter; // Operation: find non-silent regions in src, remove them from dest. double nsStart = curDest->start; @@ -824,17 +817,16 @@ void EffectTruncSilence::Intersect(RegionList &dest, const RegionList &src) while (srcIter != src.end() || lastRun) { // Don't use curSrc unless lastRun is false! - Region *curSrc; + RegionList::const_iterator curSrc; if (lastRun) { // The last non-silent region extends as far as possible - curSrc = NULL; nsEnd = std::numeric_limits::max(); } else { - curSrc = *srcIter; + curSrc = srcIter; nsEnd = curSrc->start; } @@ -848,16 +840,14 @@ void EffectTruncSilence::Intersect(RegionList &dest, const RegionList &src) { return; } - curDest = *destIter; + curDest = destIter; } // Check for splitting dest region in two if (nsStart > curDest->start && nsEnd < curDest->end) { // The second region - Region *r = new Region; - r->start = nsEnd; - r->end = curDest->end; + Region r(nsEnd, curDest->end); // The first region curDest->end = nsStart; @@ -871,16 +861,12 @@ void EffectTruncSilence::Intersect(RegionList &dest, const RegionList &src) // versions it returns the wrong value. Second, in some versions, // it crashes when you insert at list end. if (nextIt == dest.end()) - { - dest.Append(r); - } + dest.push_back(r); else - { dest.insert(nextIt, r); - } ++destIter; // (now points at the newly-inserted region) - curDest = *destIter; + curDest = destIter; } // Check for truncating the end of dest region @@ -894,7 +880,7 @@ void EffectTruncSilence::Intersect(RegionList &dest, const RegionList &src) { return; } - curDest = *destIter; + curDest = destIter; } // Check for all dest regions that need to be removed completely @@ -905,7 +891,7 @@ void EffectTruncSilence::Intersect(RegionList &dest, const RegionList &src) { return; } - curDest = *destIter; + curDest = destIter; } // Check for truncating the beginning of dest region diff --git a/src/effects/TruncSilence.h b/src/effects/TruncSilence.h index 0d89a494a..5fd68587d 100644 --- a/src/effects/TruncSilence.h +++ b/src/effects/TruncSilence.h @@ -31,10 +31,7 @@ class wxCheckBox; #define TRUNCATESILENCE_PLUGIN_SYMBOL XO("Truncate Silence") -// Declaration of RegionList -struct REGION; -typedef struct REGION Region; -WX_DECLARE_LIST(Region, RegionList); +class RegionList; class EffectTruncSilence : public Effect {