Debian Codegraph @

src/tracks/playabletrack/wavetrack/ui/SampleHandle.cpp

raw
1 /**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 SampleHandle.cpp
6
7 Paul Licameli split from TrackPanel.cpp
8
9 **********************************************************************/
10
11
12 #include "SampleHandle.h"
13
14 #include <algorithm>
15 #include <wx/gdicmn.h>
16
17 #include "Envelope.h"
18 #include "../../../../HitTestResult.h"
19 #include "WaveformSettings.h"
20 #include "ProjectAudioIO.h"
21 #include "ProjectHistory.h"
22 #include "../../../../RefreshCode.h"
23 #include "../../../../TrackArt.h"
24 #include "../../../../TrackArtist.h"
25 #include "../../../../TrackPanelMouseEvent.h"
26 #include "UndoManager.h"
27 #include "ViewInfo.h"
28 #include "WaveChannelUtilities.h"
29 #include "WaveClip.h"
30 #include "WaveTrack.h"
31 #include "../../../../../images/Cursors.h"
32 #include "AudacityMessageBox.h"
33 #include "prefs/WaveformScale.h"
34
35
36 static const int SMOOTHING_KERNEL_RADIUS = 3;
37 static const int SMOOTHING_BRUSH_RADIUS = 5;
38 static const double SMOOTHING_PROPORTION_MAX = 0.7;
39 static const double SMOOTHING_PROPORTION_MIN = 0.0;
40
41 SampleHandle::SampleHandle(const std::shared_ptr<WaveChannel> &pTrack)
42 : mClickedTrack{ pTrack }
43 {
44 }
45
46 void SampleHandle::Enter(bool, AudacityProject *)
47 {
48 #ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
49 mChangeHighlight = RefreshCode::RefreshCell;
50 #endif
51 }
52
53 HitTestPreview SampleHandle::HitPreview
54 (const wxMouseState &state, const AudacityProject *WXUNUSED(pProject), bool unsafe)
55 {
56 static auto disabledCursor =
57 ::MakeCursor(wxCURSOR_NO_ENTRY, DisabledCursorXpm, 16, 16);
58 static wxCursor smoothCursor{ wxCURSOR_SPRAYCAN };
59 static auto pencilCursor =
60 ::MakeCursor(wxCURSOR_PENCIL, DrawCursorXpm, 12, 22);
61
62 // TODO: message should also mention the brush. Describing the modifier key
63 // (alt, or other) varies with operating system.
64 auto message = XO("Click and drag to edit the samples");
65
66 return {
67 message,
68 (unsafe
69 ? &*disabledCursor
70 : (state.AltDown()
71 ? &smoothCursor
72 : &*pencilCursor))
73 };
74 }
75
76 UIHandlePtr SampleHandle::HitAnywhere(std::weak_ptr<SampleHandle> &holder,
77 const wxMouseState &, const std::shared_ptr<WaveChannel> &pChannel)
78 {
79 auto result = std::make_shared<SampleHandle>(pChannel);
80 result = AssignUIHandlePtr(holder, result);
81 return result;
82 }
83
84 namespace {
85 inline double adjustTime(const WaveChannel& wt, double time)
86 {
87 // Round to an exact sample time
88 const auto clip = WaveChannelUtilities::GetClipAtTime(wt, time);
89 if (!clip)
90 return wt.SnapToSample(time);
91 const auto sampleOffset =
92 clip->TimeToSamples(time - clip->GetPlayStartTime());
93 return clip->SamplesToTime(sampleOffset) + clip->GetPlayStartTime();
94 }
95
96 // Is the sample horizontally nearest to the cursor sufficiently separated
97 // from its neighbors that the pencil tool should be allowed to drag it?
98 bool SampleResolutionTest(
99 const ViewInfo& viewInfo, const WaveChannelInterval& clip,
100 const ZoomInfo::Intervals& intervals)
101 {
102 // Require more than 3 pixels per sample
103 const auto xx = std::max<ZoomInfo::int64>(
104 0, viewInfo.TimeToPosition(clip.GetPlayStartTime()));
105 const double rate = clip.GetRate() / clip.GetStretchRatio();
106 ZoomInfo::Intervals::const_iterator it = intervals.begin(),
107 end = intervals.end(), prev;
108 wxASSERT(it != end && it->position == 0);
109 do
110 prev = it++;
111 while (it != end && it->position <= xx);
112 const double threshold = 3 * rate;
113 // three times as many pixels per second, as samples
114 return prev->averageZoom > threshold;
115 }
116 }
117
118 UIHandlePtr SampleHandle::HitTest(std::weak_ptr<SampleHandle> &holder,
119 const wxMouseState &state, const wxRect &rect,
120 const AudacityProject *pProject, const std::shared_ptr<WaveChannel> &pChannel)
121 {
122 using namespace WaveChannelUtilities;
123 const auto &viewInfo = ViewInfo::Get( *pProject );
124
125 /// method that tells us if the mouse event landed on an
126 /// editable sample
127 if (!pChannel)
128 return {};
129 auto &waveChannel = *pChannel;
130 const auto time = viewInfo.PositionToTime(state.m_x, rect.x);
131 const auto clickedClip =
132 WaveChannelUtilities::GetClipAtTime(waveChannel, time);
133 if (!clickedClip)
134 return {};
135
136 const double tt = adjustTime(waveChannel, time);
137 const auto intervals = viewInfo.FindIntervals(rect.width);
138 if (!SampleResolutionTest(viewInfo, *clickedClip, intervals))
139 return {};
140
141 // Just get one sample.
142 float oneSample;
143 constexpr auto mayThrow = false;
144 if (!WaveChannelUtilities::GetFloatAtTime(waveChannel,
145 tt, oneSample, mayThrow))
146 return {};
147
148 // Get y distance of envelope point from center line (in pixels).
149 auto &cache = WaveformScale::Get(waveChannel);
150 float zoomMin, zoomMax;
151 cache.GetDisplayBounds(zoomMin, zoomMax);
152
153 double envValue = 1.0;
154 if (const auto env =
155 WaveChannelUtilities::GetEnvelopeAtTime(waveChannel, time))
156 // Calculate sample as it would be rendered
157 envValue = env->GetValue(tt);
158
159 auto &settings = WaveformSettings::Get(waveChannel);
160 const bool dB = !settings.isLinear();
161 int yValue = GetWaveYPos(oneSample * envValue,
162 zoomMin, zoomMax,
163 rect.height, dB, true,
164 settings.dBRange, false) + rect.y;
165
166 // Get y position of mouse (in pixels)
167 int yMouse = state.m_y;
168
169 // Perhaps yTolerance should be put into preferences?
170 const int yTolerance = 10; // More tolerance on samples than on envelope.
171 if (abs(yValue - yMouse) >= yTolerance)
172 return {};
173
174 return HitAnywhere(holder, state, pChannel);
175 }
176
177 SampleHandle::~SampleHandle()
178 {
179 }
180
181 std::shared_ptr<const Track> SampleHandle::FindTrack() const
182 {
183 return TrackFromChannel(mClickedTrack);
184 }
185
186 UIHandle::Result SampleHandle::Click
187 (const TrackPanelMouseEvent &evt, AudacityProject *pProject)
188 {
189 using namespace RefreshCode;
190 using namespace WaveChannelUtilities;
191 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
192 if ( unsafe )
193 return Cancelled;
194
195 const wxMouseEvent &event = evt.event;
196 const wxRect &rect = evt.rect;
197 const auto &viewInfo = ViewInfo::Get( *pProject );
198
199 const double t0 = viewInfo.PositionToTime(event.m_x, rect.x);
200 const auto pTrack = mClickedTrack.get();
201 mClickedClip = WaveChannelUtilities::GetClipAtTime(*pTrack, t0);
202 if (!mClickedClip)
203 return Cancelled;
204
205 const auto intervals = viewInfo.FindIntervals(rect.width);
206 /// Someone has just clicked the mouse. What do we do?
207 if (!SampleResolutionTest(viewInfo, *mClickedClip, intervals))
208 {
209 AudacityMessageBox(
210 XO("To use Draw, zoom in further until you can see the individual samples."),
211 XO("Draw Tool"));
212 return Cancelled;
213 }
214
215 /// We're in a track view and zoomed enough to see the samples.
216 mRect = rect;
217
218 //convert t0 to samples
219 mClickedStartPixel = viewInfo.TimeToPosition(t0);
220
221 //Determine how drawing should occur. If alt is down,
222 //do a smoothing, instead of redrawing.
223 if (event.m_altDown)
224 {
225 mAltKey = true;
226 //*************************************************
227 //*** ALT-DOWN-CLICK (SAMPLE SMOOTHING) ***
228 //*************************************************
229 //
230 // Smoothing works like this: There is a smoothing kernel radius constant that
231 // determines how wide the averaging window is. Plus, there is a smoothing brush radius,
232 // which determines how many pixels wide around the selected pixel this smoothing is applied.
233 //
234 // Samples will be replaced by a mixture of the original points and the smoothed points,
235 // with a triangular mixing probability whose value at the center point is
236 // SMOOTHING_PROPORTION_MAX and at the far bounds is SMOOTHING_PROPORTION_MIN
237
238 //Get the region of samples around the selected point
239 size_t sampleRegionSize = 1 + 2 * (SMOOTHING_KERNEL_RADIUS + SMOOTHING_BRUSH_RADIUS);
240 Floats sampleRegion{ sampleRegionSize };
241 Floats newSampleRegion{ 1 + 2 * (size_t)SMOOTHING_BRUSH_RADIUS };
242
243 //Get a sample from the clip to do some tricks on.
244 constexpr auto mayThrow = false;
245 const auto sampleRegionRange = GetFloatsCenteredAroundTime(*mClickedTrack,
246 t0, sampleRegion.get(),
247 SMOOTHING_KERNEL_RADIUS + SMOOTHING_BRUSH_RADIUS, mayThrow);
248
249 //Go through each point of the smoothing brush and apply a smoothing operation.
250 for (auto jj = -SMOOTHING_BRUSH_RADIUS; jj <= SMOOTHING_BRUSH_RADIUS; ++jj) {
251 float sumOfSamples = 0;
252 for (auto ii = -SMOOTHING_KERNEL_RADIUS; ii <= SMOOTHING_KERNEL_RADIUS; ++ii) {
253 //Go through each point of the smoothing kernel and find the average
254 const auto sampleRegionIndex =
255 ii + jj + SMOOTHING_KERNEL_RADIUS + SMOOTHING_BRUSH_RADIUS;
256 const auto inRange = sampleRegionRange.first <= sampleRegionIndex &&
257 sampleRegionIndex < sampleRegionRange.second;
258 if (!inRange)
259 continue;
260 //The average is a weighted average, scaled by a weighting kernel that is simply triangular
261 // A triangular kernel across N items, with a radius of R ( 2 R + 1 points), if the farthest:
262 // points have a probability of a, the entire triangle has total probability of (R + 1)^2.
263 // For sample number ii and middle brush sample M, (R + 1 - abs(M-ii))/ ((R+1)^2) gives a
264 // legal distribution whose total probability is 1.
265 //
266 //
267 // weighting factor value
268 sumOfSamples += (SMOOTHING_KERNEL_RADIUS + 1 - abs(ii)) *
269 sampleRegion[sampleRegionIndex];
270 }
271 newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] =
272 sumOfSamples /
273 ((SMOOTHING_KERNEL_RADIUS + 1) *(SMOOTHING_KERNEL_RADIUS + 1));
274 }
275
276
277 // Now that the NEW sample levels are determined, go through each and mix it appropriately
278 // with the original point, according to a 2-part linear function whose center has probability
279 // SMOOTHING_PROPORTION_MAX and extends out SMOOTHING_BRUSH_RADIUS, at which the probability is
280 // SMOOTHING_PROPORTION_MIN. _MIN and _MAX specify how much of the smoothed curve make it through.
281
282 float prob;
283
284 for (auto jj = -SMOOTHING_BRUSH_RADIUS; jj <= SMOOTHING_BRUSH_RADIUS; ++jj) {
285
286 prob =
287 SMOOTHING_PROPORTION_MAX -
288 (float)abs(jj) / SMOOTHING_BRUSH_RADIUS *
289 (SMOOTHING_PROPORTION_MAX - SMOOTHING_PROPORTION_MIN);
290
291 newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] =
292 newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] * prob +
293 sampleRegion[SMOOTHING_BRUSH_RADIUS + SMOOTHING_KERNEL_RADIUS + jj] *
294 (1 - prob);
295 }
296 // Set a range of samples around the mouse event
297 // Don't require dithering later
298 SetFloatsCenteredAroundTime(*mClickedTrack,
299 t0, newSampleRegion.get(), SMOOTHING_BRUSH_RADIUS,
300 narrowestSampleFormat);
301
302 // mLastDragSampleValue will not be used
303 }
304 else
305 {
306 mAltKey = false;
307 //*************************************************
308 //*** PLAIN DOWN-CLICK (NORMAL DRAWING) ***
309 //*************************************************
310
311 //Otherwise (e.g., the alt button is not down) do normal redrawing, based on the mouse position.
312 const float newLevel = FindSampleEditingLevel(event, viewInfo, t0);
313
314 //Set the sample to the point of the mouse event
315 // Don't require dithering later
316
317 SetFloatAtTime(*mClickedTrack, t0, newLevel, narrowestSampleFormat);
318
319 mLastDragSampleValue = newLevel;
320 }
321
322 //Set the member data structures for drawing
323 mLastDragPixel = mClickedStartPixel;
324
325 // Sample data changed on either branch, so refresh the track display.
326 return RefreshCell;
327 }
328
329 namespace
330 {
331 size_t GetLastEditableClipStartingFromNthClip(size_t n, bool forward,
332 const WaveChannelUtilities::ClipPointers& sortedClips,
333 const ViewInfo& viewInfo, const ZoomInfo::Intervals& intervals)
334 {
335 assert(n < sortedClips.size());
336 const auto increment = forward ? 1 : -1;
337 int last = n + increment;
338 const auto limit = forward ? sortedClips.size() : -1;
339 while (last != limit)
340 {
341 if (!SampleResolutionTest(viewInfo, *sortedClips[last], intervals))
342 break;
343 last += increment;
344 }
345 last -= increment;
346 assert(last >= 0 && last < sortedClips.size());
347 return last;
348 }
349 } // namespace
350
351 UIHandle::Result SampleHandle::Drag
352 (const TrackPanelMouseEvent &evt, AudacityProject *pProject)
353 {
354 using namespace RefreshCode;
355 using namespace WaveChannelUtilities;
356 const wxMouseEvent &event = evt.event;
357 const auto &viewInfo = ViewInfo::Get( *pProject );
358
359 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
360 if (unsafe)
361 {
362 this->Cancel(pProject);
363 return RefreshCell | Cancelled;
364 }
365
366 // There must have been some clicking before dragging ...
367 assert(mClickedTrack && mClickedClip);
368 if (!(mClickedTrack && mClickedClip))
369 return Cancelled;
370
371 //*************************************************
372 //*** DRAG-DRAWING ***
373 //*************************************************
374
375 //No dragging effects if the alt key is down--
376 //Don't allow left-right dragging for smoothing operation
377 if (mAltKey)
378 return RefreshNone;
379
380 const double t0 = viewInfo.PositionToTime(mLastDragPixel);
381 const double t1 = viewInfo.PositionToTime(event.m_x, mRect.x);
382
383 const int x1 =
384 event.m_controlDown ? mClickedStartPixel : viewInfo.TimeToPosition(t1);
385 const float newLevel = FindSampleEditingLevel(event, viewInfo, t0);
386 const auto start = std::min(t0, t1);
387 const auto end = std::max(t0, t1);
388
389 // Starting from the originally clicked clip, restrict the editing boundaries
390 // to the succession of clips with visible samples. If, of clips A, B and C,
391 // only B had invisible samples, it'd mean one could not drag-draw from A
392 // into C, but that probably isn't a behavior worthwhile much implementation
393 // complications.
394 const auto clips = WaveChannelUtilities::SortedClipArray(*mClickedTrack);
395 const auto iter = std::find_if(clips.begin(), clips.end(),
396 // Compare the intervals, not the pointers to them
397 [this](const auto &pClip){ return *pClip == *mClickedClip; });
398 const auto clickedClipIndex = std::distance(clips.begin(), iter);
399 constexpr auto forward = true;
400 const auto intervals = viewInfo.FindIntervals(mRect.width);
401 const size_t leftmostEditable = GetLastEditableClipStartingFromNthClip(
402 clickedClipIndex, !forward, clips, viewInfo, intervals);
403 const size_t rightmostEditable = GetLastEditableClipStartingFromNthClip(
404 clickedClipIndex, forward, clips, viewInfo, intervals);
405
406 const auto editStart =
407 std::max(start, clips[leftmostEditable]->GetPlayStartTime());
408 const auto editEnd =
409 std::min(end, clips[rightmostEditable]->GetPlayEndTime());
410
411 // For fast pencil movements covering more than one sample between two
412 // updates, we draw a line going from v0 at t0 to v1 at t1.
413 const auto interpolator = [t0, t1, v0 = mLastDragSampleValue,
414 v1 = newLevel](double t) {
415 if (t0 == t1)
416 return v1;
417 const auto gradient = (v1 - v0) / (t1 - t0);
418 const auto value = static_cast<float>(gradient * (t - t0) + v0);
419 // t may be outside t0 and t1, and we don't want to extrapolate.
420 return std::clamp(value, std::min(v0, v1), std::max(v0, v1));
421 };
422 SetFloatsWithinTimeRange(*mClickedTrack,
423 editStart, editEnd, interpolator, narrowestSampleFormat);
424
425 mLastDragPixel = x1;
426 mLastDragSampleValue = newLevel;
427
428 return RefreshCell;
429 }
430
431 HitTestPreview SampleHandle::Preview
432 (const TrackPanelMouseState &st, AudacityProject *pProject)
433 {
434 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
435 return HitPreview(st.state, pProject, unsafe);
436 }
437
438 UIHandle::Result SampleHandle::Release
439 (const TrackPanelMouseEvent &, AudacityProject *pProject,
440 wxWindow *)
441 {
442 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
443 if (unsafe)
444 return this->Cancel(pProject);
445
446 //*************************************************
447 //*** UP-CLICK (Finish drawing) ***
448 //*************************************************
449 //On up-click, send the state to the undo stack
450 mClickedTrack.reset(); //Set this to NULL so it will catch improper drag events.
451 mClickedClip = nullptr;
452 ProjectHistory::Get( *pProject ).PushState(XO("Moved Samples"),
453 XO("Sample Edit"),
454 UndoPush::CONSOLIDATE);
455
456 // No change to draw since last drag
457 return RefreshCode::RefreshNone;
458 }
459
460 UIHandle::Result SampleHandle::Cancel(AudacityProject *pProject)
461 {
462 mClickedTrack.reset();
463 ProjectHistory::Get( *pProject ).RollbackState();
464 return RefreshCode::RefreshCell;
465 }
466
467 float SampleHandle::FindSampleEditingLevel
468 (const wxMouseEvent &event, const ViewInfo &viewInfo, double t0)
469 {
470 using namespace WaveChannelUtilities;
471 // Calculate where the mouse is located vertically (between +/- 1)
472 float zoomMin, zoomMax;
473 auto &cache = WaveformScale::Get(*mClickedTrack);
474 cache.GetDisplayBounds(zoomMin, zoomMax);
475
476 const int yy = event.m_y - mRect.y;
477 const int height = mRect.GetHeight();
478 auto &settings = WaveformSettings::Get(*mClickedTrack);
479 const bool dB = !settings.isLinear();
480 float newLevel =
481 ::ValueOfPixel(yy, height, false, dB,
482 settings.dBRange, zoomMin, zoomMax);
483
484 //Take the envelope into account
485 const auto time = viewInfo.PositionToTime(event.m_x, mRect.x);
486 if (const auto env = GetEnvelopeAtTime(*mClickedTrack, time)){
487 // Calculate sample as it would be rendered
488 double envValue = env->GetValue(t0);
489 if (envValue > 0)
490 newLevel /= envValue;
491 else
492 newLevel = 0;
493
494 //Make sure the NEW level is between +/-1
495 newLevel = std::max(-1.0f, std::min(1.0f, newLevel));
496 }
497
498 return newLevel;
499 }