File: Envelope.cpp

package info (click to toggle)
audacity 0.98-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,896 kB
  • ctags: 4,089
  • sloc: cpp: 26,099; ansic: 4,961; sh: 2,465; makefile: 156; perl: 23
file content (577 lines) | stat: -rw-r--r-- 12,272 bytes parent folder | download
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/**********************************************************************

  Audacity: A Digital Audio Editor

  Envelope.cpp

  Dominic Mazzoni

**********************************************************************/

#include <math.h>

#include <wx/dc.h>
#include <wx/brush.h>
#include <wx/pen.h>

#include "AColor.h"
#include "Envelope.h"
#include "DirManager.h"

Envelope::Envelope()
{
   mOffset = 0.0;
   mTrackLen = 1000000000.0;

   Insert(0.0, 1.0);
   Insert(1000000000.0, 1.0);

   mDragPoint = -1;
   mDirty = false;

   mIsDeleting = false;

   mMirror = true;
}

Envelope::~Envelope()
{
   int len = mEnv.Count();
   for (int i = 0; i < len; i++)
      delete mEnv[i];
}

void Envelope::Mirror(bool mirror)
{
   mMirror = mirror;
}

void Envelope::Flatten(double value)
{
   mEnv.Clear();
   Insert(0.0, value);
   Insert(1000000000.0, value);
}

void Envelope::CopyFrom(Envelope * e)
{
   mOffset = e->mOffset;
   int len = e->mEnv.Count();
   mTrackLen = e->mEnv[len - 1]->t;
   int i;
   for (i = 0; i < mEnv.Count(); i++)
      delete mEnv[i];
   mEnv.Clear();
   mEnv.Alloc(len);
   for (i = 0; i < len; i++) {
      EnvPoint *pt = new EnvPoint();
      pt->t = e->mEnv[i]->t;
      pt->val = e->mEnv[i]->val;
      mEnv.Add(pt);
   }
}

double Envelope::toDB(double value)
{
   if (value == 0)
      return 0;

   double sign = (value >= 0 ? 1 : -1);

   double db = 10 * log10(fabs(value));
   // The smallest value we will see is -45.15 (10*log10(1/32768))
   double val = (db + 45.0) / 45.0;
   if (val < 0.0)
      val = 0.0;
   if (val > 1.0)
      val = 1.0;

   return sign * val;
}

double Envelope::fromDB(double value)
{
   if (value == 0)
      return 0;

   return pow(10.0, ((value * 45.0) - 45.0) / 10.0);
}

void Envelope::Draw(wxDC & dc, wxRect & r, double h, double pps, bool dB)
{
   h -= mOffset;

   double tright = h + (r.width / pps);

   dc.SetPen(AColor::envelopePen);
   dc.SetBrush(*wxWHITE_BRUSH);

   int ctr, height;
   if (mMirror) {
      height = r.height / 2;
      ctr = r.y + height;
   }
   else {
      height = r.height;
      ctr = r.y + height;
   }

   int len = mEnv.Count();
   for (int i = 0; i < len; i++) {
      if (mEnv[i]->t >= h && mEnv[i]->t <= tright) {
         if (i == mDragPoint) {
            dc.SetPen(AColor::envelopePen);
            dc.SetBrush(AColor::envelopeBrush);
         }

         double v = mEnv[i]->val;
         int x = int ((mEnv[i]->t - h) * pps);
         int y;

         if (dB)
            y = int (toDB(v) * height);
         else
         y = int (v * height);

         wxRect circle(r.x + x - 1, ctr - y, 4, 4);
         dc.DrawEllipse(circle);
         if (mMirror) {
            circle.y = ctr + y - 2;
            dc.DrawEllipse(circle);
         }

         if (i == mDragPoint) {
            dc.SetPen(AColor::envelopePen);
            dc.SetBrush(*wxWHITE_BRUSH);
         }
      }
   }
}

bool Envelope::Load(wxTextFile * in, DirManager * dirManager)
{
   if (in->GetNextLine() != "EnvNumPoints")
      return false;

   long len;
   if (!(in->GetNextLine().ToLong(&len)))
      return false;

   int i;
   for (i = 0; i < mEnv.Count(); i++)
      delete mEnv[i];
   mEnv.Clear();
   mEnv.Alloc(len);

   for (i = 0; i < len; i++) {
      EnvPoint *e = new EnvPoint();
      if (!(in->GetNextLine().ToDouble(&e->t)))
         return false;
      if (!(in->GetNextLine().ToDouble(&e->val)))
         return false;
      if (i > 0 && mEnv[i - 1]->t > e->t)
         return false;
      mEnv.Add(e);
   }

   mTrackLen = mEnv[len-1]->t;

   if (in->GetNextLine() != "EnvEnd")
      return false;

   return true;
}

