Skip to content

Commit

Permalink
Rewrite WaveTrack::ExpandCutLine as channel-major
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Licameli committed Nov 9, 2023
1 parent ab89481 commit 31dd689
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions libraries/lib-wave-track/WaveClip.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ class WAVE_TRACK_API WaveClip final :
* in cutLineStart and cutLineEnd (if specified) if a cut line at this
* position could be found. Return false otherwise. */
bool FindCutLine(double cutLinePosition,
double* cutLineStart = NULL,
double *cutLineEnd = NULL) const;
double* cutLineStart = nullptr,
double *cutLineEnd = nullptr) const;

/** 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 successfully
Expand Down
49 changes: 21 additions & 28 deletions libraries/lib-wave-track/WaveTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,17 @@ const Envelope& WaveTrack::Interval::GetEnvelope() const
return *mpClip->GetEnvelope();
}

bool WaveTrack::Interval::FindCutLine(double cutLinePosition,
double* cutLineStart, double *cutLineEnd) const
{
return mpClip->FindCutLine(cutLinePosition, cutLineStart, cutLineEnd);
}

void WaveTrack::Interval::ExpandCutLine(double cutlinePosition)
{
ForEachClip([=](auto &clip){ clip.ExpandCutLine(cutlinePosition); });
}

void WaveTrack::Interval::SetEnvelope(const Envelope& envelope)
{
mpClip->SetEnvelope(std::make_unique<Envelope>(envelope));
Expand Down Expand Up @@ -4009,39 +4020,26 @@ void WaveTrack::SplitAt(double t)
}

// Expand cut line (that is, re-insert audio, then DELETE audio saved in cut line)
// Can't promise strong exception safety for a pair of tracks together
// Can't yet promise strong exception safety for a pair of channels together
void WaveTrack::ExpandCutLine(double cutLinePosition, double* cutlineStart,
double* cutlineEnd)
{
assert(IsLeader());
for (const auto pChannel : TrackList::Channels(this)) {
pChannel->ExpandOneCutLine(cutLinePosition, cutlineStart, cutlineEnd);
// Assign the out parameters at most once
cutlineStart = cutlineEnd = nullptr;
}
}

/*! @excsafety{Strong} */
void WaveTrack::ExpandOneCutLine(double cutLinePosition,
double* cutlineStart, double* cutlineEnd)
{
bool editClipCanMove = GetEditClipsCanMove();
const bool editClipCanMove = GetEditClipsCanMove();

// Find clip which contains this cut line
double start = 0, end = 0;
auto pEnd = mClips.end();
auto pClip = std::find_if( mClips.begin(), pEnd,
[&](const WaveClipHolder &clip) {
return clip->FindCutLine(cutLinePosition, &start, &end); } );
if (pClip != pEnd)
{
auto &clip = *pClip;
if (!editClipCanMove)
{
const auto &clips = Intervals();
const auto pEnd = clips.end();
const auto pClip = std::find_if(clips.begin(), pEnd,
[&](const auto &clip) {
return clip->FindCutLine(cutLinePosition, &start, &end); });
if (pClip != pEnd) {
auto clip = *pClip;
if (!editClipCanMove) {
// We are not allowed to move the other clips, so see if there
// is enough room to expand the cut line
for (const auto &clip2: mClips)
{
if (clip2->GetPlayStartTime() > clip->GetPlayStartTime() &&
clip->GetPlayEndTime() + end - start > clip2->GetPlayStartTime())
// Strong-guarantee in case of this path
Expand All @@ -4051,7 +4049,6 @@ void WaveTrack::ExpandOneCutLine(double cutLinePosition,
XO("Warning"),
"Error:_Insufficient_space_in_track"
};
}
}

clip->ExpandCutLine(cutLinePosition);
Expand All @@ -4065,13 +4062,9 @@ void WaveTrack::ExpandOneCutLine(double cutLinePosition,

// Move clips which are to the right of the cut line
if (editClipCanMove)
{
for (const auto &clip2 : mClips)
{
if (clip2->GetPlayStartTime() > clip->GetPlayStartTime())
clip2->ShiftBy(end - start);
}
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions libraries/lib-wave-track/WaveTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,15 @@ class WAVE_TRACK_API WaveTrack final

const Envelope& GetEnvelope() const;

/** Find cut line at (approximately) this position. Returns true and fills
* in cutLineStart and cutLineEnd (if specified) if a cut line at this
* position could be found. Return false otherwise. */
bool FindCutLine(double cutLinePosition,
double* cutLineStart = nullptr,
double *cutLineEnd = nullptr) const;

void ExpandCutLine(double cutlinePosition);

private:
void SetEnvelope(const Envelope& envelope);

Expand Down

0 comments on commit 31dd689

Please sign in to comment.