Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ add_library(mixxx-lib STATIC EXCLUDE_FROM_ALL
src/engine/cachingreader/cachingreader.cpp
src/engine/cachingreader/cachingreaderchunk.cpp
src/engine/cachingreader/cachingreaderworker.cpp
src/engine/channelmixer_autogen.cpp
src/engine/channelmixer.cpp
src/engine/channels/engineaux.cpp
src/engine/channels/enginechannel.cpp
src/engine/channels/enginedeck.cpp
Expand Down
91 changes: 91 additions & 0 deletions src/engine/channelmixer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "engine/channelmixer.h"

#include "util/sample.h"
#include "util/timer.h"

// static
void ChannelMixer::applyEffectsAndMixChannels(const EngineMaster::GainCalculator& gainCalculator,
QVarLengthArray<EngineMaster::ChannelInfo*, kPreallocatedChannels>* activeChannels,
QVarLengthArray<EngineMaster::GainCache, kPreallocatedChannels>* channelGainCache,
CSAMPLE* pOutput,
const ChannelHandle& outputHandle,
unsigned int iBufferSize,
unsigned int iSampleRate,
EngineEffectsManager* pEngineEffectsManager) {
// Signal flow overview:
// 1. Clear pOutput buffer
// 2. Calculate gains for each channel
// 3. Pass each channel's calculated gain and input buffer to pEngineEffectsManager, which then:
// A) Copies each channel input buffer to a temporary buffer
// B) Applies gain to the temporary buffer
// C) Processes effects on the temporary buffer
// D) Mixes the temporary buffer into pOutput
// The original channel input buffers are not modified.
SampleUtil::clear(pOutput, iBufferSize);
ScopedTimer t("EngineMaster::applyEffectsAndMixChannels");
for (int i = 0; i < activeChannels->size(); ++i) {
EngineMaster::ChannelInfo* pChannelInfo = activeChannels->at(i);
EngineMaster::GainCache& gainCache = (*channelGainCache)[pChannelInfo->m_index];
CSAMPLE_GAIN oldGain = gainCache.m_gain;
CSAMPLE_GAIN newGain;
if (gainCache.m_fadeout) {
newGain = 0;
gainCache.m_fadeout = false;
} else {
newGain = gainCalculator.getGain(pChannelInfo);
}
gainCache.m_gain = newGain;
pEngineEffectsManager->processPostFaderAndMix(pChannelInfo->m_handle,
outputHandle,
pChannelInfo->m_pBuffer,
pOutput,
iBufferSize,
iSampleRate,
pChannelInfo->m_features,
oldGain,
newGain);
}
}

void ChannelMixer::applyEffectsInPlaceAndMixChannels(
const EngineMaster::GainCalculator& gainCalculator,
QVarLengthArray<EngineMaster::ChannelInfo*, kPreallocatedChannels>*
activeChannels,
QVarLengthArray<EngineMaster::GainCache, kPreallocatedChannels>*
channelGainCache,
CSAMPLE* pOutput,
const ChannelHandle& outputHandle,
unsigned int iBufferSize,
unsigned int iSampleRate,
EngineEffectsManager* pEngineEffectsManager) {
// Signal flow overview:
// 1. Calculate gains for each channel
// 2. Pass each channel's calculated gain and input buffer to pEngineEffectsManager, which then:
// A) Applies the calculated gain to the channel buffer, modifying the original input buffer
// B) Applies effects to the buffer, modifying the original input buffer
// 4. Mix the channel buffers together to make pOutput, overwriting the pOutput buffer from the last engine callback
ScopedTimer t("EngineMaster::applyEffectsInPlaceAndMixChannels");
SampleUtil::clear(pOutput, iBufferSize);
for (int i = 0; i < activeChannels->size(); ++i) {
EngineMaster::ChannelInfo* pChannelInfo = activeChannels->at(i);
Comment on lines +69 to +70
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider replacing with ranged-for, might result in some performance benefit even.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could that provide a performance benefit?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compilers may be able to optimize better when conveying the intent better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, currently activeChannels->size() is probably called on every iteration while the ranged-for implementation would only compare the begin- and end-pointers of the iterator. I'm not saying this is a huge benefit, I just believe that we should do what compiler writers expect from us so we get better code and they can possibly produce better binaries.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability would benefit form a range based for loop. That's the main advantage, everything else is just bonus.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EngineMaster::GainCache& gainCache = (*channelGainCache)[pChannelInfo->m_index];
CSAMPLE_GAIN oldGain = gainCache.m_gain;
CSAMPLE_GAIN newGain;
if (gainCache.m_fadeout) {
newGain = 0;
gainCache.m_fadeout = false;
} else {
newGain = gainCalculator.getGain(pChannelInfo);
}
gainCache.m_gain = newGain;
pEngineEffectsManager->processPostFaderInPlace(pChannelInfo->m_handle,
outputHandle,
pChannelInfo->m_pBuffer,
iBufferSize,
iSampleRate,
pChannelInfo->m_features,
oldGain,
newGain);
SampleUtil::add(pOutput, pChannelInfo->m_pBuffer, iBufferSize);
}
}
Loading