From 33cc4a65181b1907153e50c7bec65613a9c8a5e6 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Mon, 10 Feb 2020 14:16:21 +0100 Subject: [PATCH] Changes to the mono and stereo processes - Corrected possible issues in the computation - Removed an unnecessary copy in the mono process - Trying to make the processes clearer --- src/sfizz/Voice.cpp | 74 +++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/src/sfizz/Voice.cpp b/src/sfizz/Voice.cpp index d200a1c9..fbe2674e 100644 --- a/src/sfizz/Voice.cpp +++ b/src/sfizz/Voice.cpp @@ -290,23 +290,23 @@ void sfz::Voice::processMono(AudioSpan buffer) noexcept auto leftBuffer = buffer.getSpan(0); auto rightBuffer = buffer.getSpan(1); - auto span1 = tempSpan1.first(numSamples); + auto modulationSpan = tempSpan1.first(numSamples); // Amplitude envelope - amplitudeEnvelope.getBlock(span1); - applyGain(span1, leftBuffer); + amplitudeEnvelope.getBlock(modulationSpan); + applyGain(modulationSpan, leftBuffer); // Crossfade envelope - crossfadeEnvelope.getBlock(span1); - applyGain(span1, leftBuffer); + crossfadeEnvelope.getBlock(modulationSpan); + applyGain(modulationSpan, leftBuffer); // Volume envelope - volumeEnvelope.getBlock(span1); - applyGain(span1, leftBuffer); + volumeEnvelope.getBlock(modulationSpan); + applyGain(modulationSpan, leftBuffer); // AmpEG envelope - egEnvelope.getBlock(span1); - applyGain(span1, leftBuffer); + egEnvelope.getBlock(modulationSpan); + applyGain(modulationSpan, leftBuffer); // Filtering and EQ const float* inputChannel[1] { leftBuffer.data() }; @@ -322,59 +322,67 @@ void sfz::Voice::processMono(AudioSpan buffer) noexcept // Prepare for stereo output copy(leftBuffer, rightBuffer); - panEnvelope.getBlock(span1); - copy(leftBuffer, rightBuffer); - pan(span1, leftBuffer, rightBuffer); + // Apply panning + panEnvelope.getBlock(modulationSpan); + pan(modulationSpan, leftBuffer, rightBuffer); } void sfz::Voice::processStereo(AudioSpan buffer) noexcept { const auto numSamples = buffer.getNumFrames(); - auto span1 = tempSpan1.first(numSamples); + auto modulationSpan = tempSpan1.first(numSamples); auto leftBuffer = buffer.getSpan(0); auto rightBuffer = buffer.getSpan(1); // Amplitude envelope - amplitudeEnvelope.getBlock(span1); - buffer.applyGain(span1); + amplitudeEnvelope.getBlock(modulationSpan); + buffer.applyGain(modulationSpan); // Crossfade envelope - crossfadeEnvelope.getBlock(span1); - buffer.applyGain(span1); + crossfadeEnvelope.getBlock(modulationSpan); + buffer.applyGain(modulationSpan); // Volume envelope - volumeEnvelope.getBlock(span1); - buffer.applyGain(span1); + volumeEnvelope.getBlock(modulationSpan); + buffer.applyGain(modulationSpan); // AmpEG envelope - egEnvelope.getBlock(span1); - buffer.applyGain(span1); + egEnvelope.getBlock(modulationSpan); + buffer.applyGain(modulationSpan); // Create mid/side from left/right in the output buffer - copy(rightBuffer, span1); - add(leftBuffer, rightBuffer); - subtract(span1, leftBuffer); - // Add const aliases to be slightly more readable + const auto leftBufferCopy = tempSpan2.first(numSamples); + copy(leftBuffer, leftBufferCopy); + const auto midBuffer = leftBuffer; + add(rightBuffer, midBuffer); + const auto sideBuffer = rightBuffer; + applyGain(-1.0f, sideBuffer); + add(leftBufferCopy, sideBuffer); + applyGain(sqrtTwoInv, midBuffer); applyGain(sqrtTwoInv, sideBuffer); // Apply the width process - widthEnvelope.getBlock(span1); - width(span1, midBuffer, sideBuffer); + widthEnvelope.getBlock(modulationSpan); + width(modulationSpan, midBuffer, sideBuffer); // Copy the mid channel into another span - const auto midBufferRight = tempSpan2.first(numSamples); - copy(midBuffer, midBufferRight); - positionEnvelope.getBlock(span1); - pan(span1, midBuffer, midBufferRight); + const auto midBufferCopy = tempSpan3.first(numSamples); + copy(midBuffer, midBufferCopy); + positionEnvelope.getBlock(modulationSpan); + pan(modulationSpan, midBuffer, midBufferCopy); // Rebuild left/right - add(sideBuffer, midBuffer); + // Recall that midBuffer and leftBuffer point to the same buffer + add(sideBuffer, leftBuffer); applyGain(sqrtTwoInv, leftBuffer); - add(midBufferRight, sideBuffer); + + // Recall that sideBuffer and rightBuffer point to the same buffer + applyGain(-1.0f, sideBuffer); + add(midBufferCopy, sideBuffer); applyGain(sqrtTwoInv, rightBuffer); // Filtering and EQ