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 WaveTrack &oldTrack, sampleCount start, sampleCount len,
20 WaveTrack &newContents)
21{
22 // now move the appropriate bit of the output back to the track
23 // (this could be enhanced in the future to use the tails)
24 double lenT = oldTrack.LongSamplesToTime(len);
25 // 'start' is the sample offset in 't', the passed in track
26 // 'startT' is the equivalent time value
27 // 'newContents' starts at zero
28 double startT = oldTrack.LongSamplesToTime(start);
29
30 //newContents has one waveclip for the total length, even though
31 //oldTrack might have whitespace separating multiple clips
32 //we want to maintain the original clip structure, so
33 //only paste the intersections of the NEW clip.
34
35 //Find the bits of clips that need replacing
36 std::vector<std::pair<double, double> > clipStartEndTimes;
37 //may be truncated due to a clip being partially selected
38 std::vector<std::pair<double, double> > clipRealStartEndTimes;
39 //Used to restore clip names after pasting
40 std::vector<wxString> clipNames;
41 for (const auto &clip : oldTrack.GetClips()) {
42 auto clipStartT = clip->GetPlayStartTime();
43 auto clipEndT = clip->GetPlayEndTime();
44 if ( clipEndT <= startT )
45 continue; // clip is not within selection
46 if ( clipStartT >= startT + lenT )
47 continue; // clip is not within selection
48
49 //save the actual clip start/end so that we can rejoin them after we paste.
50 clipRealStartEndTimes.emplace_back(clipStartT, clipEndT);
51
52 if ( clipStartT < startT ) // does selection cover the whole clip?
53 clipStartT = startT; // don't copy all the NEW clip
54 if( clipEndT > startT + lenT ) // does selection cover the whole clip?
55 clipEndT = startT + lenT; // don't copy all the NEW clip
56
57 //save them
58 clipStartEndTimes.emplace_back(clipStartT, clipEndT);
59 clipNames.push_back(clip->GetName());
60 }
61 //now go through and replace the old clips with NEW
62 for (unsigned int i = 0; i < clipStartEndTimes.size(); ++i) {
63 //remove the old audio and get the NEW
64 auto [start, end] = clipStartEndTimes[i];
65 oldTrack.Clear(start, end);
66 auto toClipOutput = newContents.Copy(start - startT, end - startT);
67 //put the processed audio in
68 oldTrack.Paste(start, toClipOutput.get());
69
70 //Restore original clip's name
71 auto newClip = oldTrack.GetClipAtTime(start + 0.5 / oldTrack.GetRate());
72 newClip->SetName(clipNames[i]);
73
74 //if the clip was only partially selected, the Paste will have created a
75 // split line. Join is needed to take care of this
76 //This is not true when the selection is fully contained within one clip
77 // (second half of conditional)
78 auto [realStart, realEnd] = clipRealStartEndTimes[i];
79 if ((realStart != start || realEnd != end) &&
80 !(realStart <= startT && realEnd >= startT + lenT) )
81 oldTrack.Join(realStart, realEnd);
82 }
83}
void PasteOverPreservingClips(WaveTrack &oldTrack, sampleCount start, sampleCount len, WaveTrack &newContents)
Substitute new contents into existing track, preserving clip boundaries.
double LongSamplesToTime(sampleCount pos) const
Convert correctly between a number of samples and an (absolute) time in seconds.
Definition: SampleTrack.cpp:48
void SetName(const wxString &name)
Definition: WaveClip.cpp:847
A Track that contains audio waveform data.
Definition: WaveTrack.h:51
void Paste(double t0, const Track *src) override
Definition: WaveTrack.cpp:1367
void Join(double t0, double t1)
Definition: WaveTrack.cpp:1512
WaveClip * GetClipAtTime(double time)
Definition: WaveTrack.cpp:2127
void Clear(double t0, double t1) override
Definition: WaveTrack.cpp:652
double GetRate() const override
Definition: WaveTrack.cpp:360
WaveClipHolders & GetClips()
Definition: WaveTrack.h:322
Track::Holder Copy(double t0, double t1, bool forClipboard=true) const override
Definition: WaveTrack.cpp:585
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