bool Envelope::Save(wxTextFile * out, bool overwrite)
{
   out->AddLine("EnvNumPoints");
   int len = mEnv.Count();
   out->AddLine(wxString::Format("%d", len));
   for (int i = 0; i < len; i++) {
      out->AddLine(wxString::Format("%lf", mEnv[i]->t));
      out->AddLine(wxString::Format("%lf", mEnv[i]->val));
   }
   out->AddLine("EnvEnd");

   return true;
}

// Returns true if parent needs to be redrawn
bool Envelope::MouseEvent(wxMouseEvent & event, wxRect & r,
                          double h, double pps, bool dB)
{
   //h -= mOffset;

   int ctr, height;
   bool upper;
   if (mMirror) {
      height = r.height / 2;
      ctr = r.y + height;
      upper = (event.m_y < ctr);
   }
   else {
      height = r.height;
      ctr = r.y + height;
      upper = true;
   }

   if (event.ButtonDown()) {
      mIsDeleting = false;
      double tleft = h - mOffset;
      double tright = tleft + (r.width / pps);
      int bestNum = -1;
      int bestDist = 10;

      int len = mEnv.Count();
      for (int i = 0; i < len; i++) {
         if (mEnv[i]->t >= tleft && mEnv[i]->t <= tright) {
            double v = mEnv[i]->val;
            int x = int ((mEnv[i]->t + mOffset - h) * pps) + r.x;
            int dy;

            if (dB)
               dy = int (toDB(v) * height);
            else
            dy = int (v * height);

            int y;
            if (upper)
               y = int (ctr - dy);
            else
            y = int (ctr + dy);

#ifndef SQR
#define SQR(X) ((X)*(X))
#endif

            int d =
                int (sqrt(SQR(x - event.m_x) + SQR(y - event.m_y)) + 0.5);
            if (d < bestDist) {
               bestNum = i;
               bestDist = d;
            }
         }
      }

      if (bestNum >= 0) {
         mDragPoint = bestNum;
      } else {
         // Create new point
         double when = h + (event.m_x - r.x) / pps - mOffset;

         int dy;
         if (upper)
            dy = ctr - event.m_y;
         else
            dy = event.m_y - ctr;

         double newVal;

         if (dB)
            newVal = fromDB(dy / double (height));
         else
            newVal = dy / double (height);

         if (newVal < 0.0)
            newVal = 0.0;
         if (newVal > 1.0)
            newVal = 1.0;

         mDragPoint = Insert(when, newVal);
         mDirty = true;
      }

      mUpper = upper;

      mInitialWhen = mEnv[mDragPoint]->t;
      mInitialVal = mEnv[mDragPoint]->val;

      mInitialX = event.m_x;
      mInitialY = event.m_y;

      return true;
   }

   if (event.Dragging() && mDragPoint >= 0) {
      mDirty = true;

      wxRect larger = r;
      larger.Inflate(5, 5);

      if (!mIsDeleting &&
          mDragPoint > 0 && mDragPoint < mEnv.Count() - 1 &&
          !larger.Inside(event.m_x, event.m_y)) {

         mEnv[mDragPoint]->t = mEnv[mDragPoint - 1]->t;
         mEnv[mDragPoint]->val = mEnv[mDragPoint - 1]->val;

         mIsDeleting = true;

         return true;
      }

      if (larger.Inside(event.m_x, event.m_y))
         mIsDeleting = false;

      if (mIsDeleting)
         return false;

      int y;
      if (mUpper)
         y = ctr - event.m_y;
      else
         y = event.m_y - ctr;

      double newVal;

      if (dB)
         newVal = fromDB(y / double (height));
      else
         newVal = y / double (height);

      if (newVal < 0.0)
         newVal = 0.0;
      if (newVal > 1.0)
         newVal = 1.0;

      double newWhen = mInitialWhen + (event.m_x - mInitialX) / pps;

      if (mDragPoint > 0 && newWhen < mEnv[mDragPoint - 1]->t)
         newWhen = mEnv[mDragPoint - 1]->t;

      if (mDragPoint < mEnv.Count() - 1
          && newWhen > mEnv[mDragPoint + 1]->t)
         newWhen = mEnv[mDragPoint + 1]->t;

      if (mDragPoint == 0)
         newWhen = 0;

      if (mDragPoint == mEnv.Count() - 1)
         newWhen = mTrackLen;

      mEnv[mDragPoint]->t = newWhen;
      mEnv[mDragPoint]->val = newVal;

      return true;
   }

   if (event.ButtonUp()) {
      if (mIsDeleting) {
         delete mEnv[mDragPoint];
         mEnv.RemoveAt(mDragPoint);
      }
      mDragPoint = -1;
      return true;
   }

   return false;
}

