2021-05-25 19:01:09 +02:00
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2019-2021 Ryujinx
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
using Ryujinx.Audio.Renderer.Dsp.State;
|
|
|
|
|
using Ryujinx.Audio.Renderer.Parameter;
|
|
|
|
|
using Ryujinx.Audio.Renderer.Parameter.Effect;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2021-07-18 13:05:11 +02:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2021-05-25 19:01:09 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Audio.Renderer.Dsp.Command
|
|
|
|
|
{
|
|
|
|
|
public class LimiterCommandVersion2 : ICommand
|
|
|
|
|
{
|
|
|
|
|
public bool Enabled { get; set; }
|
|
|
|
|
|
|
|
|
|
public int NodeId { get; }
|
|
|
|
|
|
|
|
|
|
public CommandType CommandType => CommandType.LimiterVersion2;
|
|
|
|
|
|
|
|
|
|
public ulong EstimatedProcessingTime { get; set; }
|
|
|
|
|
|
|
|
|
|
public LimiterParameter Parameter => _parameter;
|
|
|
|
|
public Memory<LimiterState> State { get; }
|
|
|
|
|
public Memory<EffectResultState> ResultState { get; }
|
|
|
|
|
public ulong WorkBuffer { get; }
|
|
|
|
|
public ushort[] OutputBufferIndices { get; }
|
|
|
|
|
public ushort[] InputBufferIndices { get; }
|
|
|
|
|
public bool IsEffectEnabled { get; }
|
|
|
|
|
|
|
|
|
|
private LimiterParameter _parameter;
|
|
|
|
|
|
|
|
|
|
public LimiterCommandVersion2(uint bufferOffset, LimiterParameter parameter, Memory<LimiterState> state, Memory<EffectResultState> resultState, bool isEnabled, ulong workBuffer, int nodeId)
|
|
|
|
|
{
|
|
|
|
|
Enabled = true;
|
|
|
|
|
NodeId = nodeId;
|
|
|
|
|
_parameter = parameter;
|
|
|
|
|
State = state;
|
|
|
|
|
ResultState = resultState;
|
|
|
|
|
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 13:05:11 +02:00
|
|
|
|
ProcessLimiter(context, ref state);
|
2021-05-25 19:01:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
|
private unsafe void ProcessLimiter(CommandList context, ref LimiterState state)
|
2021-05-25 19:01:09 +02:00
|
|
|
|
{
|
|
|
|
|
Debug.Assert(Parameter.IsChannelCountValid());
|
|
|
|
|
|
|
|
|
|
if (IsEffectEnabled && Parameter.IsChannelCountValid())
|
|
|
|
|
{
|
|
|
|
|
if (!ResultState.IsEmpty && Parameter.StatisticsReset)
|
|
|
|
|
{
|
|
|
|
|
ref LimiterStatistics statistics = ref MemoryMarshal.Cast<byte, LimiterStatistics>(ResultState.Span[0].SpecificData)[0];
|
|
|
|
|
|
|
|
|
|
statistics.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
|
Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
|
|
|
|
Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
2021-05-25 19:01:09 +02:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
|
inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
|
|
|
|
|
outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
|
2021-05-25 19:01:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
|
|
|
|
|
{
|
|
|
|
|
for (int sampleIndex = 0; sampleIndex < context.SampleCount; sampleIndex++)
|
|
|
|
|
{
|
2022-02-16 21:38:45 +01:00
|
|
|
|
float rawInputSample = *((float*)inputBuffers[channelIndex] + sampleIndex);
|
2021-05-25 19:01:09 +02:00
|
|
|
|
|
2022-02-16 21:38:45 +01:00
|
|
|
|
float inputSample = (rawInputSample / short.MaxValue) * Parameter.InputGain;
|
|
|
|
|
|
|
|
|
|
float sampleInputMax = Math.Abs(inputSample);
|
2021-05-25 19:01:09 +02:00
|
|
|
|
|
|
|
|
|
float inputCoefficient = Parameter.ReleaseCoefficient;
|
|
|
|
|
|
|
|
|
|
if (sampleInputMax > state.DectectorAverage[channelIndex])
|
|
|
|
|
{
|
|
|
|
|
inputCoefficient = Parameter.AttackCoefficient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.DectectorAverage[channelIndex] += inputCoefficient * (sampleInputMax - state.DectectorAverage[channelIndex]);
|
|
|
|
|
|
|
|
|
|
float attenuation = 1.0f;
|
|
|
|
|
|
|
|
|
|
if (state.DectectorAverage[channelIndex] > Parameter.Threshold)
|
|
|
|
|
{
|
|
|
|
|
attenuation = Parameter.Threshold / state.DectectorAverage[channelIndex];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float outputCoefficient = Parameter.ReleaseCoefficient;
|
|
|
|
|
|
|
|
|
|
if (state.CompressionGain[channelIndex] > attenuation)
|
|
|
|
|
{
|
|
|
|
|
outputCoefficient = Parameter.AttackCoefficient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.CompressionGain[channelIndex] += outputCoefficient * (attenuation - state.CompressionGain[channelIndex]);
|
|
|
|
|
|
|
|
|
|
ref float delayedSample = ref state.DelayedSampleBuffer[channelIndex * Parameter.DelayBufferSampleCountMax + state.DelayedSampleBufferPosition[channelIndex]];
|
|
|
|
|
|
2022-02-16 21:38:45 +01:00
|
|
|
|
float outputSample = delayedSample * state.CompressionGain[channelIndex] * Parameter.OutputGain;
|
|
|
|
|
|
|
|
|
|
*((float*)outputBuffers[channelIndex] + sampleIndex) = outputSample * short.MaxValue;
|
2021-05-25 19:01:09 +02:00
|
|
|
|
|
|
|
|
|
delayedSample = inputSample;
|
|
|
|
|
|
|
|
|
|
state.DelayedSampleBufferPosition[channelIndex]++;
|
|
|
|
|
|
|
|
|
|
while (state.DelayedSampleBufferPosition[channelIndex] >= Parameter.DelayBufferSampleCountMin)
|
|
|
|
|
{
|
|
|
|
|
state.DelayedSampleBufferPosition[channelIndex] -= Parameter.DelayBufferSampleCountMin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ResultState.IsEmpty)
|
|
|
|
|
{
|
|
|
|
|
ref LimiterStatistics statistics = ref MemoryMarshal.Cast<byte, LimiterStatistics>(ResultState.Span[0].SpecificData)[0];
|
|
|
|
|
|
|
|
|
|
statistics.InputMax[channelIndex] = Math.Max(statistics.InputMax[channelIndex], sampleInputMax);
|
|
|
|
|
statistics.CompressionGainMin[channelIndex] = Math.Min(statistics.CompressionGainMin[channelIndex], state.CompressionGain[channelIndex]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
|
|
|
{
|
|
|
|
|
if (InputBufferIndices[i] != OutputBufferIndices[i])
|
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
|
context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
|
2021-05-25 19:01:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|