aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Server/Mix/MixContext.cs
blob: 8991ceaf94997c7c91c6cc2a8ed26cd17a77fac4 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using Ryujinx.Audio.Renderer.Common;
using Ryujinx.Audio.Renderer.Server.Splitter;
using Ryujinx.Audio.Renderer.Utils;
using System;
using System.Diagnostics;

namespace Ryujinx.Audio.Renderer.Server.Mix
{
    /// <summary>
    /// Mix context.
    /// </summary>
    public class MixContext
    {
        /// <summary>
        /// The total mix count.
        /// </summary>
        private uint _mixesCount;

        /// <summary>
        /// Storage for <see cref="MixState"/>.
        /// </summary>
        private Memory<MixState> _mixes;

        /// <summary>
        /// Storage of the sorted indices to <see cref="MixState"/>.
        /// </summary>
        private Memory<int> _sortedMixes;

        /// <summary>
        /// Graph state.
        /// </summary>
        public NodeStates NodeStates { get; }

        /// <summary>
        /// The instance of the adjacent matrix.
        /// </summary>
        public EdgeMatrix EdgeMatrix { get; }

        /// <summary>
        /// Create a new instance of <see cref="MixContext"/>.
        /// </summary>
        public MixContext()
        {
            NodeStates = new NodeStates();
            EdgeMatrix = new EdgeMatrix();
        }

        /// <summary>
        /// Initialize the <see cref="MixContext"/>.
        /// </summary>
        /// <param name="sortedMixes">The storage for sorted indices.</param>
        /// <param name="mixes">The storage of <see cref="MixState"/>.</param>
        /// <param name="nodeStatesWorkBuffer">The storage used for the <see cref="NodeStates"/>.</param>
        /// <param name="edgeMatrixWorkBuffer">The storage used for the <see cref="EdgeMatrix"/>.</param>
        public void Initialize(Memory<int> sortedMixes, Memory<MixState> mixes, Memory<byte> nodeStatesWorkBuffer, Memory<byte> edgeMatrixWorkBuffer)
        {
            _mixesCount = (uint)mixes.Length;
            _mixes = mixes;
            _sortedMixes = sortedMixes;

            if (!nodeStatesWorkBuffer.IsEmpty && !edgeMatrixWorkBuffer.IsEmpty)
            {
                NodeStates.Initialize(nodeStatesWorkBuffer, mixes.Length);
                EdgeMatrix.Initialize(edgeMatrixWorkBuffer, mixes.Length);
            }

            int sortedId = 0;
            for (int i = 0; i < _mixes.Length; i++)
            {
                SetSortedState(sortedId++, i);
            }
        }

        /// <summary>
        /// Associate the given <paramref name="targetIndex"/> to a given <paramref cref="id"/>.
        /// </summary>
        /// <param name="id">The sorted id.</param>
        /// <param name="targetIndex">The index to associate.</param>
        private void SetSortedState(int id, int targetIndex)
        {
            _sortedMixes.Span[id] = targetIndex;
        }

        /// <summary>
        /// Get a reference to the final <see cref="MixState"/>.
        /// </summary>
        /// <returns>A reference to the final <see cref="MixState"/>.</returns>
        public ref MixState GetFinalState()
        {
            return ref GetState(Constants.FinalMixId);
        }

        /// <summary>
        /// Get a reference to a <see cref="MixState"/> at the given <paramref name="id"/>.
        /// </summary>
        /// <param name="id">The index to use.</param>
        /// <returns>A reference to a <see cref="MixState"/> at the given <paramref name="id"/>.</returns>
        public ref MixState GetState(int id)
        {
            return ref SpanIOHelper.GetFromMemory(_mixes, id, _mixesCount);
        }

