1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-05-06 23:02:42 +02:00

void returns from many WaveClip methods, comment safety guarantees

This commit is contained in:
Paul Licameli 2017-03-22 14:55:07 -04:00
parent aa83c4cf29
commit 5963f278f1
4 changed files with 72 additions and 110 deletions

View File

@ -599,6 +599,7 @@ bool EnvelopeEditor::MouseEvent(const wxMouseEvent & event, wxRect & r,
}
void Envelope::CollapseRegion(double t0, double t1)
// NOFAIL-GUARANTEE
{
// This gets called when somebody clears samples. All of the
// control points within the region disappear and the points
@ -635,6 +636,7 @@ void Envelope::CollapseRegion(double t0, double t1)
// envelope point applies one-past the last actual sample.
// Rather than going to a .5-offset-index, we special case the framing.
void Envelope::Paste(double t0, const Envelope *e)
// NOFAIL-GUARANTEE
{
const bool wasEmpty = (this->mEnv.size() == 0);
@ -828,6 +830,7 @@ Old analysis of cases:
// 'Unneeded' means that the envelope doesn't change by more than
// 'tolerence' without the point being there.
void Envelope::RemoveUnneededPoints(double time, double tolerence)
// NOFAIL-GUARANTEE
{
unsigned int len = mEnv.size();
unsigned int i;
@ -864,6 +867,7 @@ void Envelope::RemoveUnneededPoints(double time, double tolerence)
}
void Envelope::InsertSpace(double t0, double tlen)
// NOFAIL-GUARANTEE
{
unsigned int len = mEnv.size();
unsigned int i;
@ -997,6 +1001,7 @@ int Envelope::Insert(double when, double value)
// Control
void Envelope::SetOffset(double newOffset)
// NOFAIL-GUARANTEE
{
mOffset = newOffset;
}

View File

@ -345,6 +345,7 @@ WaveClip::WaveClip(const WaveClip& orig,
mIsPlaceholder = orig.GetIsPlaceholder();
}
// to do
WaveClip::WaveClip(const WaveClip& orig,
const std::shared_ptr<DirManager> &projDirManager,
bool copyCutlines,
@ -407,12 +408,15 @@ bool WaveClip::GetSamples(samplePtr buffer, sampleFormat format,
return mSequence->Get(buffer, format, start, len, mayThrow);
}
bool WaveClip::SetSamples(samplePtr buffer, sampleFormat format,
void WaveClip::SetSamples(samplePtr buffer, sampleFormat format,
sampleCount start, size_t len)
// STRONG-GUARANTEE
{
bool bResult = mSequence->Set(buffer, format, start, len);
// use STRONG-GUARANTEE
mSequence->Set(buffer, format, start, len);
// use NOFAIL-GUARANTEE
MarkChanged();
return bResult;
}
BlockArray* WaveClip::GetSequenceBlockArray()
@ -1377,7 +1381,7 @@ void WaveClip::GetDisplayRect(wxRect* r)
*r = mDisplayRect;
}
bool WaveClip::Append(samplePtr buffer, sampleFormat format,
void WaveClip::Append(samplePtr buffer, sampleFormat format,
size_t len, unsigned int stride /* = 1 */,
XMLWriter* blockFileLog /*=NULL*/)
// PARTIAL-GUARANTEE in case of exceptions:
@ -1407,7 +1411,7 @@ bool WaveClip::Append(samplePtr buffer, sampleFormat format,
mSequence->Append(mAppendBuffer.ptr(), seqFormat, blockSize,
blockFileLog);
if (!success)
return false;
return;
// use NOFAIL-GUARANTEE for rest of this "if"
memmove(mAppendBuffer.ptr(),
@ -1435,11 +1439,9 @@ bool WaveClip::Append(samplePtr buffer, sampleFormat format,
buffer += toCopy * SAMPLE_SIZE(format) * stride;
len -= toCopy;
}
return true;
}
bool WaveClip::AppendAlias(const wxString &fName, sampleCount start,
void WaveClip::AppendAlias(const wxString &fName, sampleCount start,
size_t len, int channel,bool useOD)
// STRONG-GUARANTEE
{
@ -1452,10 +1454,9 @@ bool WaveClip::AppendAlias(const wxString &fName, sampleCount start,
UpdateEnvelopeTrackLen();
MarkChanged();
}
return result;
}
bool WaveClip::AppendCoded(const wxString &fName, sampleCount start,
void WaveClip::AppendCoded(const wxString &fName, sampleCount start,
size_t len, int channel, int decodeType)
// STRONG-GUARANTEE
{
@ -1468,7 +1469,6 @@ if (result)
UpdateEnvelopeTrackLen();
MarkChanged();
}
return result;
}
bool WaveClip::Flush()
@ -1569,7 +1569,7 @@ void WaveClip::WriteXML(XMLWriter &xmlFile) const
xmlFile.EndTag(wxT("waveclip"));
}
bool WaveClip::Paste(double t0, const WaveClip* other)
void WaveClip::Paste(double t0, const WaveClip* other)
// STRONG-GUARANTEE
{
const bool clipNeedsResampling = other->mRate != mRate;
@ -1584,8 +1584,7 @@ bool WaveClip::Paste(double t0, const WaveClip* other)
std::make_unique<WaveClip>(*other, mSequence->GetDirManager(), true);
if (clipNeedsResampling)
// The other clip's rate is different from ours, so resample
if (!newClip->Resample(mRate))
return false;
newClip->Resample(mRate);
if (clipNeedsNewFormat)
// Force sample formats to match.
newClip->ConvertToSampleFormat(mSequence->GetSampleFormat());
@ -1613,8 +1612,6 @@ bool WaveClip::Paste(double t0, const WaveClip* other)
sampleCount s0;
TimeToSamplesClip(t0, &s0);
bool result = false;
// Assume STRONG-GUARANTEE from Sequence::Paste
if (mSequence->Paste(s0, pastedClip->mSequence.get()))
{
@ -1627,13 +1624,10 @@ bool WaveClip::Paste(double t0, const WaveClip* other)
for (auto &holder : newCutlines)
mCutLines.push_back(std::move(holder));
result = true;
}
return result;
}
bool WaveClip::InsertSilence(double t, double len)
void WaveClip::InsertSilence(double t, double len)
// STRONG-GUARANTEE
{
sampleCount s0;
@ -1644,25 +1638,26 @@ bool WaveClip::InsertSilence(double t, double len)
if (!GetSequence()->InsertSilence(s0, slen))
{
wxASSERT(false);
return false;
return;
}
// use NOFAIL-GUARANTEE
OffsetCutLines(t, len);
GetEnvelope()->InsertSpace(t, len);
MarkChanged();
return true;
}
bool WaveClip::Clear(double t0, double t1)
void WaveClip::Clear(double t0, double t1)
// STRONG-GUARANTEE
{
sampleCount s0, s1;
TimeToSamplesClip(t0, &s0);
TimeToSamplesClip(t1, &s1);
// use STRONG-GUARANTEE
if (GetSequence()->Delete(s0, s1-s0))
// use NOFAIL-GUARANTEE in the remaining
{
// msmeyer
//
@ -1709,24 +1704,24 @@ bool WaveClip::Clear(double t0, double t1)
Offset(-(GetStartTime() - t0));
MarkChanged();
return true;
}
return false;
}
bool WaveClip::ClearAndAddCutLine(double t0, double t1)
void WaveClip::ClearAndAddCutLine(double t0, double t1)
// WEAK-GUARANTEE
// this WaveClip remains destructible in case of AudacityException.
// But some cutlines may be deleted
{
if (t0 > GetEndTime() || t1 < GetStartTime())
return true; // time out of bounds
return; // time out of bounds
const double clip_t0 = std::max( t0, GetStartTime() );
const double clip_t1 = std::min( t1, GetEndTime() );
auto newClip = make_movable<WaveClip>
auto newClip = make_movable< WaveClip >
(*this, mSequence->GetDirManager(), true, clip_t0, clip_t1);
newClip->SetOffset(clip_t0-mOffset);
newClip->SetOffset( clip_t0 - mOffset );
// Remove cutlines from this clip that were in the selection, shift
// left those that were after the selection
@ -1753,6 +1748,7 @@ bool WaveClip::ClearAndAddCutLine(double t0, double t1)
TimeToSamplesClip(t0, &s0);
TimeToSamplesClip(t1, &s1);
// use WEAK-GUARANTEE
if (GetSequence()->Delete(s0, s1-s0))
{
// Collapse envelope
@ -1763,10 +1759,7 @@ bool WaveClip::ClearAndAddCutLine(double t0, double t1)
MarkChanged();
mCutLines.push_back(std::move(newClip));
return true;
}
else
return false;
}
bool WaveClip::FindCutLine(double cutLinePosition,
@ -1788,7 +1781,7 @@ bool WaveClip::FindCutLine(double cutLinePosition,
return false;
}
bool WaveClip::ExpandCutLine(double cutLinePosition)
void WaveClip::ExpandCutLine(double cutLinePosition)
// STRONG-GUARANTEE
{
auto end = mCutLines.end();
@ -1800,8 +1793,7 @@ bool WaveClip::ExpandCutLine(double cutLinePosition)
if ( it != end ) {
auto cutline = it->get();
// assume STRONG-GUARANTEE from Paste
if (!Paste(mOffset+cutline->GetOffset(), cutline))
return false;
Paste(mOffset+cutline->GetOffset(), cutline);
// Now erase the cutline,
// but be careful to find it again, because Paste above may
// have modified the array of cutlines (if our cutline contained
@ -1815,10 +1807,7 @@ bool WaveClip::ExpandCutLine(double cutLinePosition)
// THROW_INCONSISTENCY_EXCEPTION;
wxASSERT(false);
}
return true;
}
return false;
}
bool WaveClip::RemoveCutLine(double cutLinePosition)
@ -1874,13 +1863,14 @@ void WaveClip::SetRate(int rate)
MarkChanged();
}
bool WaveClip::Resample(int rate, ProgressDialog *progress)
void WaveClip::Resample(int rate, ProgressDialog *progress)
// STRONG-GUARANTEE
{
// Note: it is not necessary to do this recursively to cutlines.
// They get resampled as needed when they are expanded.
if (rate == mRate)
return true; // Nothing to do
return; // Nothing to do
double factor = (double)rate / (double)mRate;
::Resample resample(true, factor, factor); // constant rate resampling
@ -1940,22 +1930,23 @@ bool WaveClip::Resample(int rate, ProgressDialog *progress)
);
error = (updateResult != ProgressResult::Success);
if (error)
{
break;
}
//throw UserException{};
}
}
if (!error)
if (error)
;
else
{
mSequence = std::move(newSequence);
mRate = rate;
// Use NOFAIL-GUARANTEE in these steps
// Invalidate wave display cache
mWaveCache = std::make_unique<WaveCache>();
// Invalidate the spectrum display cache
mSpecCache = std::make_unique<SpecCache>();
}
return !error;
mSequence = std::move(newSequence);
mRate = rate;
}
}

View File

@ -233,7 +233,7 @@ public:
// Resample clip. This also will set the rate, but without changing
// the length of the clip
bool Resample(int rate, ProgressDialog *progress = NULL);
void Resample(int rate, ProgressDialog *progress = NULL);
void SetOffset(double offset);
double GetOffset() const { return mOffset; }
@ -254,7 +254,7 @@ public:
bool GetSamples(samplePtr buffer, sampleFormat format,
sampleCount start, size_t len, bool mayThrow = true) const;
bool SetSamples(samplePtr buffer, sampleFormat format,
void SetSamples(samplePtr buffer, sampleFormat format,
sampleCount start, size_t len);
Envelope* GetEnvelope() { return mEnvelope.get(); }
@ -297,31 +297,31 @@ public:
void UpdateEnvelopeTrackLen();
/// You must call Flush after the last Append
bool Append(samplePtr buffer, sampleFormat format,
void Append(samplePtr buffer, sampleFormat format,
size_t len, unsigned int stride=1,
XMLWriter* blockFileLog = NULL);
/// Flush must be called after last Append
bool Flush();
bool AppendAlias(const wxString &fName, sampleCount start,
void AppendAlias(const wxString &fName, sampleCount start,
size_t len, int channel,bool useOD);
bool AppendCoded(const wxString &fName, sampleCount start,
void AppendCoded(const wxString &fName, sampleCount start,
size_t len, int channel, int decodeType);
/// This name is consistent with WaveTrack::Clear. It performs a "Cut"
/// operation (but without putting the cutted audio to the clipboard)
bool Clear(double t0, double t1);
void Clear(double t0, double t1);
/// Clear, and add cut line that starts at t0 and contains everything until t1.
bool ClearAndAddCutLine(double t0, double t1);
void ClearAndAddCutLine(double t0, double t1);
/// Paste data from other clip, resampling it if not equal rate
bool Paste(double t0, const WaveClip* other);
void Paste(double t0, const WaveClip* other);
/** Insert silence - note that this is an efficient operation for large
* amounts of silence */
bool InsertSilence(double t, double len);
void InsertSilence(double t, double len);
/// Get access to cut lines list
WaveClipHolders &GetCutLines() { return mCutLines; }
@ -339,7 +339,7 @@ public:
/** Expand cut line (that is, re-insert audio, then DELETE audio saved in
* cut line). Returns true if a cut line could be found and sucessfully
* expanded, false otherwise */
bool ExpandCutLine(double cutLinePosition);
void ExpandCutLine(double cutLinePosition);
/// Remove cut line, without expanding the audio in it
bool RemoveCutLine(double cutLinePosition);

View File

@ -600,15 +600,13 @@ void WaveTrack::Trim (double t0, double t1)
if(t1 > clip->GetStartTime() && t1 < clip->GetEndTime())
{
if (!clip->Clear(t1,clip->GetEndTime()))
return;
clip->Clear(t1,clip->GetEndTime());
inside1 = true;
}
if(t0 > clip->GetStartTime() && t0 < clip->GetEndTime())
{
if (!clip->Clear(clip->GetStartTime(),t0))
return;
clip->Clear(clip->GetStartTime(),t0);
clip->SetOffset(t0);
inside0 = true;
}
@ -686,15 +684,9 @@ Track::Holder WaveTrack::Copy(double t0, double t1, bool forClipboard) const
newTrack->GetSampleFormat(),
static_cast<int>(newTrack->GetRate()));
placeholder->SetIsPlaceholder(true);
if ( ! placeholder->InsertSilence(
0, (t1 - t0) - newTrack->GetEndTime()) )
{
}
else
{
placeholder->Offset(newTrack->GetEndTime());
newTrack->mClips.push_back(std::move(placeholder)); // transfer ownership
}
placeholder->InsertSilence(0, (t1 - t0) - newTrack->GetEndTime());
placeholder->Offset(newTrack->GetEndTime());
newTrack->mClips.push_back(std::move(placeholder)); // transfer ownership
}
return result;
@ -1143,8 +1135,7 @@ void WaveTrack::HandleClear(double t0, double t1,
newClip->GetEnvelope()->Insert(t1 - clip->GetOffset(), val);
}
}
if (!newClip->Clear(t0,t1))
return;
newClip->Clear(t0,t1);
newClip->GetEnvelope()->RemoveUnneededPoints(t0);
clipsToAdd.push_back( std::move( newClip ) );
@ -1410,11 +1401,7 @@ void WaveTrack::Silence(double t0, double t1)
startDelta = 0;
}
if (!clip->GetSequence()->SetSilence(inclipDelta, samplesToCopy))
{
wxASSERT(false); // should always work
return;
}
clip->GetSequence()->SetSilence(inclipDelta, samplesToCopy);
clip->MarkChanged();
}
}
@ -1444,8 +1431,7 @@ void WaveTrack::InsertSilence(double t, double len)
// use STRONG-GUARANTEE
if (it != end)
if(!it->get()->InsertSilence(t, len))
return;
it->get()->InsertSilence(t, len);
// use NOFAIL-GUARANTEE
for (const auto &clip : mClips)
@ -1579,9 +1565,7 @@ void WaveTrack::Join(double t0, double t1)
}
//printf("Pasting at %.6f\n", t);
bool bResult = newClip->Paste(t, clip);
wxASSERT(bResult); // TO DO: Actually handle this.
wxUnusedVar(bResult);
newClip->Paste(t, clip);
t = newClip->GetEndTime();
auto it = FindClip(mClips, clip);
@ -2122,15 +2106,11 @@ void WaveTrack::Set(samplePtr buffer, sampleFormat format,
// samplesToCopy is positive and not more than len
}
if (!clip->SetSamples(
clip->SetSamples(
(samplePtr)(((char*)buffer) +
startDelta.as_size_t() *
SAMPLE_SIZE(format)),
format, inclipDelta, samplesToCopy.as_size_t() ))
{
wxASSERT(false); // should always work
return;
}
format, inclipDelta, samplesToCopy.as_size_t() );
clip->MarkChanged();
}
}
@ -2393,14 +2373,8 @@ void WaveTrack::SplitAt(double t)
c->GetEnvelope()->Insert(t - c->GetOffset() - 1.0/c->GetRate(), val); // frame end points
c->GetEnvelope()->Insert(t - c->GetOffset(), val);
auto newClip = make_movable<WaveClip>( *c, mDirManager, true );
if (!c->Clear(t, c->GetEndTime()))
{
return;
}
if (!newClip->Clear(c->GetStartTime(), t))
{
return;
}
c->Clear(t, c->GetEndTime());
newClip->Clear(c->GetStartTime(), t);
//offset the NEW clip by the splitpoint (noting that it is already offset to c->GetStartTime())
sampleCount here = llrint(floor(((t - c->GetStartTime()) * mRate) + 0.5));
@ -2511,8 +2485,7 @@ void WaveTrack::ExpandCutLine(double cutLinePosition, double* cutlineStart,
}
}
if (!clip->ExpandCutLine(cutLinePosition))
return;
clip->ExpandCutLine(cutLinePosition);
// STRONG-GUARANTEE provided that the following gives NOFAIL-GUARANTEE
@ -2553,9 +2526,8 @@ void WaveTrack::MergeClips(int clipidx1, int clipidx2)
// Append data from second clip to first clip
// use STRONG-GUARANTEE
if (!clip1->Paste(clip1->GetEndTime(), clip2))
return;
clip1->Paste(clip1->GetEndTime(), clip2);
// use NOFAIL-GUARANTEE for the rest
// Delete second clip
auto it = FindClip(mClips, clip2);
@ -2567,13 +2539,7 @@ void WaveTrack::Resample(int rate, ProgressDialog *progress)
// Partial completion may leave clips at differing sample rates!
{
for (const auto &clip : mClips)
if (!clip->Resample(rate, progress))
{
wxLogDebug( wxT("Resampling problem! We're partially resampled") );
// FIXME: The track is now in an inconsistent state since some
// clips are resampled and some are not
return;
}
clip->Resample(rate, progress);
mRate = rate;
}