2020-08-18 01:49:37 +00:00
|
|
|
//
|
2020-12-31 23:10:44 +00:00
|
|
|
// Copyright (c) 2019-2021 Ryujinx
|
2020-08-18 01:49:37 +00: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;
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
2021-07-18 11:05:11 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-08-18 01:49:37 +00: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;
|
|
|
|
|
|
|
|
public DelayCommand(uint bufferOffset, DelayParameter parameter, Memory<DelayState> state, bool isEnabled, ulong workBuffer, int nodeId)
|
|
|
|
{
|
|
|
|
Enabled = true;
|
|
|
|
NodeId = nodeId;
|
|
|
|
_parameter = parameter;
|
|
|
|
State = state;
|
|
|
|
WorkBuffer = workBuffer;
|
|
|
|
|
|
|
|
IsEffectEnabled = isEnabled;
|
|
|
|
|
2021-02-26 00:11:56 +00:00
|
|
|
InputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
|
|
|
|
OutputBufferIndices = new ushort[Constants.VoiceChannelCountMax];
|
2020-08-18 01:49:37 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
|
|
{
|
|
|
|
InputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Input[i]);
|
|
|
|
OutputBufferIndices[i] = (ushort)(bufferOffset + Parameter.Output[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private unsafe void ProcessDelayMono(ref DelayState state, float* outputBuffer, float* inputBuffer, uint sampleCount)
|
2020-08-18 01:49:37 +00: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();
|
|
|
|
|
2021-04-18 11:34:20 +00:00
|
|
|
float lowPassResult = (input * inGain + delayLineValue * feedbackGain) * state.LowPassBaseGain + state.LowPassZ[0] * state.LowPassFeedbackGain;
|
2020-08-18 01:49:37 +00:00
|
|
|
|
|
|
|
state.LowPassZ[0] = lowPassResult;
|
|
|
|
|
|
|
|
state.DelayLines[0].Update(lowPassResult);
|
|
|
|
|
|
|
|
outputBuffer[i] = (input * dryGain + delayLineValue * outGain) / 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private unsafe void ProcessDelayStereo(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
const ushort channelCount = 2;
|
2020-08-18 01:49:37 +00:00
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
Span<float> channelInput = stackalloc float[channelCount];
|
|
|
|
Span<float> delayLineValues = stackalloc float[channelCount];
|
|
|
|
Span<float> temp = stackalloc float[channelCount];
|
2020-08-18 01:49:37 +00: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);
|
|
|
|
|
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
for (int j = 0; j < channelCount; j++)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
channelInput[j] = *((float*)inputBuffers[j] + i) * 64;
|
2020-08-18 01:49:37 +00:00
|
|
|
delayLineValues[j] = state.DelayLines[j].Read();
|
|
|
|
}
|
|
|
|
|
|
|
|
temp[0] = channelInput[0] * inGain + delayLineValues[1] * delayFeedbackCrossGain + delayLineValues[0] * delayFeedbackBaseGain;
|
|
|
|
temp[1] = channelInput[1] * inGain + delayLineValues[0] * delayFeedbackCrossGain + delayLineValues[1] * delayFeedbackBaseGain;
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
for (int j = 0; j < channelCount; j++)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
|
|
|
float lowPassResult = state.LowPassFeedbackGain * state.LowPassZ[j] + temp[j] * state.LowPassBaseGain;
|
|
|
|
|
|
|
|
state.LowPassZ[j] = lowPassResult;
|
|
|
|
state.DelayLines[j].Update(lowPassResult);
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
*((float*)outputBuffers[j] + i) = (channelInput[j] * dryGain + delayLineValues[j] * outGain) / 64;
|
2020-08-18 01:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private unsafe void ProcessDelayQuadraphonic(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
const ushort channelCount = 4;
|
2020-08-18 01:49:37 +00:00
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
Span<float> channelInput = stackalloc float[channelCount];
|
|
|
|
Span<float> delayLineValues = stackalloc float[channelCount];
|
|
|
|
Span<float> temp = stackalloc float[channelCount];
|
2020-08-18 01:49:37 +00: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);
|
|
|
|
|
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
for (int j = 0; j < channelCount; j++)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
channelInput[j] = *((float*)inputBuffers[j] + i) * 64;
|
2020-08-18 01:49:37 +00:00
|
|
|
delayLineValues[j] = state.DelayLines[j].Read();
|
|
|
|
}
|
|
|
|
|
|
|
|
temp[0] = channelInput[0] * inGain + (delayLineValues[2] + delayLineValues[1]) * delayFeedbackCrossGain + delayLineValues[0] * delayFeedbackBaseGain;
|
|
|
|
temp[1] = channelInput[1] * inGain + (delayLineValues[0] + delayLineValues[3]) * delayFeedbackCrossGain + delayLineValues[1] * delayFeedbackBaseGain;
|
|
|
|
temp[2] = channelInput[2] * inGain + (delayLineValues[3] + delayLineValues[0]) * delayFeedbackCrossGain + delayLineValues[2] * delayFeedbackBaseGain;
|
|
|
|
temp[3] = channelInput[3] * inGain + (delayLineValues[1] + delayLineValues[2]) * delayFeedbackCrossGain + delayLineValues[3] * delayFeedbackBaseGain;
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
for (int j = 0; j < channelCount; j++)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
|
|
|
float lowPassResult = state.LowPassFeedbackGain * state.LowPassZ[j] + temp[j] * state.LowPassBaseGain;
|
|
|
|
|
|
|
|
state.LowPassZ[j] = lowPassResult;
|
|
|
|
state.DelayLines[j].Update(lowPassResult);
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
*((float*)outputBuffers[j] + i) = (channelInput[j] * dryGain + delayLineValues[j] * outGain) / 64;
|
2020-08-18 01:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private unsafe void ProcessDelaySurround(ref DelayState state, Span<IntPtr> outputBuffers, ReadOnlySpan<IntPtr> inputBuffers, uint sampleCount)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
const ushort channelCount = 6;
|
2020-08-18 01:49:37 +00:00
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
Span<float> channelInput = stackalloc float[channelCount];
|
|
|
|
Span<float> delayLineValues = stackalloc float[channelCount];
|
|
|
|
Span<float> temp = stackalloc float[channelCount];
|
2020-08-18 01:49:37 +00: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);
|
|
|
|
|
|
|
|
for (int i = 0; i < sampleCount; i++)
|
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
for (int j = 0; j < channelCount; j++)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
channelInput[j] = *((float*)inputBuffers[j] + i) * 64;
|
2020-08-18 01:49:37 +00:00
|
|
|
delayLineValues[j] = state.DelayLines[j].Read();
|
|
|
|
}
|
|
|
|
|
|
|
|
temp[0] = channelInput[0] * inGain + (delayLineValues[2] + delayLineValues[4]) * delayFeedbackCrossGain + delayLineValues[0] * delayFeedbackBaseGain;
|
|
|
|
temp[1] = channelInput[1] * inGain + (delayLineValues[4] + delayLineValues[3]) * delayFeedbackCrossGain + delayLineValues[1] * delayFeedbackBaseGain;
|
|
|
|
temp[2] = channelInput[2] * inGain + (delayLineValues[3] + delayLineValues[0]) * delayFeedbackCrossGain + delayLineValues[2] * delayFeedbackBaseGain;
|
|
|
|
temp[3] = channelInput[3] * inGain + (delayLineValues[1] + delayLineValues[2]) * delayFeedbackCrossGain + delayLineValues[3] * delayFeedbackBaseGain;
|
|
|
|
temp[4] = channelInput[4] * inGain + (delayLineValues[0] + delayLineValues[1]) * delayFeedbackCrossGain + delayLineValues[4] * delayFeedbackBaseGain;
|
|
|
|
temp[5] = channelInput[5] * inGain + delayLineValues[5] * delayFeedbackBaseGain;
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
for (int j = 0; j < channelCount; j++)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
|
|
|
float lowPassResult = state.LowPassFeedbackGain * state.LowPassZ[j] + temp[j] * state.LowPassBaseGain;
|
|
|
|
|
|
|
|
state.LowPassZ[j] = lowPassResult;
|
|
|
|
state.DelayLines[j].Update(lowPassResult);
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
*((float*)outputBuffers[j] + i) = (channelInput[j] * dryGain + delayLineValues[j] * outGain) / 64;
|
2020-08-18 01:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 11:05:11 +00:00
|
|
|
private unsafe void ProcessDelay(CommandList context, ref DelayState state)
|
2020-08-18 01:49:37 +00:00
|
|
|
{
|
|
|
|
Debug.Assert(Parameter.IsChannelCountValid());
|
|
|
|
|
|
|
|
if (IsEffectEnabled && Parameter.IsChannelCountValid())
|
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
Span<IntPtr> inputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
|
|
|
Span<IntPtr> outputBuffers = stackalloc IntPtr[Parameter.ChannelCount];
|
2020-08-18 01:49:37 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
inputBuffers[i] = context.GetBufferPointer(InputBufferIndices[i]);
|
|
|
|
outputBuffers[i] = context.GetBufferPointer(OutputBufferIndices[i]);
|
2020-08-18 01:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (Parameter.ChannelCount)
|
|
|
|
{
|
|
|
|
case 1:
|
2021-07-18 11:05:11 +00:00
|
|
|
ProcessDelayMono(ref state, (float*)outputBuffers[0], (float*)inputBuffers[0], context.SampleCount);
|
2020-08-18 01:49:37 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-07-18 11:05:11 +00:00
|
|
|
ProcessDelayStereo(ref state, outputBuffers, inputBuffers, context.SampleCount);
|
2020-08-18 01:49:37 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2021-07-18 11:05:11 +00:00
|
|
|
ProcessDelayQuadraphonic(ref state, outputBuffers, inputBuffers, context.SampleCount);
|
2020-08-18 01:49:37 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2021-07-18 11:05:11 +00:00
|
|
|
ProcessDelaySurround(ref state, outputBuffers, inputBuffers, context.SampleCount);
|
2020-08-18 01:49:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-07-18 11:05:11 +00:00
|
|
|
throw new NotImplementedException(Parameter.ChannelCount.ToString());
|
2020-08-18 01:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < Parameter.ChannelCount; i++)
|
|
|
|
{
|
|
|
|
if (InputBufferIndices[i] != OutputBufferIndices[i])
|
|
|
|
{
|
2021-07-18 11:05:11 +00:00
|
|
|
context.CopyBuffer(OutputBufferIndices[i], InputBufferIndices[i]);
|
2020-08-18 01:49:37 +00: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 11:05:11 +00:00
|
|
|
ProcessDelay(context, ref state);
|
2020-08-18 01:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|