Audacity 3.2.0
PlotSpectrumBase.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 PlotSpectrumBase.cpp
6
7 Dominic Mazzoni
8 Matthieu Hodgkinson split from FreqWindow.cpp
9
10**********************************************************************/
11#include "PlotSpectrumBase.h"
12#include "BasicUI.h"
13#include "Prefs.h"
14#include "SampleFormat.h"
15#include "ViewInfo.h"
16#include "WaveTrack.h"
17
19 : mProject { &project }
20 , mAnalyst(std::make_unique<SpectrumAnalyst>())
21{
22 mRate = 0;
23 mDataLen = 0;
24
25 gPrefs->Read(wxT("/FrequencyPlotDialog/DrawGrid"), &mDrawGrid, true);
26 gPrefs->Read(wxT("/FrequencyPlotDialog/SizeChoice"), &mSize, 3);
27
28 int alg;
29 gPrefs->Read(wxT("/FrequencyPlotDialog/AlgChoice"), &alg, 0);
30 mAlg = static_cast<SpectrumAnalyst::Algorithm>(alg);
31
32 gPrefs->Read(wxT("/FrequencyPlotDialog/FuncChoice"), &mFunc, 3);
33 gPrefs->Read(wxT("/FrequencyPlotDialog/AxisChoice"), &mAxis, 1);
34}
35
37{
38 mData.reset();
39 mDataLen = 0;
40
41 int selcount = 0;
42 bool warning = false;
43 for (auto track : TrackList::Get(*mProject).Selected<const WaveTrack>())
44 {
45 auto& selectedRegion = ViewInfo::Get(*mProject).selectedRegion;
46 auto start = track->TimeToLongSamples(selectedRegion.t0());
47 if (selcount == 0)
48 {
49 mRate = track->GetRate();
50 auto end = track->TimeToLongSamples(selectedRegion.t1());
51 auto dataLen = end - start;
52 // Permit approximately 46.60 minutes of selected samples at
53 // a sampling frequency of 48 kHz (11.65 minutes at 192 kHz).
54 auto maxDataLen = size_t(2) << 26;
55 if (dataLen > maxDataLen)
56 {
57 warning = true;
58 mDataLen = maxDataLen;
59 }
60 else
61 mDataLen = dataLen.as_size_t();
62 mData = Floats { mDataLen };
63 }
64 const auto nChannels = track->NChannels();
65 if (track->GetRate() != mRate)
66 {
67 using namespace BasicUI;
69 XO("To plot the spectrum, all selected tracks must have the same sample rate."),
70 MessageBoxOptions {}.Caption(XO("Error")).IconStyle(Icon::Error));
71 mData.reset();
72 mDataLen = 0;
73 return false;
74 }
75 Floats buffer1 { mDataLen };
76 Floats buffer2 { mDataLen };
77 float* const buffers[] { buffer1.get(), buffer2.get() };
78 // Don't allow throw for bad reads
79 if (!track->GetFloats(
80 0, nChannels, buffers, start, mDataLen, false,
82 {
83 using namespace BasicUI;
85 XO("Audio could not be analyzed. This may be due to a stretched or pitch-shifted clip.\nTry resetting any stretched clips, or mixing and rendering the tracks before analyzing"),
86 MessageBoxOptions {}.Caption(XO("Error")).IconStyle(Icon::Error));
87 mData.reset();
88 mDataLen = 0;
89 return false;
90 }
91 size_t iChannel = 0;
92 if (selcount == 0)
93 {
94 // First channel -- assign into mData
95 for (size_t i = 0; i < mDataLen; i++)
96 mData[i] = buffers[0][i];
97 ++iChannel;
98 }
99 // Later channels -- accumulate
100 for (; iChannel < nChannels; ++iChannel)
101 {
102 const auto buffer = buffers[iChannel];
103 for (size_t i = 0; i < mDataLen; i++)
104 mData[i] += buffer[i];
105 }
106 ++selcount;
107 }
108
109 if (selcount == 0)
110 return false;
111
112 if (warning)
113 {
114 auto msg =
115 XO("Too much audio was selected. Only the first %.1f seconds of audio will be analyzed.")
116 .Format(mDataLen / mRate);
118 }
119 return true;
120}
wxT("CloseDown"))
Toolkit-neutral facade for basic user interface services.
XO("Cut/Copy/Paste")
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
const auto project
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
SpectrumAnalyst::Algorithm mAlg
ArrayOf< float > mData
PlotSpectrumBase(AudacityProject &project)
Used for finding the peaks, for snapping to peaks.
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
virtual bool Read(const wxString &key, bool *value) const =0
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
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
STL namespace.
MessageBoxOptions && Caption(TranslatableString caption_) &&
Definition: BasicUI.h:101