Audacity 3.2.0
SpectrumView.h
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5SpectrumView.h
6
7Paul Licameli split from WaveChannelView.h
8
9**********************************************************************/
10
11#ifndef __AUDACITY_SPECTRUM_VIEW__
12#define __AUDACITY_SPECTRUM_VIEW__
13
14#include <functional>
15#include <map>
16#include <set>
17#include "WaveChannelView.h" // to inherit
18
19
20class WaveTrack;
21class BrushHandle;
22using HopsAndBinsMap = std::map<long long, std::set<int>>;
23
25private:
29 long long mStartSample;
30 long long mEndSample;
31
32public:
33 SpectralData(double sr)
34 :mSampleRate(sr)
35 ,mWindowSize( 2048 )
36 ,mHopSize ( mWindowSize / 4 )
37 // Set start and end in reverse for comparison during data addition
38 ,mStartSample(std::numeric_limits<long long>::max())
39 ,mEndSample( 0 )
40 {}
41 SpectralData(const SpectralData& src) = delete;
42
44 std::vector<HopsAndBinsMap> dataHistory;
45 // TODO: replace with two pairs to save space
46 std::vector<std::pair<int, int>> coordHistory;
47
48 // Abstracted the copy method for future extension
49 void CopyFrom(const SpectralData &src){
52
53 // std containers will perform deepcopy automatically
57 }
58
59 int GetHopSize() const {
60 return mHopSize;
61 }
62
63 int GetWindowSize() const{
64 return mWindowSize;
65 };
66
67 double GetSR() const{
68 return mSampleRate;
69 }
70
71 long long GetStartSample() const{
72 return mStartSample;
73 }
74
75 long long GetEndSample() const{
76 return mEndSample;
77 }
78
79 long long GetCorrectedStartSample() const {
80 // Correct the start of range so that the first full window is
81 // centered at that position
82 return std::max<long long>(0, GetStartSample() - 2 * GetHopSize());
83 }
84
85 long long GetLength() const {
87 }
88
89 // The double time points is quantized into long long
90 void addHopBinData(int hopNum, int freqBin){
91 // Update the start and end sampleCount of current selection
92 if(hopNum * mHopSize > mEndSample)
93 mEndSample = hopNum * mHopSize;
94 if(hopNum * mHopSize < mStartSample)
95 mStartSample = hopNum * mHopSize;
96
97 dataBuffer[hopNum].insert(freqBin);
98 }
99
100 void removeHopBinData(int hopNum, int freqBin){
101 // TODO: Recalculate the start and end in case hop falls in 0 || end
102 for(auto &dataBuf: dataHistory){
103 if(dataBuf.find(hopNum) != dataBuf.end()){
104 dataBuf[hopNum].erase(freqBin);
105 }
106 }
107 }
108
110 // DataBuffer should be clear when the user release cursor
111 dataHistory.clear();
112 mStartSample = std::numeric_limits<long long>::max();
113 mEndSample = 0;
114 }
115
117 dataHistory.emplace_back(dataBuffer);
118 dataBuffer.clear();
119 coordHistory.clear();
120 }
121};
122
124{
126public:
127 SpectrumView(WaveChannelView &waveChannelView, const SpectrumView &src)
128 = delete;
129 explicit SpectrumView(WaveChannelView &waveChannelView);
130 ~SpectrumView() override;
131
132 const Type &SubViewType() const override;
133
134 std::shared_ptr<ChannelVRulerControls> DoGetVRulerControls() override;
135
136 std::shared_ptr<SpectralData> GetSpectralData();
137
138 bool IsSpectral() const override;
139
140 static int mBrushRadius;
141
142 void CopyToSubView(WaveChannelSubView *destSubView) const override;
143
144 class SpectralDataSaver;
145
146private:
147 std::weak_ptr<BrushHandle> mBrushHandle;
148 std::shared_ptr<SpectralData> mpSpectralData, mpBackupSpectralData;
150
151 // TrackPanelDrawable implementation
152 void Draw(
154 const wxRect &rect, unsigned iPass ) override;
155
156 void DoDraw(TrackPanelDrawingContext &context,
157 const WaveChannel &channel, const WaveTrack::Interval* selectedClip,
158 const wxRect & rect);
159
160 std::vector<UIHandlePtr> DetailedHitTest(
161 const TrackPanelMouseState &state,
162 const AudacityProject *pProject, int currentTool, bool bMultiTool )
163 override;
164
165 unsigned CaptureKey
166 (wxKeyEvent& event, ViewInfo& viewInfo, wxWindow* pParent,
167 AudacityProject* project) override;
168
169 unsigned KeyDown(wxKeyEvent& event, ViewInfo& viewInfo, wxWindow* pParent,
170 AudacityProject* project) override;
171
172 unsigned Char
173 (wxKeyEvent& event, ViewInfo& viewInfo, wxWindow* pParent,
174 AudacityProject* project) override;
175
176 static void ForAll( AudacityProject &project,
177 std::function<void(SpectrumView &view)> fn );
178
179 void DoSetMinimized( bool minimized ) override;
180};
181
182#endif
std::map< long long, std::set< int > > HopsAndBinsMap
Definition: SpectrumView.h:22
const auto project
static const auto fn
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
SpectralData(const SpectralData &src)=delete
long long GetStartSample() const
Definition: SpectrumView.h:71
int GetWindowSize() const
Definition: SpectrumView.h:63
long long GetEndSample() const
Definition: SpectrumView.h:75
double GetSR() const
Definition: SpectrumView.h:67
void addHopBinData(int hopNum, int freqBin)
Definition: SpectrumView.h:90
void clearAllData()
Definition: SpectrumView.h:109
void removeHopBinData(int hopNum, int freqBin)
Definition: SpectrumView.h:100
long long GetLength() const
Definition: SpectrumView.h:85
std::vector< HopsAndBinsMap > dataHistory
Definition: SpectrumView.h:44
double mSampleRate
Definition: SpectrumView.h:26
long long mEndSample
Definition: SpectrumView.h:30
int GetHopSize() const
Definition: SpectrumView.h:59
long long mStartSample
Definition: SpectrumView.h:29
void CopyFrom(const SpectralData &src)
Definition: SpectrumView.h:49
long long GetCorrectedStartSample() const
Definition: SpectrumView.h:79
SpectralData(double sr)
Definition: SpectrumView.h:33
void saveAndClearBuffer()
Definition: SpectrumView.h:116
std::vector< std::pair< int, int > > coordHistory
Definition: SpectrumView.h:46
HopsAndBinsMap dataBuffer
Definition: SpectrumView.h:43
~SpectrumView() override
static void ForAll(AudacityProject &project, std::function< void(SpectrumView &view)> fn)
void DoSetMinimized(bool minimized) override
unsigned CaptureKey(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent, AudacityProject *project) override
bool IsSpectral() const override
void DoDraw(TrackPanelDrawingContext &context, const WaveChannel &channel, const WaveTrack::Interval *selectedClip, const wxRect &rect)
static int mBrushRadius
Definition: SpectrumView.h:140
std::shared_ptr< SpectralData > mpSpectralData
Definition: SpectrumView.h:148
std::weak_ptr< BrushHandle > mBrushHandle
Definition: SpectrumView.h:144
unsigned KeyDown(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent, AudacityProject *project) override
SpectrumView & operator=(const SpectrumView &)=delete
std::shared_ptr< SpectralData > mpBackupSpectralData
Definition: SpectrumView.h:148
const Type & SubViewType() const override
unsigned Char(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent, AudacityProject *project) override
std::shared_ptr< SpectralData > GetSpectralData()
SpectrumView(WaveChannelView &waveChannelView, const SpectrumView &src)=delete
std::shared_ptr< ChannelVRulerControls > DoGetVRulerControls() override
std::vector< UIHandlePtr > DetailedHitTest(const TrackPanelMouseState &state, const AudacityProject *pProject, int currentTool, bool bMultiTool) override
void CopyToSubView(WaveChannelSubView *destSubView) const override
void Draw(TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass) override
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:238
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
STL namespace.