forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrack.cpp
1298 lines (1095 loc) · 32.3 KB
/
Track.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
Track.cpp
Dominic Mazzoni
*******************************************************************//**
\class Track
\brief Fundamental data object of Sneedacity, displayed in the TrackPanel.
Classes derived form it include the WaveTrack, NoteTrack, LabelTrack
and TimeTrack.
\class AudioTrack
\brief A Track that can load/save audio data to/from XML.
\class PlayableTrack
\brief An AudioTrack that can be played and stopped.
*//*******************************************************************/
#include "Track.h"
#include <algorithm>
#include <numeric>
#include <float.h>
#include <wx/file.h>
#include <wx/textfile.h>
#include <wx/log.h>
#include "tracks/ui/CommonTrackPanelCell.h"
#include "Project.h"
#include "ProjectSettings.h"
#include "InconsistencyException.h"
#ifdef _MSC_VER
//Disable truncation warnings
#pragma warning( disable : 4786 )
#endif
Track::Track()
: vrulerSize(36,0)
{
mSelected = false;
mLinked = false;
mIndex = 0;
mOffset = 0.0;
mChannel = MonoChannel;
}
Track::Track(const Track &orig)
: vrulerSize( orig.vrulerSize )
{
mIndex = 0;
Init(orig);
mOffset = orig.mOffset;
}
// Copy all the track properties except the actual contents
void Track::Init(const Track &orig)
{
mId = orig.mId;
mDefaultName = orig.mDefaultName;
mName = orig.mName;
mSelected = orig.mSelected;
mLinked = orig.mLinked;
mChannel = orig.mChannel;
}
void Track::SetName( const wxString &n )
{
if ( mName != n ) {
mName = n;
Notify();
}
}
void Track::SetSelected(bool s)
{
if (mSelected != s) {
mSelected = s;
auto pList = mList.lock();
if (pList)
pList->SelectionEvent( SharedPointer() );
}
}
void Track::EnsureVisible( bool modifyState )
{
auto pList = mList.lock();
if (pList)
pList->EnsureVisibleEvent( SharedPointer(), modifyState );
}
void Track::Merge(const Track &orig)
{
mSelected = orig.mSelected;
}
Track::Holder Track::Duplicate() const
{
// invoke "virtual constructor" to copy track object proper:
auto result = Clone();
if (mpView)
// Copy view state that might be important to undo/redo
mpView->CopyTo( *result );
return result;
}
Track::~Track()
{
}
TrackNodePointer Track::GetNode() const
{
wxASSERT(mList.lock() == NULL || this == mNode.first->get());
return mNode;
}
void Track::SetOwner
(const std::weak_ptr<TrackList> &list, TrackNodePointer node)
{
// BUG: When using this function to clear an owner, we may need to clear
// focused track too. Otherwise focus could remain on an invisible (or deleted) track.
mList = list;
mNode = node;
}
const std::shared_ptr<CommonTrackCell> &Track::GetTrackView()
{
return mpView;
}
void Track::SetTrackView( const std::shared_ptr<CommonTrackCell> &pView )
{
mpView = pView;
}
const std::shared_ptr<CommonTrackCell> &Track::GetTrackControls()
{
return mpControls;
}
void Track::SetTrackControls( const std::shared_ptr<CommonTrackCell> &pControls )
{
mpControls = pControls;
}
int Track::GetIndex() const
{
return mIndex;
}
void Track::SetIndex(int index)
{
mIndex = index;
}
void Track::SetLinked(bool l)
{
auto pList = mList.lock();
if (pList && !pList->mPendingUpdates.empty()) {
auto orig = pList->FindById( GetId() );
if (orig && orig != this) {
orig->SetLinked(l);
return;
}
}
DoSetLinked(l);
if (pList) {
pList->RecalcPositions(mNode);
pList->ResizingEvent(mNode);
}
}
void Track::DoSetLinked(bool l)
{
mLinked = l;
}
Track *Track::GetLink() const
{
auto pList = mList.lock();
if (!pList)
return nullptr;
if (!pList->isNull(mNode)) {
if (mLinked) {
auto next = pList->getNext( mNode );
if ( !pList->isNull( next ) )
return next.first->get();
}
if (mNode.first != mNode.second->begin()) {
auto prev = pList->getPrev( mNode );
if ( !pList->isNull( prev ) ) {
auto track = prev.first->get();
if (track && track->GetLinked())
return track;
}
}
}
return nullptr;
}
namespace {
inline bool IsSyncLockableNonLabelTrack( const Track *pTrack )
{
return nullptr != track_cast< const AudioTrack * >( pTrack );
}
bool IsGoodNextSyncLockTrack(const Track *t, bool inLabelSection)
{
if (!t)
return false;
const bool isLabel = ( nullptr != track_cast<const LabelTrack*>(t) );
if (inLabelSection)
return isLabel;
else if (isLabel)
return true;
else
return IsSyncLockableNonLabelTrack( t );
}
}
bool Track::IsSyncLockSelected() const
{
#ifdef EXPERIMENTAL_SYNC_LOCK
auto pList = mList.lock();
if (!pList)
return false;
auto p = pList->GetOwner();
if (!p || !ProjectSettings::Get( *p ).IsSyncLocked())
return false;
auto shTrack = this->SubstituteOriginalTrack();
if (!shTrack)
return false;
const auto pTrack = shTrack.get();
auto trackRange = TrackList::SyncLockGroup( pTrack );
if (trackRange.size() <= 1) {
// Not in a sync-locked group.
// Return true iff selected and of a sync-lockable type.
return (IsSyncLockableNonLabelTrack( pTrack ) ||
track_cast<const LabelTrack*>( pTrack )) && GetSelected();
}
// Return true iff any track in the group is selected.
return *(trackRange + &Track::IsSelected).begin();
#endif
return false;
}
void Track::Notify( int code )
{
auto pList = mList.lock();
if (pList)
pList->DataEvent( SharedPointer(), code );
}
void Track::SyncLockAdjust(double oldT1, double newT1)
{
if (newT1 > oldT1) {
// Insert space within the track
if (oldT1 > GetEndTime())
return;
auto tmp = Cut(oldT1, GetEndTime());
Paste(newT1, tmp.get());
}
else if (newT1 < oldT1) {
// Remove from the track
Clear(newT1, oldT1);
}
}
void PlayableTrack::Init( const PlayableTrack &orig )
{
mMute = orig.mMute;
mSolo = orig.mSolo;
AudioTrack::Init( orig );
}
void PlayableTrack::Merge( const Track &orig )
{
auto pOrig = dynamic_cast<const PlayableTrack *>(&orig);
wxASSERT( pOrig );
mMute = pOrig->mMute;
mSolo = pOrig->mSolo;
AudioTrack::Merge( *pOrig );
}
void PlayableTrack::SetMute( bool m )
{
if ( mMute != m ) {
mMute = m;
Notify();
}
}
void PlayableTrack::SetSolo( bool s )
{
if ( mSolo != s ) {
mSolo = s;
Notify();
}
}
// Serialize, not with tags of its own, but as attributes within a tag.
void PlayableTrack::WriteXMLAttributes(XMLWriter &xmlFile) const
{
xmlFile.WriteAttr(wxT("mute"), mMute);
xmlFile.WriteAttr(wxT("solo"), mSolo);
AudioTrack::WriteXMLAttributes(xmlFile);
}
// Return true iff the attribute is recognized.
bool PlayableTrack::HandleXMLAttribute(const wxChar *attr, const wxChar *value)
{
const wxString strValue{ value };
long nValue;
if (!wxStrcmp(attr, wxT("mute")) &&
XMLValueChecker::IsGoodInt(strValue) && strValue.ToLong(&nValue)) {
mMute = (nValue != 0);
return true;
}
else if (!wxStrcmp(attr, wxT("solo")) &&
XMLValueChecker::IsGoodInt(strValue) && strValue.ToLong(&nValue)) {
mSolo = (nValue != 0);
return true;
}
return AudioTrack::HandleXMLAttribute(attr, value);
}
bool Track::Any() const
{ return true; }
bool Track::IsSelected() const
{ return GetSelected(); }
bool Track::IsSelectedOrSyncLockSelected() const
{ return GetSelected() || IsSyncLockSelected(); }
bool Track::IsLeader() const
{ return !GetLink() || GetLinked(); }
bool Track::IsSelectedLeader() const
{ return IsSelected() && IsLeader(); }
void Track::FinishCopy
(const Track *n, Track *dest)
{
if (dest) {
dest->SetChannel(n->GetChannel());
dest->SetLinked(n->GetLinked());
dest->SetName(n->GetName());
}
}
bool Track::LinkConsistencyCheck()
{
// Sanity checks for linked tracks; unsetting the linked property
// doesn't fix the problem, but it likely leaves us with orphaned
// sample blocks instead of much worse problems.
bool err = false;
if (GetLinked())
{
Track *l = GetLink();
if (l)
{
// A linked track's partner should never itself be linked
if (l->GetLinked())
{
wxLogWarning(
wxT("Left track %s had linked right track %s with extra right track link.\n Removing extra link from right track."),
GetName(), l->GetName());
err = true;
l->SetLinked(false);
}
// Channels should be left and right
if ( !( (GetChannel() == Track::LeftChannel &&
l->GetChannel() == Track::RightChannel) ||
(GetChannel() == Track::RightChannel &&
l->GetChannel() == Track::LeftChannel) ) )
{
wxLogWarning(
wxT("Track %s and %s had left/right track links out of order. Setting tracks to not be linked."),
GetName(), l->GetName());
err = true;
SetLinked(false);
}
}
else
{
wxLogWarning(
wxT("Track %s had link to NULL track. Setting it to not be linked."),
GetName());
err = true;
SetLinked(false);
}
}
return ! err;
}
std::pair<Track *, Track *> TrackList::FindSyncLockGroup(Track *pMember) const
{
if (!pMember)
return { nullptr, nullptr };
// A non-trivial sync-locked group is a maximal sub-sequence of the tracks
// consisting of any positive number of audio tracks followed by zero or
// more label tracks.
// Step back through any label tracks.
auto member = pMember;
while (member && ( nullptr != track_cast<const LabelTrack*>(member) )) {
member = GetPrev(member);
}
// Step back through the wave and note tracks before the label tracks.
Track *first = nullptr;
while (member && IsSyncLockableNonLabelTrack(member)) {
first = member;
member = GetPrev(member);
}
if (!first)
// Can't meet the criteria described above. In that case,
// consider the track to be the sole member of a group.
return { pMember, pMember };
Track *last = first;
bool inLabels = false;
while (const auto next = GetNext(last)) {
if ( ! IsGoodNextSyncLockTrack(next, inLabels) )
break;
last = next;
inLabels = (nullptr != track_cast<const LabelTrack*>(last) );
}
return { first, last };
}
// TrackList
//
// The TrackList sends events whenever certain updates occur to the list it
// is managing. Any other classes that may be interested in get these updates
// should use TrackList::Connect() or TrackList::Bind().
//
wxDEFINE_EVENT(EVT_TRACKLIST_TRACK_DATA_CHANGE, TrackListEvent);
wxDEFINE_EVENT(EVT_TRACKLIST_SELECTION_CHANGE, TrackListEvent);
wxDEFINE_EVENT(EVT_TRACKLIST_TRACK_REQUEST_VISIBLE, TrackListEvent);
wxDEFINE_EVENT(EVT_TRACKLIST_PERMUTED, TrackListEvent);
wxDEFINE_EVENT(EVT_TRACKLIST_RESIZING, TrackListEvent);
wxDEFINE_EVENT(EVT_TRACKLIST_ADDITION, TrackListEvent);
wxDEFINE_EVENT(EVT_TRACKLIST_DELETION, TrackListEvent);
// same value as in the default constructed TrackId:
long TrackList::sCounter = -1;
static const SneedacityProject::AttachedObjects::RegisteredFactory key{
[](SneedacityProject &project) { return TrackList::Create( &project ); }
};
TrackList &TrackList::Get( SneedacityProject &project )
{
return project.AttachedObjects::Get< TrackList >( key );
}
const TrackList &TrackList::Get( const SneedacityProject &project )
{
return Get( const_cast< SneedacityProject & >( project ) );
}
TrackList::TrackList( SneedacityProject *pOwner )
: wxEvtHandler()
, mOwner{ pOwner }
{
}
// Factory function
std::shared_ptr<TrackList> TrackList::Create( SneedacityProject *pOwner )
{
return std::make_shared<TrackList>( pOwner );
}
#if 0
TrackList &TrackList::operator= (TrackList &&that)
{
if (this != &that) {
this->Clear();
Swap(that);
}
return *this;
}
#endif
void TrackList::Swap(TrackList &that)
{
auto SwapLOTs = [](
ListOfTracks &a, const std::weak_ptr< TrackList > &aSelf,
ListOfTracks &b, const std::weak_ptr< TrackList > &bSelf )
{
a.swap(b);
for (auto it = a.begin(), last = a.end(); it != last; ++it)
(*it)->SetOwner(aSelf, {it, &a});
for (auto it = b.begin(), last = b.end(); it != last; ++it)
(*it)->SetOwner(bSelf, {it, &b});
};
const auto self = shared_from_this();
const auto otherSelf = that.shared_from_this();
SwapLOTs( *this, self, that, otherSelf );
SwapLOTs( this->mPendingUpdates, self, that.mPendingUpdates, otherSelf );
mUpdaters.swap(that.mUpdaters);
}
TrackList::~TrackList()
{
Clear(false);
}
void TrackList::RecalcPositions(TrackNodePointer node)
{
if ( isNull( node ) )
return;
Track *t;
int i = 0;
auto prev = getPrev( node );
if ( !isNull( prev ) ) {
t = prev.first->get();
i = t->GetIndex() + 1;
}
const auto theEnd = end();
for (auto n = Find( node.first->get() ); n != theEnd; ++n) {
t = *n;
t->SetIndex(i++);
}
UpdatePendingTracks();
}
void TrackList::SelectionEvent( const std::shared_ptr<Track> &pTrack )
{
// wxWidgets will own the event object
QueueEvent(
safenew TrackListEvent{ EVT_TRACKLIST_SELECTION_CHANGE, pTrack } );
}
void TrackList::DataEvent( const std::shared_ptr<Track> &pTrack, int code )
{
// wxWidgets will own the event object
QueueEvent(
safenew TrackListEvent{ EVT_TRACKLIST_TRACK_DATA_CHANGE, pTrack, code } );
}
void TrackList::EnsureVisibleEvent(
const std::shared_ptr<Track> &pTrack, bool modifyState )
{
auto pEvent = std::make_unique<TrackListEvent>(
EVT_TRACKLIST_TRACK_REQUEST_VISIBLE, pTrack, 0 );
pEvent->SetInt( modifyState ? 1 : 0 );
// wxWidgets will own the event object
QueueEvent( pEvent.release() );
}
void TrackList::PermutationEvent(TrackNodePointer node)
{
// wxWidgets will own the event object
QueueEvent( safenew TrackListEvent{ EVT_TRACKLIST_PERMUTED, *node.first } );
}
void TrackList::DeletionEvent(TrackNodePointer node)
{
// wxWidgets will own the event object
QueueEvent( safenew TrackListEvent{
EVT_TRACKLIST_DELETION,
node.second && node.first != node.second->end()
? *node.first
: nullptr
} );
}
void TrackList::AdditionEvent(TrackNodePointer node)
{
// wxWidgets will own the event object
QueueEvent( safenew TrackListEvent{ EVT_TRACKLIST_ADDITION, *node.first } );
}
void TrackList::ResizingEvent(TrackNodePointer node)
{
// wxWidgets will own the event object
QueueEvent( safenew TrackListEvent{ EVT_TRACKLIST_RESIZING, *node.first } );
}
auto TrackList::EmptyRange() const
-> TrackIterRange< Track >
{
auto it = const_cast<TrackList*>(this)->getEnd();
return {
{ it, it, it, &Track::Any },
{ it, it, it, &Track::Any }
};
}
auto TrackList::SyncLockGroup( Track *pTrack )
-> TrackIterRange< Track >
{
auto pList = pTrack->GetOwner();
auto tracks =
pList->FindSyncLockGroup( const_cast<Track*>( pTrack ) );
return pList->Any().StartingWith(tracks.first).EndingAfter(tracks.second);
}
auto TrackList::FindLeader( Track *pTrack )
-> TrackIter< Track >
{
auto iter = Find(pTrack);
while( *iter && ! ( *iter )->IsLeader() )
--iter;
return iter.Filter( &Track::IsLeader );
}
void TrackList::Permute(const std::vector<TrackNodePointer> &permutation)
{
for (const auto iter : permutation) {
ListOfTracks::value_type track = *iter.first;
erase(iter.first);
Track *pTrack = track.get();
pTrack->SetOwner(shared_from_this(),
{ insert(ListOfTracks::end(), track), this });
}
auto n = getBegin();
RecalcPositions(n);
PermutationEvent(n);
}
Track *TrackList::FindById( TrackId id )
{
// Linear search. Tracks in a project are usually very few.
// Search only the non-pending tracks.
auto it = std::find_if( ListOfTracks::begin(), ListOfTracks::end(),
[=](const ListOfTracks::value_type &ptr){ return ptr->GetId() == id; } );
if (it == ListOfTracks::end())
return {};
return it->get();
}
Track *TrackList::DoAddToHead(const std::shared_ptr<Track> &t)
{
Track *pTrack = t.get();
push_front(ListOfTracks::value_type(t));
auto n = getBegin();
pTrack->SetOwner(shared_from_this(), n);
pTrack->SetId( TrackId{ ++sCounter } );
RecalcPositions(n);
AdditionEvent(n);
return front().get();
}
Track *TrackList::DoAdd(const std::shared_ptr<Track> &t)
{
push_back(t);
auto n = getPrev( getEnd() );
t->SetOwner(shared_from_this(), n);
t->SetId( TrackId{ ++sCounter } );
RecalcPositions(n);
AdditionEvent(n);
return back().get();
}
void TrackList::GroupChannels(
Track &track, size_t groupSize, bool resetChannels )
{
// If group size is exactly two, group as stereo, else mono (bug 2195).
auto list = track.mList.lock();
if ( groupSize > 0 && list.get() == this ) {
auto iter = track.mNode.first;
auto after = iter;
auto end = this->ListOfTracks::end();
auto count = groupSize;
for ( ; after != end && count; ++after, --count )
;
if ( count == 0 ) {
auto unlink = [&] ( Track &tr ) {
if ( tr.GetLinked() ) {
if ( resetChannels ) {
auto link = tr.GetLink();
if ( link )
link->SetChannel( Track::MonoChannel );
}
tr.SetLinked( false );
}
if ( resetChannels )
tr.SetChannel( Track::MonoChannel );
};
// Disassociate previous tracks -- at most one
auto pLeader = this->FindLeader( &track );
if ( *pLeader && *pLeader != &track )
unlink( **pLeader );
// First disassociate given and later tracks, then reassociate them
for ( auto iter2 = iter; iter2 != after; ++iter2 )
unlink( **iter2 );
if ( groupSize > 1 ) {
const auto channel = *iter++;
channel->SetLinked( groupSize == 2 );
channel->SetChannel( groupSize == 2? Track::LeftChannel : Track::MonoChannel );
(*iter++)->SetChannel( groupSize == 2? Track::RightChannel : Track::MonoChannel );
while (iter != after)
(*iter++)->SetChannel( Track::MonoChannel );
}
return;
}
}
// *this does not contain the track or sufficient following channels
// or group size is zero
THROW_INCONSISTENCY_EXCEPTION;
}
auto TrackList::Replace(Track * t, const ListOfTracks::value_type &with) ->
ListOfTracks::value_type
{
ListOfTracks::value_type holder;
if (t && with) {
auto node = t->GetNode();
t->SetOwner({}, {});
holder = *node.first;
Track *pTrack = with.get();
*node.first = with;
pTrack->SetOwner(shared_from_this(), node);
pTrack->SetId( t->GetId() );
RecalcPositions(node);
DeletionEvent(node);
AdditionEvent(node);
}
return holder;
}
TrackNodePointer TrackList::Remove(Track *t)
{
auto result = getEnd();
if (t) {
auto node = t->GetNode();
t->SetOwner({}, {});
if ( !isNull( node ) ) {
ListOfTracks::value_type holder = *node.first;
result = getNext( node );
erase(node.first);
if ( !isNull( result ) )
RecalcPositions(result);
DeletionEvent(result);
}
}
return result;
}
void TrackList::Clear(bool sendEvent)
{
// Null out the back-pointers to this in tracks, in case there
// are outstanding shared_ptrs to those tracks, making them outlive
// the temporary ListOfTracks below.
for ( auto pTrack: *this )
pTrack->SetOwner( {}, {} );
for ( auto pTrack: mPendingUpdates )
pTrack->SetOwner( {}, {} );
ListOfTracks tempList;
tempList.swap( *this );
ListOfTracks updating;
updating.swap( mPendingUpdates );
mUpdaters.clear();
if (sendEvent)
DeletionEvent();
}
/// Return a track in the list that comes after Track t
Track *TrackList::GetNext(Track * t, bool linked) const
{
if (t) {
auto node = t->GetNode();
if ( !isNull( node ) ) {
if ( linked && t->GetLinked() )
node = getNext( node );
if ( !isNull( node ) )
node = getNext( node );
if ( !isNull( node ) )
return node.first->get();
}
}
return nullptr;
}
Track *TrackList::GetPrev(Track * t, bool linked) const
{
if (t) {
TrackNodePointer prev;
auto node = t->GetNode();
if ( !isNull( node ) ) {
// linked is true and input track second in team?
if (linked) {
prev = getPrev( node );
if( !isNull( prev ) &&
!t->GetLinked() && t->GetLink() )
// Make it the first
node = prev;
}
prev = getPrev( node );
if ( !isNull( prev ) ) {
// Back up once
node = prev;
// Back up twice sometimes when linked is true
if (linked) {
prev = getPrev( node );
if( !isNull( prev ) &&
!(*node.first)->GetLinked() && (*node.first)->GetLink() )
node = prev;
}
return node.first->get();
}
}
}
return nullptr;
}
bool TrackList::CanMoveUp(Track * t) const
{
return GetPrev(t, true) != NULL;
}
bool TrackList::CanMoveDown(Track * t) const
{
return GetNext(t, true) != NULL;
}
// This is used when you want to swap the channel group starting
// at s1 with that starting at s2.
// The complication is that the tracks are stored in a single
// linked list.
void TrackList::SwapNodes(TrackNodePointer s1, TrackNodePointer s2)
{
// if a null pointer is passed in, we want to know about it
wxASSERT(!isNull(s1));
wxASSERT(!isNull(s2));
// Deal with first track in each team
s1 = ( * FindLeader( s1.first->get() ) )->GetNode();
s2 = ( * FindLeader( s2.first->get() ) )->GetNode();
// Safety check...
if (s1 == s2)
return;
// Be sure s1 is the earlier iterator
if ((*s1.first)->GetIndex() >= (*s2.first)->GetIndex())
std::swap(s1, s2);
// For saving the removed tracks
using Saved = std::vector< ListOfTracks::value_type >;
Saved saved1, saved2;
auto doSave = [&] ( Saved &saved, TrackNodePointer &s ) {
size_t nn = Channels( s.first->get() ).size();
saved.resize( nn );
// Save them in backwards order
while( nn-- )
saved[nn] = *s.first, s.first = erase(s.first);
};
doSave( saved1, s1 );
// The two ranges are assumed to be disjoint but might abut
const bool same = (s1 == s2);
doSave( saved2, s2 );
if (same)
// Careful, we invalidated s1 in the second doSave!
s1 = s2;
// Reinsert them
auto doInsert = [&] ( Saved &saved, TrackNodePointer &s ) {
Track *pTrack;
for (auto & pointer : saved)
pTrack = pointer.get(),
// Insert before s, and reassign s to point at the new node before
// old s; which is why we saved pointers in backwards order
pTrack->SetOwner(shared_from_this(),
s = { insert(s.first, pointer), this } );
};
// This does not invalidate s2 even when it equals s1:
doInsert( saved2, s1 );
// Even if s2 was same as s1, this correctly inserts the saved1 range
// after the saved2 range, when done after:
doInsert( saved1, s2 );
// Now correct the Index in the tracks, and other things
RecalcPositions(s1);
PermutationEvent(s1);
}
bool TrackList::MoveUp(Track * t)
{
if (t) {
Track *p = GetPrev(t, true);
if (p) {
SwapNodes(p->GetNode(), t->GetNode());
return true;
}
}
return false;
}
bool TrackList::MoveDown(Track * t)
{
if (t) {
Track *n = GetNext(t, true);
if (n) {
SwapNodes(t->GetNode(), n->GetNode());
return true;
}
}
return false;
}
bool TrackList::Contains(const Track * t) const
{
return make_iterator_range( *this ).contains( t );
}
bool TrackList::empty() const
{
return begin() == end();
}
size_t TrackList::size() const
{
int cnt = 0;
if (!empty())
cnt = getPrev( getEnd() ).first->get()->GetIndex() + 1;
return cnt;
}
namespace {
// Abstract the common pattern of the following three member functions
inline double Accumulate