1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-21 15:08:01 +02:00

Simplify some internals of TrackList, don't assume at most 2 channels

This commit is contained in:
Paul Licameli 2017-01-09 14:14:15 -05:00
parent 7ec734b087
commit 1a59ba6331

View File

@ -129,16 +129,11 @@ void Track::SetOwner
int Track::GetMinimizedHeight() const int Track::GetMinimizedHeight() const
{ {
auto height = TrackInfo::MinimumTrackHeight(); auto height = TrackInfo::MinimumTrackHeight();
auto channels = TrackList::Channels(this);
if (GetLink()) { auto nChannels = channels.size();
auto halfHeight = height / 2; auto begin = channels.begin();
if (GetLinked()) auto index = std::distance(begin, std::find(begin, channels.end(), this));
return halfHeight; return (height * (index + 1) / nChannels) - (height * index / nChannels);
else
return height - halfHeight;
}
return height;
} }
int Track::GetIndex() const int Track::GetIndex() const
@ -320,18 +315,19 @@ bool Track::IsSyncLockSelected() const
return false; return false;
auto pList = mList.lock(); auto pList = mList.lock();
SyncLockedTracksIterator git(pList.get()); if (!pList)
Track *t = git.StartWith(const_cast<Track*>(this)); return false;
auto trackRange = TrackList::SyncLockGroup(this);
if (!t) { if (trackRange.size() <= 1) {
// Not in a sync-locked group. // Not in a sync-locked group.
return ((this->GetKind() == Track::Wave) || (this->GetKind() == Track::Label)) && GetSelected(); // Return true iff selected and of a sync-lockable type.
return (IsSyncLockableNonLabelTrack(this) ||
track_cast<const LabelTrack*>(this)) && GetSelected();
} }
for (; t; t = git.Next()) { // Return true iff any track in the group is selected.
if (t->GetSelected()) return *(trackRange + &Track::IsSelected).begin();
return true;
}
#endif #endif
return false; return false;
@ -1138,13 +1134,7 @@ Track *TrackList::GetPrev(Track * t, bool linked) const
/// For stereo track combined height of both channels. /// For stereo track combined height of both channels.
int TrackList::GetGroupHeight(const Track * t) const int TrackList::GetGroupHeight(const Track * t) const
{ {
int height = t->GetHeight(); return Channels(t).sum( &Track::GetHeight );
t = t->GetLink();
if (t) {
height += t->GetHeight();
}
return height;
} }
bool TrackList::CanMoveUp(Track * t) const bool TrackList::CanMoveUp(Track * t) const
@ -1268,13 +1258,7 @@ size_t TrackList::size() const
TimeTrack *TrackList::GetTimeTrack() TimeTrack *TrackList::GetTimeTrack()
{ {
auto iter = std::find_if(begin(), end(), return *Any<TimeTrack>().begin();
[] ( Track *t ) { return t->GetKind() == Track::Time; }
);
if (iter == end())
return nullptr;
else
return static_cast<TimeTrack*>(*iter);
} }
const TimeTrack *TrackList::GetTimeTrack() const const TimeTrack *TrackList::GetTimeTrack() const
@ -1289,23 +1273,13 @@ unsigned TrackList::GetNumExportChannels(bool selectionOnly) const
int numRight = 0; int numRight = 0;
//int numMono = 0; //int numMono = 0;
/* track iteration kit */ /* track iteration kit */
const Track *tr;
TrackListConstIterator iter;
for (tr = iter.First(this); tr != NULL; tr = iter.Next()) {
// Want only unmuted wave tracks. // Want only unmuted wave tracks.
auto wt = static_cast<const WaveTrack *>(tr); for (auto tr :
if ((tr->GetKind() != Track::Wave) || Any< const WaveTrack >()
wt->GetMute()) + (selectionOnly ? &Track::IsSelected : &Track::Any)
continue; - &WaveTrack::GetMute
) {
// do we only want selected ones?
if (selectionOnly && !(tr->GetSelected())) {
//want selected but this one is not
continue;
}
// Found a left channel // Found a left channel
if (tr->GetChannel() == Track::LeftChannel) { if (tr->GetChannel() == Track::LeftChannel) {
numLeft++; numLeft++;
@ -1318,7 +1292,7 @@ unsigned TrackList::GetNumExportChannels(bool selectionOnly) const
// Found a mono channel, but it may be panned // Found a mono channel, but it may be panned
else if (tr->GetChannel() == Track::MonoChannel) { else if (tr->GetChannel() == Track::MonoChannel) {
float pan = ((WaveTrack*)tr)->GetPan(); float pan = tr->GetPan();
// Figure out what kind of channel it should be // Figure out what kind of channel it should be
if (pan == -1.0) { // panned hard left if (pan == -1.0) { // panned hard left
@ -1346,51 +1320,43 @@ unsigned TrackList::GetNumExportChannels(bool selectionOnly) const
} }
namespace { namespace {
template<typename Array> template<typename Array, typename TrackRange>
Array GetWaveTracks(TrackListIterator p, const TrackListIterator end, Array GetTypedTracks(const TrackRange &trackRange,
bool selectionOnly, bool includeMuted) bool selectionOnly, bool includeMuted)
{ {
Array waveTrackArray; Array array;
for (; p != end; ++p) { using Type = typename
const auto &track = *p; std::remove_reference< decltype( *array[0] ) >::type;
auto wt = static_cast<const WaveTrack *>(&*track); auto subRange =
if (track->GetKind() == Track::Wave && trackRange.template Filter<Type>();
(includeMuted || !wt->GetMute()) && if ( selectionOnly )
(track->GetSelected() || !selectionOnly)) { subRange = subRange + &Track::IsSelected;
waveTrackArray.push_back( Track::Pointer< WaveTrack >( track ) ); if ( ! includeMuted )
} subRange = subRange - &Type::GetMute;
} std::transform(
subRange.begin(), subRange.end(), std::back_inserter( array ),
[]( Type *t ){ return Track::Pointer<Type>( t ); }
);
return waveTrackArray; return array;
} }
} }
WaveTrackArray TrackList::GetWaveTrackArray(bool selectionOnly, bool includeMuted) WaveTrackArray TrackList::GetWaveTrackArray(bool selectionOnly, bool includeMuted)
{ {
return GetWaveTracks<WaveTrackArray>(begin(), end(), selectionOnly, includeMuted); return GetTypedTracks<WaveTrackArray>(Any(), selectionOnly, includeMuted);
} }
WaveTrackConstArray TrackList::GetWaveTrackConstArray(bool selectionOnly, bool includeMuted) const WaveTrackConstArray TrackList::GetWaveTrackConstArray(bool selectionOnly, bool includeMuted) const
{ {
auto list = const_cast<TrackList*>(this); return GetTypedTracks<WaveTrackConstArray>(Any(), selectionOnly, includeMuted);
return GetWaveTracks<WaveTrackConstArray>(
list->begin(), list->end(), selectionOnly, includeMuted);
} }
#if defined(USE_MIDI) #if defined(USE_MIDI)
NoteTrackConstArray TrackList::GetNoteTrackConstArray(bool selectionOnly) const NoteTrackConstArray TrackList::GetNoteTrackConstArray(bool selectionOnly) const
{ {
NoteTrackConstArray noteTrackArray; return GetTypedTracks<NoteTrackConstArray>(Any(), selectionOnly, true);
for(const auto &track : *this) {
if (track->GetKind() == Track::Note &&
(track->GetSelected() || !selectionOnly)) {
noteTrackArray.push_back( Track::Pointer<const NoteTrack>(track) );
}
}
return noteTrackArray;
} }
#endif #endif
@ -1408,12 +1374,11 @@ int TrackList::GetHeight() const
namespace { namespace {
// Abstract the common pattern of the following three member functions // Abstract the common pattern of the following three member functions
double doubleMin(double a, double b) { return std::min(a, b); }
double doubleMax(double a, double b) { return std::max(a, b); }
inline double Accumulate inline double Accumulate
(const TrackList &list, (const TrackList &list,
double (Track::*memfn)() const, double (Track::*memfn)() const,
double (*combine)(double, double)) double ident,
const double &(*combine)(const double&, const double&))
{ {
// Default the answer to zero for empty list // Default the answer to zero for empty list
if (list.empty()) { if (list.empty()) {
@ -1421,28 +1386,23 @@ namespace {
} }
// Otherwise accumulate minimum or maximum of track values // Otherwise accumulate minimum or maximum of track values
auto iter = list.begin(); return list.Any().accumulate(ident, combine, memfn);
double acc = (**iter++.*memfn)();
return std::accumulate(iter, list.end(), acc,
[=](double acc, const Track *pTrack) {
return combine(acc, (*pTrack.*memfn)());
});
} }
} }
double TrackList::GetMinOffset() const double TrackList::GetMinOffset() const
{ {
return Accumulate(*this, &Track::GetOffset, doubleMin); return Accumulate(*this, &Track::GetOffset, DBL_MAX, std::min);
} }
double TrackList::GetStartTime() const double TrackList::GetStartTime() const
{ {
return Accumulate(*this, &Track::GetStartTime, doubleMin); return Accumulate(*this, &Track::GetStartTime, DBL_MAX, std::min);
} }
double TrackList::GetEndTime() const double TrackList::GetEndTime() const
{ {
return Accumulate(*this, &Track::GetEndTime, doubleMax); return Accumulate(*this, &Track::GetEndTime, -DBL_MAX, std::max);
} }
std::shared_ptr<Track> std::shared_ptr<Track>