Audacity  3.0.3
SpectrumVRulerControls.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 SpectrumVRulerControls.cpp
6 
7 Paul Licameli split from WaveTrackVRulerControls.cpp
8 
9 **********************************************************************/
10 
11 #include "SpectrumVRulerControls.h"
12 
13 #include "SpectrumVZoomHandle.h"
15 
16 #include "NumberScale.h"
17 #include "../../../../ProjectHistory.h"
18 #include "../../../../RefreshCode.h"
19 #include "../../../../TrackPanelMouseEvent.h"
20 #include "../../../../WaveTrack.h"
21 #include "../../../../prefs/SpectrogramSettings.h"
22 #include "../../../../widgets/Ruler.h"
23 
25 
26 std::vector<UIHandlePtr> SpectrumVRulerControls::HitTest(
27  const TrackPanelMouseState &st,
28  const AudacityProject *pProject)
29 {
30  std::vector<UIHandlePtr> results;
31 
32  if ( st.state.GetX() <= st.rect.GetRight() - kGuard ) {
33  auto pTrack = FindTrack()->SharedPointer<WaveTrack>( );
34  if (pTrack) {
35  auto result = std::make_shared<SpectrumVZoomHandle>(
36  pTrack, st.rect, st.state.m_y );
37  result = AssignUIHandlePtr(mVZoomHandle, result);
38  results.push_back(result);
39  }
40  }
41 
42  auto more = TrackVRulerControls::HitTest(st, pProject);
43  std::copy(more.begin(), more.end(), std::back_inserter(results));
44 
45  return results;
46 }
47 
49  const TrackPanelMouseEvent &evt, AudacityProject *pProject)
50 {
51  using namespace RefreshCode;
52  const auto pTrack = FindTrack();
53  if (!pTrack)
54  return RefreshNone;
55  const auto wt = static_cast<WaveTrack*>(pTrack.get());
56  return DoHandleWheelRotation( evt, pProject, wt );
57 }
58 
60  const TrackPanelMouseEvent &evt, AudacityProject *pProject, WaveTrack *wt)
61 {
62  using namespace RefreshCode;
63  const wxMouseEvent &event = evt.event;
64 
65  if (!(event.ShiftDown() || event.CmdDown()))
66  return RefreshNone;
67 
68  // Always stop propagation even if the ruler didn't change. The ruler
69  // is a narrow enough target.
70  evt.event.Skip(false);
71 
72  auto steps = evt.steps;
73 
74  using namespace WaveTrackViewConstants;
75  if (event.CmdDown() && !event.ShiftDown()) {
76  const int yy = event.m_y;
78  pProject, wt,
79  (steps < 0)
80  ? kZoomOut
81  : kZoomIn,
82  evt.rect, yy, yy, true);
83  }
84  else if (!event.CmdDown() && event.ShiftDown()) {
85  // Scroll some fixed number of pixels, independent of zoom level or track height:
86  static const float movement = 10.0f;
87  const int height = evt.rect.GetHeight();
88  {
89  const float delta = steps * movement / height;
91  const bool isLinear = settings.scaleType == SpectrogramSettings::stLinear;
92  float bottom, top;
93  wt->GetSpectrumBounds(&bottom, &top);
94  const double rate = wt->GetRate();
95  const float bound = rate / 2;
96  const NumberScale numberScale(settings.GetScale(bottom, top));
97  float newTop =
98  std::min(bound, numberScale.PositionToValue(1.0f + delta));
99  const float newBottom =
100  std::max((isLinear ? 0.0f : 1.0f),
101  numberScale.PositionToValue(numberScale.ValueToPosition(newTop) - 1.0f));
102  newTop =
103  std::min(bound,
104  numberScale.PositionToValue(numberScale.ValueToPosition(newBottom) + 1.0f));
105 
106  for (auto channel : TrackList::Channels(wt))
107  channel->SetSpectrumBounds(newBottom, newTop);
108  }
109  }
110  else
111  return RefreshNone;
112 
113  ProjectHistory::Get( *pProject ).ModifyState(true);
114 
115  return RefreshCell | UpdateVRuler;
116 }
117 
119  TrackPanelDrawingContext &context,
120  const wxRect &rect_, unsigned iPass )
121 {
122  TrackVRulerControls::Draw( context, rect_, iPass );
123  WaveTrackVRulerControls::DoDraw( *this, context, rect_, iPass );
124 }
125 
126 void SpectrumVRulerControls::UpdateRuler( const wxRect &rect )
127 {
128  const auto wt = std::static_pointer_cast< WaveTrack >( FindTrack() );
129  if (!wt)
130  return;
131  DoUpdateVRuler( rect, wt.get() );
132 }
133 
135  const wxRect &rect, const WaveTrack *wt )
136 {
137  auto vruler = &WaveTrackVRulerControls::ScratchRuler();
139  float minFreq, maxFreq;
140  wt->GetSpectrumBounds(&minFreq, &maxFreq);
141  vruler->SetDbMirrorValue( 0.0 );
142 
143  switch (settings.scaleType) {
144  default:
145  wxASSERT(false);
147  {
148  // Spectrum
149 
150  /*
151  draw the ruler
152  we will use Hz if maxFreq is < 2000, otherwise we represent kHz,
153  and append to the numbers a "k"
154  */
155  vruler->SetBounds(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height - 1);
156  vruler->SetOrientation(wxVERTICAL);
157  vruler->SetFormat(Ruler::RealFormat);
158  vruler->SetLabelEdges(true);
159  // use kHz in scale, if appropriate
160  if (maxFreq >= 2000) {
161  vruler->SetRange((maxFreq / 1000.), (minFreq / 1000.));
162  /* i18n-hint k abbreviating kilo meaning thousands */
163  vruler->SetUnits(XO("k"));
164  }
165  else {
166  // use Hz
167  vruler->SetRange((int)(maxFreq), (int)(minFreq));
168  vruler->SetUnits({});
169  }
170  vruler->SetLog(false);
171  }
172  break;
178  {
179  // SpectrumLog
180 
181  /*
182  draw the ruler
183  we will use Hz if maxFreq is < 2000, otherwise we represent kHz,
184  and append to the numbers a "k"
185  */
186  vruler->SetBounds(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height - 1);
187  vruler->SetOrientation(wxVERTICAL);
188  vruler->SetFormat(Ruler::IntFormat);
189  vruler->SetLabelEdges(true);
190  vruler->SetRange(maxFreq, minFreq);
191  vruler->SetUnits({});
192  vruler->SetLog(true);
193  NumberScale scale(
194  wt->GetSpectrogramSettings().GetScale( minFreq, maxFreq )
195  .Reversal() );
196  vruler->SetNumberScale(scale);
197  }
198  break;
199  }
200  vruler->GetMaxSize( &wt->vrulerSize.x, &wt->vrulerSize.y );
201 }
ProjectHistory::ModifyState
void ModifyState(bool bWantsAutoSave)
Definition: ProjectHistory.cpp:124
SpectrogramSettings
Spectrogram settings, either for one track or as defaults.
Definition: SpectrogramSettings.h:27
SpectrogramSettings::stErb
@ stErb
Definition: SpectrogramSettings.h:63
WaveTrack
A Track that contains audio waveform data.
Definition: WaveTrack.h:69
TrackPanelMouseEvent::rect
const wxRect & rect
Definition: TrackPanelMouseEvent.h:59
SpectrumVRulerControls::Draw
void Draw(TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass) override
Definition: SpectrumVRulerControls.cpp:118
NumberScale.h
RefreshCode::RefreshNone
@ RefreshNone
Definition: RefreshCode.h:21
SpectrumVRulerControls::HandleWheelRotation
unsigned HandleWheelRotation(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
Definition: SpectrumVRulerControls.cpp:48
CommonTrackPanelCell::FindTrack
std::shared_ptr< Track > FindTrack()
Definition: CommonTrackPanelCell.h:46
WaveTrackViewConstants
Definition: WaveTrackView.h:17
TrackPanelDrawingContext
Definition: TrackPanelDrawingContext.h:22
TrackList::Channels
static auto Channels(TrackType *pTrack) -> TrackIterRange< TrackType >
Definition: Track.h:1484
SpectrumVRulerControls.h
SpectrogramSettings::stMel
@ stMel
Definition: SpectrogramSettings.h:61
SpectrumVRulerControls::DoUpdateVRuler
static void DoUpdateVRuler(const wxRect &rect, const WaveTrack *wt)
Definition: SpectrumVRulerControls.cpp:134
SpectrumVRulerControls::DoHandleWheelRotation
static unsigned DoHandleWheelRotation(const TrackPanelMouseEvent &evt, AudacityProject *pProject, WaveTrack *wt)
Definition: SpectrumVRulerControls.cpp:59
XO
#define XO(s)
Definition: Internat.h:31
Ruler::RealFormat
@ RealFormat
Definition: Ruler.h:31
WaveTrack::GetSpectrogramSettings
const SpectrogramSettings & GetSpectrogramSettings() const
Definition: WaveTrack.cpp:763
TrackPanelMouseState::rect
const wxRect & rect
Definition: TrackPanelMouseEvent.h:39
SpectrogramSettings::stBark
@ stBark
Definition: SpectrogramSettings.h:62
SpectrogramSettings::GetScale
NumberScale GetScale(float minFreq, float maxFreq) const
Definition: SpectrogramSettings.cpp:622
WaveTrack::GetIndependentSpectrogramSettings
SpectrogramSettings & GetIndependentSpectrogramSettings()
Definition: WaveTrack.cpp:779
NumberScale::ValueToPosition
float ValueToPosition(float val) const
Definition: NumberScale.h:255
WaveTrackVRulerControls::DoDraw
AUDACITY_DLL_API void DoDraw(TrackVRulerControls &controls, TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass)
Definition: WaveTrackVRulerControls.cpp:32
WaveTrack::GetSpectrumBounds
void GetSpectrumBounds(float *min, float *max) const
Definition: WaveTrack.cpp:332
RefreshCode::UpdateVRuler
@ UpdateVRuler
Definition: RefreshCode.h:30
Ruler::IntFormat
@ IntFormat
Definition: Ruler.h:30
WaveTrackViewConstants::kZoomIn
@ kZoomIn
Definition: WaveTrackViewConstants.h:77
WaveTrackVRulerControls.h
RefreshCode::RefreshCell
@ RefreshCell
Definition: RefreshCode.h:24
kGuard
const int kGuard
Definition: TrackVRulerControls.h:20
NumberScale::Reversal
NumberScale Reversal() const
Definition: NumberScale.h:83
TrackVRulerControls::HitTest
std::vector< UIHandlePtr > HitTest(const TrackPanelMouseState &state, const AudacityProject *pProject) override
Definition: TrackVRulerControls.cpp:55
SpectrogramSettings::stLinear
@ stLinear
Definition: SpectrogramSettings.h:59
SpectrogramSettings::stLogarithmic
@ stLogarithmic
Definition: SpectrogramSettings.h:60
SpectrumVZoomHandle::DoZoom
static void DoZoom(AudacityProject *pProject, WaveTrack *pTrack, WaveTrackViewConstants::ZoomActions ZoomKind, const wxRect &rect, int zoomStart, int zoomEnd, bool fixedMousePoint)
Definition: SpectrumVZoomHandle.cpp:105
min
int min(int a, int b)
Definition: CompareAudioCommand.cpp:106
NumberScale::PositionToValue
float PositionToValue(float pp) const
Definition: NumberScale.h:154
WaveTrackVRulerControls::ScratchRuler
AUDACITY_DLL_API Ruler & ScratchRuler()
Definition: WaveTrackVRulerControls.cpp:26
TrackVRulerControls::Draw
void Draw(TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass) override
Definition: TrackVRulerControls.cpp:97
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:92
TrackPanelMouseEvent
Definition: TrackPanelMouseEvent.h:46
SpectrumVRulerControls::UpdateRuler
void UpdateRuler(const wxRect &rect) override
Definition: SpectrumVRulerControls.cpp:126
TrackPanelMouseState
Definition: TrackPanelMouseEvent.h:28
WaveTrackViewConstants::kZoomOut
@ kZoomOut
Definition: WaveTrackViewConstants.h:78
SpectrumVRulerControls::HitTest
std::vector< UIHandlePtr > HitTest(const TrackPanelMouseState &state, const AudacityProject *) override
Definition: SpectrumVRulerControls.cpp:26
NumberScale
Definition: NumberScale.h:31
SpectrumVZoomHandle.h
AssignUIHandlePtr
std::shared_ptr< Subclass > AssignUIHandlePtr(std::weak_ptr< Subclass > &holder, const std::shared_ptr< Subclass > &pNew)
Definition: UIHandle.h:151
SpectrumVRulerControls::~SpectrumVRulerControls
~SpectrumVRulerControls() override
SpectrumVRulerControls::mVZoomHandle
std::weak_ptr< SpectrumVZoomHandle > mVZoomHandle
Definition: SpectrumVRulerControls.h:52
settings
static Settings & settings()
Definition: TrackInfo.cpp:86
RefreshCode
Namespace containing an enum 'what to do on a refresh?'.
Definition: RefreshCode.h:16
SpectrogramSettings::stPeriod
@ stPeriod
Definition: SpectrogramSettings.h:64
TrackPanelMouseEvent::event
wxMouseEvent & event
Definition: TrackPanelMouseEvent.h:58
TrackPanelMouseState::state
wxMouseState & state
Definition: TrackPanelMouseEvent.h:38
TrackPanelMouseEvent::steps
double steps
Definition: TrackPanelMouseEvent.h:62
ProjectHistory::Get
static ProjectHistory & Get(AudacityProject &project)
Definition: ProjectHistory.cpp:26
WaveTrack::GetRate
double GetRate() const
Definition: WaveTrack.cpp:457
Track::vrulerSize
wxSize vrulerSize
Definition: Track.h:354