1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-19 09:30:06 +02:00

Bug819 - Paste should not change clipboard contents when sample formats differ

This commit is contained in:
Paul-Licameli 2015-04-08 12:09:30 -04:00
parent 44fc5b9f4d
commit fe8d3535d8
2 changed files with 24 additions and 29 deletions

View File

@ -26,6 +26,7 @@ drawing). Cache's the Spectrogram frequency samples.
*//*******************************************************************/ *//*******************************************************************/
#include <math.h> #include <math.h>
#include <memory>
#include <vector> #include <vector>
#include <wx/log.h> #include <wx/log.h>
@ -311,7 +312,7 @@ WaveClip::WaveClip(DirManager *projDirManager, sampleFormat format, int rate)
mIsPlaceholder = false; 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 // essentially a copy constructor - but you must pass in the
// current project's DirManager, because we might be copying // current project's DirManager, because we might be copying
@ -1221,34 +1222,34 @@ bool WaveClip::CreateFromCopy(double t0, double t1, WaveClip* other)
return true; 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<WaveClip> newClip;
const WaveClip* pastedClip;
bool clipNeedsResampling = other->mRate != mRate; if (clipNeedsResampling || clipNeedsNewFormat)
if (clipNeedsResampling)
{ {
// The other clip's rate is different to our's, so resample newClip.reset(new WaveClip(*other, mSequence->GetDirManager()));
pastedClip = new WaveClip(*other, mSequence->GetDirManager()); if (clipNeedsResampling)
if (!pastedClip->Resample(mRate)) // The other clip's rate is different from ours, so resample
{ if (!newClip->Resample(mRate))
delete pastedClip; return false;
return false; if (clipNeedsNewFormat)
} // Force sample formats to match.
newClip->ConvertToSampleFormat(mSequence->GetSampleFormat());
pastedClip = newClip.get();
} else } 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; pastedClip = other;
} }
sampleCount s0; sampleCount s0;
TimeToSamplesClip(t0, &s0); TimeToSamplesClip(t0, &s0);
// Force sample formats to match.
if (pastedClip->mSequence->GetSampleFormat() != mSequence->GetSampleFormat())
pastedClip->ConvertToSampleFormat(mSequence->GetSampleFormat());
bool result = false; bool result = false;
if (mSequence->Paste(s0, pastedClip->mSequence)) if (mSequence->Paste(s0, pastedClip->mSequence))
{ {
@ -1270,12 +1271,6 @@ bool WaveClip::Paste(double t0, WaveClip* other)
result = true; result = true;
} }
if (clipNeedsResampling)
{
// Clip was constructed as a copy, so delete it
delete pastedClip;
}
return result; return result;
} }

View File

@ -69,7 +69,7 @@ private:
WaveClip(const WaveClip&) WaveClip(const WaveClip&)
{ {
wxFAIL_MSG(wxT("It is an error to copy a WaveClip without specifying the DirManager.")); wxFAIL_MSG(wxT("It is an error to copy a WaveClip without specifying the DirManager."));
}; }
WaveClip& operator=(const WaveClip& orig) WaveClip& operator=(const WaveClip& orig)
{ {
WaveClip bogus(orig); WaveClip bogus(orig);
@ -83,7 +83,7 @@ public:
// essentially a copy constructor - but you must pass in the // essentially a copy constructor - but you must pass in the
// current project's DirManager, because we might be copying // current project's DirManager, because we might be copying
// from one project to another // from one project to another
WaveClip(WaveClip& orig, DirManager *projDirManager); WaveClip(const WaveClip& orig, DirManager *projDirManager);
virtual ~WaveClip(); virtual ~WaveClip();
@ -179,7 +179,7 @@ public:
bool ClearAndAddCutLine(double t0, double t1); bool ClearAndAddCutLine(double t0, double t1);
/// Paste data from other clip, resampling it if not equal rate /// 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 /** Insert silence - note that this is an efficient operation for large
* amounts of silence */ * amounts of silence */
@ -232,8 +232,8 @@ public:
SpecPxCache *mSpecPxCache; SpecPxCache *mSpecPxCache;
// AWD, Oct 2009: for pasting whitespace at the end of selection // AWD, Oct 2009: for pasting whitespace at the end of selection
bool GetIsPlaceholder() { return mIsPlaceholder; }; bool GetIsPlaceholder() const { return mIsPlaceholder; }
void SetIsPlaceholder(bool val) { mIsPlaceholder = val; }; void SetIsPlaceholder(bool val) { mIsPlaceholder = val; }
protected: protected:
wxRect mDisplayRect; wxRect mDisplayRect;