2020-08-18 03:49:37 +02:00
|
|
|
using Ryujinx.Audio.Renderer.Dsp.State;
|
|
|
|
using Ryujinx.Audio.Renderer.Parameter.Effect;
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
2021-07-18 13:05:11 +02:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Audio.Renderer.Dsp.Command
|
|
|
|
{
|
|
|
|
public class ReverbCommand : ICommand
|
|
|
|
{
|
|
|
|
private static readonly int[] OutputEarlyIndicesTableMono = new int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
private static readonly int[] TargetEarlyDelayLineIndicesTableMono = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
private static readonly int[] OutputIndicesTableMono = new int[4] { 0, 0, 0, 0 };
|
|
|
|
private static readonly int[] TargetOutputFeedbackIndicesTableMono = new int[4] { 0, 1, 2, 3 };
|
|
|
|
|
|
|
|
private static readonly int[] OutputEarlyIndicesTableStereo = new int[10] { 0, 0, 1, 1, 0, 1, 0, 0, 1, 1 };
|
|
|
|
private static readonly int[] TargetEarlyDelayLineIndicesTableStereo = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
private static readonly int[] OutputIndicesTableStereo = new int[4] { 0, 0, 1, 1 };
|
|
|
|
private static readonly int[] TargetOutputFeedbackIndicesTableStereo = new int[4] { 2, 0, 3, 1 };
|
|
|
|
|
|
|
|
private static readonly int[] OutputEarlyIndicesTableQuadraphonic = new int[10] { 0, 0, 1, 1, 0, 1, 2, 2, 3, 3 };
|
|
|
|
private static readonly int[] TargetEarlyDelayLineIndicesTableQuadraphonic = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
private static readonly int[] OutputIndicesTableQuadraphonic = new int[4] { 0, 1, 2, 3 };
|
|
|
|
private static readonly int[] TargetOutputFeedbackIndicesTableQuadraphonic = new int[4] { 0, 1, 2, 3 };
|
|
|
|
|
|
|
|
private static readonly int[] OutputEarlyIndicesTableSurround = new int[20] { 0, 5, 0, 5, 1, 5, 1, 5, 4, 5, 4, 5, 2, 5, 2, 5, 3, 5, 3, 5 };
|
|
|
|
private static readonly int[] TargetEarlyDelayLineIndicesTableSurround = new int[20] { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9 };
|
2021-02-26 01:11:56 +01:00
|
|
|
private static readonly int[] OutputIndicesTableSurround = new int[Constants.ChannelCountMax] { 0, 1, 2, 3, 4, 5 };
|
|
|
|
private static readonly int[] TargetOutputFeedbackIndicesTableSurround = new int[Constants.ChannelCountMax] { 0, 1, 2, 3, -1, 3 };
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
public bool Enabled { get; set; }
|
|
|
|
|
|
|
|
public int NodeId { get; }
|
|
|
|
|
|
|
|
public CommandType CommandType => CommandType.Reverb;
|
|
|
|
|
|
|
|
public ulong EstimatedProcessingTime { get; set; }
|
|
|
|
|
|
|
|
public ReverbParameter Parameter => _parameter;
|
|
|
|
public Memory<ReverbState> State { get; }
|
|
|
|
public ulong WorkBuffer { get; }
|
|
|
|
public ushort[] OutputBufferIndices { get; }
|
|
|
|
public ushort[] InputBufferIndices { get; }
|
|
|
|
public bool IsLongSizePreDelaySupported { get; }
|
|
|
|
|
|
|
|
public bool IsEffectEnabled { get; }
|
|
|
|
|
|
|
|
private ReverbParameter _parameter;
|
|
|
|
|
|
|
|
private const int FixedPointPrecision = 14;
|
|
|
|
|
2022-04-06 09:12:38 +02:00
|
|
|
public ReverbCommand(uint bufferOffset, ReverbParameter parameter, Memory<ReverbState> state, bool isEnabled, ulong workBuffer, int nodeId, bool isLongSizePreDelaySupported, bool newEffectChannelMappingSupported)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
Enabled = true;
|
|
|
|
IsEffectEnabled = isEnabled;
|
|
|
|
NodeId = nodeId;
|
|
|
|
_parameter = parameter;
|
|
|
|
State = state;
|
|
|
|
WorkBuffer = workBuffer;
|
|
|
|
|
2021-02-26 01:11:56 +01:00
|
|
|
InputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
|
|
|
|
OutputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
|
|
{
|
|
|
|
InputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Input[i]);
|
|
|
|
OutputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Output[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
IsLongSizePreDelaySupported = isLongSizePreDelaySupported;
|
2022-04-06 09:12:38 +02:00
|
|
|
|
|
|
|
// NOTE: We do the opposite as Nintendo here for now to restore previous behaviour
|
|
|
|
// TODO: Update reverb processing and remove this to use RemapLegacyChannelEffectMappingToChannelResourceMapping.
|
|
|
|
DataSourceHelper.RemapChannelResourceMappingToLegacy(newEffectChannelMappingSupported, InputBufferIndices);
|
|
|
|
DataSourceHelper.RemapChannelResourceMappingToLegacy(newEffectChannelMappingSupported, OutputBufferIndices);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void ProcessReverbMono(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverbGeneric(ref state,
|
|
|
|
outputBuffers,
|
2020-08-18 03:49:37 +02:00
|
|
|
inputBuffers,
|
|
|
|
sampleCount,
|
|
|
|
OutputEarlyIndicesTableMono,
|
|
|
|
TargetEarlyDelayLineIndicesTableMono,
|
|
|
|
TargetOutputFeedbackIndicesTableMono,
|
|
|
|
OutputIndicesTableMono);
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void ProcessReverbStereo(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverbGeneric(ref state,
|
|
|
|
outputBuffers,
|
2020-08-18 03:49:37 +02:00
|
|
|
inputBuffers,
|
|
|
|
sampleCount,
|
|
|
|
OutputEarlyIndicesTableStereo,
|
|
|
|
TargetEarlyDelayLineIndicesTableStereo,
|
|
|
|
TargetOutputFeedbackIndicesTableStereo,
|
|
|
|
OutputIndicesTableStereo);
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void ProcessReverbQuadraphonic(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverbGeneric(ref state,
|
|
|
|
outputBuffers,
|
2020-08-18 03:49:37 +02:00
|
|
|
inputBuffers,
|
|
|
|
sampleCount,
|
|
|
|
OutputEarlyIndicesTableQuadraphonic,
|
|
|
|
TargetEarlyDelayLineIndicesTableQuadraphonic,
|
|
|
|
TargetOutputFeedbackIndicesTableQuadraphonic,
|
|
|
|
OutputIndicesTableQuadraphonic);
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private void ProcessReverbSurround(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverbGeneric(ref state,
|
|
|
|
outputBuffers,
|
2020-08-18 03:49:37 +02:00
|
|
|
inputBuffers,
|
|
|
|
sampleCount,
|
|
|
|
OutputEarlyIndicesTableSurround,
|
|
|
|
TargetEarlyDelayLineIndicesTableSurround,
|
|
|
|
TargetOutputFeedbackIndicesTableSurround,
|
|
|
|
OutputIndicesTableSurround);
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
private unsafe void ProcessReverbGeneric(ref ReverbState state, ReadOnlySpan<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount, ReadOnlySpan<int> outputEarlyIndicesTable, ReadOnlySpan<int> targetEarlyDelayLineIndicesTable, ReadOnlySpan<int> targetOutputFeedbackIndicesTable, ReadOnlySpan<int> outputIndicesTable)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
bool isSurround = Parameter.ChannelCount == 6;
|
|
|
|
|
|
|
|
float reverbGain = FixedPointHelper.ToFloat(Parameter.ReverbGain, FixedPointPrecision);
|
|
|
|
float lateGain = FixedPointHelper.ToFloat(Parameter.LateGain, FixedPointPrecision);
|
|
|
|
float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
|
|
|
|
float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
Span<float> outputValues = stackalloc float[Constants.ChannelCountMax];
|
|
|
|
Span<float> feedbackValues = stackalloc float[4];
|
|
|
|
Span<float> feedbackOutputValues = stackalloc float[4];
|
|
|
|
Span<float> channelInput = stackalloc float[Parameter.ChannelCount];
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
for (int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
outputValues.Fill(0);
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < targetEarlyDelayLineIndicesTable.Length; i++)
|
|
|
|
{
|
|
|
|
int earlyDelayIndex = targetEarlyDelayLineIndicesTable[i];
|
|
|
|
int outputIndex = outputEarlyIndicesTable[i];
|
|
|
|
|
|
|
|
float tapOutput = state.PreDelayLine.TapUnsafe(state.EarlyDelayTime[earlyDelayIndex], 0);
|
|
|
|
|
|
|
|
outputValues[outputIndex] += tapOutput * state.EarlyGain[earlyDelayIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSurround)
|
|
|
|
{
|
|
|
|
outputValues[5] *= 0.2f;
|
|
|
|
}
|
|
|
|
|
|
|
|
float targetPreDelayValue = 0;
|
|
|
|
|
|
|
|
for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
channelInput[channelIndex] = *((float*)inputBuffers[channelIndex] + sampleIndex) * 64;
|
2020-08-18 03:49:37 +02:00
|
|
|
targetPreDelayValue += channelInput[channelIndex] * reverbGain;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.PreDelayLine.Update(targetPreDelayValue);
|
|
|
|
|
|
|
|
float lateValue = state.PreDelayLine.Tap(state.PreDelayLineDelayTime) * lateGain;
|
|
|
|
|
|
|
|
for (int i = 0; i < state.FdnDelayLines.Length; i++)
|
|
|
|
{
|
|
|
|
feedbackOutputValues[i] = state.FdnDelayLines[i].Read() * state.HighFrequencyDecayDirectGain[i] + state.PreviousFeedbackOutput[i] * state.HighFrequencyDecayPreviousGain[i];
|
|
|
|
state.PreviousFeedbackOutput[i] = feedbackOutputValues[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
feedbackValues[0] = feedbackOutputValues[2] + feedbackOutputValues[1];
|
|
|
|
feedbackValues[1] = -feedbackOutputValues[0] - feedbackOutputValues[3];
|
|
|
|
feedbackValues[2] = feedbackOutputValues[0] - feedbackOutputValues[3];
|
|
|
|
feedbackValues[3] = feedbackOutputValues[1] - feedbackOutputValues[2];
|
|
|
|
|
|
|
|
for (int i = 0; i < state.FdnDelayLines.Length; i++)
|
|
|
|
{
|
|
|
|
feedbackOutputValues[i] = state.DecayDelays[i].Update(feedbackValues[i] + lateValue);
|
|
|
|
state.FdnDelayLines[i].Update(feedbackOutputValues[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < targetOutputFeedbackIndicesTable.Length; i++)
|
|
|
|
{
|
|
|
|
int targetOutputFeedbackIndex = targetOutputFeedbackIndicesTable[i];
|
|
|
|
int outputIndex = outputIndicesTable[i];
|
|
|
|
|
|
|
|
if (targetOutputFeedbackIndex >= 0)
|
|
|
|
{
|
|
|
|
outputValues[outputIndex] += feedbackOutputValues[targetOutputFeedbackIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSurround)
|
|
|
|
{
|
2022-04-06 09:12:38 +02:00
|
|
|
outputValues[4] += state.FrontCenterDelayLine.Update((feedbackOutputValues[2] - feedbackOutputValues[3]) * 0.5f);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int channelIndex = 0; channelIndex < Parameter.ChannelCount; channelIndex++)
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
*((float*)outputBuffers[channelIndex] + sampleIndex) = (outputValues[channelIndex] * outGain + channelInput[channelIndex] * dryGain) / 64;
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
private void ProcessReverb(CommandList context, ref ReverbState state)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
Debug.Assert(Parameter.IsChannelCountValid());
|
|
|
|
|
|
|
|
if (IsEffectEnabled && Parameter.IsChannelCountValid())
|
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
|
|
|
Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
2020-08-18 03:49:37 +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]);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (Parameter.ChannelCount)
|
|
|
|
{
|
|
|
|
case 1:
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverbMono(ref state, outputBuffers, inputBuffers, context.SampleCount);
|
2020-08-18 03:49:37 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverbStereo(ref state, outputBuffers, inputBuffers, context.SampleCount);
|
2020-08-18 03:49:37 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverbQuadraphonic(ref state, outputBuffers, inputBuffers, context.SampleCount);
|
2020-08-18 03:49:37 +02:00
|
|
|
break;
|
|
|
|
case 6:
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverbSurround(ref state, outputBuffers, inputBuffers, context.SampleCount);
|
2020-08-18 03:49:37 +02:00
|
|
|
break;
|
|
|
|
default:
|
2021-07-18 13:05:11 +02:00
|
|
|
throw new NotImplementedException(Parameter.ChannelCount.ToString());
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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]);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Process(CommandList context)
|
|
|
|
{
|
|
|
|
ref ReverbState state = ref State.Span[0];
|
|
|
|
|
|
|
|
if (IsEffectEnabled)
|
|
|
|
{
|
|
|
|
if (Parameter.Status == Server.Effect.UsageState.Invalid)
|
|
|
|
{
|
|
|
|
state = new ReverbState(ref _parameter, WorkBuffer, IsLongSizePreDelaySupported);
|
|
|
|
}
|
|
|
|
else if (Parameter.Status == Server.Effect.UsageState.New)
|
|
|
|
{
|
|
|
|
state.UpdateParameter(ref _parameter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessReverb(context, ref state);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|