diff --git a/src/WaveClip.cpp b/src/WaveClip.cpp index 4e4d0c185620..0543d08eceec 100644 --- a/src/WaveClip.cpp +++ b/src/WaveClip.cpp @@ -26,6 +26,7 @@ drawing). Cache's the Spectrogram frequency samples. *//*******************************************************************/ #include +#include #include #include @@ -311,7 +312,7 @@ WaveClip::WaveClip(DirManager *projDirManager, sampleFormat format, int rate) mIsPlaceholder = false; } -WaveClip::WaveClip(WaveClip& orig, DirManager *projDirManager) +WaveClip::WaveClip(const WaveClip& orig, DirManager *projDirManager) { // essentially a copy constructor - but you must pass in the // current project's DirManager, because we might be copying @@ -1221,34 +1222,34 @@ bool WaveClip::CreateFromCopy(double t0, double t1, WaveClip* other) return true; } -bool WaveClip::Paste(double t0, WaveClip* other) +bool WaveClip::Paste(double t0, const WaveClip* other) { - WaveClip* pastedClip; + const bool clipNeedsResampling = other->mRate != mRate; + const bool clipNeedsNewFormat = + other->mSequence->GetSampleFormat() != mSequence->GetSampleFormat(); + std::auto_ptr newClip; + const WaveClip* pastedClip; - bool clipNeedsResampling = other->mRate != mRate; - - if (clipNeedsResampling) + if (clipNeedsResampling || clipNeedsNewFormat) { - // The other clip's rate is different to our's, so resample - pastedClip = new WaveClip(*other, mSequence->GetDirManager()); - if (!pastedClip->Resample(mRate)) - { - delete pastedClip; - return false; - } + newClip.reset(new WaveClip(*other, mSequence->GetDirManager())); + if (clipNeedsResampling) + // The other clip's rate is different from ours, so resample + if (!newClip->Resample(mRate)) + return false; + if (clipNeedsNewFormat) + // Force sample formats to match. + newClip->ConvertToSampleFormat(mSequence->GetSampleFormat()); + pastedClip = newClip.get(); } else { - // No resampling needed, just use original clip without making a copy + // No resampling or format change needed, just use original clip without making a copy pastedClip = other; } sampleCount s0; TimeToSamplesClip(t0, &s0); - // Force sample formats to match. - if (pastedClip->mSequence->GetSampleFormat() != mSequence->GetSampleFormat()) - pastedClip->ConvertToSampleFormat(mSequence->GetSampleFormat()); - bool result = false; if (mSequence->Paste(s0, pastedClip->mSequence)) { @@ -1270,12 +1271,6 @@ bool WaveClip::Paste(double t0, WaveClip* other) result = true; } - if (clipNeedsResampling) - { - // Clip was constructed as a copy, so delete it - delete pastedClip; - } - return result; } diff --git a/src/WaveClip.h b/src/WaveClip.h index 22c2c838332b..6a296291e4ae 100644 --- a/src/WaveClip.h +++ b/src/WaveClip.h @@ -69,7 +69,7 @@ class AUDACITY_DLL_API WaveClip : public XMLTagHandler WaveClip(const WaveClip&) { wxFAIL_MSG(wxT("It is an error to copy a WaveClip without specifying the DirManager.")); - }; + } WaveClip& operator=(const WaveClip& orig) { WaveClip bogus(orig); @@ -83,7 +83,7 @@ class AUDACITY_DLL_API WaveClip : public XMLTagHandler // essentially a copy constructor - but you must pass in the // current project's DirManager, because we might be copying // from one project to another - WaveClip(WaveClip& orig, DirManager *projDirManager); + WaveClip(const WaveClip& orig, DirManager *projDirManager); virtual ~WaveClip(); @@ -179,7 +179,7 @@ class AUDACITY_DLL_API WaveClip : public XMLTagHandler bool ClearAndAddCutLine(double t0, double t1); /// Paste data from other clip, resampling it if not equal rate - bool Paste(double t0, WaveClip* other); + bool Paste(double t0, const WaveClip* other); /** Insert silence - note that this is an efficient operation for large * amounts of silence */ @@ -232,8 +232,8 @@ class AUDACITY_DLL_API WaveClip : public XMLTagHandler SpecPxCache *mSpecPxCache; // AWD, Oct 2009: for pasting whitespace at the end of selection - bool GetIsPlaceholder() { return mIsPlaceholder; }; - void SetIsPlaceholder(bool val) { mIsPlaceholder = val; }; + bool GetIsPlaceholder() const { return mIsPlaceholder; } + void SetIsPlaceholder(bool val) { mIsPlaceholder = val; } protected: wxRect mDisplayRect;