forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaveTrack.cpp
2823 lines (2430 loc) · 82.7 KB
/
WaveTrack.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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
Sneedacity: A Digital Audio Editor
WaveTrack.cpp
Dominic Mazzoni
*******************************************************************//**
\class WaveTrack
\brief A Track that contains audio waveform data.
*//****************************************************************//**
\class WaveTrack::Location
\brief Used only by WaveTrack, a special way to hold location that
can accommodate merged regions.
*//****************************************************************/
/*!
@class WaveTrackFactory
@brief Used to create or clone a WaveTrack, with appropriate context
from the project that will own the track.
*/
#include "WaveTrack.h"
#include "WaveClip.h"
#include <wx/defs.h>
#include <wx/intl.h>
#include <wx/debug.h>
#include <float.h>
#include <math.h>
#include <algorithm>
#include "float_cast.h"
#include "Envelope.h"
#include "Sequence.h"
#include "ProjectFileIORegistry.h"
#include "ProjectSettings.h"
#include "Prefs.h"
#include "effects/TimeWarper.h"
#include "prefs/QualitySettings.h"
#include "prefs/SpectrogramSettings.h"
#include "prefs/TracksPrefs.h"
#include "prefs/TracksBehaviorsPrefs.h"
#include "prefs/WaveformSettings.h"
#include "InconsistencyException.h"
#include "tracks/ui/TrackView.h"
#include "tracks/ui/TrackControls.h"
using std::max;
static ProjectFileIORegistry::Entry registerFactory{
wxT( "wavetrack" ),
[]( SneedacityProject &project ){
auto &trackFactory = WaveTrackFactory::Get( project );
auto &tracks = TrackList::Get( project );
auto result = tracks.Add(trackFactory.NewWaveTrack());
TrackView::Get( *result );
TrackControls::Get( *result );
return result;
}
};
WaveTrack::Holder WaveTrackFactory::DuplicateWaveTrack(const WaveTrack &orig)
{
return std::static_pointer_cast<WaveTrack>( orig.Duplicate() );
}
WaveTrack::Holder WaveTrackFactory::NewWaveTrack(sampleFormat format, double rate)
{
if (format == (sampleFormat)0)
format = QualitySettings::SampleFormatChoice();
if (rate == 0)
rate = mSettings.GetRate();
return std::make_shared<WaveTrack> ( mpFactory, format, rate );
}
WaveTrack::WaveTrack( const SampleBlockFactoryPtr &pFactory,
sampleFormat format, double rate )
: PlayableTrack()
, mpFactory(pFactory)
{
mLegacyProjectFileOffset = 0;
mFormat = format;
mRate = (int) rate;
mGain = 1.0;
mPan = 0.0;
mOldGain[0] = 0.0;
mOldGain[1] = 0.0;
mWaveColorIndex = 0;
SetDefaultName(TracksPrefs::GetDefaultAudioTrackNamePreference());
SetName(GetDefaultName());
mDisplayMin = -1.0;
mDisplayMax = 1.0;
mSpectrumMin = mSpectrumMax = -1; // so values will default to settings
mLastScaleType = -1;
mLastdBRange = -1;
}
WaveTrack::WaveTrack(const WaveTrack &orig):
PlayableTrack(orig)
, mpFactory( orig.mpFactory )
, mpSpectrumSettings(orig.mpSpectrumSettings
? std::make_unique<SpectrogramSettings>(*orig.mpSpectrumSettings)
: nullptr
)
, mpWaveformSettings(orig.mpWaveformSettings
? std::make_unique<WaveformSettings>(*orig.mpWaveformSettings)
: nullptr
)
{
mLastScaleType = -1;
mLastdBRange = -1;
mLegacyProjectFileOffset = 0;
Init(orig);
for (const auto &clip : orig.mClips)
mClips.push_back
( std::make_unique<WaveClip>( *clip, mpFactory, true ) );
}
// Copy the track metadata but not the contents.
void WaveTrack::Init(const WaveTrack &orig)
{
PlayableTrack::Init(orig);
mpFactory = orig.mpFactory;
mFormat = orig.mFormat;
mWaveColorIndex = orig.mWaveColorIndex;
mRate = orig.mRate;
mGain = orig.mGain;
mPan = orig.mPan;
mOldGain[0] = 0.0;
mOldGain[1] = 0.0;
SetDefaultName(orig.GetDefaultName());
SetName(orig.GetName());
mDisplayMin = orig.mDisplayMin;
mDisplayMax = orig.mDisplayMax;
mSpectrumMin = orig.mSpectrumMin;
mSpectrumMax = orig.mSpectrumMax;
mDisplayLocationsCache.clear();
}
void WaveTrack::Reinit(const WaveTrack &orig)
{
Init(orig);
{
auto &settings = orig.mpSpectrumSettings;
if (settings)
mpSpectrumSettings = std::make_unique<SpectrogramSettings>(*settings);
else
mpSpectrumSettings.reset();
}
{
auto &settings = orig.mpWaveformSettings;
if (settings)
mpWaveformSettings = std::make_unique<WaveformSettings>(*settings);
else
mpWaveformSettings.reset();
}
this->SetOffset(orig.GetOffset());
}
void WaveTrack::Merge(const Track &orig)
{
orig.TypeSwitch( [&](const WaveTrack *pwt) {
const WaveTrack &wt = *pwt;
mGain = wt.mGain;
mPan = wt.mPan;
mDisplayMin = wt.mDisplayMin;
mDisplayMax = wt.mDisplayMax;
SetSpectrogramSettings(wt.mpSpectrumSettings
? std::make_unique<SpectrogramSettings>(*wt.mpSpectrumSettings) : nullptr);
SetWaveformSettings
(wt.mpWaveformSettings ? std::make_unique<WaveformSettings>(*wt.mpWaveformSettings) : nullptr);
});
PlayableTrack::Merge(orig);
}
WaveTrack::~WaveTrack()
{
}
double WaveTrack::GetOffset() const
{
return GetStartTime();
}
/*! @excsafety{No-fail} */
void WaveTrack::SetOffset(double o)
{
double delta = o - GetOffset();
for (const auto &clip : mClips)
// assume No-fail-guarantee
clip->SetOffset(clip->GetOffset() + delta);
mOffset = o;
}
auto WaveTrack::GetChannelIgnoringPan() const -> ChannelType {
return mChannel;
}
auto WaveTrack::GetChannel() const -> ChannelType
{
if( mChannel != Track::MonoChannel )
return mChannel;
auto pan = GetPan();
if( pan < -0.99 )
return Track::LeftChannel;
if( pan > 0.99 )
return Track::RightChannel;
return mChannel;
}
void WaveTrack::SetPanFromChannelType()
{
if( mChannel == Track::LeftChannel )
SetPan( -1.0f );
else if( mChannel == Track::RightChannel )
SetPan( 1.0f );
};
void WaveTrack::SetLastScaleType() const
{
mLastScaleType = GetWaveformSettings().scaleType;
}
void WaveTrack::SetLastdBRange() const
{
mLastdBRange = GetWaveformSettings().dBRange;
}
void WaveTrack::GetDisplayBounds(float *min, float *max) const
{
*min = mDisplayMin;
*max = mDisplayMax;
}
void WaveTrack::SetDisplayBounds(float min, float max) const
{
mDisplayMin = min;
mDisplayMax = max;
}
void WaveTrack::GetSpectrumBounds(float *min, float *max) const
{
const double rate = GetRate();
const SpectrogramSettings &settings = GetSpectrogramSettings();
const SpectrogramSettings::ScaleType type = settings.scaleType;
const float top = (rate / 2.);
float bottom;
if (type == SpectrogramSettings::stLinear)
bottom = 0.0f;
else if (type == SpectrogramSettings::stPeriod) {
// special case
const auto half = settings.GetFFTLength() / 2;
// EAC returns no data for below this frequency:
const float bin2 = rate / half;
bottom = bin2;
}
else
// logarithmic, etc.
bottom = 1.0f;
{
float spectrumMax = mSpectrumMax;
if (spectrumMax < 0)
spectrumMax = settings.maxFreq;
if (spectrumMax < 0)
*max = top;
else
*max = std::max(bottom, std::min(top, spectrumMax));
}
{
float spectrumMin = mSpectrumMin;
if (spectrumMin < 0)
spectrumMin = settings.minFreq;
if (spectrumMin < 0)
*min = std::max(bottom, top / 1000.0f);
else
*min = std::max(bottom, std::min(top, spectrumMin));
}
}
void WaveTrack::SetSpectrumBounds(float min, float max) const
{
mSpectrumMin = min;
mSpectrumMax = max;
}
int WaveTrack::ZeroLevelYCoordinate(wxRect rect) const
{
return rect.GetTop() +
(int)((mDisplayMax / (mDisplayMax - mDisplayMin)) * rect.height);
}
template< typename Container >
static Container MakeIntervals(const std::vector<WaveClipHolder> &clips)
{
Container result;
for (const auto &clip: clips) {
result.emplace_back( clip->GetStartTime(), clip->GetEndTime(),
std::make_unique<WaveTrack::IntervalData>( clip ) );
}
return result;
}
Track::Holder WaveTrack::PasteInto( SneedacityProject &project ) const
{
auto &trackFactory = WaveTrackFactory::Get( project );
auto &pSampleBlockFactory = trackFactory.GetSampleBlockFactory();
auto pNewTrack = EmptyCopy( pSampleBlockFactory );
pNewTrack->Paste(0.0, this);
return pNewTrack;
}
auto WaveTrack::GetIntervals() const -> ConstIntervals
{
return MakeIntervals<ConstIntervals>( mClips );
}
auto WaveTrack::GetIntervals() -> Intervals
{
return MakeIntervals<Intervals>( mClips );
}
Track::Holder WaveTrack::Clone() const
{
return std::make_shared<WaveTrack>( *this );
}
double WaveTrack::GetRate() const
{
return mRate;
}
void WaveTrack::SetRate(double newRate)
{
wxASSERT( newRate > 0 );
newRate = std::max( 1.0, newRate );
auto ratio = mRate / newRate;
mRate = (int) newRate;
for (const auto &clip : mClips) {
clip->SetRate((int)newRate);
clip->SetOffset( clip->GetOffset() * ratio );
}
}
float WaveTrack::GetGain() const
{
return mGain;
}
void WaveTrack::SetGain(float newGain)
{
if (mGain != newGain) {
mGain = newGain;
Notify();
}
}
float WaveTrack::GetPan() const
{
return mPan;
}
void WaveTrack::SetPan(float newPan)
{
if (newPan > 1.0)
newPan = 1.0;
else if (newPan < -1.0)
newPan = -1.0;
if ( mPan != newPan ) {
mPan = newPan;
Notify();
}
}
float WaveTrack::GetChannelGain(int channel) const
{
float left = 1.0;
float right = 1.0;
if (mPan < 0)
right = (mPan + 1.0);
else if (mPan > 0)
left = 1.0 - mPan;
if ((channel%2) == 0)
return left*mGain;
else
return right*mGain;
}
float WaveTrack::GetOldChannelGain(int channel) const
{
return mOldGain[channel%2];
}
void WaveTrack::SetOldChannelGain(int channel, float gain)
{
mOldGain[channel % 2] = gain;
}
/*! @excsafety{Strong} */
void WaveTrack::SetWaveColorIndex(int colorIndex)
{
for (const auto &clip : mClips)
clip->SetColourIndex( colorIndex );
mWaveColorIndex = colorIndex;
}
sampleCount WaveTrack::GetNumSamples() const
{
sampleCount result{ 0 };
for (const auto& clip : mClips)
result += clip->GetNumSamples();
return result;
}
/*! @excsafety{Weak} -- Might complete on only some clips */
void WaveTrack::ConvertToSampleFormat(sampleFormat format,
const std::function<void(size_t)> & progressReport)
{
for (const auto& clip : mClips)
clip->ConvertToSampleFormat(format, progressReport);
mFormat = format;
}
bool WaveTrack::IsEmpty(double t0, double t1) const
{
if (t0 > t1)
return true;
//wxPrintf("Searching for overlap in %.6f...%.6f\n", t0, t1);
for (const auto &clip : mClips)
{
if (!clip->BeforeClip(t1) && !clip->AfterClip(t0)) {
//wxPrintf("Overlapping clip: %.6f...%.6f\n",
// clip->GetStartTime(),
// clip->GetEndTime());
// We found a clip that overlaps this region
return false;
}
}
//wxPrintf("No overlap found\n");
// Otherwise, no clips overlap this region
return true;
}
Track::Holder WaveTrack::Cut(double t0, double t1)
{
if (t1 < t0)
THROW_INCONSISTENCY_EXCEPTION;
auto tmp = Copy(t0, t1);
Clear(t0, t1);
return tmp;
}
/*! @excsafety{Strong} */
Track::Holder WaveTrack::SplitCut(double t0, double t1)
{
if (t1 < t0)
THROW_INCONSISTENCY_EXCEPTION;
// SplitCut is the same as 'Copy', then 'SplitDelete'
auto tmp = Copy(t0, t1);
SplitDelete(t0, t1);
return tmp;
}
#if 0
Track::Holder WaveTrack::CutAndAddCutLine(double t0, double t1)
{
if (t1 < t0)
THROW_INCONSISTENCY_EXCEPTION;
// Cut is the same as 'Copy', then 'Delete'
auto tmp = Copy(t0, t1);
ClearAndAddCutLine(t0, t1);
return tmp;
}
#endif
//Trim trims within a clip, rather than trimming everything.
//If a bound is outside a clip, it trims everything.
/*! @excsafety{Weak} */
void WaveTrack::Trim (double t0, double t1)
{
bool inside0 = false;
bool inside1 = false;
//Keeps track of the offset of the first clip greater than
// the left selection t0.
double firstGreaterOffset = -1;
for (const auto &clip : mClips)
{
//Find the first clip greater than the offset.
//If we end up clipping the entire track, this is useful.
if(firstGreaterOffset < 0 &&
clip->GetStartTime() >= t0)
firstGreaterOffset = clip->GetStartTime();
if(t1 > clip->GetStartTime() && t1 < clip->GetEndTime())
{
clip->Clear(t1,clip->GetEndTime());
inside1 = true;
}
if(t0 > clip->GetStartTime() && t0 < clip->GetEndTime())
{
clip->Clear(clip->GetStartTime(),t0);
clip->SetOffset(t0);
inside0 = true;
}
}
//if inside0 is false, then the left selector was between
//clips, so DELETE everything to its left.
if(!inside1 && t1 < GetEndTime())
Clear(t1,GetEndTime());
if(!inside0 && t0 > GetStartTime())
SplitDelete(GetStartTime(), t0);
}
WaveTrack::Holder WaveTrack::EmptyCopy(
const SampleBlockFactoryPtr &pFactory ) const
{
auto result = std::make_shared<WaveTrack>( pFactory, mFormat, mRate );
result->Init(*this);
result->mpFactory = pFactory ? pFactory : mpFactory;
return result;
}
Track::Holder WaveTrack::Copy(double t0, double t1, bool forClipboard) const
{
if (t1 < t0)
THROW_INCONSISTENCY_EXCEPTION;
auto result = EmptyCopy();
WaveTrack *newTrack = result.get();
// PRL: Why shouldn't cutlines be copied and pasted too? I don't know, but
// that was the old behavior. But this function is also used by the
// Duplicate command and I changed its behavior in that case.
for (const auto &clip : mClips)
{
if (t0 <= clip->GetStartTime() && t1 >= clip->GetEndTime())
{
// Whole clip is in copy region
//wxPrintf("copy: clip %i is in copy region\n", (int)clip);
newTrack->mClips.push_back
(std::make_unique<WaveClip>(*clip, mpFactory, ! forClipboard));
WaveClip *const newClip = newTrack->mClips.back().get();
newClip->Offset(-t0);
}
else if (t1 > clip->GetStartTime() && t0 < clip->GetEndTime())
{
// Clip is affected by command
//wxPrintf("copy: clip %i is affected by command\n", (int)clip);
const double clip_t0 = std::max(t0, clip->GetStartTime());
const double clip_t1 = std::min(t1, clip->GetEndTime());
auto newClip = std::make_unique<WaveClip>
(*clip, mpFactory, ! forClipboard, clip_t0, clip_t1);
//wxPrintf("copy: clip_t0=%f, clip_t1=%f\n", clip_t0, clip_t1);
newClip->Offset(-t0);
if (newClip->GetOffset() < 0)
newClip->SetOffset(0);
newTrack->mClips.push_back(std::move(newClip)); // transfer ownership
}
}
// AWD, Oct 2009: If the selection ends in whitespace, create a placeholder
// clip representing that whitespace
// PRL: Only if we want the track for pasting into other tracks. Not if it
// goes directly into a project as in the Duplicate command.
if (forClipboard &&
newTrack->GetEndTime() + 1.0 / newTrack->GetRate() < t1 - t0)
{
auto placeholder = std::make_unique<WaveClip>(mpFactory,
newTrack->GetSampleFormat(),
static_cast<int>(newTrack->GetRate()),
0 /*colourindex*/);
placeholder->SetIsPlaceholder(true);
placeholder->InsertSilence(0, (t1 - t0) - newTrack->GetEndTime());
placeholder->Offset(newTrack->GetEndTime());
newTrack->mClips.push_back(std::move(placeholder)); // transfer ownership
}
return result;
}
Track::Holder WaveTrack::CopyNonconst(double t0, double t1)
{
return Copy(t0, t1);
}
/*! @excsafety{Strong} */
void WaveTrack::Clear(double t0, double t1)
{
HandleClear(t0, t1, false, false);
}
/*! @excsafety{Strong} */
void WaveTrack::ClearAndAddCutLine(double t0, double t1)
{
HandleClear(t0, t1, true, false);
}
const SpectrogramSettings &WaveTrack::GetSpectrogramSettings() const
{
if (mpSpectrumSettings)
return *mpSpectrumSettings;
else
return SpectrogramSettings::defaults();
}
SpectrogramSettings &WaveTrack::GetSpectrogramSettings()
{
if (mpSpectrumSettings)
return *mpSpectrumSettings;
else
return SpectrogramSettings::defaults();
}
SpectrogramSettings &WaveTrack::GetIndependentSpectrogramSettings()
{
if (!mpSpectrumSettings)
mpSpectrumSettings =
std::make_unique<SpectrogramSettings>(SpectrogramSettings::defaults());
return *mpSpectrumSettings;
}
void WaveTrack::SetSpectrogramSettings(std::unique_ptr<SpectrogramSettings> &&pSettings)
{
if (mpSpectrumSettings != pSettings) {
mpSpectrumSettings = std::move(pSettings);
}
}
void WaveTrack::UseSpectralPrefs( bool bUse )
{
if( bUse ){
if( !mpSpectrumSettings )
return;
// reset it, and next we will be getting the defaults.
mpSpectrumSettings.reset();
}
else {
if( mpSpectrumSettings )
return;
GetIndependentSpectrogramSettings();
}
}
const WaveformSettings &WaveTrack::GetWaveformSettings() const
{
// Create on demand
return const_cast<WaveTrack*>(this)->GetWaveformSettings();
}
WaveformSettings &WaveTrack::GetWaveformSettings()
{
// Create on demand
if (!mpWaveformSettings)
mpWaveformSettings = std::make_unique<WaveformSettings>(WaveformSettings::defaults());
return *mpWaveformSettings;
}
void WaveTrack::SetWaveformSettings(std::unique_ptr<WaveformSettings> &&pSettings)
{
if (mpWaveformSettings != pSettings) {
mpWaveformSettings = std::move(pSettings);
}
}
//
// ClearAndPaste() is a specialized version of HandleClear()
// followed by Paste() and is used mostly by effects that
// can't replace track data directly using Get()/Set().
//
// HandleClear() removes any cut/split lines with the
// cleared range, but, in most cases, effects want to preserve
// the existing cut/split lines, so they are saved before the
// HandleClear()/Paste() and restored after.
//
// If the pasted track overlaps two or more clips, then it will
// be pasted with visible split lines. Normally, effects do not
// want these extra lines, so they may be merged out.
//
/*! @excsafety{Weak} -- This WaveTrack remains destructible in case of SneedacityException.
But some of its cutline clips may have been destroyed. */
void WaveTrack::ClearAndPaste(double t0, // Start of time to clear
double t1, // End of time to clear
const Track *src, // What to paste
bool preserve, // Whether to reinsert splits/cuts
bool merge, // Whether to remove 'extra' splits
const TimeWarper *effectWarper // How does time change
)
{
double dur = std::min(t1 - t0, src->GetEndTime());
// If duration is 0, then it's just a plain paste
if (dur == 0.0) {
// use Weak-guarantee
Paste(t0, src);
return;
}
std::vector<EnvPoint> envPoints;
std::vector<double> splits;
WaveClipHolders cuts;
// If provided time warper was NULL, use a default one that does nothing
IdentityTimeWarper localWarper;
const TimeWarper *warper = (effectWarper ? effectWarper : &localWarper);
// Align to a sample
t0 = LongSamplesToTime(TimeToLongSamples(t0));
t1 = LongSamplesToTime(TimeToLongSamples(t1));
// Save the cut/split lines whether preserving or not since merging
// needs to know if a clip boundary is being crossed since Paste()
// will add split lines around the pasted clip if so.
for (const auto &clip : mClips) {
double st;
// Remember clip boundaries as locations to split
st = LongSamplesToTime(TimeToLongSamples(clip->GetStartTime()));
if (st >= t0 && st <= t1 && !make_iterator_range(splits).contains(st)) {
splits.push_back(st);
}
st = LongSamplesToTime(TimeToLongSamples(clip->GetEndTime()));
if (st >= t0 && st <= t1 && !make_iterator_range(splits).contains(st)) {
splits.push_back(st);
}
// Search for cut lines
auto &cutlines = clip->GetCutLines();
// May erase from cutlines, so don't use range-for
for (auto it = cutlines.begin(); it != cutlines.end(); ) {
WaveClip *cut = it->get();
double cs = LongSamplesToTime(TimeToLongSamples(clip->GetOffset() +
cut->GetOffset()));
// Remember cut point
if (cs >= t0 && cs <= t1) {
// Remember the absolute offset and add to our cuts array.
cut->SetOffset(cs);
cuts.push_back(std::move(*it)); // transfer ownership!
it = cutlines.erase(it);
}
else
++it;
}
// Save the envelope points
const auto &env = *clip->GetEnvelope();
for (size_t i = 0, numPoints = env.GetNumberOfPoints(); i < numPoints; ++i) {
envPoints.push_back(env[i]);
}
}
const auto tolerance = 2.0 / GetRate();
// Now, clear the selection
HandleClear(t0, t1, false, false);
{
// And paste in the NEW data
Paste(t0, src);
{
// First, merge the NEW clip(s) in with the existing clips
if (merge && splits.size() > 0)
{
// Now t1 represents the absolute end of the pasted data.
t1 = t0 + src->GetEndTime();
// Get a sorted array of the clips
auto clips = SortedClipArray();
// Scan the sorted clips for the first clip whose start time
// exceeds the pasted regions end time.
{
WaveClip *prev = nullptr;
for (const auto clip : clips) {
// Merge this clip and the previous clip if the end time
// falls within it and this isn't the first clip in the track.
if (fabs(t1 - clip->GetStartTime()) < tolerance) {
if (prev)
MergeClips(GetClipIndex(prev), GetClipIndex(clip));
break;
}
prev = clip;
}
}
}
// Refill the array since clips have changed.
auto clips = SortedClipArray();
{
// Scan the sorted clips to look for the start of the pasted
// region.
WaveClip *prev = nullptr;
for (const auto clip : clips) {
if (prev) {
// It must be that clip is what was pasted and it begins where
// prev ends.
// use Weak-guarantee
MergeClips(GetClipIndex(prev), GetClipIndex(clip));
break;
}
if (fabs(t0 - clip->GetEndTime()) < tolerance)
// Merge this clip and the next clip if the start time
// falls within it and this isn't the last clip in the track.
prev = clip;
else
prev = nullptr;
}
}
}
// Restore cut/split lines
if (preserve) {
// Restore the split lines, transforming the position appropriately
for (const auto split: splits) {
SplitAt(warper->Warp(split));
}
// Restore the saved cut lines, also transforming if time altered
for (const auto &clip : mClips) {
double st;
double et;
st = clip->GetStartTime();
et = clip->GetEndTime();
// Scan the cuts for any that live within this clip
for (auto it = cuts.begin(); it != cuts.end();) {
WaveClip *cut = it->get();
double cs = cut->GetOffset();
// Offset the cut from the start of the clip and add it to
// this clips cutlines.
if (cs >= st && cs <= et) {
cut->SetOffset(warper->Warp(cs) - st);
clip->GetCutLines().push_back( std::move(*it) ); // transfer ownership!
it = cuts.erase(it);
}
else
++it;
}
}
}
// Restore the envelope points
for (auto point : envPoints) {
auto t = warper->Warp(point.GetT());
if (auto clip = GetClipAtTime(t))
clip->GetEnvelope()->Insert(t, point.GetVal());
}
}
}
/*! @excsafety{Strong} */
void WaveTrack::SplitDelete(double t0, double t1)
{
bool addCutLines = false;
bool split = true;
HandleClear(t0, t1, addCutLines, split);
}
namespace
{
WaveClipHolders::const_iterator
FindClip(const WaveClipHolders &list, const WaveClip *clip, int *distance = nullptr)
{
if (distance)
*distance = 0;
auto it = list.begin();
for (const auto end = list.end(); it != end; ++it)
{
if (it->get() == clip)
break;
if (distance)
++*distance;
}
return it;
}
WaveClipHolders::iterator
FindClip(WaveClipHolders &list, const WaveClip *clip, int *distance = nullptr)
{
if (distance)
*distance = 0;
auto it = list.begin();
for (const auto end = list.end(); it != end; ++it)
{
if (it->get() == clip)
break;
if (distance)
++*distance;
}
return it;
}
}
std::shared_ptr<WaveClip> WaveTrack::RemoveAndReturnClip(WaveClip* clip)
{
// Be clear about who owns the clip!!
auto it = FindClip(mClips, clip);
if (it != mClips.end()) {
auto result = std::move(*it); // Array stops owning the clip, before we shrink it
mClips.erase(it);
return result;
}
else
return {};
}
bool WaveTrack::AddClip(const std::shared_ptr<WaveClip> &clip)
{
if (clip->GetSequence()->GetFactory() != this->mpFactory)
return false;
// Uncomment the following line after we correct the problem of zero-length clips
//if (CanInsertClip(clip))
mClips.push_back(clip); // transfer ownership
return true;
}
/*! @excsafety{Strong} */
void WaveTrack::HandleClear(double t0, double t1,
bool addCutLines, bool split)
{
// For debugging, use an ASSERT so that we stop
// closer to the problem.
wxASSERT( t1 >= t0 );
if (t1 < t0)