forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepair.cpp
More file actions
154 lines (121 loc) · 4.76 KB
/
Copy pathRepair.cpp
File metadata and controls
154 lines (121 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**********************************************************************
Audacity: A Digital Audio Editor
Repair.cpp
Dominic Mazzoni
*******************************************************************//**
\class EffectRepair
\brief Use the interpolation code to fill in damaged audio.
Damage can include pops, clicks, or clipping. As long as the
damaged section is short and surrounded by lots of good audio,
it is usually quite successful.
This was formerly the PopClickRemoval effect, but it was
renamed and focused on the smaller subproblem of repairing
the audio, rather than actually finding the clicks.
*//*******************************************************************/
#include "../Audacity.h"
#include "Repair.h"
#include <math.h>
#include <wx/intl.h>
#include "../InterpolateAudio.h"
#include "../WaveTrack.h"
#include "../widgets/AudacityMessageBox.h"
#include "LoadEffects.h"
const ComponentInterfaceSymbol EffectRepair::Symbol
{ XO("Repair") };
namespace{ BuiltinEffectsModule::Registration< EffectRepair > reg; }
EffectRepair::EffectRepair()
{
}
EffectRepair::~EffectRepair()
{
}
// ComponentInterface implementation
ComponentInterfaceSymbol EffectRepair::GetSymbol()
{
return Symbol;
}
TranslatableString EffectRepair::GetDescription()
{
return XO("Sets the peak amplitude of a one or more tracks");
}
// EffectDefinitionInterface implementation
EffectType EffectRepair::GetType()
{
return EffectTypeProcess;
}
bool EffectRepair::IsInteractive()
{
return false;
}
// Effect implementation
bool EffectRepair::Process()
{
//v This may be too much copying for EffectRepair. To support Cancel, may be able to copy much less.
// But for now, Cancel isn't supported without this.
this->CopyInputTracks(); // Set up mOutputTracks. //v This may be too much copying for EffectRepair.
bool bGoodResult = true;
int count = 0;
for( auto track : mOutputTracks->Selected< WaveTrack >() ) {
const
double trackStart = track->GetStartTime();
const double repair_t0 = std::max(mT0, trackStart);
const
double trackEnd = track->GetEndTime();
const double repair_t1 = std::min(mT1, trackEnd);
const
double repair_deltat = repair_t1 - repair_t0;
if (repair_deltat > 0) { // selection is within track audio
const auto repair0 = track->TimeToLongSamples(repair_t0);
const auto repair1 = track->TimeToLongSamples(repair_t1);
const auto repairLen = repair1 - repair0;
if (repairLen > 128) {
::Effect::MessageBox(
XO(
"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.") );
bGoodResult = false;
break;
}
const double rate = track->GetRate();
const double spacing = std::max(repair_deltat * 2, 128. / rate);
const double t0 = std::max(repair_t0 - spacing, trackStart);
const double t1 = std::min(repair_t1 + spacing, trackEnd);
const auto s0 = track->TimeToLongSamples(t0);
const auto s1 = track->TimeToLongSamples(t1);
// The difference is at most 2 * 128:
const auto repairStart = (repair0 - s0).as_size_t();
const auto len = s1 - s0;
if (s0 == repair0 && s1 == repair1) {
::Effect::MessageBox(
XO(
"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.") );
/// The Repair effect needs some data to go on.\n\nPlease select an area to repair with some audio on at least one side (the more the better).") );
bGoodResult = false;
break;
}
if (!ProcessOne(count, track, s0,
// len is at most 5 * 128.
len.as_size_t(),
repairStart,
// repairLen is at most 128.
repairLen.as_size_t() )) {
bGoodResult = false;
break;
}
}
count++;
}
this->ReplaceProcessedTracks(bGoodResult);
return bGoodResult;
}
bool EffectRepair::ProcessOne(int count, WaveTrack * track,
sampleCount start,
size_t len,
size_t repairStart, size_t repairLen)
{
Floats buffer{ len };
track->Get((samplePtr) buffer.get(), floatSample, start, len);
InterpolateAudio(buffer.get(), len, repairStart, repairLen);
track->Set((samplePtr)&buffer[repairStart], floatSample,
start + repairStart, repairLen);
return !TrackProgress(count, 1.0); // TrackProgress returns true on Cancel.
}