void Envelope::CollapseRegion(double t0, double t1)
{
   // This gets called when somebody clears samples.  All of the
   // control points within the region disappear and the points
   // to the right get shifted over.

   t0 -= mOffset;
   t1 -= mOffset;

   int len = mEnv.Count();
   int i;

   for (i = 1; i < len - 1; i++)
      if (mEnv[i]->t >= t0 && mEnv[i]->t < t1) {
         delete mEnv[i];
         mEnv.RemoveAt(i);
         len--;
      }

   for (i = 0; i < len; i++)
      if (mEnv[i]->t >= t1)
         mEnv[i]->t -= (t1 - t0);
}

void Envelope::ExpandRegion(double t0, double deltat)
{
   t0 -= mOffset;

   // This gets called when somebody pastes samples.  All of the
   // control points to the right of the paste get shifted to the
   // right.  It's not always exactly what you wanted, but it's
   // at least intuitive how to fix it.  If you think about it, you'll
   // realize there's no logical way to preserve envelope information
   // across a cut/paste sequence.

   int len = mEnv.Count();

   for (int i = 0; i < len; i++)
      if (mEnv[i]->t > t0)
         mEnv[i]->t += deltat;
}

// Private methods

int Envelope::Insert(double when, double value)
{
   EnvPoint *e = new EnvPoint();
   e->t = when;
   e->val = value;

   int len = mEnv.Count();
   if (len == 0) {
      mEnv.Add(e);
      return 0;
   } else {
      int i = 0;
      while (i < len && when > mEnv[i]->t)
         i++;

      if (i < len) {
         mEnv.Insert(e, i);
         return i;
      } else {
         mEnv.Add(e);
         return len;
      }
   }
}

// Control

void Envelope::SetOffset(double newOffset)
{
   mOffset = newOffset;
}

void Envelope::SetTrackLen(double trackLen)
{
   mTrackLen = trackLen;

   int len = mEnv.Count();
   for (int i = 0; i < len - 1; i++)
      if (mEnv[i]->t > mTrackLen) {
         delete mEnv[i];
         mEnv.RemoveAt(i);
         len--;
      }
   mEnv[len - 1]->t = mTrackLen;
}

// Accessors

double Envelope::GetValue(double t)
{
   t -= mOffset;

   int len = mEnv.Count();

   if (len <= 0 || t < mEnv[0]->t || t > mEnv[len - 1]->t)
      return 1.0;

   // binary search
   int lo = 0;
   int hi = len - 1;
   while (hi > (lo + 1)) {
      int mid = (lo + hi) / 2;
      if (t < mEnv[mid]->t)
         hi = mid;
      else
         lo = mid;
   }

   double t0 = mEnv[lo]->t;
   double v0 = log10(mEnv[lo]->val);
   double t1 = mEnv[hi]->t;
   double v1 = log10(mEnv[hi]->val);

   // Special case for the log of zero
   if (mEnv[lo]->val <= 0.0)
      v0 = -4.6;                // This corresponds to the log10 of 1/32768
   if (mEnv[hi]->val <= 0.0)
      v1 = -4.6;

   // Interpolate in logspace

   double dt = (t1 - t0);

   // This should never happen, but we certainly
   // don't want to divide by zero...
   if (dt <= 0.0) {
      wxASSERT(0);
      return 1.0;
   }

   double to = t - t0;
   double v = (v0 * (dt - to) + v1 * to) / dt;

   return pow(10.0, v);
}

void Envelope::GetValues(double *buffer, int bufferLen,
                         double t0, double tstep)
{
   t0 -= mOffset;

   int len = mEnv.Count();

   double t = t0;

   double tprev, vprev, tnext, vnext, vstep;

   for (int b = 0; b < bufferLen; b++) {

      if (len <= 0 || t < mEnv[0]->t || t > mEnv[len - 1]->t) {
         buffer[b] = 1.0;
         t += tstep;
         continue;
      }

      if (b == 0 || t > tnext) {

         // binary search
         int lo = 0;
         int hi = len - 1;
         while (hi > (lo + 1)) {
            int mid = (lo + hi) / 2;
            if (t < mEnv[mid]->t)
               hi = mid;
            else
               lo = mid;
         }

         tprev = mEnv[lo]->t;
         vprev = log10(mEnv[lo]->val);
         tnext = mEnv[hi]->t;
         vnext = log10(mEnv[hi]->val);

         // Special case for the log of zero
         if (mEnv[lo]->val <= 0.0)
            vprev = -4.6;       // This corresponds to the log10 of 1/32768
         if (mEnv[hi]->val <= 0.0)
            vnext = -4.6;

         // Interpolate in logspace

         double dt = (tnext - tprev);

         double to = t - tprev;
         double v;
         if (dt > 0.0)
            v = (vprev * (dt - to) + vnext * to) / dt;
         else
            v = tnext;

         buffer[b] = pow(10.0, v);

         if (dt > 0.0)
            vstep = pow(10.0, (vnext - vprev) * tstep / dt);
         else
            vstep = 1.0;
      } else {
         buffer[b] = buffer[b - 1] * vstep;
      }

      t += tstep;
   }

}