aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Server/Splitter/SplitterDestination.cs
blob: 36dfa5e4130797e5aa49326666c8b50db0a5d136 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using Ryujinx.Audio.Renderer.Parameter;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Ryujinx.Audio.Renderer.Server.Splitter
{
    /// <summary>
    /// Server state for a splitter destination.
    /// </summary>
    public ref struct SplitterDestination
    {
        private ref SplitterDestinationVersion1 _v1;
        private ref SplitterDestinationVersion2 _v2;

        /// <summary>
        /// Checks if the splitter destination data reference is null.
        /// </summary>
        public bool IsNull => Unsafe.IsNullRef(ref _v1) && Unsafe.IsNullRef(ref _v2);

        /// <summary>
        /// The splitter unique id.
        /// </summary>
        public int Id
        {
            get
            {
                if (Unsafe.IsNullRef(ref _v2))
                {
                    if (Unsafe.IsNullRef(ref _v1))
                    {
                        return 0;
                    }
                    else
                    {
                        return _v1.Id;
                    }
                }
                else
                {
                    return _v2.Id;
                }
            }
        }

        /// <summary>
        /// The mix to output the result of the splitter.
        /// </summary>
        public int DestinationId
        {
            get
            {
                if (Unsafe.IsNullRef(ref _v2))
                {
                    if (Unsafe.IsNullRef(ref _v1))
                    {
                        return 0;
                    }
                    else
                    {
                        return _v1.DestinationId;
                    }
                }
                else
                {
                    return _v2.DestinationId;
                }
            }
        }

        /// <summary>
        /// Mix buffer volumes.
        /// </summary>
        /// <remarks>Used when a splitter id is specified in the mix.</remarks>
        public Span<float> MixBufferVolume
        {
            get
            {
                if (Unsafe.IsNullRef(ref _v2))
                {
                    if (Unsafe.IsNullRef(ref _v1))
                    {
                        return Span<float>.Empty;
                    }
                    else
                    {
                        return _v1.MixBufferVolume;
                    }
                }
                else
                {
                    return _v2.MixBufferVolume;
                }
            }
        }

        /// <summary>
        /// Previous mix buffer volumes.
        /// </summary>
        /// <remarks>Used when a splitter id is specified in the mix.</remarks>
        public Span<float> PreviousMixBufferVolume
        {
            get
            {
                if (Unsafe.IsNullRef(ref _v2))
                {
                    if (Unsafe.IsNullRef(ref _v1))
                    {
                        return Span<float>.Empty;
                    }
                    else
                    {
                        return _v1.PreviousMixBufferVolume;
                    }
                }
                else
                {
                    return _v2.PreviousMixBufferVolume;
                }
            }
        }

        /// <summary>
        /// Get the <see cref="SplitterDestination"/> of the next element or null if not present.
        /// </summary>
        public readonly SplitterDestination Next
        {
            get
            {
                unsafe
                {
                    if (Unsafe.IsNullRef(ref _v2))
                    {
                        if (Unsafe.IsNullRef(ref _v1))
                        {
                            return new SplitterDestination();
                        }
                        else
                        {
                            return new SplitterDestination(ref _v1.Next);
                        }
                    }
                    else
                    {
                        return new SplitterDestination(ref _v2.Next);
                    }
                }
            }
        }

        /// <summary>
        /// Creates a new splitter destination wrapper for the version 1 splitter destination data.
        /// </summary>
        /// <param name="v1">Version 1 splitter destination data</param>
        public SplitterDestination(ref SplitterDestinationVersion1 v1)
        {
            _v1 = ref v1;
            _v2 = ref Unsafe.NullRef<SplitterDestinationVersion2>();
        }

        /// <summary>
        /// Creates a new splitter destination wrapper for the version 2 splitter destination data.
        /// </summary>
        /// <param name="v2">Version 2 splitter destination data</param>
        public SplitterDestination(ref SplitterDestinationVersion2 v2)
        {

            _v1 = ref Unsafe.NullRef<SplitterDestinationVersion1>();
            _v2 = ref v2;
        }

        /// <summary>
        /// Creates a new splitter destination wrapper for the splitter destination data.
        /// </summary>
        /// <param name="v1">Version 1 splitter destination data</param>
        /// <param name="v2">Version 2 splitter destination data</param>
        public unsafe SplitterDestination(SplitterDestinationVersion1* v1, SplitterDestinationVersion2* v2)
        {
            _v1 = ref Unsafe.AsRef<SplitterDestinationVersion1>(v1);
            _v2 = ref Unsafe.AsRef<SplitterDestinationVersion2>(v2);
        }

        /// <summary>
        /// Update the splitter destination data from user parameter.
        /// </summary>
        /// <param name="parameter">The user parameter.</param>
        public void Update<T>(in T parameter) where T : ISplitterDestinationInParameter
        {
            if (Unsafe.IsNullRef(ref _v2))
            {
                _v1.Update(parameter);
            }
            else
            {
                _v2.Update(parameter);
            }
        }

        /// <summary>
        /// Update the internal state of the instance.
        /// </summary>
        public void UpdateInternalState()
        {
            if (Unsafe.IsNullRef(ref _v2))
            {
                _v1.UpdateInternalState();
            }
            else
            {
                _v2.UpdateInternalState();
            }
        }

        /// <summary>
        /// Set the update internal state marker.
        /// </summary>
        public void MarkAsNeedToUpdateInternalState()
        {
            if (Unsafe.IsNullRef(ref _v2))
            {
                _v1.MarkAsNeedToUpdateInternalState();
            }
            else
            {
                _v2.MarkAsNeedToUpdateInternalState();
            }
        }

        /// <summary>
        /// Return true if the splitter destination is used and has a destination.
        /// </summary>
        /// <returns>True if the splitter destination is used and has a destination.</returns>
        public readonly bool IsConfigured()
        {
            return Unsafe.IsNullRef(ref _v2) ? _v1.IsConfigured() : _v2.IsConfigured();
        }

        /// <summary>
        /// Get the volume for a given destination.
        /// </summary>
        /// <param name="destinationIndex">The destination index to use.</param>
        /// <returns>The volume for the given destination.</returns>
        public float GetMixVolume(int destinationIndex)
        {
            return Unsafe.IsNullRef(ref _v2) ? _v1.GetMixVolume(destinationIndex) : _v2.GetMixVolume(destinationIndex);
        }

        /// <summary>
        /// Get the previous volume for a given destination.
        /// </summary>
        /// <param name="destinationIndex">The destination index to use.</param>
        /// <returns>The volume for the given destination.</returns>
        public float GetMixVolumePrev(int destinationIndex)
        {
            return Unsafe.IsNullRef(ref _v2) ? _v1.GetMixVolumePrev(destinationIndex) : _v2.GetMixVolumePrev(destinationIndex);
        }

        /// <summary>
        /// Clear the volumes.
        /// </summary>
        public void ClearVolumes()
        {
            if (Unsafe.IsNullRef(ref _v2))
            {
                _v1.ClearVolumes();
            }
            else
            {
                _v2.ClearVolumes();
            }
        }

        /// <summary>
        /// Link the next element to the given splitter destination.
        /// </summary>
        /// <param name="next">The given splitter destination to link.</param>
        public void Link(SplitterDestination next)
        {
            if (Unsafe.IsNullRef(ref _v2))
            {
                Debug.Assert(!Unsafe.IsNullRef(ref next._v1));

                _v1.Link(ref next._v1);
            }
            else
            {
                Debug.Assert(!Unsafe.IsNullRef(ref next._v2));

                _v2.Link(ref next._v2);
            }
        }

        /// <summary>
        /// Remove the link to the next element.
        /// </summary>
        public void Unlink()
        {
            if (Unsafe.IsNullRef(ref _v2))
            {
                _v1.Unlink();
            }
            else
            {
                _v2.Unlink();
            }
        }

        /// <summary>
        /// Checks if any biquad filter is enabled.
        /// </summary>
        /// <returns>True if any biquad filter is enabled.</returns>
        public bool IsBiquadFilterEnabled()
        {
            return !Unsafe.IsNullRef(ref _v2) && _v2.IsBiquadFilterEnabled();
        }

        /// <summary>
        /// Checks if any biquad filter was previously enabled.
        /// </summary>
        /// <returns>True if any biquad filter was previously enabled.</returns>
        public bool IsBiquadFilterEnabledPrev()
        {
            return !Unsafe.IsNullRef(ref _v2) && _v2.IsBiquadFilterEnabledPrev();
        }

        /// <summary>
        /// Gets the biquad filter parameters.
        /// </summary>
        /// <param name="index">Biquad filter index (0 or 1).</param>
        /// <returns>Biquad filter parameters.</returns>
        public ref BiquadFilterParameter GetBiquadFilterParameter(int index)
        {
            Debug.Assert(!Unsafe.IsNullRef(ref _v2));

            return ref _v2.GetBiquadFilterParameter(index);
        }

        /// <summary>
        /// Checks if any biquad filter was previously enabled.
        /// </summary>
        /// <param name="index">Biquad filter index (0 or 1).</param>
        public void UpdateBiquadFilterEnabledPrev(int index)
        {
            if (!Unsafe.IsNullRef(ref _v2))
            {
                _v2.UpdateBiquadFilterEnabledPrev(index);
            }
        }

        /// <summary>
        /// Get the reference for the version 1 splitter destination data, or null if version 2 is being used or the destination is null.
        /// </summary>
        /// <returns>Reference for the version 1 splitter destination data.</returns>
        public ref SplitterDestinationVersion1 GetV1RefOrNull()
        {
            return ref _v1;
        }

        /// <summary>
        /// Get the reference for the version 2 splitter destination data, or null if version 1 is being used or the destination is null.
        /// </summary>
        /// <returns>Reference for the version 2 splitter destination data.</returns>
        public ref SplitterDestinationVersion2 GetV2RefOrNull()
        {
            return ref _v2;
        }
    }
}