Audacity 3.2.0
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)
 

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 }
 
NumericFormatID 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 auto project
const AudacityProject * mProject
Definition: Snap.h:96
void Reinit()
Definition: Snap.cpp:76
SnapPointArray mCandidates
Definition: Snap.h:103
SnapPointArray mSnapPoints
Definition: Snap.h:104
bool mNoTimeSnap
Definition: Snap.h:99
int mPixelTolerance
Definition: Snap.h:98
const ZoomInfo * mZoomInfo
Definition: Snap.h:97

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 58 of file Snap.cpp.

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

◆ ~SnapManager()

SnapManager::~SnapManager ( )

Definition at line 72 of file Snap.cpp.

73{
74}

Member Function Documentation

◆ CondListAdd()

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

Definition at line 115 of file Snap.cpp.

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

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:

◆ Find() [1/2]

size_t SnapManager::Find ( double  t)
private

Definition at line 156 of file Snap.cpp.

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

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 138 of file Snap.cpp.

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

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 124 of file Snap.cpp.

125{
126 return mSnapPoints[index].t;
127}

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 130 of file Snap.cpp.

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

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 76 of file Snap.cpp.

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

References CondListAdd(), anonymous_namespace{ExportPCM.cpp}::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 261 of file Snap.cpp.

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

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

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 103 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 102 of file Snap.h.

Referenced by SnapToPoints().

◆ mFormat

NumericFormatID SnapManager::mFormat {}
private

Definition at line 111 of file Snap.h.

Referenced by Reinit().

◆ mNoTimeSnap

bool SnapManager::mNoTimeSnap
private

Definition at line 99 of file Snap.h.

Referenced by Reinit().

◆ mPixelTolerance

int SnapManager::mPixelTolerance
private

Definition at line 98 of file Snap.h.

Referenced by SnapToPoints().

◆ mProject

const AudacityProject* SnapManager::mProject
private

Definition at line 96 of file Snap.h.

Referenced by Reinit().

◆ mRate

double SnapManager::mRate { 0.0 }
private

Definition at line 110 of file Snap.h.

Referenced by Reinit().

◆ mSnapPoints

SnapPointArray SnapManager::mSnapPoints
private

Definition at line 104 of file Snap.h.

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

◆ mSnapTo

Identifier SnapManager::mSnapTo {}
private

Definition at line 109 of file Snap.h.

Referenced by Reinit().

◆ mSnapToTime

bool SnapManager::mSnapToTime { false }
private

Definition at line 107 of file Snap.h.

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

◆ mZoomInfo

const ZoomInfo* SnapManager::mZoomInfo
private

Definition at line 97 of file Snap.h.

Referenced by PixelDiff(), and Snap().


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