aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Dsp/State/DelayState.cs
blob: 2a1e7f834a1a3a4195e00838e3e1803167998556 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Ryujinx.Audio.Renderer.Dsp.Effect;
using Ryujinx.Audio.Renderer.Parameter.Effect;
using System.Runtime.CompilerServices;

namespace Ryujinx.Audio.Renderer.Dsp.State
{
    public class DelayState
    {
        public DelayLine[] DelayLines { get; }
        public float[] LowPassZ { get; set; }
        public float FeedbackGain { get; private set; }
        public float DelayFeedbackBaseGain { get; private set; }
        public float DelayFeedbackCrossGain { get; private set; }
        public float LowPassFeedbackGain { get; private set; }
        public float LowPassBaseGain { get; private set; }

        private const int FixedPointPrecision = 14;

        public DelayState(ref DelayParameter parameter, ulong workBuffer)
        {
            DelayLines = new DelayLine[parameter.ChannelCount];
            LowPassZ = new float[parameter.ChannelCount];

            uint sampleRate = (uint)FixedPointHelper.ToInt(parameter.SampleRate, FixedPointPrecision) / 1000;

            for (int i = 0; i < DelayLines.Length; i++)
            {
                DelayLines[i] = new DelayLine(sampleRate, parameter.DelayTimeMax);
                DelayLines[i].SetDelay(parameter.DelayTime);
            }

            UpdateParameter(ref parameter);
        }

        public void UpdateParameter(ref DelayParameter parameter)
        {
            FeedbackGain = FixedPointHelper.ToFloat(parameter.FeedbackGain, FixedPointPrecision) * 0.98f;

            float channelSpread = FixedPointHelper.ToFloat(parameter.ChannelSpread, FixedPointPrecision);

            DelayFeedbackBaseGain = (1.0f - channelSpread) * FeedbackGain;

            if (parameter.ChannelCount == 4 || parameter.ChannelCount == 6)
            {
                DelayFeedbackCrossGain = channelSpread * 0.5f * FeedbackGain;
            }
            else
            {
                DelayFeedbackCrossGain = channelSpread * FeedbackGain;
            }

            LowPassFeedbackGain = 0.95f * FixedPointHelper.ToFloat(parameter.LowPassAmount, FixedPointPrecision);
            LowPassBaseGain = 1.0f - LowPassFeedbackGain;
        }

        public void UpdateLowPassFilter(ref float tempRawRef, uint channelCount)
        {
            for (int i = 0; i < channelCount; i++)
            {
                float lowPassResult = LowPassFeedbackGain * LowPassZ[i] + Unsafe.Add(ref tempRawRef, i) * LowPassBaseGain;

                LowPassZ[i] = lowPassResult;
                DelayLines[i].Update(lowPassResult);
            }
        }
    }
}