From b9647586af859ea4d9073266303a331fadb78fd0 Mon Sep 17 00:00:00 2001 From: lllucius Date: Sun, 4 Jan 2015 19:44:54 +0000 Subject: [PATCH] Committing patch from bug #794 --- src/AutoRecovery.cpp | 2 +- src/WaveTrack.cpp | 48 +++++++++++++++++++++++++++++++++----------- src/WaveTrack.h | 19 ++++++++++++------ 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/AutoRecovery.cpp b/src/AutoRecovery.cpp index 14948c2d7..8d1bfa870 100644 --- a/src/AutoRecovery.cpp +++ b/src/AutoRecovery.cpp @@ -282,7 +282,7 @@ bool RecordingRecoveryHandler::HandleXMLTag(const wxChar *tag, return false; } WaveTrack* track = tracks.Item(index); - WaveClip* clip = track->GetLastOrCreateClip(); + WaveClip* clip = track->NewestOrNewClip(); Sequence* seq = clip->GetSequence(); // Load the blockfile from the XML diff --git a/src/WaveTrack.cpp b/src/WaveTrack.cpp index a624b560a..bb9ca5330 100644 --- a/src/WaveTrack.cpp +++ b/src/WaveTrack.cpp @@ -1326,21 +1326,21 @@ bool WaveTrack::Append(samplePtr buffer, sampleFormat format, sampleCount len, unsigned int stride /* = 1 */, XMLWriter *blockFileLog /* = NULL */) { - return GetLastOrCreateClip()->Append(buffer, format, len, stride, + return RightmostOrNewClip()->Append(buffer, format, len, stride, blockFileLog); } bool WaveTrack::AppendAlias(wxString fName, sampleCount start, sampleCount len, int channel,bool useOD) { - return GetLastOrCreateClip()->AppendAlias(fName, start, len, channel,useOD); + return RightmostOrNewClip()->AppendAlias(fName, start, len, channel, useOD); } bool WaveTrack::AppendCoded(wxString fName, sampleCount start, sampleCount len, int channel, int decodeType) { - return GetLastOrCreateClip()->AppendCoded(fName, start, len, channel, decodeType); + return RightmostOrNewClip()->AppendCoded(fName, start, len, channel, decodeType); } ///gets an int with OD flags so that we can determine which ODTasks should be run on this track after save/open, etc. @@ -1401,12 +1401,13 @@ sampleCount WaveTrack::GetMaxBlockSize() sampleCount WaveTrack::GetIdealBlockSize() { - return GetLastOrCreateClip()->GetSequence()->GetIdealBlockSize(); + return NewestOrNewClip()->GetSequence()->GetIdealBlockSize(); } bool WaveTrack::Flush() { - return GetLastOrCreateClip()->Flush(); + // After appending, presumably. Do this to the clip that gets appended. + return RightmostOrNewClip()->Flush(); } bool WaveTrack::HandleXMLTag(const wxChar *tag, const wxChar **attrs) @@ -1491,7 +1492,7 @@ void WaveTrack::HandleXMLEndTag(const wxChar * WXUNUSED(tag)) { // In case we opened a pre-multiclip project, we need to // simulate closing the waveclip tag. - GetLastOrCreateClip()->HandleXMLEndTag( wxT("waveclip") ); + NewestOrNewClip()->HandleXMLEndTag(wxT("waveclip")); } XMLTagHandler *WaveTrack::HandleXMLChild(const wxChar *tag) @@ -1502,13 +1503,13 @@ XMLTagHandler *WaveTrack::HandleXMLChild(const wxChar *tag) if (!wxStrcmp(tag, wxT("sequence")) || !wxStrcmp(tag, wxT("envelope"))) { // This is a legacy project, so set the cached offset - GetLastOrCreateClip()->SetOffset(mLegacyProjectFileOffset); + NewestOrNewClip()->SetOffset(mLegacyProjectFileOffset); // Legacy project file tracks are imported as one single wave clip if (!wxStrcmp(tag, wxT("sequence"))) - return GetLastOrCreateClip()->GetSequence(); + return NewestOrNewClip()->GetSequence(); else if (!wxStrcmp(tag, wxT("envelope"))) - return GetLastOrCreateClip()->GetEnvelope(); + return NewestOrNewClip()->GetEnvelope(); } // JKC... for 1.1.0, one step better than what we had, but still badly broken. @@ -1516,8 +1517,8 @@ XMLTagHandler *WaveTrack::HandleXMLChild(const wxChar *tag) if( !wxStrcmp( tag, wxT("waveblock" ))) { // This is a legacy project, so set the cached offset - GetLastOrCreateClip()->SetOffset(mLegacyProjectFileOffset); - Sequence *pSeq = GetLastOrCreateClip()->GetSequence(); + NewestOrNewClip()->SetOffset(mLegacyProjectFileOffset); + Sequence *pSeq = NewestOrNewClip()->GetSequence(); return pSeq; } @@ -1977,7 +1978,7 @@ WaveClip* WaveTrack::CreateClip() return clip; } -WaveClip* WaveTrack::GetLastOrCreateClip() +WaveClip* WaveTrack::NewestOrNewClip() { if (mClips.IsEmpty()) { WaveClip *clip = CreateClip(); @@ -1988,6 +1989,29 @@ WaveClip* WaveTrack::GetLastOrCreateClip() return mClips.GetLast()->GetData(); } +WaveClip* WaveTrack::RightmostOrNewClip() +{ + if (mClips.IsEmpty()) { + WaveClip *clip = CreateClip(); + clip->SetOffset(mOffset); + return clip; + } + else + { + WaveClipList::compatibility_iterator it = GetClipIterator(); + WaveClip *rightmost = it->GetData(); + double maxOffset = rightmost->GetOffset(); + for (it = it->GetNext(); it; it = it->GetNext()) + { + WaveClip *clip = it->GetData(); + double offset = clip->GetOffset(); + if (maxOffset < offset) + maxOffset = offset, rightmost = clip; + } + return rightmost; + } +} + int WaveTrack::GetClipIndex(WaveClip* clip) { return mClips.IndexOf(clip); diff --git a/src/WaveTrack.h b/src/WaveTrack.h index cbe739c4e..ed27006b7 100644 --- a/src/WaveTrack.h +++ b/src/WaveTrack.h @@ -313,12 +313,19 @@ class AUDACITY_DLL_API WaveTrack: public Track { // to the newly created clip. WaveClip* CreateClip(); - /** @brief Get access to the last clip, or create a clip, if there is not - * already one. - * - * @return a pointer to a WaveClip at the end of the track - */ - WaveClip* GetLastOrCreateClip(); + /** @brief Get access to the most recently added clip, or create a clip, + * if there is not already one. THIS IS NOT NECESSARILY RIGHTMOST. + * + * @return a pointer to the most recently added WaveClip + */ + WaveClip* NewestOrNewClip(); + + /** @brief Get access to the last (rightmost) clip, or create a clip, + * if there is not already one. + * + * @return a pointer to a WaveClip at the end of the track + */ + WaveClip* RightmostOrNewClip(); // Get the linear index of a given clip (-1 if the clip is not found) int GetClipIndex(WaveClip* clip);