mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
DryWetMixer: Update implementation to use SingleThreadedAbstractFifo
This commit is contained in:
parent
b0bd1c4f63
commit
abd5fe4a69
2 changed files with 28 additions and 56 deletions
|
|
@ -93,62 +93,36 @@ void DryWetMixer<SampleType>::reset()
|
||||||
|
|
||||||
dryDelayLine.reset();
|
dryDelayLine.reset();
|
||||||
|
|
||||||
offsetInBuffer = 0;
|
fifo = SingleThreadedAbstractFifo (nextPowerOfTwo (bufferDry.getNumSamples()));
|
||||||
numUsedSamples = 0;
|
bufferDry.setSize (bufferDry.getNumChannels(), fifo.getSize(), false, false, true);
|
||||||
}
|
|
||||||
|
|
||||||
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>
|
template <typename SampleType>
|
||||||
void DryWetMixer<SampleType>::pushDrySamples (const AudioBlock<const SampleType> drySamples)
|
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.getNumChannels() <= (size_t) bufferDry.getNumChannels());
|
||||||
jassert (drySamples.getNumSamples() <= remainingSpace);
|
jassert (drySamples.getNumSamples() <= (size_t) fifo.getRemainingSpace());
|
||||||
|
|
||||||
auto blocks = getFirstAndSecondPartBlocks (bufferDry,
|
auto offset = 0;
|
||||||
(offsetInBuffer + numUsedSamples) % (size_t) bufferDry.getNumSamples(),
|
|
||||||
drySamples.getNumChannels(),
|
|
||||||
jmin (drySamples.getNumSamples(), remainingSpace));
|
|
||||||
|
|
||||||
const auto processSubBlock = [this, &drySamples] (AudioBlock<SampleType> block, size_t startOffset)
|
for (const auto& range : fifo.write ((int) drySamples.getNumSamples()))
|
||||||
{
|
{
|
||||||
auto inputBlock = drySamples.getSubBlock (startOffset, block.getNumSamples());
|
if (range.getLength() == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto block = AudioBlock<SampleType> (bufferDry).getSubsetChannelBlock (0, drySamples.getNumChannels())
|
||||||
|
.getSubBlock ((size_t) range.getStart(), (size_t) range.getLength());
|
||||||
|
|
||||||
|
auto inputBlock = drySamples.getSubBlock ((size_t) offset, (size_t) range.getLength());
|
||||||
|
|
||||||
if (maximumWetLatencyInSamples == 0)
|
if (maximumWetLatencyInSamples == 0)
|
||||||
block.copyFrom (inputBlock);
|
block.copyFrom (inputBlock);
|
||||||
else
|
else
|
||||||
dryDelayLine.process (ProcessContextNonReplacing<SampleType> (inputBlock, block));
|
dryDelayLine.process (ProcessContextNonReplacing<SampleType> (inputBlock, block));
|
||||||
};
|
|
||||||
|
|
||||||
processSubBlock (blocks.first, 0);
|
offset += range.getLength();
|
||||||
|
}
|
||||||
if (blocks.second.getNumSamples() > 0)
|
|
||||||
processSubBlock (blocks.second, blocks.first.getNumSamples());
|
|
||||||
|
|
||||||
numUsedSamples += blocks.first.getNumSamples() + blocks.second.getNumSamples();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename SampleType>
|
template <typename SampleType>
|
||||||
|
|
@ -156,24 +130,22 @@ void DryWetMixer<SampleType>::mixWetSamples (AudioBlock<SampleType> inOutBlock)
|
||||||
{
|
{
|
||||||
inOutBlock.multiplyBy (wetVolume);
|
inOutBlock.multiplyBy (wetVolume);
|
||||||
|
|
||||||
jassert (inOutBlock.getNumSamples() <= numUsedSamples);
|
jassert (inOutBlock.getNumSamples() <= (size_t) fifo.getNumReadable());
|
||||||
|
|
||||||
auto blocks = getFirstAndSecondPartBlocks (bufferDry,
|
auto offset = 0;
|
||||||
offsetInBuffer,
|
|
||||||
inOutBlock.getNumChannels(),
|
|
||||||
jmin (inOutBlock.getNumSamples(), numUsedSamples));
|
|
||||||
blocks.first.multiplyBy (dryVolume);
|
|
||||||
inOutBlock.add (blocks.first);
|
|
||||||
|
|
||||||
if (blocks.second.getNumSamples() != 0)
|
for (const auto& range : fifo.read ((int) inOutBlock.getNumSamples()))
|
||||||
{
|
{
|
||||||
blocks.second.multiplyBy (dryVolume);
|
if (range.getLength() == 0)
|
||||||
inOutBlock.getSubBlock (blocks.first.getNumSamples()).add (blocks.second);
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
const auto samplesToCopy = blocks.first.getNumSamples() + blocks.second.getNumSamples();
|
auto block = AudioBlock<SampleType> (bufferDry).getSubsetChannelBlock (0, inOutBlock.getNumChannels())
|
||||||
offsetInBuffer = (offsetInBuffer + samplesToCopy) % (size_t) bufferDry.getNumSamples();
|
.getSubBlock ((size_t) range.getStart(), (size_t) range.getLength());
|
||||||
numUsedSamples -= samplesToCopy;
|
block.multiplyBy (dryVolume);
|
||||||
|
inOutBlock.getSubBlock ((size_t) offset).add (block);
|
||||||
|
|
||||||
|
offset += range.getLength();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//==============================================================================
|
//==============================================================================
|
||||||
|
|
@ -271,7 +243,7 @@ struct DryWetMixerTests : public UnitTest
|
||||||
const auto wetBuffer = getRampBuffer (spec, Kind::up);
|
const auto wetBuffer = getRampBuffer (spec, Kind::up);
|
||||||
const auto dryBuffer = getRampBuffer (spec, Kind::down);
|
const auto dryBuffer = getRampBuffer (spec, Kind::down);
|
||||||
|
|
||||||
for (auto maxLatency : { 0, 512 })
|
for (auto maxLatency : { 0, 100, 200, 512 })
|
||||||
{
|
{
|
||||||
beginTest ("Mixer can push multiple small buffers");
|
beginTest ("Mixer can push multiple small buffers");
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -109,10 +109,10 @@ private:
|
||||||
DelayLine<SampleType, DelayLineInterpolationTypes::Thiran> dryDelayLine;
|
DelayLine<SampleType, DelayLineInterpolationTypes::Thiran> dryDelayLine;
|
||||||
AudioBuffer<SampleType> bufferDry;
|
AudioBuffer<SampleType> bufferDry;
|
||||||
|
|
||||||
|
SingleThreadedAbstractFifo fifo;
|
||||||
SampleType mix = 1.0;
|
SampleType mix = 1.0;
|
||||||
MixingRule currentMixingRule = MixingRule::linear;
|
MixingRule currentMixingRule = MixingRule::linear;
|
||||||
double sampleRate = 44100.0;
|
double sampleRate = 44100.0;
|
||||||
size_t offsetInBuffer = 0, numUsedSamples = 0;
|
|
||||||
int maximumWetLatencyInSamples = 0;
|
int maximumWetLatencyInSamples = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue