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

#include <Snap.h>

Public Member Functions

 SnapManager (const TrackList *tracks, const ZoomInfo *zoomInfo, const TrackClipArray *clipExclusions=NULL, const TrackArray *trackExclusions=NULL, bool noTimeSnap=false, int pixelTolerance=kPixelTolerance)
 
 ~SnapManager ()
 
SnapResults Snap (Track *currentTrack, double t, bool rightEdge)
 

Static Public Member Functions

static wxArrayString GetSnapLabels ()
 
static wxArrayString GetSnapValues ()
 
static const wxString & GetSnapValue (int index)
 
static int GetSnapIndex (const wxString &value)
 
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 TrackListmTracks
 
const TrackClipArraymClipExclusions
 
const TrackArray * mTrackExclusions
 
const ZoomInfomZoomInfo
 
int mPixelTolerance
 
bool mNoTimeSnap
 
double mEpsilon
 
SnapPointArray mSnapPoints
 
NumericConverter mConverter
 
bool mSnapToTime
 
int mSnapTo
 
double mRate
 
NumericFormatId mFormat
 

Detailed Description

Definition at line 84 of file Snap.h.

Constructor & Destructor Documentation

SnapManager::SnapManager ( const TrackList tracks,
const ZoomInfo zoomInfo,
const TrackClipArray clipExclusions = NULL,
const TrackArray *  trackExclusions = NULL,
bool  noTimeSnap = false,
int  pixelTolerance = kPixelTolerance 
)

Definition at line 38 of file Snap.cpp.

References GetActiveProject(), mClipExclusions, mEpsilon, mFormat, mNoTimeSnap, mPixelTolerance, mProject, mRate, mSnapTo, mTrackExclusions, mTracks, mZoomInfo, and Reinit().

45 {
46  mTracks = tracks;
47  mZoomInfo = zoomInfo;
48  mClipExclusions = clipExclusions;
49  mTrackExclusions = trackExclusions;
50  mPixelTolerance = pixelTolerance;
51  mNoTimeSnap = noTimeSnap;
52 
54  wxASSERT(mProject);
55 
56  mSnapTo = 0;
57  mRate = 0.0;
58  mFormat = {};
59 
60  // Two time points closer than this are considered the same
61  mEpsilon = 1 / 44100.0;
62 
63  Reinit();
64 }
double mEpsilon
Definition: Snap.h:131
const TrackList * mTracks
Definition: Snap.h:124
const ZoomInfo * mZoomInfo
Definition: Snap.h:127
const AudacityProject * mProject
Definition: Snap.h:123
void Reinit()
Definition: Snap.cpp:70
const TrackArray * mTrackExclusions
Definition: Snap.h:126
int mPixelTolerance
Definition: Snap.h:128
double mRate
Definition: Snap.h:139
const TrackClipArray * mClipExclusions
Definition: Snap.h:125
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:308
int mSnapTo
Definition: Snap.h:138
NumericFormatId mFormat
Definition: Snap.h:140
bool mNoTimeSnap
Definition: Snap.h:129
NumericConverter mConverter
Definition: Snap.h:135
SnapManager::~SnapManager ( )

Definition at line 66 of file Snap.cpp.

67 {
68 }

Member Function Documentation

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

Definition at line 170 of file Snap.cpp.

References NumericConverter::GetValue(), mConverter, mSnapPoints, mSnapToTime, and NumericConverter::SetValue().

Referenced by Reinit().

171 {
172  if (mSnapToTime)
173  {
174  mConverter.SetValue(t);
175  }
176 
177  if (!mSnapToTime || mConverter.GetValue() == t)
178  {
179  mSnapPoints.push_back(SnapPoint{ t, track });
180  }
181 }
Definition: Snap.h:59
bool mSnapToTime
Definition: Snap.h:136
SnapPointArray mSnapPoints
Definition: Snap.h:132
void SetValue(double newValue)
NumericConverter mConverter
Definition: Snap.h:135
void SnapManager::Draw ( wxDC *  dc,
wxInt64  snap0,
wxInt64  snap1 
)
static

Definition at line 413 of file Snap.cpp.

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

