From c7ab9f2338810ba8a1ee16a2fbf1e52df2306dc4 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 18 Sep 2018 11:58:44 -0400 Subject: [PATCH] Rewrite many iterations over tracks and channels in src/commands --- src/commands/CompareAudioCommand.cpp | 8 +-- src/commands/GetInfoCommand.cpp | 82 +++++++++++----------------- src/commands/ScreenshotCommand.cpp | 8 +-- src/commands/SelectCommand.cpp | 41 +++++++------- src/commands/SetLabelCommand.cpp | 26 ++++----- src/commands/SetTrackInfoCommand.cpp | 24 ++++---- 6 files changed, 77 insertions(+), 112 deletions(-) diff --git a/src/commands/CompareAudioCommand.cpp b/src/commands/CompareAudioCommand.cpp index 687daaf85..067bf555e 100644 --- a/src/commands/CompareAudioCommand.cpp +++ b/src/commands/CompareAudioCommand.cpp @@ -76,20 +76,20 @@ bool CompareAudioCommand::GetSelection(const CommandContext &context, AudacityPr // Get the selected tracks and check that there are at least two to // compare - SelectedTrackListOfKindIterator iter(Track::Wave, proj.GetTracks()); - mTrack0 = (WaveTrack*)(iter.First()); + auto trackRange = proj.GetTracks()->Selected< const WaveTrack >(); + mTrack0 = *trackRange.first; if (mTrack0 == NULL) { context.Error(wxT("No tracks selected! Select two tracks to compare.")); return false; } - mTrack1 = (WaveTrack*)(iter.Next()); + mTrack1 = * ++ trackRange.first; if (mTrack1 == NULL) { context.Error(wxT("Only one track selected! Select two tracks to compare.")); return false; } - if (iter.Next() != NULL) + if ( * ++ trackRange.first ) { context.Status(wxT("More than two tracks selected - only the first two will be compared.")); } diff --git a/src/commands/GetInfoCommand.cpp b/src/commands/GetInfoCommand.cpp index 7396bfb42..0890907d2 100644 --- a/src/commands/GetInfoCommand.cpp +++ b/src/commands/GetInfoCommand.cpp @@ -267,7 +267,7 @@ bool GetInfoCommand::SendTracks(const CommandContext & context) context.AddItem( t->GetEndTime(), "end" ); context.AddItem( t->GetPan() , "pan"); context.AddItem( t->GetGain() , "gain"); - context.AddItem( t->GetLinked() ? 2:1, "channels"); + context.AddItem( TrackList::Channels(t).size(), "channels"); context.AddBool( t->GetSolo(), "solo" ); context.AddBool( t->GetMute(), "mute"); } ); @@ -280,28 +280,18 @@ bool GetInfoCommand::SendTracks(const CommandContext & context) bool GetInfoCommand::SendClips(const CommandContext &context) { TrackList *tracks = context.GetProject()->GetTracks(); - TrackListIterator iter(tracks); - Track *t = iter.First(); int i=0; context.StartArray(); - while (t) { - if (t->GetKind() == Track::Wave) { - WaveTrack *waveTrack = static_cast(t); - WaveClipPointers ptrs( waveTrack->SortedClipArray()); - for(WaveClip * pClip : ptrs ) { - context.StartStruct(); - context.AddItem( (double)i, "track" ); - context.AddItem( pClip->GetStartTime(), "start" ); - context.AddItem( pClip->GetEndTime(), "end" ); - context.AddItem( pClip->GetColourIndex(), "color" ); - context.EndStruct(); - } + for (auto waveTrack : tracks->Leaders()) { + WaveClipPointers ptrs( waveTrack->SortedClipArray()); + for(WaveClip * pClip : ptrs ) { + context.StartStruct(); + context.AddItem( (double)i, "track" ); + context.AddItem( pClip->GetStartTime(), "start" ); + context.AddItem( pClip->GetEndTime(), "end" ); + context.AddItem( pClip->GetColourIndex(), "color" ); + context.EndStruct(); } - // Skip second tracks of stereo... - if( t->GetLinked() ) - t= iter.Next(); - if( t ) - t=iter.Next(); i++; } context.EndArray(); @@ -312,43 +302,33 @@ bool GetInfoCommand::SendClips(const CommandContext &context) bool GetInfoCommand::SendEnvelopes(const CommandContext &context) { TrackList *tracks = context.GetProject()->GetTracks(); - TrackListIterator iter(tracks); - Track *t = iter.First(); int i=0; int j=0; context.StartArray(); - while (t) { - if (t->GetKind() == Track::Wave) { - WaveTrack *waveTrack = static_cast(t); - WaveClipPointers ptrs( waveTrack->SortedClipArray()); - for(WaveClip * pClip : ptrs ) { - context.StartStruct(); - context.AddItem( (double)i, "track" ); - context.AddItem( (double)j, "clip" ); - context.AddItem( pClip->GetStartTime(), "start" ); - Envelope * pEnv = pClip->GetEnvelope(); - context.StartField( "points" ); - context.StartArray(); - double offset = pEnv->mOffset; - for( size_t k=0;kmEnv.size(); k++) - { - context.StartStruct( ); - context.AddItem( pEnv->mEnv[k].GetT()+offset, "t" ); - context.AddItem( pEnv->mEnv[k].GetVal(), "y" ); - context.EndStruct(); - } - context.EndArray(); - context.EndField(); - context.AddItem( pClip->GetEndTime(), "end" ); + for (auto waveTrack : tracks->Leaders()) { + WaveClipPointers ptrs( waveTrack->SortedClipArray()); + for(WaveClip * pClip : ptrs ) { + context.StartStruct(); + context.AddItem( (double)i, "track" ); + context.AddItem( (double)j, "clip" ); + context.AddItem( pClip->GetStartTime(), "start" ); + Envelope * pEnv = pClip->GetEnvelope(); + context.StartField( "points" ); + context.StartArray(); + double offset = pEnv->mOffset; + for( size_t k=0;kmEnv.size(); k++) + { + context.StartStruct( ); + context.AddItem( pEnv->mEnv[k].GetT()+offset, "t" ); + context.AddItem( pEnv->mEnv[k].GetVal(), "y" ); context.EndStruct(); - j++; } + context.EndArray(); + context.EndField(); + context.AddItem( pClip->GetEndTime(), "end" ); + context.EndStruct(); + j++; } - // Skip second tracks of stereo... - if( t->GetLinked() ) - t= iter.Next(); - if( t ) - t=iter.Next(); } context.EndArray(); diff --git a/src/commands/ScreenshotCommand.cpp b/src/commands/ScreenshotCommand.cpp index 8efe32b89..991f722f0 100644 --- a/src/commands/ScreenshotCommand.cpp +++ b/src/commands/ScreenshotCommand.cpp @@ -777,20 +777,14 @@ wxRect ScreenshotCommand::GetTrackRect( AudacityProject * pProj, TrackPanel * pa return rect; }; - TrackListIterator iter(pProj->GetTracks()); int count = 0; - for (auto t = iter.First(); t; t = iter.Next()) { + for (auto t : pProj->GetTracks()->Leaders()) { count += 1; if( count > n ) { wxRect r = FindRectangle( *panel, *t ); return r; } - if( t->GetLinked() ){ - t = iter.Next(); - if( !t ) - break; - } } return wxRect( 0,0,0,0); } diff --git a/src/commands/SelectCommand.cpp b/src/commands/SelectCommand.cpp index 4a4144c6d..c5b2d3550 100644 --- a/src/commands/SelectCommand.cpp +++ b/src/commands/SelectCommand.cpp @@ -220,31 +220,30 @@ bool SelectTracksCommand::Apply(const CommandContext &context) if( !bHasFirstTrack ) mFirstTrack = 0.0; - // Stereo second tracks count as 0.5 of a track. + // Multiple channels count as fractions of a track. double last = mFirstTrack+mNumTracks; double first = mFirstTrack; - bool bIsSecondChannel = false; - TrackListIterator iter(tracks); - Track *t = iter.First(); - while (t) { + + for (auto t : tracks->Leaders()) { + auto channels = TrackList::Channels(t); + double term = 0.0; // Add 0.01 so we are free of rounding errors in comparisons. - // Optionally add 0.5 for second track which counts as is half a track - double track = index + 0.01 + (bIsSecondChannel ? 0.5 : 0.0); - bool sel = first <= track && track <= last; - if( mMode == 0 ){ // Set - t->SetSelected(sel); + constexpr double fudge = 0.01; + for (auto channel : channels) { + double track = index + fudge + term; + bool sel = first <= track && track <= last; + if( mMode == 0 ){ // Set + t->SetSelected(sel); + } + else if( mMode == 1 && sel ){ // Add + t->SetSelected(sel); + } + else if( mMode == 2 && sel ){ // Remove + t->SetSelected(!sel); + } + term += (1.0 - fudge) / channels.size(); } - else if( mMode == 1 && sel ){ // Add - t->SetSelected(sel); - } - else if( mMode == 2 && sel ){ // Remove - t->SetSelected(!sel); - } - // Do second channel in stereo track too. - bIsSecondChannel = t->GetLinked(); - if( !bIsSecondChannel ) - ++index; - t = iter.Next(); + ++index; } return true; } diff --git a/src/commands/SetLabelCommand.cpp b/src/commands/SetLabelCommand.cpp index d269c304a..dea7eb8d9 100644 --- a/src/commands/SetLabelCommand.cpp +++ b/src/commands/SetLabelCommand.cpp @@ -67,27 +67,21 @@ bool SetLabelCommand::Apply(const CommandContext & context) //wxString mode = GetString(wxT("Type")); AudacityProject * p = context.GetProject(); TrackList *tracks = context.GetProject()->GetTracks(); - TrackListIterator iter(tracks); - Track *t = iter.First(); LabelStruct * pLabel = NULL; int i=0; int nn=0; - LabelTrack *labelTrack = nullptr; - while (t && i<=mLabelIndex) { - if (t->GetKind() == Track::Label) { - labelTrack = static_cast(t); - if( labelTrack ) - { - for (nn = 0; - (nn< (int)labelTrack->mLabels.size()) && i<=mLabelIndex; - nn++) { - i++; - pLabel = &labelTrack->mLabels[nn]; - } - } + LabelTrack *labelTrack {}; + for (auto lt : tracks->Any()) { + if( i > mLabelIndex ) + break; + labelTrack = lt; + for (nn = 0; + (nn< (int)labelTrack->mLabels.size()) && i<=mLabelIndex; + nn++) { + i++; + pLabel = &labelTrack->mLabels[nn]; } - t = iter.Next(); } if ( (i< mLabelIndex) || (pLabel == NULL)) diff --git a/src/commands/SetTrackInfoCommand.cpp b/src/commands/SetTrackInfoCommand.cpp index b1974b46b..d324f3573 100644 --- a/src/commands/SetTrackInfoCommand.cpp +++ b/src/commands/SetTrackInfoCommand.cpp @@ -92,28 +92,26 @@ bool SetTrackBase::Apply(const CommandContext & context ) { long i = 0;// track counter long j = 0;// channel counter - TrackListIterator iter(context.GetProject()->GetTracks()); - Track *t = iter.First(); - bIsSecondChannel = false; - while (t ) + auto tracks = context.GetProject()->GetTracks(); + for ( auto t : tracks->Leaders() ) { - bool bThisTrack = + auto channels = TrackList::Channels(t); + for ( auto channel : channels ) { + bool bThisTrack = #ifdef USE_OWN_TRACK_SELECTION (bHasTrackIndex && (i==mTrackIndex)) || (bHasChannelIndex && (j==mChannelIndex ) ) || (!bHasTrackIndex && !bHasChannelIndex) ; #else - t->GetSelected(); + channel->GetSelected(); #endif - if( bThisTrack ){ - ApplyInner( context, t ); + if( bThisTrack ){ + ApplyInner( context, t ); + } + ++j; // count all channels } - bIsSecondChannel = t->GetLinked(); - if( !bIsSecondChannel ) - ++i; - j++; - t = iter.Next(); + ++i; // count groups of channels } return true; }