Audacity 3.2.0
PasteOverPreservingClips.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 PasteOverPreservingClips.cpp
6
7 Mitch Golden
8 Vaughan Johnson
9 Martyn Shaw
10
11 Paul Licameli split from Equalization.cpp
12
13**********************************************************************/
15#include "WaveClip.h"
16#include "WaveTrack.h"
17
19 const WaveTrack &oldTrack, sampleCount start, sampleCount len)
20{
21 assert(oldTrack.IsLeader());
22 ClipData results;
23 auto &[clipStartEndTimes, clipRealStartEndTimes, clipNames] = results;
24
25 double lenT = oldTrack.LongSamplesToTime(len);
26 // 'start' is the sample offset in 't', the passed in track
27 // 'startT' is the equivalent time value
28 double startT = oldTrack.LongSamplesToTime(start);
29
30 for (const auto &clip : oldTrack.GetClips()) {
31 auto clipStartT = clip->GetPlayStartTime();
32 auto clipEndT = clip->GetPlayEndTime();
33 if (clipEndT <= startT)
34 continue; // clip is not within selection
35 if (clipStartT >= startT + lenT)
36 continue; // clip is not within selection
37
38 //save the actual clip start/end so that we can rejoin them after we paste.
39 clipRealStartEndTimes.emplace_back(clipStartT, clipEndT);
40
41 if (clipStartT < startT) // does selection cover the whole clip?
42 clipStartT = startT; // don't copy all the NEW clip
43 if(clipEndT > startT + lenT) // does selection cover the whole clip?
44 clipEndT = startT + lenT; // don't copy all the NEW clip
45
46 //save them
47 clipStartEndTimes.emplace_back(clipStartT, clipEndT);
48 clipNames.push_back(clip->GetName());
49 }
50 return results;
51}
52
54 WaveTrack &oldTrack, sampleCount start, sampleCount len,
55 const WaveTrack &newContents)
56{
57 assert(oldTrack.IsLeader());
58 assert(newContents.IsLeader());
59 assert(oldTrack.NChannels() == newContents.NChannels());
60 const auto &[clipStartEndTimes, clipRealStartEndTimes, clipNames] = data;
61
62 //newContents has one waveclip for the total length, even though
63 //oldTrack might have whitespace separating multiple clips
64 //we want to maintain the original clip structure, so
65 //only paste the intersections of the new clip.
66
67 // now move the appropriate bit of the output back to the track
68 // (this could be enhanced in the future to use the tails)
69 double lenT = oldTrack.LongSamplesToTime(len);
70 // 'start' is the sample offset in 't', the passed in track
71 // 'startT' is the equivalent time value
72 double startT = oldTrack.LongSamplesToTime(start);
73
74 //now go through and replace the old clips with NEW
75 for (unsigned int i = 0; i < clipStartEndTimes.size(); ++i) {
76 //remove the old audio and get the NEW
77 auto [start, end] = clipStartEndTimes[i];
78 oldTrack.Clear(start, end);
79
80 auto toClipOutput = newContents.Copy(start - startT, end - startT);
81 oldTrack.Paste(start, *toClipOutput);
82
83 //Restore original clip's name
84 auto newClip = oldTrack.GetClipAtTime(start + 0.5 / oldTrack.GetRate());
85 newClip->SetName(clipNames[i]);
86
87 //if the clip was only partially selected, the Paste will have created a
88 // split line. Join is needed to take care of this
89 //This is not true when the selection is fully contained within one clip
90 // (second half of conditional)
91 auto [realStart, realEnd] = clipRealStartEndTimes[i];
92 if ((realStart != start || realEnd != end) &&
93 !(realStart <= startT && realEnd >= startT + lenT))
94 oldTrack.Join(realStart, realEnd);
95 }
96}
void PasteOverPreservingClips(const ClipData &data, WaveTrack &oldTrack, sampleCount start, sampleCount len, const WaveTrack &newContents)
Substitute new contents into existing track, preserving clip boundaries.
ClipData CollectClipData(const WaveTrack &oldTrack, sampleCount start, sampleCount len)
Collect clip boundary and name information.
void SetName(const wxString &name)
Definition: WaveClip.cpp:1300
A Track that contains audio waveform data.
Definition: WaveTrack.h:220
const WaveClip * GetClipAtTime(double time) const
Definition: WaveTrack.cpp:3374
void Join(double t0, double t1)
Definition: WaveTrack.cpp:2279
void Clear(double t0, double t1) override
Definition: WaveTrack.cpp:1222
bool IsLeader() const override
Definition: WaveTrack.cpp:2449
void Paste(double t0, const Track &src) override
Definition: WaveTrack.cpp:2112
double GetRate() const override
Definition: WaveTrack.cpp:875
TrackListHolder Copy(double t0, double t1, bool forClipboard=true) const override
Create new tracks and don't modify this track.
Definition: WaveTrack.cpp:1154
WaveClipHolders & GetClips()
Definition: WaveTrack.h:690
size_t NChannels() const override
May report more than one only when this is a leader track.
Definition: WaveTrack.cpp:620
double LongSamplesToTime(sampleCount pos) const
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159