Audacity  2.2.2
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 #include "../Audacity.h"
25 
26 #include <math.h>
27 
28 #include <wx/intl.h>
29 
30 #include "../InterpolateAudio.h"
31 #include "../WaveTrack.h"
32 #include "../widgets/ErrorDialog.h"
33 
34 #include "Repair.h"
35 
37 {
38 }
39 
41 {
42 }
43 
44 // IdentInterface implementation
45 
47 {
48  return REPAIR_PLUGIN_SYMBOL;
49 }
50 
52 {
53  return _("Sets the peak amplitude of a one or more tracks");
54 }
55 
56 // EffectDefinitionInterface implementation
57 
59 {
60  return EffectTypeProcess;
61 }
62 
64 {
65  return false;
66 }
67 
68 // Effect implementation
69 
71 {
72  //v This may be too much copying for EffectRepair. To support Cancel, may be able to copy much less.
73  // But for now, Cancel isn't supported without this.
74  this->CopyInputTracks(); // Set up mOutputTracks. //v This may be too much copying for EffectRepair.
75  bool bGoodResult = true;
76 
78  WaveTrack *track = (WaveTrack *) iter.First();
79  int count = 0;
80  while (track) {
81  const
82  double trackStart = track->GetStartTime();
83  const double repair_t0 = std::max(mT0, trackStart);
84  const
85  double trackEnd = track->GetEndTime();
86  const double repair_t1 = std::min(mT1, trackEnd);
87  const
88  double repair_deltat = repair_t1 - repair_t0;
89  if (repair_deltat > 0) { // selection is within track audio
90  const auto repair0 = track->TimeToLongSamples(repair_t0);
91  const auto repair1 = track->TimeToLongSamples(repair_t1);
92  const auto repairLen = repair1 - repair0;
93  if (repairLen > 128) {
94  ::Effect::MessageBox(_("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."));
95  bGoodResult = false;
96  break;
97  }
98 
99  const double rate = track->GetRate();
100  const double spacing = std::max(repair_deltat * 2, 128. / rate);
101  const double t0 = std::max(repair_t0 - spacing, trackStart);
102  const double t1 = std::min(repair_t1 + spacing, trackEnd);
103 
104  const auto s0 = track->TimeToLongSamples(t0);
105  const auto s1 = track->TimeToLongSamples(t1);
106  // The difference is at most 2 * 128:
107  const auto repairStart = (repair0 - s0).as_size_t();
108  const auto len = s1 - s0;
109 
110  if (s0 == repair0 && s1 == repair1) {
111  ::Effect::MessageBox(_("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."));
113  bGoodResult = false;
114  break;
115  }
116 
117  if (!ProcessOne(count, track, s0,
118  // len is at most 5 * 128.
119  len.as_size_t(),
120  repairStart,
121  // repairLen is at most 128.
122  repairLen.as_size_t() )) {
123  bGoodResult = false;
124  break;
125  }
126  }
127 
128  track = (WaveTrack *) iter.Next();
129  count++;
130  }
131 
132  this->ReplaceProcessedTracks(bGoodResult);
133  return bGoodResult;
134 }
135 
136 bool EffectRepair::ProcessOne(int count, WaveTrack * track,
137  sampleCount start,
138  size_t len,
139  size_t repairStart, size_t repairLen)
140 {
141  Floats buffer{ len };
142  track->Get((samplePtr) buffer.get(), floatSample, start, len);
143  InterpolateAudio(buffer.get(), len, repairStart, repairLen);
144  track->Set((samplePtr)&buffer[repairStart], floatSample,
145  start + repairStart, repairLen);
146  return !TrackProgress(count, 1.0); // TrackProgress returns true on Cancel.
147 }
double mT1
Definition: Effect.h:461
int MessageBox(const wxString &message, long style=DefaultMessageBoxStyle, const wxString &titleStr=wxString{})
Definition: Effect.cpp:2660
bool TrackProgress(int whichTrack, double frac, const wxString &=wxEmptyString)
Definition: Effect.cpp:1985
bool IsInteractive() override
Definition: Repair.cpp:63
EffectRepair()
Definition: Repair.cpp:36
void CopyInputTracks()
Definition: Effect.cpp:2036
IdentInterfaceSymbol GetSymbol() override
Definition: Repair.cpp:46
void ReplaceProcessedTracks(const bool bGoodResult)
Definition: Effect.cpp:2162
virtual ~EffectRepair()
Definition: Repair.cpp:40
void InterpolateAudio(float *buffer, const size_t len, size_t firstBad, size_t numBad)
double GetStartTime() const override
Get the time at which the first clip in the track starts.
Definition: WaveTrack.cpp:1853
void Set(samplePtr buffer, sampleFormat format, sampleCount start, size_t len)
Definition: WaveTrack.cpp:2052
char * samplePtr
Definition: Types.h:203
A Track that contains audio waveform data.
Definition: WaveTrack.h:60
int min(int a, int b)
bool ProcessOne(int count, WaveTrack *track, sampleCount start, size_t len, size_t repairStart, size_t repairLen)
Definition: Repair.cpp:136
IdentInterfaceSymbol pairs a persistent string identifier used internally with an optional...
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
EffectType
EffectType GetType() override
Definition: Repair.cpp:58
wxString GetDescription() override
Definition: Repair.cpp:51
bool Get(samplePtr buffer, sampleFormat format, sampleCount start, size_t len, fillFormat fill=fillZero, bool mayThrow=true, sampleCount *pNumCopied=nullptr) const
Definition: WaveTrack.cpp:1971
bool Process() override
Definition: Repair.cpp:70
#define REPAIR_PLUGIN_SYMBOL
Definition: Repair.h:18
std::shared_ptr< TrackList > mOutputTracks
Definition: Effect.h:459
double mT0
Definition: Effect.h:460