Audacity 3.2.0
ContrastBase.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 ContrastBase.cpp
6
7*//*******************************************************************/
8#include "ContrastBase.h"
9#include "BasicUI.h"
10#include "Prefs.h"
11#include "Project.h"
12#include "ViewInfo.h"
14#include "WaveTrack.h"
15
16bool ContrastBase::GetDB(float& dB)
17{
18 float rms = float(0.0);
19
20 // For stereo tracks: sqrt((mean(L)+mean(R))/2)
21 double meanSq = 0.0;
22
23 auto& p = GetProject();
24 auto range = TrackList::Get(p).Selected<const WaveTrack>();
25 auto numberSelectedTracks = range.size();
26 using namespace BasicUI;
27 if (numberSelectedTracks > 1)
28 {
30 XO("You can only measure one track at a time."),
31 MessageBoxOptions {}.IconStyle(Icon::Error));
32 return false;
33 }
34 if (numberSelectedTracks == 0)
35 {
37 XO("Please select an audio track."),
38 MessageBoxOptions {}.IconStyle(Icon::Error));
39 return false;
40 }
41
42 const auto first = *range.begin();
43 const auto channels = first->Channels();
44 assert(mT0 <= mT1);
45 // Ignore whitespace beyond ends of track.
46 mT0 = std::max(mT0, first->GetStartTime());
47 mT1 = std::min(mT1, first->GetEndTime());
48 for (auto t : channels)
49 {
50
51 auto SelT0 = t->TimeToLongSamples(mT0);
52 auto SelT1 = t->TimeToLongSamples(mT1);
53
54 if (SelT0 > SelT1)
55 {
57 XO("Invalid audio selection.\nPlease ensure that audio is selected."),
58 MessageBoxOptions {}.IconStyle(Icon::Error));
59 return false;
60 }
61
62 if (SelT0 == SelT1)
63 {
65 XO("Nothing to measure.\nPlease select a section of a track."),
66 MessageBoxOptions {}.IconStyle(Icon::Error));
67 return false;
68 }
69
70 // Don't throw in this analysis dialog
71 rms = WaveChannelUtilities::GetRMS(*t, mT0, mT1, false);
72 meanSq += rms * rms;
73 }
74 // TODO: This works for stereo, provided the audio clips are in both
75 // channels. We should really count gaps between clips as silence.
76 rms = (meanSq > 0.0) ? sqrt(meanSq / static_cast<double>(channels.size())) :
77 0.0;
78
79 // Gives warning C4056, Overflow in floating-point constant arithmetic
80 // -INFINITY is intentional here.
81 // Looks like we are stuck with this warning, as
82 // #pragma warning( disable : 4056)
83 // even around the whole function does not disable it successfully.
84
85 dB = (rms == 0.0) ? -INFINITY : LINEAR_TO_DB(rms);
86 return true;
87}
88
90{
91 auto& p = GetProject();
92 auto& selectedRegion = ViewInfo::Get(p).selectedRegion;
93 mT0 = selectedRegion.t0();
94 mT1 = selectedRegion.t1();
95}
Toolkit-neutral facade for basic user interface services.
int min(int a, int b)
XO("Cut/Copy/Paste")
#define LINEAR_TO_DB(x)
Definition: MemoryX.h:339
size_t size() const
How many attachment pointers are in the Site.
Definition: ClientData.h:260
virtual AudacityProject & GetProject()=0
bool GetDB(float &dB)
void SetStartAndEndTime()
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
auto Selected() -> TrackIterRange< TrackType >
Definition: Track.h:967
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:216
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
#define INFINITY
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 float GetRMS(const WaveChannel &channel, double t0, double t1, bool mayThrow=true)
__finl float_x4 __vecc sqrt(const float_x4 &a)
MessageBoxOptions && IconStyle(Icon style) &&
Definition: BasicUI.h:104