Audacity 3.2.0
WaveformVZoomHandle.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5WaveformVZoomHandle.cpp
6
7Paul Licameli split from WaveChannelVZoomHandle.cpp
8
9**********************************************************************/
10
11
12#include "WaveformVZoomHandle.h"
13
15
16#include "../../../../HitTestResult.h"
17#include "NumberScale.h"
18#include "Prefs.h"
19#include "ProjectHistory.h"
20#include "../../../../RefreshCode.h"
21#include "../../../../TrackPanelMouseEvent.h"
22#include "WaveTrack.h"
23#include "../../../../prefs/WaveformSettings.h"
24
26 const std::shared_ptr<WaveTrack> &pTrack, const wxRect &rect, int y)
27 : mpTrack{ pTrack } , mZoomStart(y), mZoomEnd(y), mRect(rect)
28{
29}
30
32
33std::shared_ptr<const Channel> WaveformVZoomHandle::FindChannel() const
34{
35 return std::dynamic_pointer_cast<const Channel>(mpTrack.lock());
36}
37
39{
40#ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
42#endif
43}
44
46{
47 return true;
48}
49
52{
54}
55
57(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
58{
59 using namespace RefreshCode;
60 auto pTrack = TrackList::Get( *pProject ).Lock(mpTrack);
61 if (!pTrack)
62 return Cancelled;
64}
65
68{
70}
71
73(const TrackPanelMouseEvent &evt, AudacityProject *pProject,
74 wxWindow *pParent)
75{
76 auto pTrack = TrackList::Get( *pProject ).Lock(mpTrack);
78 evt, pProject, pParent, pTrack.get(), mRect,
81}
82
84{
85 // Cancel is implemented! And there is no initial state to restore,
86 // so just return a code.
88}
89
92 const wxRect &rect, unsigned iPass )
93{
94 if (!mpTrack.lock()) //? TrackList::Lock()
95 return;
97 context, rect, iPass, mZoomStart, mZoomEnd);
98}
99
102 const wxRect &rect, const wxRect &panelRect, unsigned iPass )
103{
104 return WaveChannelVZoomHandle::DoDrawingArea(rect, panelRect, iPass);
105}
106
107// ZoomKind says how to zoom.
108// If ZoomStart and ZoomEnd are not equal, this may override
109// the zoomKind and cause a drag-zoom-in.
111 AudacityProject *pProject,
112 WaveTrack *pTrack,
114 const wxRect &rect, int zoomStart, int zoomEnd,
115 bool fixedMousePoint)
116{
117 using namespace WaveChannelViewConstants;
118 static const float ZOOMLIMIT = 0.001f;
119
120 int height = rect.height;
121 int ypos = rect.y;
122
123 // Ensure start and end are in order (swap if not).
124 if (zoomEnd < zoomStart)
125 std::swap( zoomStart, zoomEnd );
126
127 float min, max, minBand = 0;
128 const double rate = pTrack->GetRate();
129 const float halfrate = rate / 2;
130 float maxFreq = 8000.0;
131
132 bool bDragZoom = WaveChannelVZoomHandle::IsDragZooming(zoomStart, zoomEnd);
133
134 // Possibly override the zoom kind.
135 if( bDragZoom )
136 ZoomKind = kZoomInByDrag;
137
138 float top=2.0;
139 float half=0.5;
140
141 {
142 auto &cache = WaveformScale::Get(*pTrack);
143 cache.GetDisplayBounds(min, max);
144 auto &waveSettings = WaveformSettings::Get(*pTrack);
145 const bool linear = waveSettings.isLinear();
146 if( !linear ){
147 top = (LINEAR_TO_DB(2.0) + waveSettings.dBRange) / waveSettings.dBRange;
148 half = (LINEAR_TO_DB(0.5) + waveSettings.dBRange) / waveSettings.dBRange;
149 }
150 }
151
152
153 // Compute min and max.
154 switch(ZoomKind)
155 {
156 default:
157 // If we have covered all the cases, this won't happen.
158 // In release builds Audacity will ignore the zoom.
159 wxFAIL_MSG("Zooming Case not implemented by Audacity");
160 break;
161 case kZoomReset:
162 case kZoom1to1:
163 {
164 // Zoom out full
165 min = -1.0;
166 max = 1.0;
167 }
168 break;
169 case kZoomDiv2:
170 {
171 // Zoom out even more than full :-)
172 // -2.0..+2.0 (or logarithmic equivalent)
173 min = -top;
174 max = top;
175 }
176 break;
177 case kZoomTimes2:
178 {
179 // Zoom in to -0.5..+0.5
180 min = -half;
181 max = half;
182 }
183 break;
184 case kZoomHalfWave:
185 {
186 // Zoom to show fractionally more than the top half of the wave.
187 min = -0.01f;
188 max = 1.0;
189 }
190 break;
191 case kZoomInByDrag:
192 {
193 const float tmin = min, tmax = max;
194 const float p1 = (zoomStart - ypos) / (float)height;
195 const float p2 = (zoomEnd - ypos) / (float)height;
196 max = (tmax * (1.0 - p1) + tmin * p1);
197 min = (tmax * (1.0 - p2) + tmin * p2);
198
199 // Waveform view - allow zooming down to a range of ZOOMLIMIT
200 if (max - min < ZOOMLIMIT) { // if user attempts to go smaller...
201 float c = (min + max) / 2; // ...set centre of view to centre of dragged area and top/bottom to ZOOMLIMIT/2 above/below
202 min = c - ZOOMLIMIT / 2.0;
203 max = c + ZOOMLIMIT / 2.0;
204 }
205 }
206 break;
207 case kZoomIn:
208 {
209 // Enforce maximum vertical zoom
210 const float oldRange = max - min;
211 const float l = std::max(ZOOMLIMIT, 0.5f * oldRange);
212 const float ratio = l / (max - min);
213
214 const float p1 = (zoomStart - ypos) / (float)height;
215 float c = (max * (1.0 - p1) + min * p1);
216 if (fixedMousePoint)
217 min = c - ratio * (1.0f - p1) * oldRange,
218 max = c + ratio * p1 * oldRange;
219 else
220 min = c - 0.5 * l,
221 max = c + 0.5 * l;
222 }
223 break;
224 case kZoomOut:
225 {
226 // Zoom out
227 if (min <= -1.0 && max >= 1.0) {
228 min = -top;
229 max = top;
230 }
231 else {
232 // limit to +/- 1 range unless already outside that range...
233 float minRange = (min < -1) ? -top : -1.0;
234 float maxRange = (max > 1) ? top : 1.0;
235 // and enforce vertical zoom limits.
236 const float p1 = (zoomStart - ypos) / (float)height;
237 if (fixedMousePoint) {
238 const float oldRange = max - min;
239 const float c = (max * (1.0 - p1) + min * p1);
240 min = std::min(maxRange - ZOOMLIMIT,
241 std::max(minRange, c - 2 * (1.0f - p1) * oldRange));
242 max = std::max(minRange + ZOOMLIMIT,
243 std::min(maxRange, c + 2 * p1 * oldRange));
244 }
245 else {
246 const float c = p1 * min + (1 - p1) * max;
247 const float l = (max - min);
248 min = std::min(maxRange - ZOOMLIMIT,
249 std::max(minRange, c - l));
250 max = std::max(minRange + ZOOMLIMIT,
251 std::min(maxRange, c + l));
252 }
253 }
254 }
255 break;
256 }
257
258 // Now actually apply the zoom.
260
261 zoomEnd = zoomStart = 0;
262 if( pProject )
263 ProjectHistory::Get( *pProject ).ModifyState(true);
264}
265
267// Table class
268
270{
271 static WaveformVRulerMenuTable instance;
272 return instance;
273}
274
276 // Accelerators only if zooming enabled.
277 bool bVZoom;
278 gPrefs->Read(wxT("/GUI/VerticalZooming"), &bVZoom, false);
279
280 BeginSection( "Scales" );
281 {
282 const auto & names = WaveformSettings::GetScaleNames();
283 for (int ii = 0, nn = names.size(); ii < nn; ++ii) {
285 OnFirstWaveformScaleID + ii, names[ii].Msgid(),
286 POPUP_MENU_FN( OnWaveformScaleType ),
287 []( PopupMenuHandler &handler, wxMenu &menu, int id ){
288 const auto pData =
289 static_cast< WaveformVRulerMenuTable& >( handler ).mpData;
290 WaveTrack *const wt = pData->pTrack;
291 if ( id ==
293 static_cast<int>(WaveformSettings::Get(*wt).scaleType) )
294 menu.Check(id, true);
295 }
296 );
297 }
298 }
300
301 BeginSection( "Zoom" );
302 BeginSection( "Basic" );
304 MakeLabel( XXO("Zoom Reset"), bVZoom, XXO("Shift-Right-Click") ),
305 POPUP_MENU_FN( OnZoomReset ) );
306 AppendItem( "TimesHalf", OnZoomDiv2ID, XXO("Zoom x1/2"),
307 POPUP_MENU_FN( OnZoomDiv2Vertical ) );
308 AppendItem( "TimesTwo", OnZoomTimes2ID, XXO("Zoom x2"), POPUP_MENU_FN( OnZoomTimes2Vertical ) );
309
310 #ifdef EXPERIMENTAL_HALF_WAVE
311 AppendItem( "HalfWave", OnZoomHalfWaveID, XXO("Half Wave"), POPUP_MENU_FN( OnZoomHalfWave ) );
312 #endif
313 EndSection();
314
315 BeginSection( "InOut" );
317 MakeLabel( XXO("Zoom In"), bVZoom, XXO("Left-Click/Left-Drag") ),
318 POPUP_MENU_FN( OnZoomInVertical ) );
320 MakeLabel( XXO("Zoom Out"), bVZoom, XXO("Shift-Left-Click") ),
321 POPUP_MENU_FN( OnZoomOutVertical ) );
322 EndSection();
323 EndSection();
324
326
327void WaveformVRulerMenuTable::OnWaveformScaleType(wxCommandEvent &evt)
328{
329 WaveTrack *const wt = mpData->pTrack;
330 // Assume linked track is wave or null
331 const WaveformSettings::ScaleType newScaleType =
333 std::max(0,
335 evt.GetId() - OnFirstWaveformScaleID
336 )));
337
338 if (WaveformSettings::Get(*wt).scaleType != newScaleType) {
339 WaveformSettings::Get(*wt).scaleType = newScaleType;
340
341 AudacityProject *const project = &mpData->project;
342 ProjectHistory::Get( *project ).ModifyState(true);
343
344 using namespace RefreshCode;
345 mpData->result = UpdateVRuler | RefreshAll;
346 }
347}
wxT("CloseDown"))
@ Internal
Indicates internal failure from Audacity.
int min(int a, int b)
XXO("&Cut/Copy/Paste Toolbar")
#define LINEAR_TO_DB(x)
Definition: MemoryX.h:562
@ OnZoomTimes2ID
@ OnZoomOutVerticalID
@ OnZoomFitVerticalID
@ OnZoomHalfWaveID
@ OnZoomInVerticalID
#define END_POPUP_MENU()
#define BEGIN_POPUP_MENU(HandlerClass)
#define POPUP_MENU_FN(memFn)
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
static TranslatableStrings names
Definition: TagsEditor.cpp:153
const auto project
@ OnFirstWaveformScaleID
gPrefs Read(wxT("/GUI/VerticalZooming"), &bVZoom, false)
EndSection()
bool bVZoom
BeginSection("Scales")
AppendItem("Reset", OnZoomFitVerticalID, MakeLabel(XXO("Zoom Reset"), bVZoom, XXO("Shift-Right-Click")), POPUP_MENU_FN(OnZoomReset))
AppendRadioItem("Instrument1", OnInstrument1ID, GetWaveColorStr(0), POPUP_MENU_FN(OnWaveColorChange), 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
void ModifyState(bool bWantsAutoSave)
static ProjectHistory & Get(AudacityProject &project)
std::shared_ptr< Subclass > Lock(const std::weak_ptr< Subclass > &wTrack)
Definition: Track.h:1228
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:347
Result mChangeHighlight
Definition: UIHandle.h:147
unsigned Result
Definition: UIHandle.h:39
A Track that contains audio waveform data.
Definition: WaveTrack.h:222
double GetRate() const override
Definition: WaveTrack.cpp:1085
void SetDisplayBounds(float min, float max)
static WaveformScale & Get(const WaveTrack &track)
Mutative access to attachment even if the track argument is const.
static const EnumValueSymbols & GetScaleNames()
static WaveformSettings & Get(const WaveTrack &track)
static PopupMenuTable & Instance()
void Enter(bool forward, AudacityProject *) override
~WaveformVZoomHandle() override
Result Drag(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
wxRect DrawingArea(TrackPanelDrawingContext &, const wxRect &rect, const wxRect &panelRect, unsigned iPass) override
std::weak_ptr< WaveTrack > mpTrack
Result Click(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
WaveformVZoomHandle(const WaveformVZoomHandle &)
static void DoZoom(AudacityProject *pProject, WaveTrack *pTrack, WaveChannelViewConstants::ZoomActions ZoomKind, const wxRect &rect, int zoomStart, int zoomEnd, bool fixedMousePoint)
Result Cancel(AudacityProject *pProject) override
void Draw(TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass) override
std::shared_ptr< const Channel > FindChannel() const override
bool HandlesRightClick() override
Whether the handle has any special right-button handling.
HitTestPreview Preview(const TrackPanelMouseState &state, AudacityProject *pProject) override
Result Release(const TrackPanelMouseEvent &event, AudacityProject *pProject, wxWindow *pParent) override
Namespace containing an enum 'what to do on a refresh?'.
Definition: RefreshCode.h:16
AUDACITY_DLL_API Result DoRelease(const TrackPanelMouseEvent &event, AudacityProject *pProject, wxWindow *pParent, WaveTrack *pTrack, const wxRect &mRect, DoZoomFunction doZoom, PopupMenuTable &table, int zoomStart, int zoomEnd)
AUDACITY_DLL_API Result DoDrag(const TrackPanelMouseEvent &event, AudacityProject *pProject, int zoomStart, int &zoomEnd)
AUDACITY_DLL_API HitTestPreview HitPreview(const wxMouseState &state)
AUDACITY_DLL_API void DoDraw(TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass, int zoomStart, int zoomEnd)
AUDACITY_DLL_API wxRect DoDrawingArea(const wxRect &rect, const wxRect &panelRect, unsigned iPass)
AUDACITY_DLL_API bool IsDragZooming(int zoomStart, int zoomEnd)
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:645