forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnvelopeEditor.cpp
342 lines (283 loc) · 11.3 KB
/
EnvelopeEditor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**********************************************************************
Audacity: A Digital Audio Editor
EnvelopeEditor.cpp
Paul Licameli split this from Envelope.cpp
**********************************************************************/
#include "EnvelopeEditor.h"
#include <wx/dc.h>
#include <wx/event.h>
#include "AColor.h"
#include "Envelope.h"
#include "TrackArt.h"
#include "TrackArtist.h"
#include "TrackPanelDrawingContext.h"
#include "ViewInfo.h"
#include "tracks/ui/EnvelopeHandle.h"
namespace {
void DrawPoint(wxDC & dc, const wxRect & r, int x, int y, bool top)
{
if (y >= 0 && y <= r.height) {
wxRect circle(r.x + x, r.y + (top ? y - 1: y - 2), 4, 4);
dc.DrawEllipse(circle);
}
}
}
void EnvelopeEditor::DrawPoints(const Envelope &env,
TrackPanelDrawingContext &context, const wxRect & r,
bool dB, double dBRange,
float zoomMin, float zoomMax, bool mirrored, int origin)
{
auto &dc = context.dc;
const auto artist = TrackArtist::Get( context );
const auto &zoomInfo = *artist->pZoomInfo;
bool highlight = false;
#ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
auto target = dynamic_cast<EnvelopeHandle*>(context.target.get());
highlight = target && target->GetEnvelope() == &env;
#endif
wxPen &pen = highlight ? AColor::uglyPen : AColor::envelopePen;
dc.SetPen( pen );
dc.SetBrush(*wxWHITE_BRUSH);
for (int i = 0; i < (int)env.GetNumberOfPoints(); i++) {
const double time = env[i].GetT() + env.GetOffset();
const wxInt64 position = zoomInfo.TimeToPosition(time, origin);
if (position >= 0 && position < r.width) {
// Change colour if this is the draggable point...
if (i == env.GetDragPoint()) {
dc.SetPen( pen );
dc.SetBrush(AColor::envelopeBrush);
}
double v = env[i].GetVal();
int x = (int)(position);
int y, y2;
y = GetWaveYPos(v, zoomMin, zoomMax, r.height, dB,
true, dBRange, false);
if (!mirrored) {
DrawPoint(dc, r, x, y, true);
}
else {
y2 = GetWaveYPos(-v-.000000001, zoomMin, zoomMax, r.height, dB,
true, dBRange, false);
// This follows the same logic as the envelop drawing in
// TrackArt::DrawEnvelope().
// TODO: make this calculation into a reusable function.
if (y2 - y < 9) {
int value = (int)((zoomMax / (zoomMax - zoomMin)) * r.height);
y = value - 4;
y2 = value + 4;
}
DrawPoint(dc, r, x, y, true);
DrawPoint(dc, r, x, y2, false);
// Contour
y = GetWaveYPos(v, zoomMin, zoomMax, r.height, dB,
false, dBRange, false);
y2 = GetWaveYPos(-v-.000000001, zoomMin, zoomMax, r.height, dB,
false, dBRange, false);
if (y <= y2) {
DrawPoint(dc, r, x, y, true);
DrawPoint(dc, r, x, y2, false);
}
}
// Change colour back again if was the draggable point.
if (i == env.GetDragPoint()) {
dc.SetPen( pen );
dc.SetBrush(*wxWHITE_BRUSH);
}
}
}
}
EnvelopeEditor::EnvelopeEditor(Envelope &envelope, bool mirrored)
: mEnvelope(envelope)
, mMirrored(mirrored)
, mContourOffset(-1)
// , mInitialVal(-1.0)
// , mInitialY(-1)
, mUpper(false)
, mButton(wxMOUSE_BTN_NONE)
, mDirty(false)
{
}
EnvelopeEditor::~EnvelopeEditor()
{
}
namespace
{
inline int SQR(int x) { return x * x; }
}
/// ValueOfPixel() converts a y position on screen to an envelope value.
/// @param y - y position, usually of the mouse.relative to the clip.
/// @param height - height of the rectangle we are in.
/// @upper - true if we are on the upper line, false if on lower.
/// @dB - display mode either linear or log.
/// @zoomMin - vertical scale, typically -1.0
/// @zoomMax - vertical scale, typically +1.0
float EnvelopeEditor::ValueOfPixel( int y, int height, bool upper,
bool dB, double dBRange,
float zoomMin, float zoomMax)
{
float v = ::ValueOfPixel(y, height, 0 != mContourOffset, dB, dBRange, zoomMin, zoomMax);
// MB: this is mostly equivalent to what the old code did, I'm not sure
// if anything special is needed for asymmetric ranges
if(upper)
return mEnvelope.ClampValue(v);
else
return mEnvelope.ClampValue(-v);
}
/// HandleMouseButtonDown either finds an existing control point or adds a NEW one
/// which is then recorded as the point to drag.
/// This is slightly complicated by there possibly being four control points for
/// a given time value:
/// We have an upper and lower envelope line.
/// Also we may be showing an inner envelope (at 0.5 the range).
bool EnvelopeEditor::HandleMouseButtonDown(const wxMouseEvent & event, wxRect & r,
const ZoomInfo &zoomInfo,
bool dB, double dBRange,
float zoomMin, float zoomMax)
{
int ctr = (int)(r.height * zoomMax / (zoomMax - zoomMin));
bool upper = !mMirrored || (zoomMin >= 0.0) || (event.m_y - r.y < ctr);
int clip_y = event.m_y - r.y;
if(clip_y < 0) clip_y = 0; //keeps point in rect r, even if mouse isn't
if(clip_y > r.GetBottom()) clip_y = r.GetBottom();
int bestNum = -1;
int bestDistSqr = 100; // Must be within 10 pixel radius.
// Member variables hold state that will be needed in dragging.
mButton = event.GetButton();
mContourOffset = false;
// wxLogDebug(wxT("Y:%i Height:%i Offset:%i"), y, height, mContourOffset );
int len = mEnvelope.GetNumberOfPoints();
// TODO: extract this into a function FindNearestControlPoint()
// TODO: also fix it so that we can drag the last point on an envelope.
for (int i = 0; i < len; i++) { //search for control point nearest click
const double time = mEnvelope[i].GetT() + mEnvelope.GetOffset();
const wxInt64 position = zoomInfo.TimeToPosition(time);
if (position >= 0 && position < r.width) {
int x = (int)(position);
int y[4];
int numControlPoints;
// Outer control points
double value = mEnvelope[i].GetVal();
y[0] = GetWaveYPos(value, zoomMin, zoomMax, r.height,
dB, true, dBRange, false);
y[1] = GetWaveYPos(-value, zoomMin, zoomMax, r.height,
dB, true, dBRange, false);
// Inner control points(contour)
y[2] = GetWaveYPos(value, zoomMin, zoomMax, r.height,
dB, false, dBRange, false);
y[3] = GetWaveYPos(-value -.00000001, zoomMin, zoomMax,
r.height, dB, false, dBRange, false);
numControlPoints = 4;
if (y[2] > y[3])
numControlPoints = 2;
if (!mMirrored)
numControlPoints = 1;
const int deltaXSquared = SQR(x - (event.m_x - r.x));
for(int j=0; j<numControlPoints; j++){
const int dSqr = deltaXSquared + SQR(y[j] - (event.m_y - r.y));
if (dSqr < bestDistSqr) {
bestNum = i;
bestDistSqr = dSqr;
mContourOffset = (bool)(j > 1);
}
}
}
}
if (bestNum >= 0) {
mEnvelope.SetDragPoint(bestNum);
}
else {
// TODO: Extract this into a function CreateNewPoint
const double when = zoomInfo.PositionToTime(event.m_x, r.x);
// if (when <= 0 || when >= mTrackLen)
// return false;
const double v = mEnvelope.GetValue( when );
int ct = GetWaveYPos( v, zoomMin, zoomMax, r.height, dB,
false, dBRange, false) ;
int cb = GetWaveYPos( -v-.000000001, zoomMin, zoomMax, r.height, dB,
false, dBRange, false) ;
if (ct <= cb || !mMirrored) {
int t = GetWaveYPos( v, zoomMin, zoomMax, r.height, dB,
true, dBRange, false) ;
int b = GetWaveYPos( -v, zoomMin, zoomMax, r.height, dB,
true, dBRange, false) ;
ct = (t + ct) / 2;
cb = (b + cb) / 2;
if (mMirrored &&
(event.m_y - r.y) > ct &&
((event.m_y - r.y) < cb))
mContourOffset = true;
else
mContourOffset = false;
}
double newVal = ValueOfPixel(clip_y, r.height, upper, dB, dBRange,
zoomMin, zoomMax);
mEnvelope.SetDragPoint(mEnvelope.InsertOrReplace(when, newVal));
mDirty = true;
}
mUpper = upper;
// const int dragPoint = mEnvelope.GetDragPoint();
// mInitialVal = mEnvelope[dragPoint].GetVal();
// mInitialY = event.m_y+mContourOffset;
return true;
}
void EnvelopeEditor::MoveDragPoint(const wxMouseEvent & event, wxRect & r,
const ZoomInfo &zoomInfo, bool dB, double dBRange,
float zoomMin, float zoomMax)
{
int clip_y = event.m_y - r.y;
if(clip_y < 0) clip_y = 0;
if(clip_y > r.height) clip_y = r.height;
double newVal = ValueOfPixel(clip_y, r.height, mUpper, dB, dBRange,
zoomMin, zoomMax);
// We no longer tolerate multiple envelope points at the same t.
// epsilon is less than the time offset of a single sample
// TODO: However because mTrackEpsilon assumes 200KHz this use
// of epsilon is a tad bogus. What we need to do instead is DELETE
// a duplicated point on a mouse up.
double newWhen = zoomInfo.PositionToTime(event.m_x, r.x) - mEnvelope.GetOffset();
mEnvelope.MoveDragPoint(newWhen, newVal);
}
bool EnvelopeEditor::HandleDragging(const wxMouseEvent & event, wxRect & r,
const ZoomInfo &zoomInfo, bool dB, double dBRange,
float zoomMin, float zoomMax,
float WXUNUSED(eMin), float WXUNUSED(eMax))
{
mDirty = true;
wxRect larger = r;
larger.Inflate(10, 10);
if (larger.Contains(event.m_x, event.m_y))
{
// IF we're in the rect THEN we're not deleting this point (anymore).
// ...we're dragging it.
MoveDragPoint( event, r, zoomInfo, dB, dBRange, zoomMin, zoomMax);
return true;
}
if(!mEnvelope.GetDragPointValid())
// IF we already know we're deleting THEN no envelope point to update.
return false;
// Invalidate the point
mEnvelope.SetDragPointValid(false);
return true;
}
// Exit dragging mode and delete dragged point if necessary.
bool EnvelopeEditor::HandleMouseButtonUp()
{
mEnvelope.ClearDragPoint();
mButton = wxMOUSE_BTN_NONE;
return true;
}
// Returns true if parent needs to be redrawn
bool EnvelopeEditor::MouseEvent(const wxMouseEvent & event, wxRect & r,
const ZoomInfo &zoomInfo, bool dB, double dBRange,
float zoomMin, float zoomMax)
{
if (event.ButtonDown() && mButton == wxMOUSE_BTN_NONE)
return HandleMouseButtonDown( event, r, zoomInfo, dB, dBRange,
zoomMin, zoomMax);
if (event.Dragging() && mEnvelope.GetDragPoint() >= 0)
return HandleDragging( event, r, zoomInfo, dB, dBRange,
zoomMin, zoomMax);
if (event.ButtonUp() && event.GetButton() == mButton)
return HandleMouseButtonUp();
return false;
}