Audacity  3.2.0
SpectrumView.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 SpectrumView.h
6 
7 Paul Licameli split from WaveTrackView.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 "WaveTrackView.h" // to inherit
18 
19 
20 class WaveTrack;
21 class BrushHandle;
22 using HopsAndBinsMap = std::map<long long, std::set<int>>;
23 
25 private:
26  double mSampleRate;
28  int mHopSize;
29  long long mStartSample;
30  long long mEndSample;
31 
32 public:
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){
51  mEndSample = src.GetEndSample();
52 
53  // std containers will perform deepcopy automatically
55  dataBuffer = src.dataBuffer;
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  // The double time points is quantized into long long
80  void addHopBinData(int hopNum, int freqBin){
81  // Update the start and end sampleCount of current selection
82  if(hopNum * mHopSize > mEndSample)
83  mEndSample = hopNum * mHopSize;
84  if(hopNum * mHopSize < mStartSample)
85  mStartSample = hopNum * mHopSize;
86 
87  dataBuffer[hopNum].insert(freqBin);
88  }
89 
90  void removeHopBinData(int hopNum, int freqBin){
91  // TODO: Recalculate the start and end in case hop falls in 0 || end
92  for(auto &dataBuf: dataHistory){
93  if(dataBuf.find(hopNum) != dataBuf.end()){
94  dataBuf[hopNum].erase(freqBin);
95  }
96  }
97  }
98 
99  void clearAllData(){
100  // DataBuffer should be clear when the user release cursor
101  dataHistory.clear();
102  mStartSample = std::numeric_limits<long long>::max();
103  mEndSample = 0;
104  }
105 
107  dataHistory.emplace_back(dataBuffer);
108  dataBuffer.clear();
109  coordHistory.clear();
110  }
111 };
112 
113 class SpectrumView final : public WaveTrackSubView
114 {
115  SpectrumView &operator=( const SpectrumView& ) = delete;
116 public:
117  SpectrumView(WaveTrackView &waveTrackView, const SpectrumView &src) = delete;
118  explicit SpectrumView(WaveTrackView &waveTrackView);
119  ~SpectrumView() override;
120 
121  const Type &SubViewType() const override;
122 
123  std::shared_ptr<TrackVRulerControls> DoGetVRulerControls() override;
124 
125  std::shared_ptr<SpectralData> GetSpectralData();
126 
127  bool IsSpectral() const override;
128 
129  static int mBrushRadius;
130 
131  void CopyToSubView( WaveTrackSubView *destSubView ) const override;
132 
133  class SpectralDataSaver;
134 
135 private:
136  std::weak_ptr<BrushHandle> mBrushHandle;
137  std::shared_ptr<SpectralData> mpSpectralData, mpBackupSpectralData;
139 
140  // TrackPanelDrawable implementation
141  void Draw(
142  TrackPanelDrawingContext &context,
143  const wxRect &rect, unsigned iPass ) override;
144 
145  void DoDraw( TrackPanelDrawingContext &context,
146  const WaveTrack *track,
147  const WaveClip* selectedClip,
148  const wxRect & rect );
149 
150  std::vector<UIHandlePtr> DetailedHitTest(
151  const TrackPanelMouseState &state,
152  const AudacityProject *pProject, int currentTool, bool bMultiTool )
153  override;
154 
155  unsigned CaptureKey
156  (wxKeyEvent& event, ViewInfo& viewInfo, wxWindow* pParent,
157  AudacityProject* project) override;
158 
159  unsigned KeyDown(wxKeyEvent& event, ViewInfo& viewInfo, wxWindow* pParent,
160  AudacityProject* project) override;
161 
162  unsigned Char
163  (wxKeyEvent& event, ViewInfo& viewInfo, wxWindow* pParent,
164  AudacityProject* project) override;
165 
166  static void ForAll( AudacityProject &project,
167  std::function<void(SpectrumView &view)> fn );
168 
169  void DoSetMinimized( bool minimized ) override;
170 };
171 
172 #endif
SpectralData::removeHopBinData
void removeHopBinData(int hopNum, int freqBin)
Definition: SpectrumView.h:90
BrushHandle
Definition: BrushHandle.h:31
SpectrumView::CaptureKey
unsigned CaptureKey(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent, AudacityProject *project) override
Definition: SpectrumView.cpp:1059
WaveTrack
A Track that contains audio waveform data.
Definition: WaveTrack.h:75
SpectrumView::~SpectrumView
~SpectrumView() override
WaveTrackView
Definition: WaveTrackView.h:98
SpectrumView::mpBackupSpectralData
std::shared_ptr< SpectralData > mpBackupSpectralData
Definition: SpectrumView.h:137
fn
static const auto fn
Definition: WaveformView.cpp:1122
SpectrumView::mBrushRadius
static int mBrushRadius
Definition: SpectrumView.h:129
SpectrumView::Char
unsigned Char(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent, AudacityProject *project) override
Definition: SpectrumView.cpp:1077
SpectralData::dataBuffer
HopsAndBinsMap dataBuffer
Definition: SpectrumView.h:43
SpectralData::mEndSample
long long mEndSample
Definition: SpectrumView.h:30
SpectralData::mHopSize
int mHopSize
Definition: SpectrumView.h:28
TrackPanelDrawingContext
Definition: TrackPanelDrawingContext.h:22
ViewInfo
Definition: ViewInfo.h:188
SpectrumView::Draw
void Draw(TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass) override
Definition: SpectrumView.cpp:872
SpectralData::GetHopSize
int GetHopSize() const
Definition: SpectrumView.h:59
SpectralData::mSampleRate
double mSampleRate
Definition: SpectrumView.h:26
SpectrumView::SpectralDataSaver
Definition: SpectrumView.cpp:67
SpectralData::mWindowSize
int mWindowSize
Definition: SpectrumView.h:27
SpectralData::clearAllData
void clearAllData()
Definition: SpectrumView.h:99
SpectralData::mStartSample
long long mStartSample
Definition: SpectrumView.h:29
WaveTrackSubView
Definition: WaveTrackView.h:40
SpectrumView::mBrushHandle
std::weak_ptr< BrushHandle > mBrushHandle
Definition: SpectrumView.h:133
SpectralData::GetEndSample
long long GetEndSample() const
Definition: SpectrumView.h:75
SpectrumView::DoGetVRulerControls
std::shared_ptr< TrackVRulerControls > DoGetVRulerControls() override
Definition: SpectrumView.cpp:219
WaveTrackView.h
SpectrumView
Definition: SpectrumView.h:114
SpectrumView::mpSpectralData
std::shared_ptr< SpectralData > mpSpectralData
Definition: SpectrumView.h:137
WaveClip
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:101
SpectralData::saveAndClearBuffer
void saveAndClearBuffer()
Definition: SpectrumView.h:106
WaveTrackSubViewType
Definition: WaveTrackViewConstants.h:89
SpectrumView::SubViewType
const Type & SubViewType() const override
Definition: SpectrumView.cpp:214
HopsAndBinsMap
std::map< long long, std::set< int > > HopsAndBinsMap
Definition: SpectrumView.h:22
SpectrumView::DoDraw
void DoDraw(TrackPanelDrawingContext &context, const WaveTrack *track, const WaveClip *selectedClip, const wxRect &rect)
Definition: SpectrumView.cpp:852
SpectrumView::CopyToSubView
void CopyToSubView(WaveTrackSubView *destSubView) const override
Definition: SpectrumView.cpp:228
SpectrumView::ForAll
static void ForAll(AudacityProject &project, std::function< void(SpectrumView &view)> fn)
Definition: SpectrumView.cpp:149
SpectralData::GetSR
double GetSR() const
Definition: SpectrumView.h:67
SpectrumView::SpectrumView
SpectrumView(WaveTrackView &waveTrackView, const SpectrumView &src)=delete
SpectralData::GetWindowSize
int GetWindowSize() const
Definition: SpectrumView.h:63
SpectralData::coordHistory
std::vector< std::pair< int, int > > coordHistory
Definition: SpectrumView.h:46
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
SpectralData::GetStartSample
long long GetStartSample() const
Definition: SpectrumView.h:71
TrackPanelMouseState
Definition: TrackPanelMouseEvent.h:28
SpectrumView::GetSpectralData
std::shared_ptr< SpectralData > GetSpectralData()
Definition: SpectrumView.cpp:224
SpectralData
Definition: SpectrumView.h:24
SpectrumView::KeyDown
unsigned KeyDown(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent, AudacityProject *project) override
Definition: SpectrumView.cpp:1067
SpectrumView::DetailedHitTest
std::vector< UIHandlePtr > DetailedHitTest(const TrackPanelMouseState &state, const AudacityProject *pProject, int currentTool, bool bMultiTool) override
Definition: SpectrumView.cpp:165
SpectralData::dataHistory
std::vector< HopsAndBinsMap > dataHistory
Definition: SpectrumView.h:44
SpectrumView::mOnBrushTool
bool mOnBrushTool
Definition: SpectrumView.h:138
SpectrumView::DoSetMinimized
void DoSetMinimized(bool minimized) override
Definition: SpectrumView.cpp:189
SpectralData::CopyFrom
void CopyFrom(const SpectralData &src)
Definition: SpectrumView.h:49
SpectrumView::IsSpectral
bool IsSpectral() const override
Definition: SpectrumView.cpp:62
SpectrumView::operator=
SpectrumView & operator=(const SpectrumView &)=delete
SpectralData::SpectralData
SpectralData(const SpectralData &src)=delete
SpectralData::addHopBinData
void addHopBinData(int hopNum, int freqBin)
Definition: SpectrumView.h:80
SpectralData::SpectralData
SpectralData(double sr)
Definition: SpectrumView.h:33