mirror of
https://git.naxdy.org/Mirror/Ryujinx.git
synced 2025-03-25 09:20:17 +00:00
* dotnet format style --severity info Some changes were manually reverted. * dotnet format analyzers --serverity info Some changes have been minimally adapted. * Restore a few unused methods and variables * Silence dotnet format IDE0060 warnings * Silence dotnet format IDE0052 warnings * Address dotnet format CA1816 warnings * Address or silence dotnet format CA2208 warnings * Address or silence dotnet format CA2211 warnings * Address review comments * Address most dotnet format whitespace warnings * Apply dotnet format whitespace formatting A few of them have been manually reverted and the corresponding warning was silenced * Format if-blocks correctly * Run dotnet format whitespace after rebase * Run dotnet format after rebase and remove unused usings - analyzers - style - whitespace * Add comments to disabled warnings * Remove a few unused parameters * Simplify properties and array initialization, Use const when possible, Remove trailing commas * Start working on disabled warnings * Fix and silence a few dotnet-format warnings again * Address IDE0251 warnings * Silence IDE0060 in .editorconfig * Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas" This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e. * dotnet format whitespace after rebase * Fix naming rule violations, remove redundant code and fix build issues * Apply suggestions from code review Co-authored-by: Ac_K <Acoustik666@gmail.com> * Add trailing commas * Apply suggestions from code review Co-authored-by: Ac_K <Acoustik666@gmail.com> * Address review feedback --------- Co-authored-by: Ac_K <Acoustik666@gmail.com>
145 lines
5.6 KiB
C#
145 lines
5.6 KiB
C#
using Ryujinx.Audio.Renderer.Dsp.State;
|
|
using Ryujinx.Audio.Renderer.Parameter.Effect;
|
|
using Ryujinx.Audio.Renderer.Server.Effect;
|
|
using System;
|
|
using System.Diagnostics;
|
|
|
|
namespace Ryujinx.Audio.Renderer.Dsp.Command
|
|
{
|
|
public class LimiterCommandVersion1 : ICommand
|
|
{
|
|
public bool Enabled { get; set; }
|
|
|
|
public int NodeId { get; }
|
|
|
|
public CommandType CommandType => CommandType.LimiterVersion1;
|
|
|
|
public uint EstimatedProcessingTime { get; set; }
|
|
|
|
public LimiterParameter Parameter => _parameter;
|
|
public Memory<LimiterState> State { get; }
|
|
public ulong WorkBuffer { get; }
|
|
public ushort[] OutputBufferIndices { get; }
|
|
public ushort[] InputBufferIndices { get; }
|
|
public bool IsEffectEnabled { get; }
|
|
|
|
private LimiterParameter _parameter;
|
|
|
|
public LimiterCommandVersion1(uint bufferOffset, LimiterParameter parameter, Memory<LimiterState> state, bool isEnabled, ulong workBuffer, int nodeId)
|
|
{
|
|
Enabled = true;
|
|
NodeId = nodeId;
|
|
_parameter = parameter;
|
|
State = state;
|
|
WorkBuffer = workBuffer;
|
|
|
|
IsEffectEnabled = isEnabled;
|
|
|
|
InputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
|
|
OutputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
{
|
|
InputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Input[i]);
|
|
OutputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Output[i]);
|
|
}
|
|
}
|
|
|
|
public void Process(CommandList context)
|
|
{
|
|
ref LimiterState state = ref State.Span[0];
|
|
|
|
if (IsEffectEnabled)
|
|
{
|
|
if (Parameter.Status == UsageState.Invalid)
|
|
{
|
|
state = new LimiterState(ref _parameter, WorkBuffer);
|
|
}
|
|
else if (Parameter.Status == UsageState.New)
|
|
{
|
|
LimiterState.UpdateParameter(ref _parameter);
|
|
}
|
|
}
|
|
|
|
ProcessLimiter(context, ref state);
|
|
}
|
|
|
|
private unsafe void ProcessLimiter(CommandList context, ref LimiterState state)
|
|
{
|
|
Debug.Assert(Parameter.IsChannelCountValid());
|
|
|
|
if (IsEffectEnabled && Parameter.IsChannelCountValid())
|
|
{
|
|
Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
|
Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
{
|
|
inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
|
|
outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
|
|
}
|
|
|
|
for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
|
|
{
|
|
for (int sampleIndex = 0; sampleIndex < context.SampleCount; sampleIndex++)
|
|
{
|
|
float rawInputSample = *((float*)inputBuffers[channelIndex] + sampleIndex);
|
|
|
|
float inputSample = (rawInputSample / short.MaxValue) * Parameter.InputGain;
|
|
|
|
float sampleInputMax = Math.Abs(inputSample);
|
|
|
|
float inputCoefficient = Parameter.ReleaseCoefficient;
|
|
|
|
if (sampleInputMax > state.DetectorAverage[channelIndex].Read())
|
|
{
|
|
inputCoefficient = Parameter.AttackCoefficient;
|
|
}
|
|
|
|
float detectorValue = state.DetectorAverage[channelIndex].Update(sampleInputMax, inputCoefficient);
|
|
float attenuation = 1.0f;
|
|
|
|
if (detectorValue > Parameter.Threshold)
|
|
{
|
|
attenuation = Parameter.Threshold / detectorValue;
|
|
}
|
|
|
|
float outputCoefficient = Parameter.ReleaseCoefficient;
|
|
|
|
if (state.CompressionGainAverage[channelIndex].Read() > attenuation)
|
|
{
|
|
outputCoefficient = Parameter.AttackCoefficient;
|
|
}
|
|
|
|
float compressionGain = state.CompressionGainAverage[channelIndex].Update(attenuation, outputCoefficient);
|
|
|
|
ref float delayedSample = ref state.DelayedSampleBuffer[channelIndex * Parameter.DelayBufferSampleCountMax + state.DelayedSampleBufferPosition[channelIndex]];
|
|
|
|
float outputSample = delayedSample * compressionGain * Parameter.OutputGain;
|
|
|
|
*((float*)outputBuffers[channelIndex] + sampleIndex) = outputSample * short.MaxValue;
|
|
|
|
delayedSample = inputSample;
|
|
|
|
state.DelayedSampleBufferPosition[channelIndex]++;
|
|
|
|
while (state.DelayedSampleBufferPosition[channelIndex] >= Parameter.DelayBufferSampleCountMin)
|
|
{
|
|
state.DelayedSampleBufferPosition[channelIndex] -= Parameter.DelayBufferSampleCountMin;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
{
|
|
if (InputBufferIndices[i] != OutputBufferIndices[i])
|
|
{
|
|
context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|