forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvelope.h
298 lines (228 loc) · 9.02 KB
/
Envelope.h
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
/**********************************************************************
Sneedacity: A Digital Audio Editor
Envelope.h
Dominic Mazzoni
**********************************************************************/
#ifndef __SNEEDACITY_ENVELOPE__
#define __SNEEDACITY_ENVELOPE__
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include "xml/XMLTagHandler.h"
class wxRect;
class wxMouseEvent;
class wxTextFile;
class Envelope;
class EnvPoint;
class ZoomInfo;
class EnvPoint final : public XMLTagHandler {
public:
EnvPoint() {}
inline EnvPoint( double t, double val ) : mT{ t }, mVal{ val } {}
double GetT() const { return mT; }
void SetT(double t) { mT = t; }
double GetVal() const { return mVal; }
inline void SetVal( Envelope *pEnvelope, double val );
bool HandleXMLTag(const wxChar *tag, const wxChar **attrs) override
{
if (!wxStrcmp(tag, wxT("controlpoint"))) {
while (*attrs) {
const wxChar *attr = *attrs++;
const wxChar *value = *attrs++;
if (!wxStrcmp(attr, wxT("t")))
SetT(Internat::CompatibleToDouble(value));
else if (!wxStrcmp(attr, wxT("val")))
SetVal( nullptr, Internat::CompatibleToDouble(value) );
}
return true;
}
else
return false;
}
XMLTagHandler *HandleXMLChild(const wxChar * WXUNUSED(tag)) override
{
return NULL;
}
private:
double mT {};
double mVal {};
};
typedef std::vector<EnvPoint> EnvArray;
struct TrackPanelDrawingContext;
class SNEEDACITY_DLL_API Envelope /* not final */ : public XMLTagHandler {
public:
// Envelope can define a piecewise linear function, or piecewise exponential.
Envelope(bool exponential, double minValue, double maxValue, double defaultValue);
Envelope(const Envelope &orig);
// Create from a subrange of another envelope.
Envelope(const Envelope &orig, double t0, double t1);
void Initialize(int numPoints);
virtual ~Envelope();
/** \brief Get many envelope points for pixel columns at once,
* but don't assume uniform time per pixel.
*/
static void GetValues
( const Envelope &env,
double aligned_time, double sampleDur,
double *buffer, int bufferLen, int leftOffset,
const ZoomInfo &zoomInfo);
// Return true if violations of point ordering invariants were detected
// and repaired
bool ConsistencyCheck();
double GetOffset() const { return mOffset; }
double GetTrackLen() const { return mTrackLen; }
bool GetExponential() const { return mDB; }
void SetExponential(bool db) { mDB = db; }
void Flatten(double value);
double GetMinValue() const { return mMinValue; }
double GetMaxValue() const { return mMaxValue; }
void SetRange(double minValue, double maxValue);
double ClampValue(double value) { return std::max(mMinValue, std::min(mMaxValue, value)); }
// Newfangled XML file I/O
bool HandleXMLTag(const wxChar *tag, const wxChar **attrs) override;
XMLTagHandler *HandleXMLChild(const wxChar *tag) override;
void WriteXML(XMLWriter &xmlFile) const /* not override */;
// Handling Cut/Copy/Paste events
// sampleDur determines when the endpoint of the collapse is near enough
// to an endpoint of the domain, that an extra control point is not needed.
void CollapseRegion(double t0, double t1, double sampleDur);
// Envelope has no notion of rate and control point times are not quantized;
// but a tolerance is needed in the Paste routine, and better to inform it
// of an appropriate number, than use hidden arbitrary constants.
// The function is called 'PasteEnvelope' rather than 'Paste' to make it
// easier to find where it is used in source code.
void PasteEnvelope(double t0, const Envelope *e, double sampleDur);
void InsertSpace(double t0, double tlen);
// Control
void SetOffset(double newOffset);
void SetTrackLen( double trackLen, double sampleDur = 0.0 );
void RescaleValues(double minValue, double maxValue);
void RescaleTimes( double newLength );
// Accessors
/** \brief Get envelope value at time t */
double GetValue( double t, double sampleDur = 0 ) const;
/** \brief Get many envelope points at once.
*
* This is much faster than calling GetValue() multiple times if you need
* more than one value in a row. */
void GetValues(double *buffer, int len, double t0, double tstep) const;
// Guarantee an envelope point at the end of the domain.
void Cap( double sampleDur );
private:
std::pair< int, int > ExpandRegion
( double t0, double tlen, double *pLeftVal, double *pRightVal );
void RemoveUnneededPoints
( size_t startAt, bool rightward, bool testNeighbors = true );
double GetValueRelative(double t, bool leftLimit = false) const;
void GetValuesRelative
(double *buffer, int len, double t0, double tstep, bool leftLimit = false)
const;
// relative time
int NumberOfPointsAfter(double t) const;
// relative time
double NextPointAfter(double t) const;
public:
double Average( double t0, double t1 ) const;
double AverageOfInverse( double t0, double t1 ) const;
double Integral( double t0, double t1 ) const;
double IntegralOfInverse( double t0, double t1 ) const;
double SolveIntegralOfInverse( double t0, double area) const;
void print() const;
void testMe();
bool IsDirty() const;
void Clear() { mEnv.clear(); }
/** \brief Add a point at a particular absolute time coordinate */
int InsertOrReplace(double when, double value)
{ return InsertOrReplaceRelative( when - mOffset, value ); }
/** \brief Move a point at when to value
*
* Returns 0 if point moved, -1 if not found.*/
int Reassign(double when, double value);
/** \brief DELETE a point by its position in array */
void Delete(int point);
/** \brief insert a point */
void Insert(int point, const EnvPoint &p);
// Insert a point (without replacement)
// for now assumed sequential.
void Insert(double when, double value);
/** \brief Return number of points */
size_t GetNumberOfPoints() const;
/** \brief Accessor for points */
const EnvPoint &operator[] (int index) const
{
return mEnv[index];
}
private:
int InsertOrReplaceRelative(double when, double value);
std::pair<int, int> EqualRange( double when, double sampleDur ) const;
public:
/** \brief Returns the sets of when and value pairs */
void GetPoints(double *bufferWhen,
double *bufferValue,
int bufferLen) const;
// UI-related
// The drag point needs to display differently.
int GetDragPoint() const { return mDragPoint; }
// Choose the drag point.
void SetDragPoint(int dragPoint);
// Mark or unmark the drag point for deletion.
void SetDragPointValid(bool valid);
bool GetDragPointValid() const { return mDragPointValid; }
// Modify the dragged point and change its value.
// But consistency constraints may move it less then you ask for.
void MoveDragPoint(double newWhen, double value);
// May delete the drag point. Restores envelope consistency.
void ClearDragPoint();
private:
void AddPointAtEnd( double t, double val );
void CopyRange(const Envelope &orig, size_t begin, size_t end);
// relative time
void BinarySearchForTime( int &Lo, int &Hi, double t ) const;
void BinarySearchForTime_LeftLimit( int &Lo, int &Hi, double t ) const;
double GetInterpolationStartValueAtPoint( int iPoint ) const;
// The list of envelope control points.
EnvArray mEnv;
/** \brief The time at which the envelope starts, i.e. the start offset */
double mOffset { 0.0 };
/** \brief The length of the envelope, which is the same as the length of the
* underlying track (normally) */
double mTrackLen { 0.0 };
// TODO: mTrackEpsilon based on assumption of 200KHz. Needs review if/when
// we support higher sample rates.
/** \brief The shortest distance apart that points on an envelope can be
* before being considered the same point */
double mTrackEpsilon { 1.0 / 200000.0 };
bool mDB;
double mMinValue, mMaxValue;
double mDefaultValue;
// UI stuff
bool mDragPointValid { false };
int mDragPoint { -1 };
mutable int mSearchGuess { -2 };
};
inline void EnvPoint::SetVal( Envelope *pEnvelope, double val )
{
if ( pEnvelope )
val = pEnvelope->ClampValue(val);
mVal = val;
}
/*
PRL: This class gives access to all the important numerical data in a TimeTrack,
without need for the entire TimeTrack.
Confusingly, Envelope already carried its own limiting values, but those
in the TimeTrack were not guaranteed to be the same.
I'm just preserving behavior as I break file dependencies and won't try to fix
that confusion now.
*/
class BoundedEnvelope final : public Envelope
{
public:
using Envelope::Envelope;
double GetRangeLower() const { return mRangeLower; }
double GetRangeUpper() const { return mRangeUpper; }
void SetRangeLower(double lower) { mRangeLower = lower; }
void SetRangeUpper(double upper) { mRangeUpper = upper; }
private:
double mRangeLower{}, mRangeUpper{};
};
#endif