mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
DryWetMixer: Make mixers with maximum delays of 0 slightly more efficient
This commit is contained in:
parent
8f29b2cb83
commit
c27279b356
2 changed files with 233 additions and 11 deletions
|
|
@ -36,8 +36,9 @@ DryWetMixer<SampleType>::DryWetMixer()
|
|||
}
|
||||
|
||||
template <typename SampleType>
|
||||
DryWetMixer<SampleType>::DryWetMixer (int maximumWetLatencyInSamples)
|
||||
: dryDelayLine (maximumWetLatencyInSamples)
|
||||
DryWetMixer<SampleType>::DryWetMixer (int maximumWetLatencyInSamplesIn)
|
||||
: dryDelayLine (maximumWetLatencyInSamplesIn),
|
||||
maximumWetLatencyInSamples (maximumWetLatencyInSamplesIn)
|
||||
{
|
||||
dryDelayLine.setDelay (0);
|
||||
|
||||
|
|
@ -91,31 +92,88 @@ void DryWetMixer<SampleType>::reset()
|
|||
wetVolume.reset (sampleRate, 0.05);
|
||||
|
||||
dryDelayLine.reset();
|
||||
|
||||
offsetInBuffer = 0;
|
||||
numUsedSamples = 0;
|
||||
}
|
||||
|
||||
template <typename SampleType>
|
||||
struct FirstAndSecondPartBlocks
|
||||
{
|
||||
AudioBlock<SampleType> first, second;
|
||||
};
|
||||
|
||||
template <typename SampleType>
|
||||
static FirstAndSecondPartBlocks<SampleType> getFirstAndSecondPartBlocks (AudioBuffer<SampleType>& bufferDry,
|
||||
size_t firstPartStart,
|
||||
size_t channelsToCopy,
|
||||
size_t samplesToCopy)
|
||||
{
|
||||
const auto actualChannelsToCopy = jmin (channelsToCopy, (size_t) bufferDry.getNumChannels());
|
||||
const auto firstPartLength = jmin ((size_t) bufferDry.getNumSamples() - firstPartStart, samplesToCopy);
|
||||
const auto secondPartLength = samplesToCopy - firstPartLength;
|
||||
|
||||
const auto channelBlock = AudioBlock<SampleType> (bufferDry).getSubsetChannelBlock (0, actualChannelsToCopy);
|
||||
|
||||
return { channelBlock.getSubBlock (firstPartStart, firstPartLength),
|
||||
secondPartLength != 0 ? channelBlock.getSubBlock (0, samplesToCopy - firstPartLength) : AudioBlock<SampleType>() };
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
template <typename SampleType>
|
||||
void DryWetMixer<SampleType>::pushDrySamples (const AudioBlock<const SampleType> drySamples)
|
||||
{
|
||||
const auto remainingSpace = (size_t) bufferDry.getNumSamples() - numUsedSamples;
|
||||
|
||||
jassert (drySamples.getNumChannels() <= (size_t) bufferDry.getNumChannels());
|
||||
jassert (drySamples.getNumSamples() <= remainingSpace);
|
||||
|
||||
auto dryBlock = AudioBlock<SampleType> (bufferDry);
|
||||
dryBlock = dryBlock.getSubsetChannelBlock (0, drySamples.getNumChannels()).getSubBlock (0, drySamples.getNumSamples());
|
||||
auto blocks = getFirstAndSecondPartBlocks (bufferDry,
|
||||
(offsetInBuffer + numUsedSamples) % (size_t) bufferDry.getNumSamples(),
|
||||
drySamples.getNumChannels(),
|
||||
jmin (drySamples.getNumSamples(), remainingSpace));
|
||||
|
||||
auto context = ProcessContextNonReplacing<SampleType>(drySamples, dryBlock);
|
||||
dryDelayLine.process (context);
|
||||
const auto processSubBlock = [this, &drySamples] (AudioBlock<SampleType> block, size_t startOffset)
|
||||
{
|
||||
auto inputBlock = drySamples.getSubBlock (startOffset, block.getNumSamples());
|
||||
|
||||
if (maximumWetLatencyInSamples == 0)
|
||||
block.copyFrom (inputBlock);
|
||||
else
|
||||
dryDelayLine.process (ProcessContextNonReplacing<SampleType> (inputBlock, block));
|
||||
};
|
||||
|
||||
processSubBlock (blocks.first, 0);
|
||||
|
||||
if (blocks.second.getNumSamples() > 0)
|
||||
processSubBlock (blocks.second, blocks.first.getNumSamples());
|
||||
|
||||
numUsedSamples += blocks.first.getNumSamples() + blocks.second.getNumSamples();
|
||||
}
|
||||
|
||||
template <typename SampleType>
|
||||
void DryWetMixer<SampleType>::mixWetSamples (AudioBlock<SampleType> inOutBlock)
|
||||
{
|
||||
auto dryBlock = AudioBlock<SampleType> (bufferDry);
|
||||
dryBlock = dryBlock.getSubsetChannelBlock (0, inOutBlock.getNumChannels()).getSubBlock (0, inOutBlock.getNumSamples());
|
||||
|
||||
dryBlock.multiplyBy (dryVolume);
|
||||
inOutBlock.multiplyBy (wetVolume);
|
||||
|
||||
inOutBlock.add (dryBlock);
|
||||
jassert (inOutBlock.getNumSamples() <= numUsedSamples);
|
||||
|
||||
auto blocks = getFirstAndSecondPartBlocks (bufferDry,
|
||||
offsetInBuffer,
|
||||
inOutBlock.getNumChannels(),
|
||||
jmin (inOutBlock.getNumSamples(), numUsedSamples));
|
||||
blocks.first.multiplyBy (dryVolume);
|
||||
inOutBlock.add (blocks.first);
|
||||
|
||||
if (blocks.second.getNumSamples() != 0)
|
||||
{
|
||||
blocks.second.multiplyBy (dryVolume);
|
||||
inOutBlock.getSubBlock (blocks.first.getNumSamples()).add (blocks.second);
|
||||
}
|
||||
|
||||
const auto samplesToCopy = blocks.first.getNumSamples() + blocks.second.getNumSamples();
|
||||
offsetInBuffer = (offsetInBuffer + samplesToCopy) % (size_t) bufferDry.getNumSamples();
|
||||
numUsedSamples -= samplesToCopy;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
|
|
@ -175,5 +233,167 @@ void DryWetMixer<SampleType>::update()
|
|||
template class DryWetMixer<float>;
|
||||
template class DryWetMixer<double>;
|
||||
|
||||
|
||||
//==============================================================================
|
||||
//==============================================================================
|
||||
#if JUCE_UNIT_TESTS
|
||||
|
||||
struct DryWetMixerTests : public UnitTest
|
||||
{
|
||||
DryWetMixerTests() : UnitTest ("DryWetMixer", UnitTestCategories::dsp) {}
|
||||
|
||||
enum class Kind { down, up };
|
||||
|
||||
static auto getRampBuffer (ProcessSpec spec, Kind kind)
|
||||
{
|
||||
AudioBuffer<float> buffer ((int) spec.numChannels, (int) spec.maximumBlockSize);
|
||||
|
||||
for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
|
||||
{
|
||||
for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
|
||||
{
|
||||
const auto ramp = kind == Kind::up ? sample : spec.maximumBlockSize - sample;
|
||||
|
||||
buffer.setSample ((int) channel,
|
||||
(int) sample,
|
||||
jmap ((float) ramp, 0.0f, (float) spec.maximumBlockSize, 0.0f, 1.0f));
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void runTest() override
|
||||
{
|
||||
constexpr ProcessSpec spec { 44100.0, 512, 2 };
|
||||
constexpr auto numBlocks = 5;
|
||||
|
||||
const auto wetBuffer = getRampBuffer (spec, Kind::up);
|
||||
const auto dryBuffer = getRampBuffer (spec, Kind::down);
|
||||
|
||||
for (auto maxLatency : { 0, 512 })
|
||||
{
|
||||
beginTest ("Mixer can push multiple small buffers");
|
||||
{
|
||||
DryWetMixer<float> mixer (maxLatency);
|
||||
mixer.setWetMixProportion (0.5f);
|
||||
mixer.prepare (spec);
|
||||
|
||||
for (auto block = 0; block < numBlocks; ++block)
|
||||
{
|
||||
// Push samples one-by-one
|
||||
for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
|
||||
mixer.pushDrySamples (AudioBlock<const float> (dryBuffer).getSubBlock (sample, 1));
|
||||
|
||||
// Mix wet samples in one go
|
||||
auto outputBlock = wetBuffer;
|
||||
mixer.mixWetSamples ({ outputBlock });
|
||||
|
||||
// The output block should contain the wet and dry samples averaged
|
||||
for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
|
||||
{
|
||||
for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
|
||||
{
|
||||
const auto outputValue = outputBlock.getSample ((int) channel, (int) sample);
|
||||
expectWithinAbsoluteError (outputValue, 0.5f, 0.0001f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
beginTest ("Mixer can pop multiple small buffers");
|
||||
{
|
||||
DryWetMixer<float> mixer (maxLatency);
|
||||
mixer.setWetMixProportion (0.5f);
|
||||
mixer.prepare (spec);
|
||||
|
||||
for (auto block = 0; block < numBlocks; ++block)
|
||||
{
|
||||
// Push samples in one go
|
||||
mixer.pushDrySamples ({ dryBuffer });
|
||||
|
||||
// Process wet samples one-by-one
|
||||
for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
|
||||
{
|
||||
AudioBuffer<float> outputBlock ((int) spec.numChannels, 1);
|
||||
AudioBlock<const float> (wetBuffer).getSubBlock (sample, 1).copyTo (outputBlock);
|
||||
mixer.mixWetSamples ({ outputBlock });
|
||||
|
||||
// The output block should contain the wet and dry samples averaged
|
||||
for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
|
||||
{
|
||||
const auto outputValue = outputBlock.getSample ((int) channel, 0);
|
||||
expectWithinAbsoluteError (outputValue, 0.5f, 0.0001f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
beginTest ("Mixer can push and pop multiple small buffers");
|
||||
{
|
||||
DryWetMixer<float> mixer (maxLatency);
|
||||
mixer.setWetMixProportion (0.5f);
|
||||
mixer.prepare (spec);
|
||||
|
||||
for (auto block = 0; block < numBlocks; ++block)
|
||||
{
|
||||
// Push dry samples and process wet samples one-by-one
|
||||
for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
|
||||
{
|
||||
mixer.pushDrySamples (AudioBlock<const float> (dryBuffer).getSubBlock (sample, 1));
|
||||
|
||||
AudioBuffer<float> outputBlock ((int) spec.numChannels, 1);
|
||||
AudioBlock<const float> (wetBuffer).getSubBlock (sample, 1).copyTo (outputBlock);
|
||||
mixer.mixWetSamples ({ outputBlock });
|
||||
|
||||
// The output block should contain the wet and dry samples averaged
|
||||
for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
|
||||
{
|
||||
const auto outputValue = outputBlock.getSample ((int) channel, 0);
|
||||
expectWithinAbsoluteError (outputValue, 0.5f, 0.0001f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
beginTest ("Mixer can push and pop full-sized blocks after encountering a shorter block");
|
||||
{
|
||||
DryWetMixer<float> mixer (maxLatency);
|
||||
mixer.setWetMixProportion (0.5f);
|
||||
mixer.prepare (spec);
|
||||
|
||||
constexpr auto shortBlockLength = spec.maximumBlockSize / 2;
|
||||
AudioBuffer<float> shortBlock (spec.numChannels, shortBlockLength);
|
||||
mixer.pushDrySamples (AudioBlock<const float> (dryBuffer).getSubBlock (shortBlockLength));
|
||||
mixer.mixWetSamples ({ shortBlock });
|
||||
|
||||
for (auto block = 0; block < numBlocks; ++block)
|
||||
{
|
||||
// Push a full block of dry samples
|
||||
mixer.pushDrySamples ({ dryBuffer });
|
||||
|
||||
// Mix a full block of wet samples
|
||||
auto outputBlock = wetBuffer;
|
||||
mixer.mixWetSamples ({ outputBlock });
|
||||
|
||||
// The output block should contain the wet and dry samples averaged
|
||||
for (uint32_t sample = 0; sample < spec.maximumBlockSize; ++sample)
|
||||
{
|
||||
for (uint32_t channel = 0; channel < spec.numChannels; ++channel)
|
||||
{
|
||||
const auto outputValue = outputBlock.getSample ((int) channel, (int) sample);
|
||||
expectWithinAbsoluteError (outputValue, 0.5f, 0.0001f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static const DryWetMixerTests dryWetMixerTests;
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace dsp
|
||||
} // namespace juce
|
||||
|
|
|
|||
|
|
@ -112,6 +112,8 @@ private:
|
|||
SampleType mix = 1.0;
|
||||
MixingRule currentMixingRule = MixingRule::linear;
|
||||
double sampleRate = 44100.0;
|
||||
size_t offsetInBuffer = 0, numUsedSamples = 0;
|
||||
int maximumWetLatencyInSamples = 0;
|
||||
};
|
||||
|
||||
} // namespace dsp
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue