Skip to content

Commit

Permalink
Bug819 - Paste should not change clipboard contents when sample forma…
Browse files Browse the repository at this point in the history
…ts differ
  • Loading branch information
Paul-Licameli committed Apr 8, 2015
1 parent 44fc5b9 commit fe8d353
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
43 changes: 19 additions & 24 deletions src/WaveClip.cpp
Expand Up @@ -26,6 +26,7 @@ drawing). Cache's the Spectrogram frequency samples.
*//*******************************************************************/

#include <math.h>
#include <memory>
#include <vector>
#include <wx/log.h>

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<WaveClip> 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))
{
Expand All @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/WaveClip.h
Expand Up @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fe8d353

Please sign in to comment.