        /// <summary>
        /// Get a reference to a <see cref="MixState"/> at the given <paramref name="id"/> of the sorted mix info.
        /// </summary>
        /// <param name="id">The index to use.</param>
        /// <returns>A reference to a <see cref="MixState"/> at the given <paramref name="id"/>.</returns>
        public ref MixState GetSortedState(int id)
        {
            Debug.Assert(id >= 0 && id < _mixesCount);

            return ref GetState(_sortedMixes.Span[id]);
        }

        /// <summary>
        /// Get the total mix count.
        /// </summary>
        /// <returns>The total mix count.</returns>
        public uint GetCount()
        {
            return _mixesCount;
        }

        /// <summary>
        /// Update the internal distance from the final mix value of every <see cref="MixState"/>.
        /// </summary>
        private void UpdateDistancesFromFinalMix()
        {
            foreach (ref MixState mix in _mixes.Span)
            {
                mix.ClearDistanceFromFinalMix();
            }

            for (int i = 0; i < GetCount(); i++)
            {
                ref MixState mix = ref GetState(i);

                SetSortedState(i, i);

                if (mix.IsUsed)
                {
                    uint distance;

                    if (mix.MixId != Constants.FinalMixId)
                    {
                        int mixId = mix.MixId;

                        for (distance = 0; distance < GetCount(); distance++)
                        {
                            if (mixId == Constants.UnusedMixId)
                            {
                                distance = MixState.InvalidDistanceFromFinalMix;
                                break;
                            }

                            ref MixState distanceMix = ref GetState(mixId);

                            if (distanceMix.DistanceFromFinalMix != MixState.InvalidDistanceFromFinalMix)
                            {
                                distance = distanceMix.DistanceFromFinalMix + 1;
                                break;
                            }

                            mixId = distanceMix.DestinationMixId;

                            if (mixId == Constants.FinalMixId)
                            {
                                break;
                            }
                        }

                        if (distance > GetCount())
                        {
                            distance = MixState.InvalidDistanceFromFinalMix;
                        }
                    }
                    else
                    {
                        distance = MixState.InvalidDistanceFromFinalMix;
                    }

                    mix.DistanceFromFinalMix = distance;
                }
            }
        }

        /// <summary>
        /// Update the internal mix buffer offset of all <see cref="MixState"/>.
        /// </summary>
        private void UpdateMixBufferOffset()
        {
            uint offset = 0;

            foreach (ref MixState mix in _mixes.Span)
            {
                mix.BufferOffset = offset;

                offset += mix.BufferCount;
            }
        }

        /// <summary>
        /// Sort the mixes using distance from the final mix.
        /// </summary>
        public void Sort()
        {
            UpdateDistancesFromFinalMix();

            int[] sortedMixesTemp = _sortedMixes[..(int)GetCount()].ToArray();

            Array.Sort(sortedMixesTemp, (a, b) =>
            {
                ref MixState stateA = ref GetState(a);
                ref MixState stateB = ref GetState(b);

                return stateB.DistanceFromFinalMix.CompareTo(stateA.DistanceFromFinalMix);
            });

            sortedMixesTemp.AsSpan().CopyTo(_sortedMixes.Span);

            UpdateMixBufferOffset();
        }

        /// <summary>
        /// Sort the mixes and splitters using an adjacency matrix.
        /// </summary>
        /// <param name="splitterContext">The <see cref="SplitterContext"/> used.</param>
        /// <returns>Return true, if no errors in the graph were detected.</returns>
        public bool Sort(SplitterContext splitterContext)
        {
            if (splitterContext.UsingSplitter())
            {
                bool isValid = NodeStates.Sort(EdgeMatrix);

                if (isValid)
                {
                    ReadOnlySpan<int> sortedMixesIndex = NodeStates.GetTsortResult();

                    int id = 0;

                    for (int i = sortedMixesIndex.Length - 1; i >= 0; i--)
                    {
                        SetSortedState(id++, sortedMixesIndex[i]);
                    }

                    UpdateMixBufferOffset();
                }

                return isValid;
            }

            UpdateMixBufferOffset();

            return true;
        }
    }
}