2021-05-25 13:01:09 -04:00
|
|
|
using Ryujinx.Audio.Renderer.Dsp.State;
|
|
|
|
using Ryujinx.Audio.Renderer.Parameter.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;
|
|
|
|
|
2022-11-28 02:28:45 -05:00
|
|
|
public uint EstimatedProcessingTime { get; set; }
|
2021-05-25 13:01:09 -04:00
|
|
|
|
|
|
|
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 == Server.Effect.UsageState.Invalid)
|
|
|
|
{
|
|
|
|
state = new LimiterState(ref _parameter, WorkBuffer);
|
|
|
|
}
|
|
|
|
else if (Parameter.Status == Server.Effect.UsageState.New)
|
|
|
|
{
|
|
|
|
state.UpdateParameter(ref _parameter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 07:05:11 -04:00
|
|
|
ProcessLimiter(context, ref state);
|
2021-05-25 13:01:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-18 07:05:11 -04:00
|
|
|
private unsafe void ProcessLimiter(CommandList context, ref LimiterState state)
|
2021-05-25 13:01:09 -04:00
|
|
|
{
|
|
|
|
Debug.Assert(Parameter.IsChannelCountValid());
|
|
|
|
|
|
|
|
if (IsEffectEnabled && Parameter.IsChannelCountValid())
|
|
|
|
{
|
2021-07-18 07:05:11 -04:00
|
|
|
Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
|
|
|
Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
2021-05-25 13:01:09 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
|
|
{
|
2021-07-18 07:05:11 -04:00
|
|
|
inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
|
|
|
|
outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
|
2021-05-25 13:01:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
|
|
|
|
{
|
|
|
|
for (int sampleIndex = 0; sampleIndex < context.SampleCount; sampleIndex++)
|
|
|
|
{
|
2022-02-16 15:38:45 -05:00
|
|
|
float rawInputSample = *((float*)inputBuffers[channelIndex] + sampleIndex);
|
2021-05-25 13:01:09 -04:00
|
|
|
|
2022-02-16 15:38:45 -05:00
|
|
|
float inputSample = (rawInputSample / short.MaxValue) * Parameter.InputGain;
|
|
|
|
|
|
|
|
float sampleInputMax = Math.Abs(inputSample);
|
2021-05-25 13:01:09 -04:00
|
|
|
|
|
|
|
float inputCoefficient = Parameter.ReleaseCoefficient;
|
|
|
|
|
2022-12-06 09:04:25 -05:00
|
|
|
if (sampleInputMax > state.DetectorAverage[channelIndex].Read())
|
2021-05-25 13:01:09 -04:00
|
|
|
{
|
|
|
|
inputCoefficient = Parameter.AttackCoefficient;
|
|
|
|
}
|
|
|
|
|
2022-12-06 09:04:25 -05:00
|
|
|
float detectorValue = state.DetectorAverage[channelIndex].Update(sampleInputMax, inputCoefficient);
|
2021-05-25 13:01:09 -04:00
|
|
|
float attenuation = 1.0f;
|
|
|
|
|
2022-12-06 09:04:25 -05:00
|
|
|
if (detectorValue > Parameter.Threshold)
|
2021-05-25 13:01:09 -04:00
|
|
|
{
|
2022-12-06 09:04:25 -05:00
|
|
|
attenuation = Parameter.Threshold / detectorValue;
|
2021-05-25 13:01:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float outputCoefficient = Parameter.ReleaseCoefficient;
|
|
|
|
|
2022-12-06 09:04:25 -05:00
|
|
|
if (state.CompressionGainAverage[channelIndex].Read() > attenuation)
|
2021-05-25 13:01:09 -04:00
|
|
|
{
|
|
|
|
outputCoefficient = Parameter.AttackCoefficient;
|
|
|
|
}
|
|
|
|
|
2022-12-06 09:04:25 -05:00
|
|
|
float compressionGain = state.CompressionGainAverage[channelIndex].Update(attenuation, outputCoefficient);
|
2021-05-25 13:01:09 -04:00
|
|
|
|
|
|
|
ref float delayedSample = ref state.DelayedSampleBuffer[channelIndex * Parameter.DelayBufferSampleCountMax + state.DelayedSampleBufferPosition[channelIndex]];
|
|
|
|
|
2022-12-06 09:04:25 -05:00
|
|
|
float outputSample = delayedSample * compressionGain * Parameter.OutputGain;
|
2022-02-16 15:38:45 -05:00
|
|
|
|
|
|
|
*((float*)outputBuffers[channelIndex] + sampleIndex) = outputSample * short.MaxValue;
|
2021-05-25 13:01:09 -04:00
|
|
|
|
|
|
|
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])
|
|
|
|
{
|
2021-07-18 07:05:11 -04:00
|
|
|
context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
|
2021-05-25 13:01:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 14:46:33 -04:00
|
|
|
}
|