| 1 |
/********************************************************************** |
| 2 |
|
| 3 |
Audacity: A Digital Audio Editor |
| 4 |
|
| 5 |
SpectrumView.h |
| 6 |
|
| 7 |
Paul 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 |
|
| 20 |
class WaveTrack; |
| 21 |
class BrushHandle; |
| 22 |
using HopsAndBinsMap = std::map<long long, std::set<int>>; |
| 23 |
|
| 24 |
class SpectralData{ |
| 25 |
private: |
| 26 |
double mSampleRate; |
| 27 |
int mWindowSize; |
| 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 |
|
| 43 |
HopsAndBinsMap dataBuffer; |
| 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){ |
| 50 |
mStartSample = src.GetStartSample(); |
| 51 |
mEndSample = src.GetEndSample(); |
| 52 |
|
| 53 |
// std containers will perform deepcopy automatically |
| 54 |
dataHistory = src.dataHistory; |
| 55 |
dataBuffer = src.dataBuffer; |
| 56 |
coordHistory = src.coordHistory; |
| 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 { |
| 86 |
return GetEndSample() - GetCorrectedStartSample(); |
| 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 |
|
| 109 |
void clearAllData(){ |
| 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 |
|
| 116 |
void saveAndClearBuffer(){ |
| 117 |
dataHistory.emplace_back(dataBuffer); |
| 118 |
dataBuffer.clear(); |
| 119 |
coordHistory.clear(); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
class SpectrumView final : public WaveChannelSubView |
| 124 |
{ |
| 125 |
SpectrumView &operator=( const SpectrumView& ) = delete; |
| 126 |
public: |
| 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 |
|
| 146 |
private: |
| 147 |
std::weak_ptr<BrushHandle> mBrushHandle; |
| 148 |
std::shared_ptr<SpectralData> mpSpectralData, mpBackupSpectralData; |
| 149 |
bool mOnBrushTool; |
| 150 |
|
| 151 |
// TrackPanelDrawable implementation |
| 152 |
void Draw( |
| 153 |
TrackPanelDrawingContext &context, |
| 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 |