From ab5d77c0c4925955180dc51e9f289187ce6f2901 Mon Sep 17 00:00:00 2001 From: Mary Date: Wed, 16 Feb 2022 21:38:45 +0100 Subject: [PATCH] amadeus: Fix limiter correctness (#3126) This fixes missing audio on Nintendo Switch Sports Online Play Test. --- .../Renderer/Dsp/Command/LimiterCommandVersion1.cs | 10 +++++++--- .../Renderer/Dsp/Command/LimiterCommandVersion2.cs | 10 +++++++--- Ryujinx.Audio/Renderer/Dsp/State/LimiterState.cs | 1 + Ryujinx.Audio/Renderer/Server/CommandGenerator.cs | 11 ++++++++++- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Ryujinx.Audio/Renderer/Dsp/Command/LimiterCommandVersion1.cs b/Ryujinx.Audio/Renderer/Dsp/Command/LimiterCommandVersion1.cs index a86871ef..ffbca360 100644 --- a/Ryujinx.Audio/Renderer/Dsp/Command/LimiterCommandVersion1.cs +++ b/Ryujinx.Audio/Renderer/Dsp/Command/LimiterCommandVersion1.cs @@ -100,9 +100,11 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command { for (int sampleIndex = 0; sampleIndex < context.SampleCount; sampleIndex++) { - float inputSample = *((float*)inputBuffers[channelIndex] + sampleIndex); + float rawInputSample = *((float*)inputBuffers[channelIndex] + sampleIndex); - float sampleInputMax = Math.Abs(inputSample * Parameter.InputGain); + float inputSample = (rawInputSample / short.MaxValue) * Parameter.InputGain; + + float sampleInputMax = Math.Abs(inputSample); float inputCoefficient = Parameter.ReleaseCoefficient; @@ -131,7 +133,9 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command ref float delayedSample = ref state.DelayedSampleBuffer[channelIndex * Parameter.DelayBufferSampleCountMax + state.DelayedSampleBufferPosition[channelIndex]]; - *((float*)outputBuffers[channelIndex] + sampleIndex) = delayedSample * state.CompressionGain[channelIndex] * Parameter.OutputGain; + float outputSample = delayedSample * state.CompressionGain[channelIndex] * Parameter.OutputGain; + + *((float*)outputBuffers[channelIndex] + sampleIndex) = outputSample * short.MaxValue; delayedSample = inputSample; diff --git a/Ryujinx.Audio/Renderer/Dsp/Command/LimiterCommandVersion2.cs b/Ryujinx.Audio/Renderer/Dsp/Command/LimiterCommandVersion2.cs index 3ff758ba..e5c85b94 100644 --- a/Ryujinx.Audio/Renderer/Dsp/Command/LimiterCommandVersion2.cs +++ b/Ryujinx.Audio/Renderer/Dsp/Command/LimiterCommandVersion2.cs @@ -111,9 +111,11 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command { for (int sampleIndex = 0; sampleIndex < context.SampleCount; sampleIndex++) { - float inputSample = *((float*)inputBuffers[channelIndex] + sampleIndex); + float rawInputSample = *((float*)inputBuffers[channelIndex] + sampleIndex); - float sampleInputMax = Math.Abs(inputSample * Parameter.InputGain); + float inputSample = (rawInputSample / short.MaxValue) * Parameter.InputGain; + + float sampleInputMax = Math.Abs(inputSample); float inputCoefficient = Parameter.ReleaseCoefficient; @@ -142,7 +144,9 @@ namespace Ryujinx.Audio.Renderer.Dsp.Command ref float delayedSample = ref state.DelayedSampleBuffer[channelIndex * Parameter.DelayBufferSampleCountMax + state.DelayedSampleBufferPosition[channelIndex]]; - *((float*)outputBuffers[channelIndex] + sampleIndex) = delayedSample * state.CompressionGain[channelIndex] * Parameter.OutputGain; + float outputSample = delayedSample * state.CompressionGain[channelIndex] * Parameter.OutputGain; + + *((float*)outputBuffers[channelIndex] + sampleIndex) = outputSample * short.MaxValue; delayedSample = inputSample; diff --git a/Ryujinx.Audio/Renderer/Dsp/State/LimiterState.cs b/Ryujinx.Audio/Renderer/Dsp/State/LimiterState.cs index 53913bad..3c2bbb47 100644 --- a/Ryujinx.Audio/Renderer/Dsp/State/LimiterState.cs +++ b/Ryujinx.Audio/Renderer/Dsp/State/LimiterState.cs @@ -37,6 +37,7 @@ namespace Ryujinx.Audio.Renderer.Dsp.State DectectorAverage.AsSpan().Fill(0.0f); CompressionGain.AsSpan().Fill(1.0f); DelayedSampleBufferPosition.AsSpan().Fill(0); + DelayedSampleBuffer.AsSpan().Fill(0.0f); UpdateParameter(ref parameter); } diff --git a/Ryujinx.Audio/Renderer/Server/CommandGenerator.cs b/Ryujinx.Audio/Renderer/Server/CommandGenerator.cs index 8e4ecd25..85c019c2 100644 --- a/Ryujinx.Audio/Renderer/Server/CommandGenerator.cs +++ b/Ryujinx.Audio/Renderer/Server/CommandGenerator.cs @@ -558,7 +558,16 @@ namespace Ryujinx.Audio.Renderer.Server if (_rendererContext.BehaviourContext.IsEffectInfoVersion2Supported()) { - Memory dspResultState = _effectContext.GetDspStateMemory(effectId); + Memory dspResultState; + + if (effect.Parameter.StatisticsEnabled) + { + dspResultState = _effectContext.GetDspStateMemory(effectId); + } + else + { + dspResultState = Memory.Empty; + } _commandBuffer.GenerateLimiterEffectVersion2(bufferOffset, effect.Parameter, effect.State, dspResultState, effect.IsEnabled, workBuffer, nodeId); }