From afbf80bfcb542d43f91f0ce889111dd24fa56379 Mon Sep 17 00:00:00 2001 From: Spekular Date: Thu, 9 Jul 2020 16:33:54 +0200 Subject: [PATCH] Refactor deleteUnusedChannels in FxMixerView (#5564) * Refactor deleteUnusedChannels in FxMixerView * Comments + style fix Co-authored-by: Veratil , formatting, suggestions on which lines to comment. Co-authored-by: Kevin Zander * Update weird deleteChannel loop * Use vector instead of array Co-authored-by: Dominic Clark Co-authored-by: Kevin Zander Co-authored-by: Dominic Clark --- src/gui/FxMixerView.cpp | 61 ++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/src/gui/FxMixerView.cpp b/src/gui/FxMixerView.cpp index 782aafcd6..fae1d5dad 100644 --- a/src/gui/FxMixerView.cpp +++ b/src/gui/FxMixerView.cpp @@ -412,12 +412,9 @@ void FxMixerView::deleteChannel(int index) m_channelAreaWidget->adjustSize(); // make sure every channel knows what index it is - for(int i=0; i index ) - { - m_fxChannelViews[i]->m_fxLine->setChannelIndex(i-1); - } + m_fxChannelViews[i]->m_fxLine->setChannelIndex(i-1); } m_fxChannelViews.remove(index); @@ -439,38 +436,32 @@ void FxMixerView::deleteUnusedChannels() tracks += Engine::getSong()->tracks(); tracks += Engine::getBBTrackContainer()->tracks(); - // go through all FX Channels + std::vector inUse(m_fxChannelViews.size(), false); + + //Populate inUse by checking the destination channel for every track + for (Track* t: tracks) + { + //The channel that this track sends to. Since master channel is always in use, + //setting this to 0 is a safe default (for tracks that don't sent to the mixer). + int channel = 0; + if (t->type() == Track::InstrumentTrack) + { + InstrumentTrack* inst = dynamic_cast(t); + channel = inst->effectChannelModel()->value(); + } + else if (t->type() == Track::SampleTrack) + { + SampleTrack *strack = dynamic_cast(t); + channel = strack->effectChannelModel()->value(); + } + inUse[channel] = true; + } + + //Check all channels except master, delete those with no incoming sends for(int i = m_fxChannelViews.size()-1; i > 0; --i) { - // check if an instrument references to the current channel - bool empty=true; - for( Track* t : tracks ) - { - if( t->type() == Track::InstrumentTrack ) - { - InstrumentTrack* inst = dynamic_cast( t ); - if( i == inst->effectChannelModel()->value(0) ) - { - empty=false; - break; - } - } - else if( t->type() == Track::SampleTrack ) - { - SampleTrack *strack = dynamic_cast( t ); - if( i == strack->effectChannelModel()->value(0) ) - { - empty=false; - break; - } - } - } - FxChannel * ch = Engine::fxMixer()->effectChannel( i ); - // delete channel if no references found - if( empty && ch->m_receives.isEmpty() ) - { - deleteChannel( i ); - } + if (!inUse[i] && Engine::fxMixer()->effectChannel(i)->m_receives.isEmpty()) + { deleteChannel(i); } } }