Audacity 3.2.0
SampleHandle.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5SampleHandle.cpp
6
7Paul 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 "../../../../prefs/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 "../../../../WaveTrack.h"
29#include "../../../../../images/Cursors.h"
30#include "../../../../widgets/AudacityMessageBox.h"
31
32
33static const int SMOOTHING_KERNEL_RADIUS = 3;
34static const int SMOOTHING_BRUSH_RADIUS = 5;
35static const double SMOOTHING_PROPORTION_MAX = 0.7;
36static const double SMOOTHING_PROPORTION_MIN = 0.0;
37
38SampleHandle::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 = XO("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
82namespace {
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?
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 auto &viewInfo = ViewInfo::Get( *pProject );
117
120 const auto wavetrack = pTrack.get();
121 const auto time = viewInfo.PositionToTime(state.m_x, rect.x);
122
123 const double tt = adjustTime(wavetrack, time);
124 if (!SampleResolutionTest(viewInfo, wavetrack, tt, rect.width))
125 return {};
126
127 // Just get one sample.
128 float oneSample;
129 const double rate = wavetrack->GetRate();
130 const auto s0 = (sampleCount)(tt * rate + 0.5);
131 if (! wavetrack->GetFloats(&oneSample, s0, 1, fillZero,
132 // Do not propagate exception but return a failure value
133 false) )
134 return {};
135
136 // Get y distance of envelope point from center line (in pixels).
137 float zoomMin, zoomMax;
138
139 wavetrack->GetDisplayBounds(&zoomMin, &zoomMax);
140
141 double envValue = 1.0;
142 Envelope* env = wavetrack->GetEnvelopeAtTime(time);
143 if (env)
144 // Calculate sample as it would be rendered, so quantize time
145 envValue = env->GetValue( tt, 1.0 / wavetrack->GetRate() );
146
147 const bool dB = !wavetrack->GetWaveformSettings().isLinear();
148 int yValue = GetWaveYPos(oneSample * envValue,
149 zoomMin, zoomMax,
150 rect.height, dB, true,
151 wavetrack->GetWaveformSettings().dBRange, false) + rect.y;
152
153 // Get y position of mouse (in pixels)
154 int yMouse = state.m_y;
155
156 // Perhaps yTolerance should be put into preferences?
157 const int yTolerance = 10; // More tolerance on samples than on envelope.
158 if (abs(yValue - yMouse) >= yTolerance)
159 return {};
160
161 return HitAnywhere(holder, state, pTrack);
162}
163
165{
166}
167
168namespace {
173 (const wxMouseEvent &event,
174 const wxRect &rect, const ViewInfo &viewInfo, WaveTrack *wt, int width)
175 {
176 //If we aren't zoomed in far enough, show a message dialog.
177 const double time = adjustTime(wt, viewInfo.PositionToTime(event.m_x, rect.x));
178 if (!SampleResolutionTest(viewInfo, wt, time, width))
179 {
181 XO(
182"To use Draw, zoom in further until you can see the individual samples."),
183 XO("Draw Tool"));
184 return false;
185 }
186 return true;
187 }
188}
189
191(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
192{
193 using namespace RefreshCode;
194 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
195 if ( unsafe )
196 return Cancelled;
197
198 const wxMouseEvent &event = evt.event;
199 const wxRect &rect = evt.rect;
200 const auto &viewInfo = ViewInfo::Get( *pProject );
201 const auto pTrack = mClickedTrack.get();
202
205 event, rect, viewInfo, pTrack, rect.width))
206 return Cancelled;
207
209 mRect = rect;
210
211 //If we are still around, we are drawing in earnest. Set some member data structures up:
212 //First, calculate the starting sample. To get this, we need the time
213 const double t0 =
214 adjustTime(mClickedTrack.get(), viewInfo.PositionToTime(event.m_x, rect.x));
215
216 //convert t0 to samples
217 mClickedStartSample = mClickedTrack->TimeToLongSamples(t0);
218
219 //Determine how drawing should occur. If alt is down,
220 //do a smoothing, instead of redrawing.
221 if (event.m_altDown)
222 {
223 mAltKey = true;
224 //*************************************************
225 //*** ALT-DOWN-CLICK (SAMPLE SMOOTHING) ***
226 //*************************************************
227 //
228 // Smoothing works like this: There is a smoothing kernel radius constant that
229 // determines how wide the averaging window is. Plus, there is a smoothing brush radius,
230 // which determines how many pixels wide around the selected pixel this smoothing is applied.
231 //
232 // Samples will be replaced by a mixture of the original points and the smoothed points,
233 // with a triangular mixing probability whose value at the center point is
234 // SMOOTHING_PROPORTION_MAX and at the far bounds is SMOOTHING_PROPORTION_MIN
235
236 //Get the region of samples around the selected point
237 size_t sampleRegionSize = 1 + 2 * (SMOOTHING_KERNEL_RADIUS + SMOOTHING_BRUSH_RADIUS);
238 Floats sampleRegion{ sampleRegionSize };
239 Floats newSampleRegion{ 1 + 2 * (size_t)SMOOTHING_BRUSH_RADIUS };
240
241 //Get a sample from the track to do some tricks on.
242 mClickedTrack->GetFloats(sampleRegion.get(),
244 sampleRegionSize);
245
246 //Go through each point of the smoothing brush and apply a smoothing operation.
247 for (auto jj = -SMOOTHING_BRUSH_RADIUS; jj <= SMOOTHING_BRUSH_RADIUS; ++jj) {
248 float sumOfSamples = 0;
249 for (auto ii = -SMOOTHING_KERNEL_RADIUS; ii <= SMOOTHING_KERNEL_RADIUS; ++ii) {
250 //Go through each point of the smoothing kernel and find the average
251
252 //The average is a weighted average, scaled by a weighting kernel that is simply triangular
253 // A triangular kernel across N items, with a radius of R ( 2 R + 1 points), if the farthest:
254 // points have a probability of a, the entire triangle has total probability of (R + 1)^2.
255 // For sample number ii and middle brush sample M, (R + 1 - abs(M-ii))/ ((R+1)^2) gives a
256 // legal distribution whose total probability is 1.
257 //
258 //
259 // weighting factor value
260 sumOfSamples +=
261 (SMOOTHING_KERNEL_RADIUS + 1 - abs(ii)) *
262 sampleRegion[ii + jj + SMOOTHING_KERNEL_RADIUS + SMOOTHING_BRUSH_RADIUS];
263
264 }
265 newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] =
266 sumOfSamples /
268 }
269
270
271 // Now that the NEW sample levels are determined, go through each and mix it appropriately
272 // with the original point, according to a 2-part linear function whose center has probability
273 // SMOOTHING_PROPORTION_MAX and extends out SMOOTHING_BRUSH_RADIUS, at which the probability is
274 // SMOOTHING_PROPORTION_MIN. _MIN and _MAX specify how much of the smoothed curve make it through.
275
276 float prob;
277
278 for (auto jj = -SMOOTHING_BRUSH_RADIUS; jj <= SMOOTHING_BRUSH_RADIUS; ++jj) {
279
280 prob =
282 (float)abs(jj) / SMOOTHING_BRUSH_RADIUS *
284
285 newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] =
286 newSampleRegion[jj + SMOOTHING_BRUSH_RADIUS] * prob +
288 (1 - prob);
289 }
290 //Set the sample to the point of the mouse event
291 mClickedTrack->Set((samplePtr)newSampleRegion.get(), floatSample,
293
294 // mLastDragSampleValue will not be used
295 }
296 else
297 {
298 mAltKey = false;
299 //*************************************************
300 //*** PLAIN DOWN-CLICK (NORMAL DRAWING) ***
301 //*************************************************
302
303 //Otherwise (e.g., the alt button is not down) do normal redrawing, based on the mouse position.
304 const float newLevel = FindSampleEditingLevel(event, viewInfo, t0);
305
306 //Set the sample to the point of the mouse event
308
309 mLastDragSampleValue = newLevel;
310 }
311
312 //Set the member data structures for drawing
314
315 // Sample data changed on either branch, so refresh the track display.
316 return RefreshCell;
317}
318
320(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
321{
322 using namespace RefreshCode;
323 const wxMouseEvent &event = evt.event;
324 const auto &viewInfo = ViewInfo::Get( *pProject );
325
326 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
327 if (unsafe) {
328 this->Cancel(pProject);
329 return RefreshCell | Cancelled;
330 }
331
332 //*************************************************
333 //*** DRAG-DRAWING ***
334 //*************************************************
335
336 //No dragging effects if the alt key is down--
337 //Don't allow left-right dragging for smoothing operation
338 if (mAltKey)
339 return RefreshNone;
340
341 sampleCount s0; //declare this for use below. It designates which sample number to draw.
342
343 // Figure out what time the click was at
344 //Find the point that we want to redraw at. If the control button is down,
345 //adjust only the originally clicked-on sample
346
347 if (event.m_controlDown) {
348 //*************************************************
349 //*** CTRL-DOWN (Hold Initial Sample Constant ***
350 //*************************************************
351
353 }
354 else {
355 //*************************************************
356 //*** Normal CLICK-drag (Normal drawing) ***
357 //*************************************************
358
359 //Otherwise, adjust the sample you are dragging over right now.
360 //convert this to samples
361 const double tt = viewInfo.PositionToTime(event.m_x, mRect.x);
362 s0 = mClickedTrack->TimeToLongSamples(tt);
363 }
364
365 const double t0 = mClickedTrack->LongSamplesToTime(s0);
366
367 // Do redrawing, based on the mouse position.
368 // Calculate where the mouse is located vertically (between +/- 1)
369
370 const float newLevel = FindSampleEditingLevel(event, viewInfo, t0);
371
372 //Now, redraw all samples between current and last redrawn sample, inclusive
373 //Go from the smaller to larger sample.
374 const auto start = std::min(s0, mLastDragSample);
375 const auto end = std::max(s0, mLastDragSample);
376 // Few enough samples to be drawn individually on screen will not
377 // overflow size_t:
378 const auto size = ( end - start + 1 ).as_size_t();
379 if (size == 1) {
380 mClickedTrack->Set((samplePtr)&newLevel, floatSample, start, size);
381 }
382 else {
383 std::vector<float> values(size);
384 for (auto ii = start; ii <= end; ++ii) {
385 //This interpolates each sample linearly:
386 // i - start will not overflow size_t either:
387 values[( ii - start ).as_size_t()] =
389 (ii - mLastDragSample).as_float() /
390 (s0 - mLastDragSample).as_float();
391 }
392 mClickedTrack->Set((samplePtr)&values[0], floatSample, start, size);
393 }
394
395 //Update the member data structures.
396 mLastDragSample = s0;
397 mLastDragSampleValue = newLevel;
398
399 return RefreshCell;
400}
401
403(const TrackPanelMouseState &st, AudacityProject *pProject)
404{
405 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
406 return HitPreview(st.state, pProject, unsafe);
407}
408
410(const TrackPanelMouseEvent &, AudacityProject *pProject,
411 wxWindow *)
412{
413 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
414 if (unsafe)
415 return this->Cancel(pProject);
416
417 //*************************************************
418 //*** UP-CLICK (Finish drawing) ***
419 //*************************************************
420 //On up-click, send the state to the undo stack
421 mClickedTrack.reset(); //Set this to NULL so it will catch improper drag events.
422 ProjectHistory::Get( *pProject ).PushState(XO("Moved Samples"),
423 XO("Sample Edit"),
425
426 // No change to draw since last drag
428}
429
431{
432 mClickedTrack.reset();
433 ProjectHistory::Get( *pProject ).RollbackState();
435}
436
438 (const wxMouseEvent &event, const ViewInfo &viewInfo, double t0)
439{
440 // Calculate where the mouse is located vertically (between +/- 1)
441 float zoomMin, zoomMax;
442 mClickedTrack->GetDisplayBounds(&zoomMin, &zoomMax);
443
444 const int yy = event.m_y - mRect.y;
445 const int height = mRect.GetHeight();
446 const bool dB = !mClickedTrack->GetWaveformSettings().isLinear();
447 float newLevel =
448 ::ValueOfPixel(yy, height, false, dB,
449 mClickedTrack->GetWaveformSettings().dBRange, zoomMin, zoomMax);
450
451 //Take the envelope into account
452 const auto time = viewInfo.PositionToTime(event.m_x, mRect.x);
453 Envelope *const env = mClickedTrack->GetEnvelopeAtTime(time);
454 if (env)
455 {
456 // Calculate sample as it would be rendered, so quantize time
457 double envValue = env->GetValue( t0, 1.0 / mClickedTrack->GetRate());
458 if (envValue > 0)
459 newLevel /= envValue;
460 else
461 newLevel = 0;
462
463 //Make sure the NEW level is between +/-1
464 newLevel = std::max(-1.0f, std::min(1.0f, newLevel));
465 }
466
467 return newLevel;
468}
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
std::shared_ptr< UIHandle > UIHandlePtr
Definition: CellularPanel.h:28
int min(int a, int b)
const wxChar * values
#define XO(s)
Definition: Internat.h:31
@ floatSample
Definition: SampleFormat.h:34
char * samplePtr
Definition: SampleFormat.h:49
@ fillZero
Definition: SampleFormat.h:54
static const int SMOOTHING_BRUSH_RADIUS
static const double SMOOTHING_PROPORTION_MAX
static const int SMOOTHING_KERNEL_RADIUS
static const double SMOOTHING_PROPORTION_MIN
int GetWaveYPos(float value, float min, float max, int height, bool dB, bool outer, float dBr, bool clip)
Definition: TrackArt.cpp:49
float ValueOfPixel(int yy, int height, bool offset, bool dB, double dBRange, float zoomMin, float zoomMax)
Definition: TrackArt.cpp:105
std::unique_ptr< wxCursor > MakeCursor(int WXUNUSED(CursorId), const char *const pXpm[36], int HotX, int HotY)
Definition: TrackPanel.cpp:185
std::shared_ptr< Subclass > AssignUIHandlePtr(std::weak_ptr< Subclass > &holder, const std::shared_ptr< Subclass > &pNew)
Definition: UIHandle.h:151
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
Piecewise linear or piecewise exponential function from double to double.
Definition: Envelope.h:72
double GetValue(double t, double sampleDur=0) const
Get envelope value at time t.
Definition: Envelope.cpp:823
bool IsAudioActive() const
static ProjectAudioIO & Get(AudacityProject &project)
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
static ProjectHistory & Get(AudacityProject &project)
Result Release(const TrackPanelMouseEvent &event, AudacityProject *pProject, wxWindow *pParent) override
virtual ~SampleHandle()
float FindSampleEditingLevel(const wxMouseEvent &event, const ViewInfo &viewInfo, double t0)
float mLastDragSampleValue
Definition: SampleHandle.h:76
sampleCount mClickedStartSample
Definition: SampleHandle.h:74
Result Cancel(AudacityProject *pProject) override
static HitTestPreview HitPreview(const wxMouseState &state, const AudacityProject *pProject, bool unsafe)
static UIHandlePtr HitAnywhere(std::weak_ptr< SampleHandle > &holder, const wxMouseState &state, const std::shared_ptr< WaveTrack > &pTrack)
void Enter(bool forward, AudacityProject *) override
sampleCount mLastDragSample
Definition: SampleHandle.h:75
static UIHandlePtr HitTest(std::weak_ptr< SampleHandle > &holder, const wxMouseState &state, const wxRect &rect, const AudacityProject *pProject, const std::shared_ptr< WaveTrack > &pTrack)
Result Click(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
wxRect mRect
Definition: SampleHandle.h:72
std::shared_ptr< WaveTrack > mClickedTrack
Definition: SampleHandle.h:71
HitTestPreview Preview(const TrackPanelMouseState &state, AudacityProject *pProject) override
Result Drag(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
SampleHandle(const SampleHandle &)=delete
double LongSamplesToTime(sampleCount pos) const
Convert correctly between a number of samples and an (absolute) time in seconds.
Definition: SampleTrack.cpp:47
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: SampleTrack.cpp:42
Result mChangeHighlight
Definition: UIHandle.h:139
unsigned Result
Definition: UIHandle.h:38
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
A Track that contains audio waveform data.
Definition: WaveTrack.h:57
double GetRate() const override
Definition: WaveTrack.cpp:479
std::vector< Interval > Intervals
Definition: ZoomInfo.h:146
void FindIntervals(double rate, Intervals &results, wxInt64 width, wxInt64 origin=0) const
Definition: ZoomInfo.cpp:110
double PositionToTime(wxInt64 position, wxInt64 origin=0, bool ignoreFisheye=false) const
Definition: ZoomInfo.cpp:41
wxInt64 TimeToPosition(double time, wxInt64 origin=0, bool ignoreFisheye=false) const
STM: Converts a project time to screen x position.
Definition: ZoomInfo.cpp:51
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
Namespace containing an enum 'what to do on a refresh?'.
Definition: RefreshCode.h:16
bool SampleResolutionTest(const ViewInfo &viewInfo, const WaveTrack *wt, double time, int width)
double adjustTime(const WaveTrack *wt, double time)
bool IsSampleEditingPossible(const wxMouseEvent &event, const wxRect &rect, const ViewInfo &viewInfo, WaveTrack *wt, int width)