Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
SnapManager Class Reference

#include <Snap.h>

Collaboration diagram for SnapManager:
[legend]

Public Member Functions

 SnapManager (const AudacityProject &project, SnapPointArray candidates, const ZoomInfo &zoomInfo, bool noTimeSnap=false, int pixelTolerance=kPixelTolerance)
 Construct only for specified candidate points. More...
 
 SnapManager (const AudacityProject &project, const TrackList &tracks, const ZoomInfo &zoomInfo, SnapPointArray candidates={}, bool noTimeSnap=false, int pixelTolerance=kPixelTolerance)
 
 ~SnapManager ()
 
SnapResults Snap (Track *currentTrack, double t, bool rightEdge)
 

Static Public Member Functions

static void Draw (wxDC *dc, wxInt64 snap0, wxInt64 snap1)
 

Private Member Functions

void Reinit ()
 
void CondListAdd (double t, const Track *track)
 
double Get (size_t index)
 
wxInt64 PixelDiff (double t, size_t index)
 
size_t Find (double t, size_t i0, size_t i1)
 
size_t Find (double t)
 
bool SnapToPoints (Track *currentTrack, double t, bool rightEdge, double *outT)
 

Private Attributes

const AudacityProjectmProject
 
const ZoomInfomZoomInfo
 
int mPixelTolerance
 
bool mNoTimeSnap
 
double mEpsilon { 1 / 44100.0 }
 Two time points closer than this are considered the same. More...
 
SnapPointArray mCandidates
 
SnapPointArray mSnapPoints
 
bool mSnapToTime { false }
 
Identifier mSnapTo {}
 
double mRate { 0.0 }
 
NumericFormatSymbol mFormat {}
 

Detailed Description

Definition at line 55 of file Snap.h.

Constructor & Destructor Documentation

◆ SnapManager() [1/2]

SnapManager::SnapManager ( const AudacityProject project,
SnapPointArray  candidates,
const ZoomInfo zoomInfo,
bool  noTimeSnap = false,
int  pixelTolerance = kPixelTolerance 
)

Construct only for specified candidate points.

Definition at line 29 of file Snap.cpp.

34: mProject{ &project }
35, mZoomInfo{ &zoomInfo }
36, mPixelTolerance{ pixelTolerance }
37, mNoTimeSnap{ noTimeSnap }
38, mCandidates{ move( candidates ) }
40{
41 Reinit();
42}
const AudacityProject * mProject
Definition: Snap.h:99
void Reinit()
Definition: Snap.cpp:78
SnapPointArray mCandidates
Definition: Snap.h:106
SnapPointArray mSnapPoints
Definition: Snap.h:107
bool mNoTimeSnap
Definition: Snap.h:102
int mPixelTolerance
Definition: Snap.h:101
const ZoomInfo * mZoomInfo
Definition: Snap.h:100

References Reinit().

Here is the call graph for this function:

◆ SnapManager() [2/2]

SnapManager::SnapManager ( const AudacityProject project,
const TrackList tracks,
const ZoomInfo zoomInfo,
SnapPointArray  candidates = {},
bool  noTimeSnap = false,
int  pixelTolerance = kPixelTolerance 
)

Construct for (optionally) specified points, plus significant points on the tracks in the given list

Definition at line 60 of file Snap.cpp.

66 : SnapManager{ project,
67 // Add candidates to given ones by default rules,
68 // then delegate to other ctor
69 FindCandidates( move(candidates), tracks ),
70 zoomInfo, noTimeSnap, pixelTolerance }
71{
72}
SnapPointArray FindCandidates(SnapPointArray candidates, const TrackList &tracks)
Definition: Snap.cpp:45

◆ ~SnapManager()

SnapManager::~SnapManager ( )

Definition at line 74 of file Snap.cpp.

75{
76}

Member Function Documentation

◆ CondListAdd()

void SnapManager::CondListAdd ( double  t,
const Track track 
)
private

Definition at line 117 of file Snap.cpp.

