Audacity 3.2.0
RepeatBase.cpp
Go to the documentation of this file.
1#include "RepeatBase.h"
3#include "LabelTrack.h"
4#include "SyncLock.h"
5#include "WaveClip.h"
6#include "WaveTrack.h"
7#include <cmath>
8
10{
12 return parameters;
13}
14
16
18{
19 Parameters().Reset(*this);
21}
22
24{
25}
26
27// ComponentInterface implementation
28
30{
31 return Symbol;
32}
33
35{
36 return XO("Repeats the selection the specified number of times");
37}
38
40{
41 return L"Repeat";
42}
43
44// EffectDefinitionInterface implementation
45
47{
48 return EffectTypeProcess;
49}
50
51// Effect implementation
52
54{
55 // Set up mOutputTracks.
56 // This effect needs all for sync-lock grouping.
57 EffectOutputTracks outputs { *mTracks, GetType(), { { mT0, mT1 } }, true };
58
59 int nTrack = 0;
60 bool bGoodResult = true;
61 double maxDestLen = 0.0; // used to change selection to generated bit
62
63 outputs.Get().Any().VisitWhile(
64 bGoodResult,
65 [&](LabelTrack& track) {
67 {
68 if (!track.Repeat(mT0, mT1, repeatCount))
69 bGoodResult = false;
70 }
71 },
72 [&](auto&& fallthrough) {
73 return [&](WaveTrack& track) {
74 if (!track.GetSelected())
75 return fallthrough(); // Fall through to next lambda
76 auto start = track.TimeToLongSamples(mT0);
77 auto end = track.TimeToLongSamples(mT1);
78 auto len = end - start;
79 const double tLen = track.LongSamplesToTime(len);
80 const double tc = mT0 + tLen;
81
82 if (len <= 0)
83 return;
84
85 auto firstTemp =
86 std::static_pointer_cast<WaveTrack>(track.Copy(mT0, mT1));
87
88 auto t0 = tc;
89 for (size_t j = 0; j < repeatCount; ++j)
90 {
91 if (TrackProgress(nTrack, j / repeatCount))
92 {
93 // TrackProgress returns true on Cancel.
94 bGoodResult = false;
95 return;
96 }
97 track.Paste(t0, *firstTemp);
98 t0 += tLen;
99 }
100 if (t0 > maxDestLen)
101 maxDestLen = t0;
102
103 const auto compareIntervals = [](const auto& a, const auto& b) {
104 return a->Start() < b->Start();
105 };
106
107 const auto eps = 0.5 / track.GetRate();
108 auto sortedIntervals =
109 std::vector(track.Intervals().begin(), track.Intervals().end());
110 auto sourceIntervals = std::vector(
111 firstTemp->Intervals().begin(), firstTemp->Intervals().end());
112 std::sort(
113 sortedIntervals.begin(), sortedIntervals.end(),
114 compareIntervals);
115 std::sort(
116 sourceIntervals.begin(), sourceIntervals.end(),
117 compareIntervals);
118 for (auto it = sortedIntervals.begin(); it != sortedIntervals.end();
119 ++it)
120 {
121 const auto& interval = *it;
122 // Find first pasted interval
123 if (std::abs((*it)->GetPlayStartTime() - tc) > eps)
124 continue;
125
126 // Fix pasted clips names
127 for (int j = 0; j < repeatCount; ++j)
128 {
129 for (const auto& src : sourceIntervals)
130 {
131 if (it == sortedIntervals.end())
132 break;
133 (*it++)->SetName(src->GetName());
134 }
135 }
136 break;
137 }
138 nTrack++;
139 };
140 },
141 [&](Track& t) {
143 t.SyncLockAdjust(mT1, mT1 + (mT1 - mT0) * repeatCount);
144 });
145
146 if (bGoodResult)
147 {
148 // Select the NEW bits + original bit
149 mT1 = maxDestLen;
150 }
151
152 if (bGoodResult)
153 outputs.Commit();
154 return bGoodResult;
155}
156
158{
159 return false;
160}
EffectType
@ EffectTypeProcess
XO("Cut/Copy/Paste")
Generates EffectParameterMethods overrides from variadic template arguments.
IteratorRange< IntervalIterator< IntervalType > > Intervals()
Get range of intervals with mutative access.
Definition: Channel.h:248
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
double mT1
Definition: EffectBase.h:123
void SetLinearEffectFlag(bool linearEffectFlag)
Definition: EffectBase.cpp:210
std::shared_ptr< TrackList > mTracks
Definition: EffectBase.h:116
double mT0
Definition: EffectBase.h:122
bool TrackProgress(int whichTrack, double frac, const TranslatableString &={}) const
Definition: Effect.cpp:343
Performs effect computation.
Use this object to copy the input tracks to tentative outputTracks.
Interface for manipulations of an Effect's settings.
virtual void Reset(Effect &effect) const =0
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:98
bool Repeat(double t0, double t1, int n)
Definition: LabelTrack.cpp:935
void Paste(double t, const Track &src) override
Weak precondition allows overrides to replicate one channel into many.
Definition: LabelTrack.cpp:918
Track::Holder Copy(double t0, double t1, bool forClipboard=true) const override
Create new tracks and don't modify this track.
Definition: LabelTrack.cpp:837
ManualPageID ManualPage() const override
Name of a page in the Audacity alpha manual, default is empty.
Definition: RepeatBase.cpp:39
bool NeedsDither() const override
Definition: RepeatBase.cpp:157
TranslatableString GetDescription() const override
Definition: RepeatBase.cpp:34
virtual ~RepeatBase()
Definition: RepeatBase.cpp:23
const EffectParameterMethods & Parameters() const override
Definition: RepeatBase.cpp:9
EffectType GetType() const override
Type determines how it behaves.
Definition: RepeatBase.cpp:46
ComponentInterfaceSymbol GetSymbol() const override
Definition: RepeatBase.cpp:29
bool Process(EffectInstance &instance, EffectSettings &settings) override
Definition: RepeatBase.cpp:53
int repeatCount
Definition: RepeatBase.h:34
static const ComponentInterfaceSymbol Symbol
Definition: RepeatBase.h:11
static bool IsSelectedOrSyncLockSelected(const Track &track)
Definition: SyncLock.cpp:104
static bool IsSyncLockSelected(const Track &track)
Definition: SyncLock.cpp:80
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:110
bool GetSelected() const
Selectedness is always the same for all channels of a group.
Definition: Track.cpp:78
Holds a msgid for the translation catalog; may also bind format arguments.
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
Externalized state of a plug-in.