aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterState.cs
blob: 3e7dce5592458aa086b44e7a72620e6467dff610 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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;

        private delegate void SplitterDestinationAction(SplitterDestination destination, int index);

        /// <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.
        /// </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="SplitterDestinationVersion1"/>.
        /// </summary>
        private unsafe SplitterDestinationVersion1* _destinationDataV1;

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

        /// <summary>
        /// First element of the linked list of splitter destinations data.
        /// </summary>
        public readonly SplitterDestination Destination
        {
            get
            {
                unsafe
                {
                    return new SplitterDestination(_destinationDataV1, _destinationDataV2);
                }
            }
        }

        /// <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 SplitterDestination GetData(int index)
        {
            int i = 0;

            SplitterDestination result = Destination;

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

                result = result.Next;
                i++;
            }

            return result;
        }

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

        /// <summary>
        /// Utility function to apply an action to all <see cref="Destination"/>.
        /// </summary>
        /// <param name="action">The action to execute on each elements.</param>
        private readonly void ForEachDestination(SplitterDestinationAction action)
        {
            SplitterDestination temp = Destination;

            int i = 0;

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

                SplitterDestination next = temp.Next;

                action(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);

                SplitterDestination destination = context.GetDestination(destinationId);

                SetDestination(destination);

                DestinationCount = destinationCount;

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

                    SplitterDestination nextDestination = context.GetDestination(destinationId);

                    destination.Link(nextDestination);
                    destination = nextDestination;
                }
            }

            if (destinationCount < parameter.DestinationCount)
            {
                input.Advance((parameter.DestinationCount - destinationCount) * sizeof(int));
            }

            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="Destination"/>.
        /// </summary>
        /// <param name="newValue">New destination value.</param>
        public void SetDestination(SplitterDestination newValue)
        {
            unsafe
            {
                fixed (SplitterDestinationVersion1* newValuePtr = &newValue.GetV1RefOrNull())
                {
                    _destinationDataV1 = newValuePtr;
                }

                fixed (SplitterDestinationVersion2* newValuePtr = &newValue.GetV2RefOrNull())
                {
                    _destinationDataV2 = newValuePtr;
                }
            }
        }

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

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

            unsafe
            {
                _destinationDataV1 = null;
                _destinationDataV2 = null;
            }
        }

        /// <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._destinationDataV1 = null;
                    splitter._destinationDataV2 = null;
                }

                splitter.DestinationCount = 0;
            }
        }
    }
}