Audacity  3.0.3
Public Member Functions | Static Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
CompareAudioCommand Class Referencefinal

Returns information about the amount of audio that is about a certain threshold of difference in two selected tracks. More...

#include <CompareAudioCommand.h>

Inheritance diagram for CompareAudioCommand:
[legend]
Collaboration diagram for CompareAudioCommand:
[legend]

Public Member Functions

ComponentInterfaceSymbol GetSymbol () override
 
TranslatableString GetDescription () override
 
bool DefineParams (ShuttleParams &S) override
 
void PopulateOrExchange (ShuttleGui &S) override
 
ManualPageID ManualPage () override
 
bool Apply (const CommandContext &context) override
 
- Public Member Functions inherited from AudacityCommand
 AudacityCommand ()
 
virtual ~AudacityCommand ()
 
PluginPath GetPath () override
 
VendorSymbol GetVendor () override
 
wxString GetVersion () override
 
virtual bool IsBatchProcessing ()
 
virtual void SetBatchProcessing (bool start)
 
virtual bool Apply (const CommandContext &WXUNUSED(context))
 
bool ShowInterface (wxWindow *parent, bool forceModal=false)
 
virtual void SetHostUI (EffectUIHostInterface *WXUNUSED(host))
 
wxDialog * CreateUI (wxWindow *parent, AudacityCommand *client)
 
virtual bool GetAutomationParameters (wxString &parms)
 
virtual bool SetAutomationParameters (const wxString &parms)
 
bool DoAudacityCommand (wxWindow *parent, const CommandContext &context, bool shouldPrompt=true)
 
int MessageBox (const TranslatableString &message, long style=DefaultMessageBoxStyle, const TranslatableString &titleStr={})
 
virtual bool Init ()
 
virtual bool PromptUser (wxWindow *parent)
 
virtual bool CheckWhetherSkipAudacityCommand ()
 
virtual void End ()
 
virtual void PopulateOrExchange (ShuttleGui &WXUNUSED(S))
 
virtual bool TransferDataToWindow ()
 
virtual bool TransferDataFromWindow ()
 
- Public Member Functions inherited from ComponentInterface
virtual ~ComponentInterface ()
 
TranslatableString GetName ()
 
virtual bool DefineParams (ShuttleParams &WXUNUSED(S))
 

Static Public Attributes

static const ComponentInterfaceSymbol Symbol { XO("Compare Audio") }
 

Protected Member Functions

double CompareSample (double value1, double value2)
 

Private Member Functions

bool GetSelection (const CommandContext &context, AudacityProject &proj)
 

Private Attributes

double errorThreshold
 
double mT0
 
double mT1
 
const WaveTrackmTrack0
 
const WaveTrackmTrack1
 

Additional Inherited Members

- Public Types inherited from AudacityCommand
enum  : long { DefaultMessageBoxStyle = wxOK | wxCENTRE }
 
using VetoDialogHook = bool(*)(wxDialog *)
 
- Static Public Member Functions inherited from AudacityCommand
static VetoDialogHook SetVetoDialogHook (VetoDialogHook hook)
 
- Protected Attributes inherited from AudacityCommand
ProgressDialogmProgress
 
wxDialog * mUIDialog
 
wxWindow * mUIParent
 
int mUIResultID
 

Detailed Description

Returns information about the amount of audio that is about a certain threshold of difference in two selected tracks.

Definition at line 26 of file CompareAudioCommand.h.

Member Function Documentation

◆ Apply()

bool CompareAudioCommand::Apply ( const CommandContext context)
override

Definition at line 111 of file CompareAudioCommand.cpp.

112 {
113  if (!GetSelection(context, context.project))
114  {
115  return false;
116  }
117 
118  wxString msg = wxT("Comparing tracks '");
119  msg += mTrack0->GetName() + wxT("' and '")
120  + mTrack1->GetName() + wxT("'.");
121  context.Status(msg);
122 
123  long errorCount = 0;
124  // Initialize buffers for track data to be analyzed
125  auto buffSize = std::min(mTrack0->GetMaxBlockSize(), mTrack1->GetMaxBlockSize());
126 
127  Floats buff0{ buffSize };
128  Floats buff1{ buffSize };
129 
130  // Compare tracks block by block
131  auto s0 = mTrack0->TimeToLongSamples(mT0);
132  auto s1 = mTrack0->TimeToLongSamples(mT1);
133  auto position = s0;
134  auto length = s1 - s0;
135  while (position < s1)
136  {
137  // Get a block of data into the buffers
138  auto block = limitSampleBufferSize(
139  mTrack0->GetBestBlockSize(position), s1 - position
140  );
141  mTrack0->GetFloats(buff0.get(), position, block);
142  mTrack1->GetFloats(buff1.get(), position, block);
143 
144  for (decltype(block) buffPos = 0; buffPos < block; ++buffPos)
145  {
146  if (CompareSample(buff0[buffPos], buff1[buffPos]) > errorThreshold)
147  {
148  ++errorCount;
149  }
150  }
151 
152  position += block;
153  context.Progress(
154  (position - s0).as_double() /
155  length.as_double()
156  );
157  }
158 
159  // Output the results
160  double errorSeconds = mTrack0->LongSamplesToTime(errorCount);
161  context.Status(wxString::Format(wxT("%li"), errorCount));
162  context.Status(wxString::Format(wxT("%.4f"), errorSeconds));
163  context.Status(wxString::Format(wxT("Finished comparison: %li samples (%.3f seconds) exceeded the error threshold of %f."), errorCount, errorSeconds, errorThreshold));
164  return true;
165 }

