Audacity 3.2.0
StretchHandle.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5StretchHandle.cpp
6
7Paul Licameli split from TrackPanel.cpp
8
9**********************************************************************/
10
11
12
13#ifdef USE_MIDI
14#include "../lib-src/header-substitutes/allegro.h"
15
16#include "StretchHandle.h"
17
18#include "../../../ui/CommonTrackPanelCell.h"
19#include "../../../../HitTestResult.h"
20#include "../../../../NoteTrack.h"
21#include "ProjectAudioIO.h"
22#include "ProjectHistory.h"
23#include "../../../../RefreshCode.h"
24#include "SyncLock.h"
25#include "../../../../TrackPanelMouseEvent.h"
26#include "UndoManager.h"
27#include "ViewInfo.h"
28#include "../../../../../images/Cursors.h"
29
30#include <wx/event.h>
31#include <algorithm>
32
34( const std::shared_ptr<NoteTrack> &pTrack, const StretchState &stretchState )
35 : mpTrack{ pTrack }
36 , mStretchState{ stretchState }
37{}
38
40{
41 static auto disabledCursor =
42 ::MakeCursor(wxCURSOR_NO_ENTRY, DisabledCursorXpm, 16, 16);
43 static auto stretchLeftCursor =
44 ::MakeCursor(wxCURSOR_BULLSEYE, StretchLeftCursorXpm, 16, 16);
45 static auto stretchRightCursor =
46 ::MakeCursor(wxCURSOR_BULLSEYE, StretchRightCursorXpm, 16, 16);
47 static auto stretchCursor =
48 ::MakeCursor(wxCURSOR_BULLSEYE, StretchCursorXpm, 16, 16);
49
50 if (unsafe) {
51 return { {}, &*disabledCursor };
52 }
53 else {
54 wxCursor *pCursor = NULL;
55 switch (stretchMode) {
56 default:
57 wxASSERT(false);
58 case stretchLeft:
59 pCursor = &*stretchLeftCursor; break;
60 case stretchCenter:
61 pCursor = &*stretchCursor; break;
62 case stretchRight:
63 pCursor = &*stretchRightCursor; break;
64 }
65 return {
66 XO("Click and drag to stretch selected region."),
67 pCursor
68 };
69 }
70}
71
73(std::weak_ptr<StretchHandle> &holder,
74 const TrackPanelMouseState &st, const AudacityProject *pProject,
75 const std::shared_ptr<NoteTrack> &pTrack)
76{
77 StretchState stretchState;
78 const wxMouseState &state = st.state;
79
80 // later, we may want a different policy, but for now, stretch is
81 // selected when the cursor is near the center of the track and
82 // within the selection
83 auto &viewInfo = ViewInfo::Get( *pProject );
84
85 if (!pTrack || !pTrack->GetSelected())
86 return {};
87
88 const wxRect &rect = st.rect;
89 int center = rect.y + rect.height / 2;
90 int distance = abs(state.m_y - center);
91 const int yTolerance = 10;
92 wxInt64 leftSel = viewInfo.TimeToPosition(viewInfo.selectedRegion.t0(), rect.x);
93 wxInt64 rightSel = viewInfo.TimeToPosition(viewInfo.selectedRegion.t1(), rect.x);
94 // Something is wrong if right edge comes before left edge
95 wxASSERT(!(rightSel < leftSel));
96 if (!(leftSel <= state.m_x && state.m_x <= rightSel &&
97 distance < yTolerance))
98 return {};
99
100 // find nearest beat to sel0, sel1
101 static const double minPeriod = 0.05; // minimum beat period
102 stretchState.mBeatCenter = { 0, 0 };
103
104 auto t0 = GetT0(*pTrack, viewInfo);
105 auto t1 = GetT1(*pTrack, viewInfo);
106
107 if (t0 >= t1)
108 return {};
109
110 stretchState.mBeat0 = pTrack->NearestBeatTime( t0 );
111 stretchState.mOrigSel0Quantized = stretchState.mBeat0.first;
112
113 stretchState.mBeat1 = pTrack->NearestBeatTime( t1 );
114 stretchState.mOrigSel1Quantized = stretchState.mBeat1.first;
115
116 // If there is not (almost) a beat to stretch that is slower
117 // than 20 beats per second, don't stretch
118 if ( within( stretchState.mBeat0.second,
119 stretchState.mBeat1.second, 0.9 ) ||
120 ( stretchState.mBeat1.first - stretchState.mBeat0.first ) /
121 ( stretchState.mBeat1.second - stretchState.mBeat0.second )
122 < minPeriod )
123 return {};
124
125 auto selStart = viewInfo.PositionToTime( state.m_x, rect.x );
126 selStart = std::max(t0, std::min(t1, selStart));
127 stretchState.mBeatCenter = pTrack->NearestBeatTime( selStart );
128 if ( within( stretchState.mBeat0.second,
129 stretchState.mBeatCenter.second, 0.1 ) ) {
130 stretchState.mMode = stretchLeft;
131 stretchState.mLeftBeats = 0;
132 stretchState.mRightBeats =
133 stretchState.mBeat1.second - stretchState.mBeat0.second;
134 }
135 else if ( within( stretchState.mBeat1.second,
136 stretchState.mBeatCenter.second, 0.1 ) ) {
137 stretchState.mMode = stretchRight;
138 stretchState.mLeftBeats =
139 stretchState.mBeat1.second - stretchState.mBeat0.second;
140 stretchState.mRightBeats = 0;
141 }
142 else {
143 stretchState.mMode = stretchCenter;
144 stretchState.mLeftBeats =
145 stretchState.mBeat1.second - stretchState.mBeatCenter.second;
146 stretchState.mRightBeats =
147 stretchState.mBeatCenter.second - stretchState.mBeat0.second;
148 }
149
150 auto result = std::make_shared<StretchHandle>( pTrack, stretchState );
151 result = AssignUIHandlePtr(holder, result);
152 return result;
153}
154
156{
157}
158
160(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
161{
162 using namespace RefreshCode;
163 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
164 if ( unsafe )
165 return Cancelled;
166
167 const wxMouseEvent &event = evt.event;
168
169 if (event.LeftDClick() ||
170 !event.LeftDown() ||
171 evt.pCell == NULL)
172 return Cancelled;
173
174
175 mLeftEdge = evt.rect.GetLeft();
176 auto &viewInfo = ViewInfo::Get( *pProject );
177
178 viewInfo.selectedRegion.setTimes
179 ( mStretchState.mBeat0.first, mStretchState.mBeat1.first );
180
181 // Full refresh since the label area may need to indicate
182 // newly selected tracks. (I'm really not sure if the label area
183 // needs to be refreshed or how to just refresh non-label areas.-RBD)
184
185 return RefreshAll;
186}
187
189(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
190{
191 using namespace RefreshCode;
192 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
193 if (unsafe) {
194 this->Cancel(pProject);
195 return RefreshAll | Cancelled;
196 }
197
198 const wxMouseEvent &event = evt.event;
199 const int x = event.m_x;
200
201 Track *clickedTrack=nullptr;
202 if (evt.pCell)
203 clickedTrack =
204 static_cast<CommonTrackPanelCell*>(evt.pCell.get())->FindTrack().get();
205
206 if (clickedTrack == nullptr && mpTrack != nullptr)
207 clickedTrack = mpTrack.get();
208 Stretch(pProject, x, mLeftEdge, clickedTrack);
209 return RefreshAll;
210}
211
213(const TrackPanelMouseState &, AudacityProject *pProject)
214{
215 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
216 return HitPreview( mStretchState.mMode, unsafe );
217}
218
220(const TrackPanelMouseEvent &, AudacityProject *pProject,
221 wxWindow *)
222{
223 using namespace RefreshCode;
224
225 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
226 if (unsafe) {
227 this->Cancel(pProject);
228 return RefreshAll | Cancelled;
229 }
230
231 bool left = mStretchState.mMode == stretchLeft;
232 bool right = mStretchState.mMode == stretchRight;
233 auto &viewInfo = ViewInfo::Get( *pProject );
234 if ( SyncLockState::Get(*pProject).IsSyncLocked() && ( left || right ) ) {
235 for ( auto track :
236 SyncLock::Group( mpTrack.get() ) ) {
237 if ( track != mpTrack.get() ) {
238 if ( left ) {
239 auto origT0 = mStretchState.mOrigSel0Quantized;
240 auto diff = viewInfo.selectedRegion.t0() - origT0;
241 if ( diff > 0)
242 track->SyncLockAdjust( origT0 + diff, origT0 );
243 else
244 track->SyncLockAdjust( origT0, origT0 - diff );
245 track->Offset( diff );
246 }
247 else {
248 auto origT1 = mStretchState.mOrigSel1Quantized;
249 auto diff = viewInfo.selectedRegion.t1() - origT1;
250 track->SyncLockAdjust( origT1, origT1 + diff );
251 }
252 }
253 }
254 }
255
256 /* i18n-hint: (noun) The track that is used for MIDI notes which can be
257 dragged to change their duration.*/
258 ProjectHistory::Get( *pProject ).PushState(XO("Stretch Note Track"),
259 /* i18n-hint: In the history list, indicates a MIDI note has
260 been dragged to change its duration (stretch it). Using either past
261 or present tense is fine here. If unsure, go for whichever is
262 shorter.*/
263 XO("Stretch"),
265 return RefreshAll;
266}
267
269{
270 ProjectHistory::Get( *pProject ).RollbackState();
272}
273
274double StretchHandle::GetT0(const Track &track, const ViewInfo &viewInfo)
275{
276 return std::max(track.GetStartTime(), viewInfo.selectedRegion.t0());
277}
278
279double StretchHandle::GetT1(const Track &track, const ViewInfo &viewInfo)
280{
281 return std::min(track.GetEndTime(), viewInfo.selectedRegion.t1());
282}
283
284void StretchHandle::Stretch(AudacityProject *pProject, int mouseXCoordinate, int trackLeftEdge,
285 Track *pTrack)
286{
287 auto &viewInfo = ViewInfo::Get( *pProject );
288
289 if (pTrack == NULL && mpTrack != NULL)
290 pTrack = mpTrack.get();
291
292 if (pTrack) pTrack->TypeSwitch( [&](NoteTrack *pNt) {
293 double moveto =
294 std::max(0.0, viewInfo.PositionToTime(mouseXCoordinate, trackLeftEdge));
295
296 double dur, left_dur, right_dur;
297
298 // check to make sure tempo is not higher than 20 beats per second
299 // (In principle, tempo can be higher, but not infinity.)
300 double minPeriod = 0.05; // minimum beat period
301
302 // make sure target duration is not too short
303 // Take quick exit if so, without changing the selection.
304 auto t0 = mStretchState.mBeat0.first;
305 auto t1 = mStretchState.mBeat1.first;
306 switch ( mStretchState.mMode ) {
307 case stretchLeft: {
308 dur = t1 - moveto;
309 if (dur < mStretchState.mRightBeats * minPeriod)
310 return;
311 pNt->StretchRegion
313 pNt->Offset( moveto - t0 );
314 mStretchState.mBeat0.first = moveto;
315 viewInfo.selectedRegion.setT0(moveto);
316 break;
317 }
318 case stretchRight: {
319 dur = moveto - t0;
320 if (dur < mStretchState.mLeftBeats * minPeriod)
321 return;
322 pNt->StretchRegion
324 viewInfo.selectedRegion.setT1(moveto);
325 mStretchState.mBeat1.first = moveto;
326 break;
327 }
328 case stretchCenter: {
329 moveto = std::max(t0, std::min(t1, moveto));
330 left_dur = moveto - t0;
331 right_dur = t1 - moveto;
332 if ( left_dur < mStretchState.mLeftBeats * minPeriod ||
333 right_dur < mStretchState.mRightBeats * minPeriod )
334 return;
335 pNt->StretchRegion
337 pNt->StretchRegion
339 mStretchState.mBeatCenter.first = moveto;
340 break;
341 }
342 default:
343 wxASSERT(false);
344 break;
345 }
346 });
347}
348#endif
std::shared_ptr< UIHandle > UIHandlePtr
Definition: CellularPanel.h:28
int min(int a, int b)
XO("Cut/Copy/Paste")
std::unique_ptr< wxCursor > MakeCursor(int WXUNUSED(CursorId), const char *const pXpm[36], int HotX, int HotY)
Definition: TrackPanel.cpp:186
bool within(A a, B b, DIST d)
Definition: TrackPanel.cpp:167
std::shared_ptr< Subclass > AssignUIHandlePtr(std::weak_ptr< Subclass > &holder, const std::shared_ptr< Subclass > &pNew)
Definition: UIHandle.h:151
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
A Track that is used for Midi notes. (Somewhat old code).
Definition: NoteTrack.h:63
bool StretchRegion(QuantizedTimeAndBeat t0, QuantizedTimeAndBeat t1, double newDur)
Definition: NoteTrack.cpp:739
double t1() const
Definition: ViewInfo.h:36
double t0() const
Definition: ViewInfo.h:35
bool IsAudioActive() const
static ProjectAudioIO & Get(AudacityProject &project)
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
static ProjectHistory & Get(AudacityProject &project)
Result Click(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
Result Drag(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
StretchState mStretchState
Definition: StretchHandle.h:99
static double GetT0(const Track &track, const ViewInfo &viewInfo)
std::shared_ptr< NoteTrack > mpTrack
Definition: StretchHandle.h:96
static double GetT1(const Track &track, const ViewInfo &viewInfo)
Result Release(const TrackPanelMouseEvent &event, AudacityProject *pProject, wxWindow *pParent) override
void Stretch(AudacityProject *pProject, int mouseXCoordinate, int trackLeftEdge, Track *pTrack)
HitTestPreview Preview(const TrackPanelMouseState &state, AudacityProject *pProject) override
StretchHandle(const StretchHandle &)
static HitTestPreview HitPreview(StretchEnum stretchMode, bool unsafe)
virtual ~StretchHandle()
static UIHandlePtr HitTest(std::weak_ptr< StretchHandle > &holder, const TrackPanelMouseState &state, const AudacityProject *pProject, const std::shared_ptr< NoteTrack > &pTrack)
Result Cancel(AudacityProject *pProject) override
static TrackIterRange< Track > Group(Track *pTrack)
Definition: SyncLock.cpp:161
bool IsSyncLocked() const
Definition: SyncLock.cpp:43
static SyncLockState & Get(AudacityProject &project)
Definition: SyncLock.cpp:26
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:226
void Offset(double t)
Definition: Track.h:483
virtual double GetStartTime() const =0
R TypeSwitch(const Functions &...functions)
Use this function rather than testing track type explicitly and making down-casts.
Definition: Track.h:833
virtual double GetEndTime() const =0
unsigned Result
Definition: UIHandle.h:38
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:219
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
Namespace containing an enum 'what to do on a refresh?'.
Definition: RefreshCode.h:16
std::shared_ptr< Track > FindTrack(TrackPanelCell *pCell)
Definition: TrackPanel.cpp:521
QuantizedTimeAndBeat mBeatCenter
Definition: StretchHandle.h:44
QuantizedTimeAndBeat mBeat0
Definition: StretchHandle.h:45
QuantizedTimeAndBeat mBeat1
Definition: StretchHandle.h:46
std::shared_ptr< TrackPanelCell > pCell