Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compressor: allow level detector to process out-of-place #529

Merged
merged 1 commit into from
Apr 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,67 +24,113 @@ BallisticCoeffs<T> computeBallisticCoeffs (T timeMs, T fs)
struct PeakDetector
{
template <typename T>
static void process (const BufferView<T>& buffer, const BallisticCoeffs<T>& attackCoeffs, const BallisticCoeffs<T>& releaseCoeffs, T* z, T /* thresholdGain */) noexcept
static void process (const BufferView<const T>& inBuffer,
const BufferView<T>& outBuffer,
const BallisticCoeffs<T>& attackCoeffs,
const BallisticCoeffs<T>& releaseCoeffs,
T* z,
T /* thresholdGain */) noexcept
{
for (auto [ch, data] : buffer_iters::channels (buffer))
jassert (inBuffer.getNumChannels() == outBuffer.getNumSamples());
jassert (inBuffer.getNumSamples() == outBuffer.getNumSamples());

for (auto [ch, inData, outData] : buffer_iters::zip_channels (inBuffer, outBuffer))
{
ScopedValue _z { z[ch] };
for (auto& x : data)
for (auto [x, y] : zip (inData, outData))
{
const auto abs_x = std::abs (x);
const auto b0 = abs_x > _z.get() ? attackCoeffs.b0 : releaseCoeffs.b0;
_z.get() += b0 * (abs_x - _z.get());
x = _z.get();
y = _z.get();
}
}
}

template <typename T>
static void process (const BufferView<T>& buffer,
const BallisticCoeffs<T>& attackCoeffs,
const BallisticCoeffs<T>& releaseCoeffs,
T* z,
T thresholdGain) noexcept
{
process<T> (buffer, buffer, attackCoeffs, releaseCoeffs, z, thresholdGain);
}
};

/** A return-to-threshold peak detector */
struct PeakRtTDetector
{
template <typename T>
static void process (const BufferView<T>& buffer, const BallisticCoeffs<T>& attackCoeffs, const BallisticCoeffs<T>& releaseCoeffs, T* z, T thresholdGain) noexcept
static void process (const BufferView<const T>& inBuffer,
const BufferView<T>& outBuffer,
const BallisticCoeffs<T>& attackCoeffs,
const BallisticCoeffs<T>& releaseCoeffs,
T* z,
T thresholdGain) noexcept
{
for (auto [ch, data] : buffer_iters::channels (buffer))
jassert (inBuffer.getNumChannels() == outBuffer.getNumSamples());
jassert (inBuffer.getNumSamples() == outBuffer.getNumSamples());

for (auto [ch, inData, outData] : buffer_iters::zip_channels (inBuffer, outBuffer))
{
ScopedValue _z { z[ch] };
for (auto& x : data)
for (auto [x, y] : zip (inData, outData))
{
const auto abs_x = std::abs (x);
const auto b0 = abs_x > _z.get() ? attackCoeffs.b0 : releaseCoeffs.b0;
const auto x_eff = abs_x > _z.get() ? abs_x : thresholdGain;
_z.get() += b0 * (x_eff - _z.get());
x = _z.get();
y = _z.get();
}
}
}

template <typename T>
static void process (const BufferView<T>& buffer, const BallisticCoeffs<T>& attackCoeffs, const BallisticCoeffs<T>& releaseCoeffs, T* z, T thresholdGain) noexcept
{
process<T> (buffer, buffer, attackCoeffs, releaseCoeffs, z, thresholdGain);
}
};

/** An RMS level detector */
struct RMSDetector
{
template <typename T>
static void process (const BufferView<T>& buffer, const BallisticCoeffs<T>& attackCoeffs, const BallisticCoeffs<T>& releaseCoeffs, T* z, T /* thresholdGain */) noexcept
static void process (const BufferView<const T>& inBuffer,
const BufferView<T>& outBuffer,
const BallisticCoeffs<T>& attackCoeffs,
const BallisticCoeffs<T>& releaseCoeffs,
T* z,
T /* thresholdGain */) noexcept
{
for (auto [ch, data] : buffer_iters::channels (buffer))
jassert (inBuffer.getNumChannels() == outBuffer.getNumSamples());
jassert (inBuffer.getNumSamples() == outBuffer.getNumSamples());

for (auto [ch, inData, outData] : buffer_iters::zip_channels (inBuffer, outBuffer))
{
ScopedValue _z { z[ch] };
for (auto& x : data)
for (auto [x, y] : zip (inData, outData))
{
const auto sq_x = x * x;
const auto& coeffs = sq_x > _z.get() ? attackCoeffs : releaseCoeffs;
_z.get() = coeffs.a1 * _z.get() + coeffs.b0 * sq_x;
x = _z.get();
y = _z.get();
}
}

BufferMath::applyFunction (buffer,
BufferMath::applyFunction (outBuffer,
[] (auto x)
{
CHOWDSP_USING_XSIMD_STD (sqrt);
return sqrt (x);
});
}

template <typename T>
static void process (const BufferView<T>& buffer, const BallisticCoeffs<T>& attackCoeffs, const BallisticCoeffs<T>& releaseCoeffs, T* z, T thresholdGain) noexcept
{
process<T> (buffer, buffer, attackCoeffs, releaseCoeffs, z, thresholdGain);
}
};
} // namespace chowdsp::compressor
Loading