References CompareSample(), errorThreshold, WaveTrack::GetBestBlockSize(), WaveTrack::GetFloats(), WaveTrack::GetMaxBlockSize(), Track::GetName(), GetSelection(), limitSampleBufferSize(), WaveTrack::LongSamplesToTime(), min(), mT0, mT1, mTrack0, mTrack1, CommandContext::Progress(), CommandContext::project, CommandContext::Status(), and WaveTrack::TimeToLongSamples().

Here is the call graph for this function:

◆ CompareSample()

double CompareAudioCommand::CompareSample ( double  value1,
double  value2 
)
protected

Definition at line 101 of file CompareAudioCommand.cpp.

102 {
103  return fabs(value1 - value2);
104 }

Referenced by Apply().

Here is the caller graph for this function:

◆ DefineParams()

bool CompareAudioCommand::DefineParams ( ShuttleParams S)
override

Definition at line 50 of file CompareAudioCommand.cpp.

50  {
51  S.Define( errorThreshold, wxT("Threshold"), 0.0f, 0.0f, 0.01f, 1.0f );
52  return true;
53 }

References ShuttleParams::Define(), and errorThreshold.

Here is the call graph for this function:

◆ GetDescription()

TranslatableString CompareAudioCommand::GetDescription ( )
inlineoverridevirtual

Reimplemented from AudacityCommand.

Definition at line 33 of file CompareAudioCommand.h.

33 {return XO("Compares a range on two tracks.");};

References XO.

◆ GetSelection()

bool CompareAudioCommand::GetSelection ( const CommandContext context,
AudacityProject proj 
)
private

Definition at line 67 of file CompareAudioCommand.cpp.

68 {
69  // Get the selected time interval
70  auto &selectedRegion = ViewInfo::Get( proj ).selectedRegion;
71  mT0 = selectedRegion.t0();
72  mT1 = selectedRegion.t1();
73  if (mT0 >= mT1)
74  {
75  context.Error(wxT("There is no selection!"));
76  return false;
77  }
78 
79  // Get the selected tracks and check that there are at least two to
80  // compare
81  auto trackRange = TrackList::Get( proj ).Selected< const WaveTrack >();
82  mTrack0 = *trackRange.first;
83  if (mTrack0 == NULL)
84  {
85  context.Error(wxT("No tracks selected! Select two tracks to compare."));
86  return false;
87  }
88  mTrack1 = * ++ trackRange.first;
89  if (mTrack1 == NULL)
90  {
91  context.Error(wxT("Only one track selected! Select two tracks to compare."));
92  return false;
93  }
94  if ( * ++ trackRange.first )
95  {
96  context.Status(wxT("More than two tracks selected - only the first two will be compared."));
97  }
98  return true;
99 }

References CommandContext::Error(), ViewInfo::Get(), TrackList::Get(), mT0, mT1, mTrack0, mTrack1, TrackList::Selected(), ViewInfo::selectedRegion, and CommandContext::Status().

Referenced by Apply().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSymbol()

ComponentInterfaceSymbol CompareAudioCommand::GetSymbol ( )
inlineoverridevirtual

Implements AudacityCommand.

Definition at line 32 of file CompareAudioCommand.h.

32 {return Symbol;}

References Symbol.

◆ ManualPage()

ManualPageID CompareAudioCommand::ManualPage ( )
inlineoverridevirtual

Reimplemented from AudacityCommand.

Definition at line 38 of file CompareAudioCommand.h.

38 {return L"Extra_Menu:_Scriptables_II#compare_Audio";}

◆ PopulateOrExchange()

void CompareAudioCommand::PopulateOrExchange ( ShuttleGui S)
override

Definition at line 55 of file CompareAudioCommand.cpp.

56 {
57  S.AddSpace(0, 5);
58 
59  S.StartMultiColumn(2, wxALIGN_CENTER);
60  {
61  S.TieTextBox(XXO("Threshold:"),errorThreshold);
62  }
63  S.EndMultiColumn();
64 }

References ShuttleGui::AddSpace(), ShuttleGuiBase::EndMultiColumn(), errorThreshold, ShuttleGuiBase::StartMultiColumn(), ShuttleGuiBase::TieTextBox(), and XXO.

Here is the call graph for this function:

Member Data Documentation

◆ errorThreshold

double CompareAudioCommand::errorThreshold
private

Definition at line 43 of file CompareAudioCommand.h.

Referenced by Apply(), DefineParams(), and PopulateOrExchange().

◆ mT0

double CompareAudioCommand::mT0
private

Definition at line 44 of file CompareAudioCommand.h.

Referenced by Apply(), and GetSelection().

◆ mT1

double CompareAudioCommand::mT1
private

Definition at line 44 of file CompareAudioCommand.h.

Referenced by Apply(), and GetSelection().

◆ mTrack0

const WaveTrack* CompareAudioCommand::mTrack0
private

Definition at line 45 of file CompareAudioCommand.h.

Referenced by Apply(), and GetSelection().

◆ mTrack1

const WaveTrack* CompareAudioCommand::mTrack1
private

Definition at line 46 of file CompareAudioCommand.h.

Referenced by Apply(), and GetSelection().

◆ Symbol

const ComponentInterfaceSymbol CompareAudioCommand::Symbol { XO("Compare Audio") }
static

Definition at line 29 of file CompareAudioCommand.h.

Referenced by GetSymbol().


The documentation for this class was generated from the following files:
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:161
WaveTrack
A Track that contains audio waveform data.
Definition: WaveTrack.h:69
CompareAudioCommand::mTrack1
const WaveTrack * mTrack1
Definition: CompareAudioCommand.h:46
CompareAudioCommand::mT0
double mT0
Definition: CompareAudioCommand.h:44
Track::GetName
wxString GetName() const
Definition: Track.h:426
ShuttleGui::AddSpace
wxSizerItem * AddSpace(int width, int height, int prop=0)
Definition: ShuttleGui.cpp:2459
XO
#define XO(s)
Definition: Internat.h:31
CompareAudioCommand::errorThreshold
double errorThreshold
Definition: CompareAudioCommand.h:43
ShuttleGuiBase::EndMultiColumn
void EndMultiColumn()
Definition: ShuttleGui.cpp:1238
ShuttleGuiBase::TieTextBox
wxTextCtrl * TieTextBox(const TranslatableString &Caption, wxString &Value, const int nChars=0)
Definition: ShuttleGui.cpp:1652
CommandContext::Status
virtual void Status(const wxString &message, bool bFlush=false) const
Definition: CommandContext.cpp:63
ViewInfo::selectedRegion
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:196
XXO
#define XXO(s)
Definition: Internat.h:44
WaveTrack::LongSamplesToTime
double LongSamplesToTime(sampleCount pos) const
Convert correctly between a number of samples and an (absolute) time in seconds.
Definition: WaveTrack.cpp:1870
ShuttleGuiBase::StartMultiColumn
void StartMultiColumn(int nCols, int PositionFlags=wxALIGN_LEFT)
Definition: ShuttleGui.cpp:1229
CompareAudioCommand::mT1
double mT1
Definition: CompareAudioCommand.h:44
CompareAudioCommand::Symbol
static const ComponentInterfaceSymbol Symbol
Definition: CompareAudioCommand.h:29
CommandContext::Error
virtual void Error(const wxString &message) const
Definition: CommandContext.cpp:73
CommandContext::Progress
virtual void Progress(double d) const
Definition: CommandContext.cpp:83
WaveTrack::GetMaxBlockSize
size_t GetMaxBlockSize() const
Definition: WaveTrack.cpp:1669
min
int min(int a, int b)
Definition: CompareAudioCommand.cpp:106
WaveTrack::TimeToLongSamples
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: WaveTrack.cpp:1865
TrackList::Get
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:506
CompareAudioCommand::mTrack0
const WaveTrack * mTrack0
Definition: CompareAudioCommand.h:45
WaveTrack::GetBestBlockSize
size_t GetBestBlockSize(sampleCount t) const
Definition: WaveTrack.cpp:1651
ShuttleParams::Define
virtual void Define(bool &var, const wxChar *key, const bool vdefault, const bool vmin=false, const bool vmax=false, const bool vscl=false)
Definition: Shuttle.cpp:332
CompareAudioCommand::GetSelection
bool GetSelection(const CommandContext &context, AudacityProject &proj)
Definition: CompareAudioCommand.cpp:67
limitSampleBufferSize
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:23
WaveTrack::GetFloats
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: WaveTrack.h:265
CommandContext::project
AudacityProject & project
Definition: CommandContext.h:64
TrackList::Selected
auto Selected() -> TrackIterRange< TrackType >
Definition: Track.h:1388
ArrayOf< float >
CompareAudioCommand::CompareSample
double CompareSample(double value1, double value2)
Definition: CompareAudioCommand.cpp:101