414 {
416  if ( snap0 >= 0 ) {
417  AColor::Line(*dc, (int)snap0, 0, (int)snap0, 30000);
418  }
419  if ( snap1 >= 0 ) {
420  AColor::Line(*dc, (int)snap1, 0, (int)snap1, 30000);
421  }
422 }
static void Line(wxDC &dc, wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
Definition: AColor.cpp:122
static void SnapGuidePen(wxDC *dc)
Definition: AColor.cpp:391
size_t SnapManager::Find ( double  t,
size_t  i0,
size_t  i1 
)
private

Definition at line 198 of file Snap.cpp.

References Get().

Referenced by Find(), and SnapToPoints().

199 {
200  if (i1 <= i0 + 1)
201  {
202  return i0;
203  }
204 
205  size_t half = (i0 + i1) / 2;
206 
207  if (t < Get(half))
208  {
209  return Find(t, i0, half);
210  }
211 
212  return Find(t, half, i1);
213 }
double Get(size_t index)
Definition: Snap.cpp:184
size_t Find(double t, size_t i0, size_t i1)
Definition: Snap.cpp:198
size_t SnapManager::Find ( double  t)
private

Definition at line 216 of file Snap.cpp.

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

217 {
218  size_t cnt = mSnapPoints.size();
219  size_t index = Find(t, 0, cnt);
220 
221  // At this point, either index is the closest, or the next one
222  // to the right is. Keep moving to the right until we get a
223  // different value
224  size_t next = index + 1;
225  while (next + 1 < cnt && Get(next) == Get(index))
226  {
227  next++;
228  }
229 
230  // Now return whichever one is closer to time t
231  if (next < cnt && PixelDiff(t, next) < PixelDiff(t, index))
232  {
233  return next;
234  }
235 
236  return index;
237 }
double Get(size_t index)
Definition: Snap.cpp:184
wxInt64 PixelDiff(double t, size_t index)
Definition: Snap.cpp:190
SnapPointArray mSnapPoints
Definition: Snap.h:132
size_t Find(double t, size_t i0, size_t i1)
Definition: Snap.cpp:198
double SnapManager::Get ( size_t  index)
private

Definition at line 184 of file Snap.cpp.

References mSnapPoints.

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

185 {
186  return mSnapPoints[index].t;
187 }
SnapPointArray mSnapPoints
Definition: Snap.h:132
int SnapManager::GetSnapIndex ( const wxString &  value)
static

Definition at line 398 of file Snap.cpp.

References GetSnapValues(), and SNAP_OFF.

399 {
400  wxArrayString values = SnapManager::GetSnapValues();
401  int index = values.Index(value);
402 
403  if (index != wxNOT_FOUND)
404  {
405  return index;
406  }
407 
408  return SNAP_OFF;
409 }
Definition: Snap.h:52
static wxArrayString GetSnapValues()
Definition: Snap.cpp:375
wxArrayString SnapManager::GetSnapLabels ( )
static

Definition at line 364 of file Snap.cpp.

References _().

Referenced by SelectionBar::Populate().

365 {
366  wxArrayString labels;
367 
368  labels.Add(_("Off"));
369  labels.Add(_("Nearest"));
370  labels.Add(_("Prior"));
371 
372  return labels;
373 }
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
const wxString & SnapManager::GetSnapValue ( int  index)
static

Definition at line 386 of file Snap.cpp.

References GetSnapValues(), and SNAP_OFF.

387 {
388  wxArrayString values = SnapManager::GetSnapValues();
389 
390  if (index >= 0 && index < (int) values.GetCount())
391  {
392  return values[index];
393  }
394 
395  return values[SNAP_OFF];
396 }
Definition: Snap.h:52
static wxArrayString GetSnapValues()
Definition: Snap.cpp:375
wxArrayString SnapManager::GetSnapValues ( )
static

Definition at line 375 of file Snap.cpp.

Referenced by GetSnapIndex(), and GetSnapValue().

376 {
377  wxArrayString values;
378 
379  values.Add(wxT("Off"));
380  values.Add(wxT("Nearest"));
381  values.Add(wxT("Prior"));
382 
383  return values;
384 }
wxInt64 SnapManager::PixelDiff ( double  t,
size_t  index 
)
private

Definition at line 190 of file Snap.cpp.

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

Referenced by Find(), and SnapToPoints().

191 {
192  return std::abs(mZoomInfo->TimeToPosition(t, 0) -
193  mZoomInfo->TimeToPosition(Get(index), 0));
194 }
double Get(size_t index)
Definition: Snap.cpp:184
const ZoomInfo * mZoomInfo
Definition: Snap.h:127
wxInt64 TimeToPosition(double time, wxInt64 origin=0, bool ignoreFisheye=false) const
STM: Converts a project time to screen x position.
Definition: ViewInfo.cpp:59
void SnapManager::Reinit ( )
private

Definition at line 70 of file Snap.cpp.

References CondListAdd(), TrackListConstIterator::First(), format, LabelTrack::GetLabel(), LabelTrack::GetNumLabels(), AudacityProject::GetRate(), AudacityProject::GetSelectionFormat(), AudacityProject::GetSnapTo(), LabelStruct::getT0(), LabelStruct::getT1(), Track::Label, label, mClipExclusions, mConverter, mFormat, mNoTimeSnap, mProject, mRate, mSnapPoints, mSnapTo, mSnapToTime, mTrackExclusions, mTracks, TrackListConstIterator::Next(), NumericConverter::SetFormatName(), NumericConverter::SetSampleRate(), SNAP_OFF, and Track::Wave.

Referenced by SnapManager().

71 {
72  int snapTo = mProject->GetSnapTo();
73  double rate = mProject->GetRate();
75 
76  // No need to reinit if these are still the same
77  if (snapTo == mSnapTo && rate == mRate && format == mFormat)
78  {
79  return;
80  }
81 
82  // Save NEW settings
83  mSnapTo = snapTo;
84  mRate = rate;
85  mFormat = format;
86 
87  mSnapPoints.clear();
88 
89  // Grab time-snapping prefs (unless otherwise requested)
90  mSnapToTime = false;
91 
92  // Look up the format string
93  if (mSnapTo != SNAP_OFF && !mNoTimeSnap)
94  {
95  mSnapToTime = true;
98  }
99 
100  // Add a SnapPoint at t=0
101  mSnapPoints.push_back(SnapPoint{});
102 
104  for (const Track *track = iter.First(); track; track = iter.Next())
105  {
106  if (mTrackExclusions &&
107  mTrackExclusions->end() !=
108  std::find(mTrackExclusions->begin(), mTrackExclusions->end(), track))
109  {
110  continue;
111  }
112 
113  if (track->GetKind() == Track::Label)
114  {
115  LabelTrack *labelTrack = (LabelTrack *)track;
116  for (int i = 0, cnt = labelTrack->GetNumLabels(); i < cnt; ++i)
117  {
118  const LabelStruct *label = labelTrack->GetLabel(i);
119  const double t0 = label->getT0();
120  const double t1 = label->getT1();
121  CondListAdd(t0, labelTrack);
122  if (t1 != t0)
123  {
124  CondListAdd(t1, labelTrack);
125  }
126  }
127  }
128  else if (track->GetKind() == Track::Wave)
129  {
130  auto waveTrack = static_cast<const WaveTrack *>(track);
131  for (const auto &clip: waveTrack->GetClips())
132  {
133  if (mClipExclusions)
134  {
135  bool skip = false;
136  for (size_t j = 0, cnt = mClipExclusions->size(); j < cnt; ++j)
137  {
138  if ((*mClipExclusions)[j].track == waveTrack &&
139  (*mClipExclusions)[j].clip == clip.get())
140  {
141  skip = true;
142  break;
143  }
144  }
145 
146  if (skip)
147  {
148  continue;
149  }
150  }
151 
152  CondListAdd(clip->GetStartTime(), waveTrack);
153  CondListAdd(clip->GetEndTime(), waveTrack);
154  }
155  }
156 #ifdef USE_MIDI
157  else if (track->GetKind() == Track::Note)
158  {
159  CondListAdd(track->GetStartTime(), track);
160  CondListAdd(track->GetEndTime(), track);
161  }
162 #endif
163  }
164 
165  // Sort all by time
166  std::sort(mSnapPoints.begin(), mSnapPoints.end());
167 }
Definition: Snap.h:59
const TrackList * mTracks
Definition: Snap.h:124
bool mSnapToTime
Definition: Snap.h:136
wxString label
Definition: Tags.cpp:733
int GetNumLabels() const
void SetFormatName(const NumericFormatId &formatName)
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:113
A LabelStruct holds information for ONE label in a LabelTrack.
Definition: LabelTrack.h:44
int format
Definition: ExportPCM.cpp:56
const AudacityProject * mProject
Definition: Snap.h:123
SnapPointArray mSnapPoints
Definition: Snap.h:132
const LabelStruct * GetLabel(int index) const
A Track that contains audio waveform data.
Definition: WaveTrack.h:60
Fundamental data object of Audacity, placed in the TrackPanel. Classes derived form it include the Wa...
Definition: Track.h:102
const TrackArray * mTrackExclusions
Definition: Snap.h:126
double mRate
Definition: Snap.h:139
int GetSnapTo() const
Definition: Project.cpp:5653
const TrackClipArray * mClipExclusions
Definition: Snap.h:125
double getT1() const
Definition: LabelTrack.h:62
Definition: Snap.h:52
const NumericFormatId & GetSelectionFormat() const
Definition: Project.cpp:1685
void CondListAdd(double t, const Track *track)
Definition: Snap.cpp:170
double GetRate() const
Definition: Project.h:199
int mSnapTo
Definition: Snap.h:138
NumericFormatId mFormat
Definition: Snap.h:140
double getT0() const
Definition: LabelTrack.h:61
bool mNoTimeSnap
Definition: Snap.h:129
void SetSampleRate(double sampleRate)
NumericConverter mConverter
Definition: Snap.h:135
SnapResults SnapManager::Snap ( Track currentTrack,
double  t,
bool  rightEdge 
)

Definition at line 322 of file Snap.cpp.

References GetActiveProject(), SnapResults::outCoord, SnapResults::outTime, SNAP_NEAREST, SnapResults::Snapped(), SnapResults::snappedPoint, SnapResults::snappedTime, and SnapResults::timeSnappedTime.

323 {
324 
325  SnapResults results;
326  // Check to see if we need to reinitialize
327  Reinit();
328 
329  results.timeSnappedTime = results.outTime = t;
330  results.outCoord = mZoomInfo->TimeToPosition(t);
331 
332  // First snap to points in mSnapPoints
333  results.snappedPoint =
334  SnapToPoints(currentTrack, t, rightEdge, &results.outTime);
335 
336  if (mSnapToTime) {
337  // Find where it would snap time to the grid
340  results.timeSnappedTime = mConverter.GetValue();
341  }
342 
343  results.snappedTime = false;
344  if (mSnapToTime)
345  {
346  if (results.snappedPoint)
347  {
348  // Since mSnapPoints only contains points on the grid, we're done
349  results.snappedTime = true;
350  }
351  else
352  {
353  results.outTime = results.timeSnappedTime;
354  results.snappedTime = true;
355  }
356  }
357 
358  if (results.Snapped())
359  results.outCoord = mZoomInfo->TimeToPosition(results.outTime);
360 
361  return results;
362 }
bool mSnapToTime
Definition: Snap.h:136
bool Snapped() const
Definition: Snap.h:81
bool snappedPoint
Definition: Snap.h:78
bool SnapToPoints(Track *currentTrack, double t, bool rightEdge, double *outT)
Definition: Snap.cpp:240
virtual void ControlsToValue()
const ZoomInfo * mZoomInfo
Definition: Snap.h:127
wxInt64 outCoord
Definition: Snap.h:77
void Reinit()
Definition: Snap.cpp:70
wxInt64 TimeToPosition(double time, wxInt64 origin=0, bool ignoreFisheye=false) const
STM: Converts a project time to screen x position.
Definition: ViewInfo.cpp:59
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:308
bool snappedTime
Definition: Snap.h:79
double outTime
Definition: Snap.h:76
virtual void ValueToControls()
NumericConverter mConverter
Definition: Snap.h:135
double timeSnappedTime
Definition: Snap.h:75
bool SnapManager::SnapToPoints ( Track currentTrack,
double  t,
bool  rightEdge,
double *  outT 
)
private

Definition at line 240 of file Snap.cpp.

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

244 {
245  *outT = t;
246 
247  size_t cnt = mSnapPoints.size();
248  if (cnt == 0)
249  {
250  return false;
251  }
252 
253  // Find the nearest SnapPoint
254  size_t index = Find(t);
255 
256  // If it's too far away, just give up now
257  if (PixelDiff(t, index) >= mPixelTolerance)
258  {
259  return false;
260  }
261 
262  // Otherwise, search left and right for all of the points
263  // within the allowed range.
264  size_t left = index;
265  size_t right = index;
266  size_t i;
267 
268  while (left > 0 && PixelDiff(t, left - 1) < mPixelTolerance)
269  {
270  left--;
271  }
272 
273  while (right < cnt - 1 && PixelDiff(t, right + 1) < mPixelTolerance)
274  {
275  right++;
276  }
277 
278  if (left == index && right == index)
279  {
280  // Awesome, there's only one point that matches!
281  *outT = Get(index);
282  return true;
283  }
284 
285  size_t indexInThisTrack = 0;
286  size_t countInThisTrack = 0;
287  for (i = left; i <= right; ++i)
288  {
289  if (mSnapPoints[i].track == currentTrack)
290  {
291  indexInThisTrack = i;
292  countInThisTrack++;
293  }
294  }
295 
296  if (countInThisTrack == 1)
297  {
298  // Cool, only one of the points is in the same track, so
299  // we'll use that one.
300  *outT = Get(indexInThisTrack);
301  return true;
302  }
303 
304  if (Get(right) - Get(left) < mEpsilon)
305  {
306  // OK, they're basically the same point
307  if (rightEdge)
308  {
309  *outT = Get(right); // Return rightmost
310  }
311  else {
312  *outT = Get(left); // Return leftmost
313  }
314  return true;
315  }
316 
317  // None of the points matched, bummer.
318  return false;
319 }
double mEpsilon
Definition: Snap.h:131
double Get(size_t index)
Definition: Snap.cpp:184
wxInt64 PixelDiff(double t, size_t index)
Definition: Snap.cpp:190
SnapPointArray mSnapPoints
Definition: Snap.h:132
int mPixelTolerance
Definition: Snap.h:128
size_t Find(double t, size_t i0, size_t i1)
Definition: Snap.cpp:198

Member Data Documentation

const TrackClipArray* SnapManager::mClipExclusions
private

Definition at line 125 of file Snap.h.

Referenced by Reinit(), and SnapManager().

NumericConverter SnapManager::mConverter
private

Definition at line 135 of file Snap.h.

Referenced by CondListAdd(), and Reinit().

double SnapManager::mEpsilon
private

Definition at line 131 of file Snap.h.

Referenced by SnapManager(), and SnapToPoints().

NumericFormatId SnapManager::mFormat
private

Definition at line 140 of file Snap.h.

Referenced by Reinit(), and SnapManager().

bool SnapManager::mNoTimeSnap
private

Definition at line 129 of file Snap.h.

Referenced by Reinit(), and SnapManager().

int SnapManager::mPixelTolerance
private

Definition at line 128 of file Snap.h.

Referenced by SnapManager(), and SnapToPoints().

const AudacityProject* SnapManager::mProject
private

Definition at line 123 of file Snap.h.

Referenced by Reinit(), and SnapManager().

double SnapManager::mRate
private

Definition at line 139 of file Snap.h.

Referenced by Reinit(), and SnapManager().

SnapPointArray SnapManager::mSnapPoints
private

Definition at line 132 of file Snap.h.

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

int SnapManager::mSnapTo
private

Definition at line 138 of file Snap.h.

Referenced by Reinit(), and SnapManager().

bool SnapManager::mSnapToTime
private

Definition at line 136 of file Snap.h.

Referenced by CondListAdd(), and Reinit().

const TrackArray* SnapManager::mTrackExclusions
private

Definition at line 126 of file Snap.h.

Referenced by Reinit(), and SnapManager().

const TrackList* SnapManager::mTracks
private

Definition at line 124 of file Snap.h.

Referenced by Reinit(), and SnapManager().

const ZoomInfo* SnapManager::mZoomInfo
private

Definition at line 127 of file Snap.h.

Referenced by PixelDiff(), and SnapManager().


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