aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterState.cs
blob: 109c81b23a2ee1e3a999fe35fb7998047efa9876 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using Ryujinx.Audio.Renderer.Parameter;
using Ryujinx.Common.Extensions;
using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Ryujinx.Audio.Renderer.Server.Splitter
{
    /// <summary>
    /// Server state for a splitter.
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = Alignment)]
    public struct SplitterState
    {
        public const int Alignment = 0x10;

        /// <summary>
        /// The unique id of this <see cref="SplitterState"/>.
        /// </summary>
        public int Id;

        /// <summary>
        /// Target sample rate to use on the splitter.
        /// </summary>
        public uint SampleRate;

        /// <summary>
        /// Count of splitter destinations (<see cref="SplitterDestination"/>).
        /// </summary>
        public int DestinationCount;

        /// <summary>
        /// Set to true if the splitter has a new connection.
        /// </summary>
        [MarshalAs(UnmanagedType.I1)]
        public bool HasNewConnection;

        /// <summary>
        /// Linked list of <see cref="SplitterDestination"/>.
        /// </summary>
        private unsafe SplitterDestination* _destinationsData;

        /// <summary>
        /// Span to the first element of the linked list of <see cref="SplitterDestination"/>.
        /// </summary>
        public readonly Span<SplitterDestination> Destinations
        {
            get
            {
                unsafe
                {
                    return (IntPtr)_destinationsData != IntPtr.Zero ? new Span<SplitterDestination>(_destinationsData, 1) : Span<SplitterDestination>.Empty;
                }
            }
        }

        /// <summary>
        /// Create a new <see cref="SplitterState"/>.
        /// </summary>
        /// <param name="id">The unique id of this <see cref="SplitterState"/>.</param>
        public SplitterState(int id) : this()
        {
            Id = id;
        }

        public readonly Span<SplitterDestination> GetData(int index)
        {
            int i = 0;

            Span<SplitterDestination> result = Destinations;

            while (i < index)
            {
                if (result.IsEmpty)
                {
                    break;
                }

                result = result[0].Next;
                i++;
            }

            return result;
        }

        /// <summary>
        /// Clear the new connection flag.
        /// </summary>
        public void ClearNewConnectionFlag()
        {
            HasNewConnection = false;
        }

        /// <summary>
        /// Utility function to apply a given <see cref="SpanAction{T, TArg}"/> to all <see cref="Destinations"/>.
        /// </summary>
        /// <param name="action">The action to execute on each elements.</param>
        private readonly void ForEachDestination(SpanAction<SplitterDestination, int> action)
        {
            Span<SplitterDestination> temp = Destinations;

            int i = 0;

            while (true)
            {
                if (temp.IsEmpty)
                {
                    break;
                }

                Span<SplitterDestination> next = temp[0].Next;

                action.Invoke(temp, i++);

                temp = next;
            }
        }

        /// <summary>
        /// Update the <see cref="SplitterState"/> from user parameter.
        /// </summary>
        /// <param name="context">The splitter context.</param>
        /// <param name="parameter">The user parameter.</param>
        /// <param name="input">The raw input data after the <paramref name="parameter"/>.</param>
        public void Update(SplitterContext context, in SplitterInParameter parameter, ref SequenceReader<byte> input)
        {
            ClearLinks();

            int destinationCount;

            if (context.IsBugFixed)
            {
                destinationCount = parameter.DestinationCount;
            }
            else
            {
                destinationCount = Math.Min(context.GetDestinationCountPerStateForCompatibility(), parameter.DestinationCount);
            }

            if (destinationCount > 0)
            {
                input.ReadLittleEndian(out int destinationId);

                Memory<SplitterDestination> destination = context.GetDestinationMemory(destinationId);

                SetDestination(ref destination.Span[0]);

                DestinationCount = destinationCount;

                for (int i = 1; i < destinationCount; i++)
                {
                    input.ReadLittleEndian(out destinationId);

                    Memory<SplitterDestination> nextDestination = context.GetDestinationMemory(destinationId);

                    destination.Span[0].Link(ref nextDestination.Span[0]);
                    destination = nextDestination;
                }
            }

            Debug.Assert(parameter.Id == Id);

            if (parameter.Id == Id)
            {
                SampleRate = parameter.SampleRate;
                HasNewConnection = true;
            }
        }

        /// <summary>
        /// Set the head of the linked list of <see cref="Destinations"/>.
        /// </summary>
        /// <param name="newValue">A reference to a <see cref="SplitterDestination"/>.</param>
        public void SetDestination(ref SplitterDestination newValue)
        {
            unsafe
            {
                fixed (SplitterDestination* newValuePtr = &newValue)
                {
                    _destinationsData = newValuePtr;
                }
            }
        }

        /// <summary>
        /// Update the internal state of this instance.
        /// </summary>
        public readonly void UpdateInternalState()
        {
            ForEachDestination((destination, _) => destination[0].UpdateInternalState());
        }

        /// <summary>
        /// Clear all links from the <see cref="Destinations"/>.
        /// </summary>
        public void ClearLinks()
        {
            ForEachDestination((destination, _) => destination[0].Unlink());

            unsafe
            {
                _destinationsData = (SplitterDestination*)IntPtr.Zero;
            }
        }

        /// <summary>
        /// Initialize a given <see cref="Span{SplitterState}"/>.
        /// </summary>
        /// <param name="splitters">All the <see cref="SplitterState"/> to initialize.</param>
        public static void InitializeSplitters(Span<SplitterState> splitters)
        {
            foreach (ref SplitterState splitter in splitters)
            {
                unsafe
                {
                    splitter._destinationsData = (SplitterDestination*)IntPtr.Zero;
                }

                splitter.DestinationCount = 0;
            }
        }
    }
}