1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-02 17:09:26 +02:00

Simplify SelectHandle ctor; make frequency snapping functions static

This commit is contained in:
Paul Licameli 2017-06-28 15:16:20 -04:00
parent 2b146bf543
commit f3e31eca8b
3 changed files with 30 additions and 47 deletions

View File

@ -5896,7 +5896,8 @@ void AudacityProject::DoNextPeakFrequency(bool up)
} }
if (pTrack) { if (pTrack) {
SelectHandle::Instance().SnapCenterOnce(mViewInfo, pTrack, up); SpectrumAnalyst analyst;
SelectHandle::SnapCenterOnce(analyst, mViewInfo, pTrack, up);
mTrackPanel->Refresh(false); mTrackPanel->Refresh(false);
ModifyState(false); ModifyState(false);
} }

View File

@ -50,28 +50,6 @@ enum {
// #define SPECTRAL_EDITING_ESC_KEY // #define SPECTRAL_EDITING_ESC_KEY
SelectHandle::SelectHandle() SelectHandle::SelectHandle()
: mRect()
, mInitialSelection()
, mSnapManager()
, mSnapLeft(-1)
, mSnapRight(-1)
, mSelStartValid(false)
, mSelStart(0.0)
, mSelectionBoundary(0)
, mFreqSelMode(FREQ_SEL_INVALID)
, mFreqSelPin(-1.0)
, mFrequencySnapper(new SpectrumAnalyst)
, mMostRecentX(-1)
, mMostRecentY(-1)
, mAutoScrolling(false)
, mConnectedProject(NULL)
{ {
} }
@ -1230,13 +1208,15 @@ void SelectHandle::HandleCenterFrequencyClick
mFreqSelMode = FREQ_SEL_SNAPPING_CENTER; mFreqSelMode = FREQ_SEL_SNAPPING_CENTER;
// Disable time selection // Disable time selection
mSelStartValid = false; mSelStartValid = false;
StartSnappingFreqSelection(viewInfo, pTrack); mFrequencySnapper = std::make_unique<SpectrumAnalyst>();
StartSnappingFreqSelection(*mFrequencySnapper, viewInfo, pTrack);
#endif #endif
} }
} }
void SelectHandle::StartSnappingFreqSelection void SelectHandle::StartSnappingFreqSelection
(const ViewInfo &viewInfo, const WaveTrack *pTrack) (SpectrumAnalyst &analyst,
const ViewInfo &viewInfo, const WaveTrack *pTrack)
{ {
static const size_t minLength = 8; static const size_t minLength = 8;
@ -1270,7 +1250,7 @@ void SelectHandle::StartSnappingFreqSelection
windowSize >>= 1; windowSize >>= 1;
const int windowType = settings.windowType; const int windowType = settings.windowType;
mFrequencySnapper->Calculate( analyst.Calculate(
SpectrumAnalyst::Spectrum, windowType, windowSize, rate, SpectrumAnalyst::Spectrum, windowType, windowSize, rate,
&frequencySnappingData[0], length); &frequencySnappingData[0], length);
@ -1327,7 +1307,8 @@ void SelectHandle::MoveSnappingFreqSelection
} }
void SelectHandle::SnapCenterOnce void SelectHandle::SnapCenterOnce
(ViewInfo &viewInfo, const WaveTrack *pTrack, bool up) (SpectrumAnalyst &analyst,
ViewInfo &viewInfo, const WaveTrack *pTrack, bool up)
{ {
const SpectrogramSettings &settings = pTrack->GetSpectrogramSettings(); const SpectrogramSettings &settings = pTrack->GetSpectrogramSettings();
const auto windowSize = settings.GetFFTLength(); const auto windowSize = settings.GetFFTLength();
@ -1349,18 +1330,18 @@ void SelectHandle::SnapCenterOnce
// This is crude and wasteful, doing the FFT each time the command is called. // This is crude and wasteful, doing the FFT each time the command is called.
// It would be better to cache the data, but then invalidation of the cache would // It would be better to cache the data, but then invalidation of the cache would
// need doing in all places that change the time selection. // need doing in all places that change the time selection.
StartSnappingFreqSelection(viewInfo, pTrack); StartSnappingFreqSelection(analyst, viewInfo, pTrack);
double snappedFrequency = centerFrequency; double snappedFrequency = centerFrequency;
int bin = originalBin; int bin = originalBin;
if (up) { if (up) {
while (snappedFrequency <= centerFrequency && while (snappedFrequency <= centerFrequency &&
bin < limitingBin) bin < limitingBin)
snappedFrequency = mFrequencySnapper->FindPeak(++bin * binFrequency, NULL); snappedFrequency = analyst.FindPeak(++bin * binFrequency, NULL);
} }
else { else {
while (snappedFrequency >= centerFrequency && while (snappedFrequency >= centerFrequency &&
bin > limitingBin) bin > limitingBin)
snappedFrequency = mFrequencySnapper->FindPeak(--bin * binFrequency, NULL); snappedFrequency = analyst.FindPeak(--bin * binFrequency, NULL);
} }
// PRL: added these two lines with the big TrackPanel refactor // PRL: added these two lines with the big TrackPanel refactor

View File

@ -89,37 +89,39 @@ private:
void HandleCenterFrequencyClick void HandleCenterFrequencyClick
(const ViewInfo &viewInfo, bool shiftDown, (const ViewInfo &viewInfo, bool shiftDown,
const WaveTrack *pTrack, double value); const WaveTrack *pTrack, double value);
void StartSnappingFreqSelection static void StartSnappingFreqSelection
(const ViewInfo &viewInfo, const WaveTrack *pTrack); (SpectrumAnalyst &analyst,
const ViewInfo &viewInfo, const WaveTrack *pTrack);
void MoveSnappingFreqSelection void MoveSnappingFreqSelection
(AudacityProject *pProject, ViewInfo &viewInfo, int mouseYCoordinate, (AudacityProject *pProject, ViewInfo &viewInfo, int mouseYCoordinate,
int trackTopEdge, int trackTopEdge,
int trackHeight, Track *pTrack); int trackHeight, Track *pTrack);
public: public:
// This is needed to implement a command assignable to keystrokes // This is needed to implement a command assignable to keystrokes
void SnapCenterOnce static void SnapCenterOnce
(ViewInfo &viewInfo, const WaveTrack *pTrack, bool up); (SpectrumAnalyst &analyst,
ViewInfo &viewInfo, const WaveTrack *pTrack, bool up);
private: private:
//void ResetFreqSelectionPin //void ResetFreqSelectionPin
// (const ViewInfo &viewInfo, double hintFrequency, bool logF); // (const ViewInfo &viewInfo, double hintFrequency, bool logF);
std::weak_ptr<Track> mpTrack; std::weak_ptr<Track> mpTrack;
wxRect mRect; wxRect mRect{};
SelectedRegion mInitialSelection; SelectedRegion mInitialSelection{};
// Handles snapping the selection boundaries or track boundaries to // Handles snapping the selection boundaries or track boundaries to
// line up with existing tracks or labels. mSnapLeft and mSnapRight // line up with existing tracks or labels. mSnapLeft and mSnapRight
// are the horizontal index of pixels to display user feedback // are the horizontal index of pixels to display user feedback
// guidelines so the user knows when such snapping is taking place. // guidelines so the user knows when such snapping is taking place.
std::unique_ptr<SnapManager> mSnapManager; std::unique_ptr<SnapManager> mSnapManager;
wxInt64 mSnapLeft; wxInt64 mSnapLeft{ -1 };
wxInt64 mSnapRight; wxInt64 mSnapRight{ -1 };
bool mSelStartValid; bool mSelStartValid{};
double mSelStart; double mSelStart{ 0.0 };
int mSelectionBoundary; int mSelectionBoundary{ 0 };
enum eFreqSelMode { enum eFreqSelMode {
FREQ_SEL_INVALID, FREQ_SEL_INVALID,
@ -131,7 +133,7 @@ private:
FREQ_SEL_FREE, FREQ_SEL_FREE,
FREQ_SEL_TOP_FREE, FREQ_SEL_TOP_FREE,
FREQ_SEL_BOTTOM_FREE, FREQ_SEL_BOTTOM_FREE,
} mFreqSelMode; } mFreqSelMode{ FREQ_SEL_INVALID };
std::weak_ptr<const WaveTrack> mFreqSelTrack; std::weak_ptr<const WaveTrack> mFreqSelTrack;
// Following holds: // Following holds:
// the center for FREQ_SEL_PINNED_CENTER, // the center for FREQ_SEL_PINNED_CENTER,
@ -139,16 +141,15 @@ private:
// a frequency boundary for FREQ_SEL_FREE, FREQ_SEL_TOP_FREE, or // a frequency boundary for FREQ_SEL_FREE, FREQ_SEL_TOP_FREE, or
// FREQ_SEL_BOTTOM_FREE, // FREQ_SEL_BOTTOM_FREE,
// and is ignored otherwise. // and is ignored otherwise.
double mFreqSelPin; double mFreqSelPin{ -1.0 };
std::unique_ptr<SpectrumAnalyst> mFrequencySnapper; std::unique_ptr<SpectrumAnalyst> mFrequencySnapper;
int mMostRecentX, mMostRecentY; int mMostRecentX{ -1 }, mMostRecentY{ -1 };
bool mAutoScrolling; bool mAutoScrolling{};
AudacityProject *mConnectedProject; AudacityProject *mConnectedProject{};
std::unique_ptr<SelectionStateChanger> mSelectionStateChanger; std::unique_ptr<SelectionStateChanger> mSelectionStateChanger;
}; };
#endif #endif