Audacity 3.2.0
Repair.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Repair.cpp
6
7 Dominic Mazzoni
8
9**********************************************************************/
10#include "Repair.h"
11#include "BasicUI.h"
12#include "EffectOutputTracks.h"
13#include "InterpolateAudio.h"
14#include "TimeStretching.h"
15#include "WaveTrack.h"
16#include <cmath>
17
19
20// ComponentInterface implementation
21
23{
24 return Symbol;
25}
26
28{
29 return XO("Sets the peak amplitude of a one or more tracks");
30}
31
32// EffectDefinitionInterface implementation
33
35{
36 return EffectTypeProcess;
37}
38
40{
41 return false;
42}
43
44// Effect implementation
45
47{
48 // This may be too much copying for Repair. To support Cancel, may be
49 // able to copy much less.
50 // But for now, Cancel isn't supported without this.
51 // Repair doesn't make sense for stretched clips, so don't pass a stretch
52 // interval.
53 EffectOutputTracks outputs { *mTracks, GetType(), {} };
54 bool bGoodResult = true;
55
56 int count = 0;
57 for (auto track : outputs.Get().Selected<WaveTrack>())
58 {
59 const double trackStart = track->GetStartTime();
60 const double repair_t0 = std::max(mT0, trackStart);
61 const double trackEnd = track->GetEndTime();
62 const double repair_t1 = std::min(mT1, trackEnd);
63 const double repair_deltat = repair_t1 - repair_t0;
64 if (repair_deltat > 0)
65 { // selection is within track audio
66 const auto repair0 = track->TimeToLongSamples(repair_t0);
67 const auto repair1 = track->TimeToLongSamples(repair_t1);
68 const auto repairLen = repair1 - repair0;
69 if (TimeStretching::HasPitchOrSpeed(*track, repair_t0, repair_t1))
70 {
72 "The Repair effect cannot be applied within stretched or shrunk clips"));
73 bGoodResult = false;
74 break;
75 }
76 if (repairLen > 128)
77 {
79 "The Repair effect is intended to be used on very short sections of damaged audio (up to 128 samples).\n\nZoom in and select a tiny fraction of a second to repair."));
80 bGoodResult = false;
81 break;
82 }
83
84 const double rate = track->GetRate();
85 const double spacing = std::max(repair_deltat * 2, 128. / rate);
86 const double t0 = std::max(repair_t0 - spacing, trackStart);
87 const double t1 = std::min(repair_t1 + spacing, trackEnd);
88
89 const auto s0 = track->TimeToLongSamples(t0);
90 const auto s1 = track->TimeToLongSamples(t1);
91 // The difference is at most 2 * 128:
92 const auto repairStart = (repair0 - s0).as_size_t();
93 const auto len = s1 - s0;
94
95 if (s0 == repair0 && s1 == repair1)
96 {
98 "Repair works by using audio data outside the selection region.\n\nPlease select a region that has audio touching at least one side of it.\n\nThe more surrounding audio, the better it performs."));
102 bGoodResult = false;
103 break;
104 }
105
106 for (const auto pChannel : track->Channels())
107 if (!ProcessOne(
108 count++, *pChannel, s0,
109 // len is at most 5 * 128.
110 len.as_size_t(), repairStart,
111 // repairLen is at most 128.
112 repairLen.as_size_t()))
113 {
114 bGoodResult = false;
115 goto done;
116 }
117 }
118 }
119done:
120
121 if (bGoodResult)
122 outputs.Commit();
123
124 return bGoodResult;
125}
126
128 int count, WaveChannel& track, sampleCount start, size_t len,
129 size_t repairStart, size_t repairLen)
130{
131 Floats buffer { len };
132 track.GetFloats(buffer.get(), start, len);
133 InterpolateAudio(buffer.get(), len, repairStart, repairLen);
134 if (!track.SetFloats(
135 &buffer[repairStart], start + repairStart, repairLen,
136 // little repairs shouldn't force dither on rendering:
138 return false;
139 return !TrackProgress(count, 1.0); // TrackProgress returns true on Cancel.
140}
141
143{
144 return false;
145}
Toolkit-neutral facade for basic user interface services.
int min(int a, int b)
EffectType
@ EffectTypeProcess
XO("Cut/Copy/Paste")
void InterpolateAudio(float *buffer, const size_t len, size_t firstBad, size_t numBad)
@ narrowestSampleFormat
Two synonyms for previous values that might change if more values were added.
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
double mT1
Definition: EffectBase.h:123
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.
bool IsInteractive() const override
Whether the effect needs a dialog for entry of settings.
Definition: Repair.cpp:39
static const ComponentInterfaceSymbol Symbol
Definition: Repair.h:19
ComponentInterfaceSymbol GetSymbol() const override
Definition: Repair.cpp:22
TranslatableString GetDescription() const override
Definition: Repair.cpp:27
bool Process(EffectInstance &instance, EffectSettings &settings) override
Definition: Repair.cpp:46
bool ProcessOne(int count, WaveChannel &track, sampleCount start, size_t len, size_t repairStart, size_t repairLen)
Definition: Repair.cpp:127
EffectType GetType() const override
Type determines how it behaves.
Definition: Repair.cpp:34
bool NeedsDither() const override
Definition: Repair.cpp:142
Holds a msgid for the translation catalog; may also bind format arguments.
bool SetFloats(const float *buffer, sampleCount start, size_t len, sampleFormat effectiveFormat=widestSampleFormat)
Random-access assignment of a range of samples.
Definition: WaveTrack.h:162
bool GetFloats(float *buffer, sampleCount start, size_t len, fillFormat fill=FillFormat::fillZero, bool mayThrow=true, sampleCount *pNumWithinClips=nullptr) const
"narrow" overload fetches from the unique channel
Definition: WaveTrack.h:129
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
MessageBoxResult ShowMessageBox(const TranslatableString &message, MessageBoxOptions options={})
Show a modal message box with either Ok or Yes and No, and optionally Cancel.
Definition: BasicUI.h:287
WAVE_TRACK_API bool HasPitchOrSpeed(const WaveTrack &track, double t0, double t1)
Externalized state of a plug-in.