aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Dsp/Command/UpsampleCommand.cs
blob: 8882500cd798995295ec1673d73c46e3620cc7c1 (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
68
69
70
using Ryujinx.Audio.Renderer.Server.Upsampler;
using System;

namespace Ryujinx.Audio.Renderer.Dsp.Command
{
    public class UpsampleCommand : ICommand
    {
        public bool Enabled { get; set; }

        public int NodeId { get; }

        public CommandType CommandType => CommandType.Upsample;

        public uint EstimatedProcessingTime { get; set; }

        public uint BufferCount { get; }
        public uint InputBufferIndex { get; }
        public uint InputSampleCount { get; }
        public uint InputSampleRate { get; }

        public UpsamplerState UpsamplerInfo { get; }

        public Memory<float> OutBuffer { get; }

        public UpsampleCommand(uint bufferOffset, UpsamplerState info, uint inputCount, Span<byte> inputBufferOffset, uint bufferCount, uint sampleCount, uint sampleRate, int nodeId)
        {
            Enabled = true;
            NodeId = nodeId;

            InputBufferIndex = 0;
            OutBuffer = info.OutputBuffer;
            BufferCount = bufferCount;
            InputSampleCount = sampleCount;
            InputSampleRate = sampleRate;
            info.SourceSampleCount = inputCount;
            info.InputBufferIndices = new ushort[inputCount];

            for (int i = 0; i < inputCount; i++)
            {
                info.InputBufferIndices[i] = (ushort)(bufferOffset + inputBufferOffset[i]);
            }

            if (info.BufferStates?.Length != (int)inputCount)
            {
                // Keep state if possible.
                info.BufferStates = new UpsamplerBufferState[(int)inputCount];
            }

            UpsamplerInfo = info;
        }

        private Span<float> GetBuffer(int index, int sampleCount)
        {
            return UpsamplerInfo.OutputBuffer.Span.Slice(index * sampleCount, sampleCount);
        }

        public void Process(CommandList context)
        {
            uint bufferCount = Math.Min(BufferCount, UpsamplerInfo.SourceSampleCount);

            for (int i = 0; i < bufferCount; i++)
            {
                Span<float> inputBuffer = context.GetBuffer(UpsamplerInfo.InputBufferIndices[i]);
                Span<float> outputBuffer = GetBuffer(UpsamplerInfo.InputBufferIndices[i], (int)UpsamplerInfo.SampleCount);

                UpsamplerHelper.Upsample(outputBuffer, inputBuffer, (int)UpsamplerInfo.SampleCount, (int)InputSampleCount, ref UpsamplerInfo.BufferStates[i]);
            }
        }
    }
}