2020-08-18 03:49:37 +02:00
|
|
|
//
|
2021-01-01 00:10:44 +01:00
|
|
|
// Copyright (c) 2019-2021 Ryujinx
|
2020-08-18 03:49:37 +02:00
|
|
|
//
|
|
|
|
// 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.Effect;
|
|
|
|
using Ryujinx.Audio.Renderer.Server.Effect;
|
2022-04-08 10:52:18 +02:00
|
|
|
using Ryujinx.Audio.Renderer.Utils.Math;
|
2020-08-18 03:49:37 +02:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
2022-04-08 10:52:18 +02:00
|
|
|
using System.Numerics;
|
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 DelayCommand : ICommand
|
|
|
|
{
|
|
|
|
public bool Enabled { get; set; }
|
|
|
|
|
|
|
|
public int NodeId { get; }
|
|
|
|
|
|
|
|
public CommandType CommandType => CommandType.Delay;
|
|
|
|
|
|
|
|
public ulong EstimatedProcessingTime { get; set; }
|
|
|
|
|
|
|
|
public DelayParameter Parameter => _parameter;
|
|
|
|
public Memory<DelayState> State { get; }
|
|
|
|
public ulong WorkBuffer { get; }
|
|
|
|
public ushort[] OutputBufferIndices { get; }
|
|
|
|
public ushort[] InputBufferIndices { get; }
|
|
|
|
public bool IsEffectEnabled { get; }
|
|
|
|
|
|
|
|
private DelayParameter _parameter;
|
|
|
|
|
|
|
|
private const int FixedPointPrecision = 14;
|
|
|
|
|
2022-04-06 09:12:38 +02:00
|
|
|
public DelayCommand(uint bufferOffset, DelayParameter parameter, Memory<DelayState> state, bool isEnabled, ulong workBuffer, int nodeId, bool newEffectChannelMappingSupported)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
Enabled = true;
|
|
|
|
NodeId = nodeId;
|
|
|
|
_parameter = parameter;
|
|
|
|
State = state;
|
|
|
|
WorkBuffer = workBuffer;
|
|
|
|
|
|
|
|
IsEffectEnabled = isEnabled;
|
|
|
|
|
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]);
|
|
|
|
}
|
2022-04-06 09:12:38 +02:00
|
|
|
|
|
|
|
// NOTE: We do the opposite as Nintendo here for now to restore previous behaviour
|
|
|
|
// TODO: Update delay processing and remove this to use RemapLegacyChannelEffectMappingToChannelResourceMapping.
|
|
|
|
DataSourceHelper.RemapChannelResourceMappingToLegacy(newEffectChannelMappingSupported, InputBufferIndices);
|
|
|
|
DataSourceHelper.RemapChannelResourceMappingToLegacy(newEffectChannelMappingSupported, OutputBufferIndices);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
2021-07-18 13:05:11 +02:00
|
|
|
private unsafe void ProcessDelayMono(ref DelayState state, float* outputBuffer, float* inputBuffer, uint sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
|
|
|
float feedbackGain = FixedPointHelper.ToFloat(Parameter.FeedbackGain, FixedPointPrecision);
|
|
|
|
float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
|
|
|
|
float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
|
|
|
|
float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
|
|
|
|
|
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
float input = inputBuffer[i] * 64;
|
|
|
|
float delayLineValue = state.DelayLines[0].Read();
|
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
float temp = input * inGain + delayLineValue * feedbackGain;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
state.UpdateLowPassFilter(ref temp, 1);
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
outputBuffer[i] = (input * dryGain + delayLineValue * outGain) / 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
2021-07-18 13:05:11 +02:00
|
|
|
private unsafe void ProcessDelayStereo(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
const ushort channelCount = 2;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
|
|
|
|
float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
|
|
|
|
float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
|
|
|
|
float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
|
|
|
|
float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
|
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
Matrix2x2 delayFeedback = new Matrix2x2(delayFeedbackBaseGain , delayFeedbackCrossGain,
|
|
|
|
delayFeedbackCrossGain, delayFeedbackBaseGain);
|
|
|
|
|
2020-08-18 03:49:37 +02:00
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
Vector2 channelInput = new Vector2
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
X = *((float*)inputBuffers[0] + i) * 64,
|
|
|
|
Y = *((float*)inputBuffers[1] + i) * 64,
|
|
|
|
};
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
Vector2 delayLineValues = new Vector2()
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
X = state.DelayLines[0].Read(),
|
|
|
|
Y = state.DelayLines[1].Read(),
|
|
|
|
};
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
Vector2 temp = MatrixHelper.Transform(ref channelInput, ref delayFeedback) + channelInput * inGain;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
state.UpdateLowPassFilter(ref Unsafe.As<Vector2, float>(ref temp), channelCount);
|
|
|
|
|
|
|
|
*((float*)outputBuffers[0] + i) = (channelInput.X * dryGain + delayLineValues.X * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[1] + i) = (channelInput.Y * dryGain + delayLineValues.Y * outGain) / 64;
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
2021-07-18 13:05:11 +02:00
|
|
|
private unsafe void ProcessDelayQuadraphonic(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
const ushort channelCount = 4;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
|
|
|
float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
|
|
|
|
float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
|
|
|
|
float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
|
|
|
|
float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
|
|
|
|
float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
|
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
Matrix4x4 delayFeedback = new Matrix4x4(delayFeedbackBaseGain , delayFeedbackCrossGain, delayFeedbackCrossGain, 0.0f,
|
|
|
|
delayFeedbackCrossGain, delayFeedbackBaseGain , 0.0f , delayFeedbackCrossGain,
|
|
|
|
delayFeedbackCrossGain, 0.0f , delayFeedbackBaseGain , delayFeedbackCrossGain,
|
|
|
|
0.0f , delayFeedbackCrossGain, delayFeedbackCrossGain, delayFeedbackBaseGain);
|
|
|
|
|
|
|
|
|
2020-08-18 03:49:37 +02:00
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
Vector4 channelInput = new Vector4
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
X = *((float*)inputBuffers[0] + i) * 64,
|
|
|
|
Y = *((float*)inputBuffers[1] + i) * 64,
|
|
|
|
Z = *((float*)inputBuffers[2] + i) * 64,
|
|
|
|
W = *((float*)inputBuffers[3] + i) * 64
|
|
|
|
};
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
Vector4 delayLineValues = new Vector4()
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
X = state.DelayLines[0].Read(),
|
|
|
|
Y = state.DelayLines[1].Read(),
|
|
|
|
Z = state.DelayLines[2].Read(),
|
|
|
|
W = state.DelayLines[3].Read()
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector4 temp = MatrixHelper.Transform(ref channelInput, ref delayFeedback) + channelInput * inGain;
|
|
|
|
|
|
|
|
state.UpdateLowPassFilter(ref Unsafe.As<Vector4, float>(ref temp), channelCount);
|
|
|
|
|
|
|
|
*((float*)outputBuffers[0] + i) = (channelInput.X * dryGain + delayLineValues.X * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[1] + i) = (channelInput.Y * dryGain + delayLineValues.Y * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[2] + i) = (channelInput.Z * dryGain + delayLineValues.Z * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[3] + i) = (channelInput.W * dryGain + delayLineValues.W * outGain) / 64;
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
2021-07-18 13:05:11 +02:00
|
|
|
private unsafe void ProcessDelaySurround(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2021-07-18 13:05:11 +02:00
|
|
|
const ushort channelCount = 6;
|
2020-08-18 03:49:37 +02:00
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
float feedbackGain = FixedPointHelper.ToFloat(Parameter.FeedbackGain, FixedPointPrecision);
|
2020-08-18 03:49:37 +02:00
|
|
|
float delayFeedbackBaseGain = state.DelayFeedbackBaseGain;
|
|
|
|
float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
|
|
|
|
float inGain = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
|
|
|
|
float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
|
|
|
|
float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);
|
|
|
|
|
2022-04-08 10:52:18 +02:00
|
|
|
Matrix6x6 delayFeedback = new Matrix6x6(delayFeedbackBaseGain , 0.0f , 0.0f , 0.0f , delayFeedbackCrossGain, delayFeedbackCrossGain,
|
|
|
|
0.0f , delayFeedbackBaseGain , 0.0f , delayFeedbackCrossGain, delayFeedbackCrossGain, 0.0f ,
|
|
|
|
delayFeedbackCrossGain, 0.0f , delayFeedbackBaseGain , delayFeedbackCrossGain, 0.0f , 0.0f ,
|
|
|
|
0.0f , delayFeedbackCrossGain, delayFeedbackCrossGain, delayFeedbackBaseGain , 0.0f , 0.0f ,
|
|
|
|
delayFeedbackCrossGain, delayFeedbackCrossGain, 0.0f , 0.0f , delayFeedbackBaseGain , 0.0f ,
|
|
|
|
0.0f , 0.0f , 0.0f , 0.0f , 0.0f , feedbackGain);
|
|
|
|
|
2020-08-18 03:49:37 +02:00
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
Vector6 channelInput = new Vector6
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
X = *((float*)inputBuffers[0] + i) * 64,
|
|
|
|
Y = *((float*)inputBuffers[1] + i) * 64,
|
|
|
|
Z = *((float*)inputBuffers[2] + i) * 64,
|
|
|
|
W = *((float*)inputBuffers[3] + i) * 64,
|
|
|
|
V = *((float*)inputBuffers[4] + i) * 64,
|
|
|
|
U = *((float*)inputBuffers[5] + i) * 64
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector6 delayLineValues = new Vector6
|
2020-08-18 03:49:37 +02:00
|
|
|
{
|
2022-04-08 10:52:18 +02:00
|
|
|
X = state.DelayLines[0].Read(),
|
|
|
|
Y = state.DelayLines[1].Read(),
|
|
|
|
Z = state.DelayLines[2].Read(),
|
|
|
|
W = state.DelayLines[3].Read(),
|
|
|
|
V = state.DelayLines[4].Read(),
|
|
|
|
U = state.DelayLines[5].Read()
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector6 temp = MatrixHelper.Transform(ref channelInput, ref delayFeedback) + channelInput * inGain;
|
|
|
|
|
|
|
|
state.UpdateLowPassFilter(ref Unsafe.As<Vector6, float>(ref temp), channelCount);
|
|
|
|
|
|
|
|
*((float*)outputBuffers[0] + i) = (channelInput.X * dryGain + delayLineValues.X * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[1] + i) = (channelInput.Y * dryGain + delayLineValues.Y * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[2] + i) = (channelInput.Z * dryGain + delayLineValues.Z * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[3] + i) = (channelInput.W * dryGain + delayLineValues.W * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[4] + i) = (channelInput.V * dryGain + delayLineValues.V * outGain) / 64;
|
|
|
|
*((float*)outputBuffers[5] + i) = (channelInput.U * dryGain + delayLineValues.U * outGain) / 64;
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
private unsafe void ProcessDelay(CommandList context, ref DelayState 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
|
|
|
ProcessDelayMono(ref state, (float*)outputBuffers[0], (float*)inputBuffers[0], context.SampleCount);
|
2020-08-18 03:49:37 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessDelayStereo(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
|
|
|
ProcessDelayQuadraphonic(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
|
|
|
ProcessDelaySurround(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 DelayState state = ref State.Span[0];
|
|
|
|
|
|
|
|
if (IsEffectEnabled)
|
|
|
|
{
|
|
|
|
if (Parameter.Status == UsageState.Invalid)
|
|
|
|
{
|
|
|
|
state = new DelayState(ref _parameter, WorkBuffer);
|
|
|
|
}
|
|
|
|
else if (Parameter.Status == UsageState.New)
|
|
|
|
{
|
|
|
|
state.UpdateParameter(ref _parameter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 13:05:11 +02:00
|
|
|
ProcessDelay(context, ref state);
|
2020-08-18 03:49:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|