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*******************************************************************//*******************************************************************/
22
23
24
25#include "Repair.h"
26
27#include <math.h>
28
29#include "InterpolateAudio.h"
30#include "WaveTrack.h"
31#include "AudacityMessageBox.h"
32
33#include "LoadEffects.h"
34
36{ XO("Repair") };
37
39
41{
42}
43
45{
46}
47
48// ComponentInterface implementation
49
51{
52 return Symbol;
53}
54
56{
57 return XO("Sets the peak amplitude of a one or more tracks");
58}
59
60// EffectDefinitionInterface implementation
61
63{
64 return EffectTypeProcess;
65}
66
68{
69 return false;
70}
71
72// Effect implementation
73
75{
76 //v This may be too much copying for EffectRepair. To support Cancel, may be able to copy much less.
77 // But for now, Cancel isn't supported without this.
78 this->CopyInputTracks(); // Set up mOutputTracks. //v This may be too much copying for EffectRepair.
79 bool bGoodResult = true;
80
81 int count = 0;
82 for( auto track : mOutputTracks->Selected< WaveTrack >() ) {
83 const
84 double trackStart = track->GetStartTime();
85 const double repair_t0 = std::max(mT0, trackStart);
86 const
87 double trackEnd = track->GetEndTime();
88 const double repair_t1 = std::min(mT1, trackEnd);
89 const
90 double repair_deltat = repair_t1 - repair_t0;
91 if (repair_deltat > 0) { // selection is within track audio
92 const auto repair0 = track->TimeToLongSamples(repair_t0);
93 const auto repair1 = track->TimeToLongSamples(repair_t1);
94 const auto repairLen = repair1 - repair0;
95 if (repairLen > 128) {
97 XO(
98"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.") );
99 bGoodResult = false;
100 break;
101 }
102
103 const double rate = track->GetRate();
104 const double spacing = std::max(repair_deltat * 2, 128. / rate);
105 const double t0 = std::max(repair_t0 - spacing, trackStart);
106 const double t1 = std::min(repair_t1 + spacing, trackEnd);
107
108 const auto s0 = track->TimeToLongSamples(t0);
109 const auto s1 = track->TimeToLongSamples(t1);
110 // The difference is at most 2 * 128:
111 const auto repairStart = (repair0 - s0).as_size_t();
112 const auto len = s1 - s0;
113
114 if (s0 == repair0 && s1 == repair1) {
116 XO(
117"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.") );
119 bGoodResult = false;
120 break;
121 }
122
123 if (!ProcessOne(count, track, s0,
124 // len is at most 5 * 128.
125 len.as_size_t(),
126 repairStart,
127 // repairLen is at most 128.
128 repairLen.as_size_t() )) {
129 bGoodResult = false;
130 break;
131 }
132 }
133
134 count++;
135 }
136
137 this->ReplaceProcessedTracks(bGoodResult);
138 return bGoodResult;
139}
140
141bool EffectRepair::ProcessOne(int count, WaveTrack * track,
142 sampleCount start,
143 size_t len,
144 size_t repairStart, size_t repairLen)
145{
146 Floats buffer{ len };
147 track->GetFloats(buffer.get(), start, len);
148 InterpolateAudio(buffer.get(), len, repairStart, repairLen);
149 track->Set((samplePtr)&buffer[repairStart], floatSample,
150 start + repairStart, repairLen,
151 // little repairs shouldn't force dither on rendering:
153 );
154 return !TrackProgress(count, 1.0); // TrackProgress returns true on Cancel.
155}
156
158{
159 return false;
160}
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.
char * samplePtr
Definition: SampleFormat.h:55
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
double mT1
Definition: EffectBase.h:113
std::shared_ptr< TrackList > mOutputTracks
Definition: EffectBase.h:111
double mT0
Definition: EffectBase.h:112
void ReplaceProcessedTracks(const bool bGoodResult)
Definition: EffectBase.cpp:232
void CopyInputTracks(bool allSyncLockSelected=false)
Definition: Effect.cpp:396
bool TrackProgress(int whichTrack, double frac, const TranslatableString &={}) const
Definition: Effect.cpp:346
Performs effect computation.
EffectType GetType() const override
Type determines how it behaves.
Definition: Repair.cpp:62
ComponentInterfaceSymbol GetSymbol() const override
Definition: Repair.cpp:50
bool IsInteractive() const override
Whether the effect needs a dialog for entry of settings.
Definition: Repair.cpp:67
bool Process(EffectInstance &instance, EffectSettings &settings) override
Definition: Repair.cpp:74
virtual ~EffectRepair()
Definition: Repair.cpp:44
EffectRepair()
Definition: Repair.cpp:40
TranslatableString GetDescription() const override
Definition: Repair.cpp:55
bool ProcessOne(int count, WaveTrack *track, sampleCount start, size_t len, size_t repairStart, size_t repairLen)
Definition: Repair.cpp:141
bool NeedsDither() const override
Definition: Repair.cpp:157
static const ComponentInterfaceSymbol Symbol
Definition: Repair.h:21
static int DoMessageBox(const EffectPlugin &plugin, const TranslatableString &message, long style=DefaultMessageBoxStyle, const TranslatableString &titleStr={})
bool GetFloats(float *buffer, sampleCount start, size_t len, fillFormat fill=fillZero, bool mayThrow=true, sampleCount *pNumWithinClips=nullptr) const
Retrieve samples from a track in floating-point format, regardless of the storage format.
Definition: SampleTrack.h:82
Holds a msgid for the translation catalog; may also bind format arguments.
A Track that contains audio waveform data.
Definition: WaveTrack.h:51
void Set(constSamplePtr buffer, sampleFormat format, sampleCount start, size_t len, sampleFormat effectiveFormat=widestSampleFormat)
Definition: WaveTrack.cpp:2002
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
BuiltinEffectsModule::Registration< EffectRepair > reg
Definition: Repair.cpp:38
Externalized state of a plug-in.