118{
119 if (!mSnapToTime || ProjectSnap::Get(*mProject).SnapTime(t).time == t)
120 {
121 mSnapPoints.push_back(SnapPoint { t, track });
122 }
123}
SnapResult SnapTime(double time) const
Definition: ProjectSnap.cpp:77
static ProjectSnap & Get(AudacityProject &project)
Definition: ProjectSnap.cpp:27
bool mSnapToTime
Definition: Snap.h:110
Definition: Snap.h:31
double time
Snapped time.
Definition: SnapUtils.h:52

References ProjectSnap::Get(), mSnapPoints, mSnapToTime, ProjectSnap::SnapTime(), and SnapResult::time.

Referenced by Reinit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Draw()

void SnapManager::Draw ( wxDC *  dc,
wxInt64  snap0,
wxInt64  snap1 
)
static

Definition at line 306 of file Snap.cpp.

307{
309 if ( snap0 >= 0 ) {
310 AColor::Line(*dc, (int)snap0, 0, (int)snap0, 30000);
311 }
312 if ( snap1 >= 0 ) {
313 AColor::Line(*dc, (int)snap1, 0, (int)snap1, 30000);
314 }
315}
static void Line(wxDC &dc, wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
Definition: AColor.cpp:187
static void SnapGuidePen(wxDC *dc)
Definition: AColor.cpp:488

References AColor::Line(), and AColor::SnapGuidePen().

Referenced by WaveClipTrimHandle::AdjustBorder::Draw().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Find() [1/2]

size_t SnapManager::Find ( double  t)
private

Definition at line 158 of file Snap.cpp.

159{
160 size_t cnt = mSnapPoints.size();
161 size_t index = Find(t, 0, cnt);
162
163 // At this point, either index is the closest, or the next one
164 // to the right is. Keep moving to the right until we get a
165 // different value
166 size_t next = index + 1;
167 while (next + 1 < cnt && Get(next) == Get(index))
168 {
169 next++;
170 }
171
172 // Now return whichever one is closer to time t
173 if (next < cnt && PixelDiff(t, next) < PixelDiff(t, index))
174 {
175 return next;
176 }
177
178 return index;
179}
size_t Find(double t, size_t i0, size_t i1)
Definition: Snap.cpp:140
wxInt64 PixelDiff(double t, size_t index)
Definition: Snap.cpp:132
double Get(size_t index)
Definition: Snap.cpp:126

References Find(), Get(), mSnapPoints, and PixelDiff().

Here is the call graph for this function:

◆ Find() [2/2]

size_t SnapManager::Find ( double  t,
size_t  i0,
size_t  i1 
)
private

Definition at line 140 of file Snap.cpp.

141{
142 if (i1 <= i0 + 1)
143 {
144 return i0;
145 }
146
147 size_t half = (i0 + i1) / 2;
148
149 if (t < Get(half))
150 {
151 return Find(t, i0, half);
152 }
153
154 return Find(t, half, i1);
155}

References Find(), and Get().

Referenced by Find(), and SnapToPoints().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Get()

double SnapManager::Get ( size_t  index)
private

Definition at line 126 of file Snap.cpp.

127{
128 return mSnapPoints[index].t;
129}

References mSnapPoints.

Referenced by Find(), PixelDiff(), and SnapToPoints().

Here is the caller graph for this function:

◆ PixelDiff()

wxInt64 SnapManager::PixelDiff ( double  t,
size_t  index 
)
private

Definition at line 132 of file Snap.cpp.

133{
134 return std::abs(mZoomInfo->TimeToPosition(t, 0) -
135 mZoomInfo->TimeToPosition(Get(index), 0));
136}
int64 TimeToPosition(double time, int64 origin=0, bool ignoreFisheye=false) const
STM: Converts a project time to screen x position.
Definition: ZoomInfo.cpp:45

References Get(), mZoomInfo, and ZoomInfo::TimeToPosition().

Referenced by Find(), and SnapToPoints().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Reinit()

void SnapManager::Reinit ( )
private

Definition at line 78 of file Snap.cpp.

79{
80 const auto &formats = ProjectNumericFormats::Get(*mProject);
81 const auto &settings = ProjectSnap::Get( *mProject );
82
83 auto snapTo = settings.GetSnapTo();
84 auto snapMode = settings.GetSnapMode();
85
86 auto rate = ProjectRate::Get(*mProject).GetRate();
87 auto format = formats.GetSelectionFormat();
88
89 // No need to reinit if these are still the same
90 if (snapTo == mSnapTo && rate == mRate && format == mFormat)
91 {
92 return;
93 }
94
95 // Save NEW settings
96 mSnapTo = snapTo;
97 mRate = rate;
99
100 mSnapPoints.clear();
101
102 // Grab time-snapping prefs (unless otherwise requested)
104
105 // Add a SnapPoint at t=0
106 mSnapPoints.push_back(SnapPoint{});
107
108 // Adjust and filter the candidate points
109 for (const auto &candidate : mCandidates)
110 CondListAdd( candidate.t, candidate.track );
111
112 // Sort all by time
113 std::sort(mSnapPoints.begin(), mSnapPoints.end());
114}
int format
Definition: ExportPCM.cpp:53
static Settings & settings()
Definition: TrackInfo.cpp:83
static ProjectNumericFormats & Get(AudacityProject &project)
static ProjectRate & Get(AudacityProject &project)
Definition: ProjectRate.cpp:28
double GetRate() const
Definition: ProjectRate.cpp:53
Identifier mSnapTo
Definition: Snap.h:112
double mRate
Definition: Snap.h:113
NumericFormatSymbol mFormat
Definition: Snap.h:114
void CondListAdd(double t, const Track *track)
Definition: Snap.cpp:117

References CondListAdd(), format, ProjectNumericFormats::Get(), ProjectRate::Get(), ProjectSnap::Get(), ProjectRate::GetRate(), mCandidates, mFormat, mNoTimeSnap, mProject, mRate, mSnapPoints, mSnapTo, mSnapToTime, settings(), and SNAP_OFF.

Referenced by Snap(), and SnapManager().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Snap()

SnapResults SnapManager::Snap ( Track currentTrack,
double  t,
bool  rightEdge 
)

Definition at line 263 of file Snap.cpp.

265{
266
267 SnapResults results;
268 // Check to see if we need to reinitialize
269 Reinit();
270
271 results.timeSnappedTime = results.outTime = t;
272 results.outCoord = mZoomInfo->TimeToPosition(t);
273
274 // First snap to points in mSnapPoints
275 results.snappedPoint =
276 SnapToPoints(currentTrack, t, rightEdge, &results.outTime);
277
278 if (mSnapToTime) {
279 // Find where it would snap time to the grid
280 results.timeSnappedTime = ProjectSnap::Get(*mProject).SnapTime(t).time;
281 }
282
283 results.snappedTime = false;
284 if (mSnapToTime)
285 {
286 if (results.snappedPoint)
287 {
288 // Since mSnapPoints only contains points on the grid, we're done
289 results.snappedTime = true;
290 }
291 else
292 {
293 results.outTime = results.timeSnappedTime;
294 results.snappedTime = true;
295 }
296 }
297
298 if (results.Snapped())
299 results.outCoord = mZoomInfo->TimeToPosition(results.outTime);
300
301 return results;
302}
bool SnapToPoints(Track *currentTrack, double t, bool rightEdge, double *outT)
Definition: Snap.cpp:182
bool snappedTime
Definition: Snap.h:50
double outTime
Definition: Snap.h:47
double timeSnappedTime
Definition: Snap.h:46
wxInt64 outCoord
Definition: Snap.h:48
bool Snapped() const
Definition: Snap.h:52
bool snappedPoint
Definition: Snap.h:49

References ProjectSnap::Get(), mSnapToTime, mZoomInfo, SnapResults::outCoord, SnapResults::outTime, Reinit(), SnapResults::Snapped(), SnapResults::snappedPoint, SnapResults::snappedTime, ProjectSnap::SnapTime(), SnapToPoints(), SnapResult::time, SnapResults::timeSnappedTime, and ZoomInfo::TimeToPosition().

Referenced by anonymous_namespace{TimeShiftHandle.cpp}::AdjustToSnap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SnapToPoints()

bool SnapManager::SnapToPoints ( Track currentTrack,
double  t,
bool  rightEdge,
double *  outT 
)
private

Definition at line 182 of file Snap.cpp.

186{
187 *outT = t;
188
189 size_t cnt = mSnapPoints.size();
190 if (cnt == 0)
191 {
192 return false;
193 }
194
195 // Find the nearest SnapPoint
196 size_t index = Find(t);
197
198 // If it's too far away, just give up now
199 if (PixelDiff(t, index) >= mPixelTolerance)
200 {
201 return false;
202 }
203
204 // Otherwise, search left and right for all of the points
205 // within the allowed range.
206 size_t left = index;
207 size_t right = index;
208 size_t i;
209
210 while (left > 0 && PixelDiff(t, left - 1) < mPixelTolerance)
211 {
212 left--;
213 }
214
215 while (right < cnt - 1 && PixelDiff(t, right + 1) < mPixelTolerance)
216 {
217 right++;
218 }
219
220 if (left == index && right == index)
221 {
222 // Awesome, there's only one point that matches!
223 *outT = Get(index);
224 return true;
225 }
226
227 size_t indexInThisTrack = 0;
228 size_t countInThisTrack = 0;
229 for (i = left; i <= right; ++i)
230 {
231 if (mSnapPoints[i].track == currentTrack)
232 {
233 indexInThisTrack = i;
234 countInThisTrack++;
235 }
236 }
237
238 if (countInThisTrack == 1)
239 {
240 // Cool, only one of the points is in the same track, so
241 // we'll use that one.
242 *outT = Get(indexInThisTrack);
243 return true;
244 }
245
246 if (Get(right) - Get(left) < mEpsilon)
247 {
248 // OK, they're basically the same point
249 if (rightEdge)
250 {
251 *outT = Get(right); // Return rightmost
252 }
253 else {
254 *outT = Get(left); // Return leftmost
255 }
256 return true;
257 }
258
259 // None of the points matched, bummer.
260 return false;
261}
double mEpsilon
Two time points closer than this are considered the same.
Definition: Snap.h:105

References Find(), Get(), mEpsilon, mPixelTolerance, mSnapPoints, and PixelDiff().

Referenced by Snap().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ mCandidates

SnapPointArray SnapManager::mCandidates
private

Definition at line 106 of file Snap.h.

Referenced by Reinit().

◆ mEpsilon

double SnapManager::mEpsilon { 1 / 44100.0 }
private

Two time points closer than this are considered the same.

Definition at line 105 of file Snap.h.

Referenced by SnapToPoints().

◆ mFormat

NumericFormatSymbol SnapManager::mFormat {}
private

Definition at line 114 of file Snap.h.

Referenced by Reinit().

◆ mNoTimeSnap

bool SnapManager::mNoTimeSnap
private

Definition at line 102 of file Snap.h.

Referenced by Reinit().

◆ mPixelTolerance

int SnapManager::mPixelTolerance
private

Definition at line 101 of file Snap.h.

Referenced by SnapToPoints().

◆ mProject

const AudacityProject* SnapManager::mProject
private

Definition at line 99 of file Snap.h.

Referenced by Reinit().

◆ mRate

double SnapManager::mRate { 0.0 }
private

Definition at line 113 of file Snap.h.

Referenced by Reinit().

◆ mSnapPoints

SnapPointArray SnapManager::mSnapPoints
private

Definition at line 107 of file Snap.h.

Referenced by CondListAdd(), Find(), Get(), Reinit(), and SnapToPoints().

◆ mSnapTo

Identifier SnapManager::mSnapTo {}
private

Definition at line 112 of file Snap.h.

Referenced by Reinit().

◆ mSnapToTime

bool SnapManager::mSnapToTime { false }
private

Definition at line 110 of file Snap.h.

Referenced by CondListAdd(), Reinit(), and Snap().

◆ mZoomInfo

const ZoomInfo* SnapManager::mZoomInfo
private

Definition at line 100 of file Snap.h.

Referenced by PixelDiff(), and Snap().


The documentation for this class was generated from the following files: