Audacity  2.2.2
SampleHandle.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 SampleHandle.cpp
6 
7 Paul Licameli split from TrackPanel.cpp
8 
9 **********************************************************************/
10 
11 #include "../../../../Audacity.h"
12 #include "SampleHandle.h"
13 #include "../../../../Experimental.h"
14 
15 #include <algorithm>
16 #include "../../../../MemoryX.h"
17 #include <wx/gdicmn.h>
18 
19 #include "../../../../Envelope.h"
20 #include "../../../../HitTestResult.h"
21 #include "../../../../prefs/WaveformSettings.h"
22 #include "../../../../Project.h"
23 #include "../../../../RefreshCode.h"
24 #include "../../../../TrackArtist.h"
25 #include "../../../../TrackPanelMouseEvent.h"
26 #include "../../../../UndoManager.h"
27 #include "../../../../ViewInfo.h"
28 #include "../../../../WaveTrack.h"
29 #include "../../../../../images/Cursors.h"
30 #include "../../../../widgets/ErrorDialog.h"
31 
32 
33 static const int SMOOTHING_KERNEL_RADIUS = 3;
34 static const int SMOOTHING_BRUSH_RADIUS = 5;
35 static const double SMOOTHING_PROPORTION_MAX = 0.7;
36 static const double SMOOTHING_PROPORTION_MIN = 0.0;
37 
38 SampleHandle::SampleHandle( const std::shared_ptr<WaveTrack> &pTrack )
39  : mClickedTrack{ pTrack }
40 {
41 }
42 
44 {
45 #ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
47 #endif
48 }
49 
51 (const wxMouseState &state, const AudacityProject *WXUNUSED(pProject), bool unsafe)
52 {
53  static auto disabledCursor =
54  ::MakeCursor(wxCURSOR_NO_ENTRY, DisabledCursorXpm, 16, 16);
55  static wxCursor smoothCursor{ wxCURSOR_SPRAYCAN };
56  static auto pencilCursor =
57  ::MakeCursor(wxCURSOR_PENCIL, DrawCursorXpm, 12, 22);
58 
59  // TODO: message should also mention the brush. Describing the modifier key
60  // (alt, or other) varies with operating system.
61  auto message = _("Click and drag to edit the samples");
62 
63  return {
64  message,
65  (unsafe
66  ? &*disabledCursor
67  : (state.AltDown()
68  ? &smoothCursor
69  : &*pencilCursor))
70  };
71 }
72 
74 (std::weak_ptr<SampleHandle> &holder,
75  const wxMouseState &WXUNUSED(state), const std::shared_ptr<WaveTrack> &pTrack)
76 {
77  auto result = std::make_shared<SampleHandle>( pTrack );
78  result = AssignUIHandlePtr(holder, result);
79  return result;
80 }
81 
82 namespace {
83  inline double adjustTime(const WaveTrack *wt, double time)
84  {
85  // Round to an exact sample time
86  return wt->LongSamplesToTime(wt->TimeToLongSamples(time));
87  }
88 
89  // Is the sample horizontally nearest to the cursor sufficiently separated
90  // from its neighbors that the pencil tool should be allowed to drag it?
91  bool SampleResolutionTest
92  ( const ViewInfo &viewInfo, const WaveTrack *wt, double time, int width )
93  {
94  // Require more than 3 pixels per sample
95  const wxInt64 xx = std::max(wxInt64(0), viewInfo.TimeToPosition(time));
96  ZoomInfo::Intervals intervals;
97  const double rate = wt->GetRate();
98  viewInfo.FindIntervals(rate, intervals, width);
99  ZoomInfo::Intervals::const_iterator it = intervals.begin(),
100  end = intervals.end(), prev;
101  wxASSERT(it != end && it->position == 0);
102  do
103  prev = it++;
104  while (it != end && it->position <= xx);
105  const double threshold = 3 * rate;
106  // three times as many pixels per second, as samples
107  return prev->averageZoom > threshold;
108  }
109 }
110 
112 (std::weak_ptr<SampleHandle> &holder,
113  const wxMouseState &state, const wxRect &rect,
114  const AudacityProject *pProject, const std::shared_ptr<WaveTrack> &pTrack)
115 {
116  const ViewInfo &viewInfo = pProject->GetViewInfo();
117 
118  WaveTrack *wavetrack = pTrack.get();
119 
120  const int displayType = wavetrack->GetDisplay();
121  if (WaveTrack::Waveform != displayType)
122  return {}; // Not a wave, so return.
123 
124  const double tt =
125  adjustTime(wavetrack, viewInfo.PositionToTime(state.m_x, rect.x));
126  if (!SampleResolutionTest(viewInfo, wavetrack, tt, rect.width))
127  return {};
128 
129  // Just get one sample.
130  float oneSample;
131  const double rate = wavetrack->GetRate();
132  const auto s0 = (sampleCount)(tt * rate + 0.5);
133  if (! wavetrack->Get((samplePtr)&oneSample, floatSample, s0, 1, fillZero,
134  // Do not propagate exception but return a failure value
135  false) )
136  return {};
137 
138  // Get y distance of envelope point from center line (in pixels).
139  float zoomMin, zoomMax;
140 
141  wavetrack->GetDisplayBounds(&zoomMin, &zoomMax);
142 
143  double envValue = 1.0;
144  Envelope* env = wavetrack->GetEnvelopeAtX(state.GetX());
145  if (env)
146  // Calculate sample as it would be rendered, so quantize time
147  envValue = env->GetValue( tt, 1.0 / wavetrack->GetRate() );
148 
149  const bool dB = !wavetrack->GetWaveformSettings().isLinear();
150  int yValue = GetWaveYPos(oneSample * envValue,
151  zoomMin, zoomMax,
152  rect.height, dB, true,
153  wavetrack->GetWaveformSettings().dBRange, false) + rect.y;
154 
155  // Get y position of mouse (in pixels)
156  int yMouse = state.m_y;
157 
158  // Perhaps yTolerance should be put into preferences?
159  const int yTolerance = 10; // More tolerance on samples than on envelope.
160  if (abs(yValue - yMouse) >= yTolerance)
161  return {};
162 
163  return HitAnywhere(holder, state, pTrack);
164 }
165 
167 {
168 }
169 
170 namespace {
174  bool IsSampleEditingPossible
175  (const wxMouseEvent &event,
176  const wxRect &rect, const ViewInfo &viewInfo, Track *pTrack, int width)
177  {
178  if (pTrack->GetKind() != Track::Wave)
179  return false;
180  WaveTrack *wt = static_cast<WaveTrack*>(pTrack);
181 
182  //Get out of here if we shouldn't be drawing right now:
183  //If we aren't displaying the waveform, Display a message dialog
184  const int display = wt->GetDisplay();
185  if (WaveTrack::Waveform != display)
186  {
188 "To use Draw, choose 'Waveform' or 'Waveform (dB)' in the Track Dropdown Menu."),
189  _("Draw Tool"));
190  return false;
191  }
192 
193  //If we aren't zoomed in far enough, show a message dialog.
194  const double time = adjustTime(wt, viewInfo.PositionToTime(event.m_x, rect.x));
195  if (!SampleResolutionTest(viewInfo, wt, time, width))
196  {
198 "To use Draw, zoom in further until you can see the individual samples."),
199  _("Draw Tool"));
200  return false;
201  }
202  return true;
203  }
204 }
205 
207 (const TrackPanelMouseEvent &evt, AudacityProject *pProject)
208 {
209  using namespace RefreshCode;
210  const bool unsafe = pProject->IsAudioActive();
211  if ( unsafe )
212  return Cancelled;
213 
214  const wxMouseEvent &event = evt.event;
215  const wxRect &rect = evt.rect;
216  const ViewInfo &viewInfo = pProject->GetViewInfo();
217  const auto pTrack = mClickedTrack.get();
218 
220  if (!IsSampleEditingPossible(
221  event, rect, viewInfo, pTrack, rect.width))
222  return Cancelled;
223 
225  mRect = rect;
226 
227  //If we are still around, we are drawing in earnest. Set some member data structures up:
228  //First, calculate the starting sample. To get this, we need the time
229  const double t0 =
230  adjustTime(mClickedTrack.get(), viewInfo.PositionToTime(event.m_x, rect.x));
231 
232  //convert t0 to samples
233  mClickedStartSample = mClickedTrack->TimeToLongSamples(t0);
234 
235  //Determine how drawing should occur. If alt is down,
236  //do a smoothing, instead of redrawing.
237  if (event.m_altDown)
238  {
239  mAltKey = true;
240  //*************************************************
241  //*** ALT-DOWN-CLICK (SAMPLE SMOOTHING) ***
242  //*************************************************
243  //
244  // Smoothing works like this: There is a smoothing kernel radius constant that
245  // determines how wide the averaging window is. Plus, there is a smoothing brush radius,
246  // which determines how many pixels wide around the selected pixel this smoothing is applied.
247  //
248  // Samples will be replaced by a mixture of the original points and the smoothed points,
249  // with a triangular mixing probability whose value at the center point is
250  // SMOOTHING_PROPORTION_MAX and at the far bounds is SMOOTHING_PROPORTION_MIN
251 
252  //Get the region of samples around the selected point
253  size_t sampleRegionSize = 1 + 2 * (SMOOTHING_KERNEL_RADIUS + SMOOTHING_BRUSH_RADIUS);
254  Floats sampleRegion{ sampleRegionSize };
255  Floats newSampleRegion{ 1 + 2 * (size_t)SMOOTHING_BRUSH_RADIUS };
256 
257  //Get a sample from the track to do some tricks on.
258  mClickedTrack->Get((samplePtr)sampleRegion.get(), floatSample,
259  mClickedStartSample - SMOOTHING_KERNEL_RADIUS - SMOOTHING_BRUSH_RADIUS,
260  sampleRegionSize);
261 
262  //Go through each point of the smoothing brush and apply a smoothing operation.
263  for (auto jj = -SMOOTHING_BRUSH_RADIUS; jj <= SMOOTHING_BRUSH_RADIUS; ++jj) {
264  float sumOfSamples = 0;
265  for (auto ii = -SMOOTHING_KERNEL_RADIUS; ii <= SMOOTHING_KERNEL_RADIUS; ++ii) {
266  //Go through each point of the smoothing kernel and find the average
267 
268  //The average is a weighted average, scaled by a weighting kernel that is simply triangular
269  // A triangular kernel across N items, with a radius of R ( 2 R + 1 points), if the farthest:
270  // points have a probability of a, the entire triangle has total probability of (R + 1)^2.
271  // For sample number ii and middle brush sample M, (R + 1 - abs(M-ii))/ ((R+1)^2) gives a
272  // legal distribution whose total probability is 1.
273  //
274  //
275  // weighting factor value
276  sumOfSamples +=
277  (SMOOTHING_KERNEL_RADIUS + 1 - abs(ii)) *
278  sampleRegion[ii + jj + SMOOTHING_KERNEL_RADIUS + SMOOTHING_BRUSH_RADIUS];
279 
280  }
281  newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] =
282  sumOfSamples /
284  }
285 
286 
287  // Now that the NEW sample levels are determined, go through each and mix it appropriately
288  // with the original point, according to a 2-part linear function whose center has probability
289  // SMOOTHING_PROPORTION_MAX and extends out SMOOTHING_BRUSH_RADIUS, at which the probability is
290  // SMOOTHING_PROPORTION_MIN. _MIN and _MAX specify how much of the smoothed curve make it through.
291 
292  float prob;
293 
294  for (auto jj = -SMOOTHING_BRUSH_RADIUS; jj <= SMOOTHING_BRUSH_RADIUS; ++jj) {
295 
296  prob =
298  (float)abs(jj) / SMOOTHING_BRUSH_RADIUS *
300 
301  newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] =
302  newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] * prob +
303  sampleRegion[SMOOTHING_BRUSH_RADIUS + SMOOTHING_KERNEL_RADIUS + jj] *
304  (1 - prob);
305  }
306  //Set the sample to the point of the mouse event
307  mClickedTrack->Set((samplePtr)newSampleRegion.get(), floatSample,
308  mClickedStartSample - SMOOTHING_BRUSH_RADIUS, 1 + 2 * SMOOTHING_BRUSH_RADIUS);
309 
310  // mLastDragSampleValue will not be used
311  }
312  else
313  {
314  mAltKey = false;
315  //*************************************************
316  //*** PLAIN DOWN-CLICK (NORMAL DRAWING) ***
317  //*************************************************
318 
319  //Otherwise (e.g., the alt button is not down) do normal redrawing, based on the mouse position.
320  const float newLevel = FindSampleEditingLevel(event, viewInfo, t0);
321 
322  //Set the sample to the point of the mouse event
323  mClickedTrack->Set((samplePtr)&newLevel, floatSample, mClickedStartSample, 1);
324 
325  mLastDragSampleValue = newLevel;
326  }
327 
328  //Set the member data structures for drawing
329  mLastDragSample = mClickedStartSample;
330 
331  // Sample data changed on either branch, so refresh the track display.
332  return RefreshCell;
333 }
334 
336 (const TrackPanelMouseEvent &evt, AudacityProject *pProject)
337 {
338  using namespace RefreshCode;
339  const wxMouseEvent &event = evt.event;
340  const ViewInfo &viewInfo = pProject->GetViewInfo();
341 
342  const bool unsafe = pProject->IsAudioActive();
343  if (unsafe) {
344  this->Cancel(pProject);
345  return RefreshCell | Cancelled;
346  }
347 
348  //*************************************************
349  //*** DRAG-DRAWING ***
350  //*************************************************
351 
352  //No dragging effects if the alt key is down--
353  //Don't allow left-right dragging for smoothing operation
354  if (mAltKey)
355  return RefreshNone;
356 
357  sampleCount s0; //declare this for use below. It designates which sample number to draw.
358 
359  // Figure out what time the click was at
360  //Find the point that we want to redraw at. If the control button is down,
361  //adjust only the originally clicked-on sample
362 
363  if (event.m_controlDown) {
364  //*************************************************
365  //*** CTRL-DOWN (Hold Initial Sample Constant ***
366  //*************************************************
367 
368  s0 = mClickedStartSample;
369  }
370  else {
371  //*************************************************
372  //*** Normal CLICK-drag (Normal drawing) ***
373  //*************************************************
374 
375  //Otherwise, adjust the sample you are dragging over right now.
376  //convert this to samples
377  const double tt = viewInfo.PositionToTime(event.m_x, mRect.x);
378  s0 = mClickedTrack->TimeToLongSamples(tt);
379  }
380 
381  const double t0 = mClickedTrack->LongSamplesToTime(s0);
382 
383  // Do redrawing, based on the mouse position.
384  // Calculate where the mouse is located vertically (between +/- 1)
385 
386  const float newLevel = FindSampleEditingLevel(event, viewInfo, t0);
387 
388  //Now, redraw all samples between current and last redrawn sample, inclusive
389  //Go from the smaller to larger sample.
390  const auto start = std::min(s0, mLastDragSample);
391  const auto end = std::max(s0, mLastDragSample);
392  // Few enough samples to be drawn individually on screen will not
393  // overflow size_t:
394  const auto size = ( end - start + 1 ).as_size_t();
395  if (size == 1) {
396  mClickedTrack->Set((samplePtr)&newLevel, floatSample, start, size);
397  }
398  else {
399  std::vector<float> values(size);
400  for (auto ii = start; ii <= end; ++ii) {
401  //This interpolates each sample linearly:
402  // i - start will not overflow size_t either:
403  values[( ii - start ).as_size_t()] =
404  mLastDragSampleValue + (newLevel - mLastDragSampleValue) *
405  (ii - mLastDragSample).as_float() /
406  (s0 - mLastDragSample).as_float();
407  }
408  mClickedTrack->Set((samplePtr)&values[0], floatSample, start, size);
409  }
410 
411  //Update the member data structures.
412  mLastDragSample = s0;
413  mLastDragSampleValue = newLevel;
414 
415  return RefreshCell;
416 }
417 
419 (const TrackPanelMouseState &st, const AudacityProject *pProject)
420 {
421  const bool unsafe = pProject->IsAudioActive();
422  return HitPreview(st.state, pProject, unsafe);
423 }
424 
427  wxWindow *)
428 {
429  const bool unsafe = pProject->IsAudioActive();
430  if (unsafe)
431  return this->Cancel(pProject);
432 
433  //*************************************************
434  //*** UP-CLICK (Finish drawing) ***
435  //*************************************************
436  //On up-click, send the state to the undo stack
437  mClickedTrack.reset(); //Set this to NULL so it will catch improper drag events.
438  pProject->PushState(_("Moved Samples"),
439  _("Sample Edit"),
441 
442  // No change to draw since last drag
444 }
445 
447 {
448  pProject->RollbackState();
449  mClickedTrack.reset();
451 }
452 
454  (const wxMouseEvent &event, const ViewInfo &, double t0)
455 {
456  // Calculate where the mouse is located vertically (between +/- 1)
457  float zoomMin, zoomMax;
458  mClickedTrack->GetDisplayBounds(&zoomMin, &zoomMax);
459 
460  const int yy = event.m_y - mRect.y;
461  const int height = mRect.GetHeight();
462  const bool dB = !mClickedTrack->GetWaveformSettings().isLinear();
463  float newLevel =
464  ::ValueOfPixel(yy, height, false, dB,
465  mClickedTrack->GetWaveformSettings().dBRange, zoomMin, zoomMax);
466 
467  //Take the envelope into account
468  Envelope *const env = mClickedTrack->GetEnvelopeAtX(event.m_x);
469  if (env)
470  {
471  // Calculate sample as it would be rendered, so quantize time
472  double envValue = env->GetValue( t0, 1.0 / mClickedTrack->GetRate());
473  if (envValue > 0)
474  newLevel /= envValue;
475  else
476  newLevel = 0;
477 
478  //Make sure the NEW level is between +/-1
479  newLevel = std::max(-1.0f, std::min(1.0f, newLevel));
480  }
481 
482  return newLevel;
483 }
bool isLinear() const
ViewInfo is used mainly to hold the zooming, selection and scroll information. It also has some statu...
Definition: ViewInfo.h:141
Result Cancel(AudacityProject *pProject) override
HitTestPreview Preview(const TrackPanelMouseState &state, const AudacityProject *pProject) override
const WaveformSettings & GetWaveformSettings() const
Definition: WaveTrack.cpp:738
Envelope * GetEnvelopeAtX(int xcoord)
Definition: WaveTrack.cpp:2211
Result Release(const TrackPanelMouseEvent &event, AudacityProject *pProject, wxWindow *pParent) override
double PositionToTime(wxInt64 position, wxInt64 origin=0, bool ignoreFisheye=false) const
Definition: ViewInfo.cpp:49
Draggable curve used in TrackPanel for varying amplification.
Definition: Envelope.h:77
int AudacityMessageBox(const wxString &message, const wxString &caption=AudacityMessageBoxCaptionStr(), long style=wxOK|wxCENTRE, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord)
Definition: ErrorDialog.h:92
static const int SMOOTHING_KERNEL_RADIUS
static const double SMOOTHING_PROPORTION_MAX
Result Click(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
void FindIntervals(double rate, Intervals &results, wxInt64 width, wxInt64 origin=0) const
Definition: ViewInfo.cpp:118
float ValueOfPixel(int yy, int height, bool offset, bool dB, double dBRange, float zoomMin, float zoomMax)
std::vector< Interval > Intervals
Definition: ViewInfo.h:106
Result Drag(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
void Enter(bool forward) override
virtual int GetKind() const
Definition: Track.h:329
static const double SMOOTHING_PROPORTION_MIN
Result mChangeHighlight
Definition: UIHandle.h:150
void GetDisplayBounds(float *min, float *max) const
Definition: WaveTrack.cpp:325
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:176
unsigned Result
Definition: UIHandle.h:37
static UIHandlePtr HitAnywhere(std::weak_ptr< SampleHandle > &holder, const wxMouseState &state, const std::shared_ptr< WaveTrack > &pTrack)
void RollbackState()
Definition: Project.cpp:4735
static const int SMOOTHING_BRUSH_RADIUS
char * samplePtr
Definition: Types.h:203
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:101
static HitTestPreview HitPreview(const wxMouseState &state, const AudacityProject *pProject, bool unsafe)
wxInt64 TimeToPosition(double time, wxInt64 origin=0, bool ignoreFisheye=false) const
STM: Converts a project time to screen x position.
Definition: ViewInfo.cpp:59
if(pTrack &&pTrack->GetDisplay()!=WaveTrack::Spectrum)
int min(int a, int b)
SampleHandle(const SampleHandle &)=delete
double GetValue(double t, double sampleDur=0) const
Get envelope value at time t.
Definition: Envelope.cpp:1114
bool IsAudioActive() const
Definition: Project.cpp:1447
std::shared_ptr< UIHandle > UIHandlePtr
Definition: TrackPanel.h:59
_("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
void PushState(const wxString &desc, const wxString &shortDesc)
Definition: Project.cpp:4695
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: WaveTrack.cpp:1843
static UIHandlePtr HitTest(std::weak_ptr< SampleHandle > &holder, const wxMouseState &state, const wxRect &rect, const AudacityProject *pProject, const std::shared_ptr< WaveTrack > &pTrack)
std::unique_ptr< wxCursor > MakeCursor(int WXUNUSED(CursorId), const char *const pXpm[36], int HotX, int HotY)
Definition: TrackPanel.cpp:274
double GetRate() const
Definition: WaveTrack.cpp:398
bool Get(samplePtr buffer, sampleFormat format, sampleCount start, size_t len, fillFormat fill=fillZero, bool mayThrow=true, sampleCount *pNumCopied=nullptr) const
Definition: WaveTrack.cpp:1971
WaveTrackDisplay GetDisplay() const
Definition: WaveTrack.h:593
double LongSamplesToTime(sampleCount pos) const
Convert correctly between an number of samples and an (absolute) time in seconds. ...
Definition: WaveTrack.cpp:1848
int GetWaveYPos(float value, float min, float max, int height, bool dB, bool outer, float dBr, bool clip)
const ViewInfo & GetViewInfo() const
Definition: Project.h:207
std::shared_ptr< Subclass > AssignUIHandlePtr(std::weak_ptr< Subclass > &holder, const std::shared_ptr< Subclass > &pNew)
Definition: UIHandle.h:162
float FindSampleEditingLevel(const wxMouseEvent &event, const ViewInfo &viewInfo, double t0)
virtual ~SampleHandle()