aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Parameter/VoiceInParameter.cs
blob: c4b4ba3128679ba62d9af35af00a9c159a906035 (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
using Ryujinx.Audio.Common;
using Ryujinx.Audio.Renderer.Common;
using Ryujinx.Audio.Renderer.Dsp;
using Ryujinx.Common.Memory;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Ryujinx.Audio.Renderer.Parameter
{
    /// <summary>
    /// Input information for a voice.
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Size = 0x170, Pack = 1)]
    public struct VoiceInParameter
    {
        /// <summary>
        /// Id of the voice.
        /// </summary>
        public int Id;

        /// <summary>
        /// Node id of the voice.
        /// </summary>
        public int NodeId;

        /// <summary>
        /// Set to true if the voice is new.
        /// </summary>
        [MarshalAs(UnmanagedType.I1)]
        public bool IsNew;

        /// <summary>
        /// Set to true if the voice is used.
        /// </summary>
        [MarshalAs(UnmanagedType.I1)]
        public bool InUse;

        /// <summary>
        /// The voice <see cref="PlayState"/> wanted by the user.
        /// </summary>
        public PlayState PlayState;

        /// <summary>
        /// The <see cref="SampleFormat"/> of the voice.
        /// </summary>
        public SampleFormat SampleFormat;

        /// <summary>
        /// The sample rate of the voice.
        /// </summary>
        public uint SampleRate;

        /// <summary>
        /// The priority of the voice.
        /// </summary>
        public uint Priority;

        /// <summary>
        /// Target sorting position of the voice. (Used to sort voices with the same <see cref="Priority"/>)
        /// </summary>
        public uint SortingOrder;

        /// <summary>
        /// The total channel count used.
        /// </summary>
        public uint ChannelCount;

        /// <summary>
        /// The pitch used on the voice.
        /// </summary>
        public float Pitch;

        /// <summary>
        /// The output volume of the voice.
        /// </summary>
        public float Volume;

        /// <summary>
        /// Biquad filters to apply to the output of the voice.
        /// </summary>
        public Array2<BiquadFilterParameter> BiquadFilters;

        /// <summary>
        /// Total count of <see cref="WaveBufferInternal"/> of the voice.
        /// </summary>
        public uint WaveBuffersCount;

        /// <summary>
        /// Current playing <see cref="WaveBufferInternal"/> of the voice.
        /// </summary>
        public uint WaveBuffersIndex;

        /// <summary>
        /// Reserved/unused.
        /// </summary>
        private uint _reserved1;

        /// <summary>
        /// User state address required by the data source.
        /// </summary>
        /// <remarks>Only used for <see cref="SampleFormat.Adpcm"/> as the address of the GC-ADPCM coefficients.</remarks>
        public ulong DataSourceStateAddress;

        /// <summary>
        /// User state size required by the data source.
        /// </summary>
        /// <remarks>Only used for <see cref="SampleFormat.Adpcm"/> as the size of the GC-ADPCM coefficients.</remarks>
        public ulong DataSourceStateSize;

        /// <summary>
        /// The target mix id of the voice.
        /// </summary>
        public int MixId;

        /// <summary>
        /// The target splitter id of the voice.
        /// </summary>
        public uint SplitterId;

        /// <summary>
        /// The wavebuffer parameters of this voice.
        /// </summary>
        public Array4<WaveBufferInternal> WaveBuffers;

        /// <summary>
        /// The channel resource ids associated to the voice.
        /// </summary>
        public Array6<int> ChannelResourceIds;

        /// <summary>
        /// Reset the voice drop flag during voice server update.
        /// </summary>
        [MarshalAs(UnmanagedType.I1)]
        public bool ResetVoiceDropFlag;

        /// <summary>
        /// Flush the amount of wavebuffer specified. This will result in the wavebuffer being skipped and marked played.
        /// </summary>
        /// <remarks>This was added on REV5.</remarks>
        public byte FlushWaveBufferCount;

        /// <summary>
        /// Reserved/unused.
        /// </summary>
        private ushort _reserved2;

        /// <summary>
        /// Change the behaviour of the voice.
        /// </summary>
        /// <remarks>This was added on REV5.</remarks>
        public DecodingBehaviour DecodingBehaviourFlags;

        /// <summary>
        /// Change the Sample Rate Conversion (SRC) quality of the voice.
        /// </summary>
        /// <remarks>This was added on REV8.</remarks>
        public SampleRateConversionQuality SrcQuality;

        /// <summary>
        /// This was previously used for opus codec support on the Audio Renderer and was removed on REV3.
        /// </summary>
        public uint ExternalContext;

        /// <summary>
        /// This was previously used for opus codec support on the Audio Renderer and was removed on REV3.
        /// </summary>
        public uint ExternalContextSize;

        /// <summary>
        /// Reserved/unused.
        /// </summary>
        private unsafe fixed uint _reserved3[2];

        /// <summary>
        /// Input information for a voice wavebuffer.
        /// </summary>
        [StructLayout(LayoutKind.Sequential, Size = 0x38, Pack = 1)]
        public struct WaveBufferInternal
        {
            /// <summary>
            /// Address of the wavebuffer data.
            /// </summary>
            public ulong Address;

            /// <summary>
            /// Size of the wavebuffer data.
            /// </summary>
            public ulong Size;

            /// <summary>
            /// Offset of the first sample to play.
            /// </summary>
            public uint StartSampleOffset;

            /// <summary>
            /// Offset of the last sample to play.
            /// </summary>
            public uint EndSampleOffset;

            /// <summary>
            /// If set to true, the wavebuffer will loop when reaching <see cref="EndSampleOffset"/>.
            /// </summary>
            /// <remarks>
            /// Starting with REV8, you can specify how many times to loop the wavebuffer (<see cref="LoopCount"/>) and where it should start and end when looping (<see cref="LoopFirstSampleOffset"/> and <see cref="LoopLastSampleOffset"/>)
            /// </remarks>
            [MarshalAs(UnmanagedType.I1)]
            public bool ShouldLoop;

            /// <summary>
            /// Indicates that this is the last wavebuffer to play of the voice.
            /// </summary>
            [MarshalAs(UnmanagedType.I1)]
            public bool IsEndOfStream;

            /// <summary>
            /// Indicates if the server should update its internal state.
            /// </summary>
            [MarshalAs(UnmanagedType.I1)]
            public bool SentToServer;

            /// <summary>
            /// Reserved/unused.
            /// </summary>
            private byte _reserved;

            /// <summary>
            /// If set to anything other than 0, specifies how many times to loop the wavebuffer.
            /// </summary>
            /// <remarks>This was added in REV8.</remarks>
            public int LoopCount;

            /// <summary>
            /// Address of the context used by the sample decoder.
            /// </summary>
            /// <remarks>This is only currently used by <see cref="SampleFormat.Adpcm"/>.</remarks>
            public ulong ContextAddress;

            /// <summary>
            /// Size of the context used by the sample decoder.
            /// </summary>
            /// <remarks>This is only currently used by <see cref="SampleFormat.Adpcm"/>.</remarks>
            public ulong ContextSize;

            /// <summary>
            /// If set to anything other than 0, specifies the offset of the first sample to play when looping.
            /// </summary>
            /// <remarks>This was added in REV8.</remarks>
            public uint LoopFirstSampleOffset;

            /// <summary>
            /// If set to anything other than 0, specifies the offset of the last sample to play when looping.
            /// </summary>
            /// <remarks>This was added in REV8.</remarks>
            public uint LoopLastSampleOffset;

            /// <summary>
            /// Check if the sample offsets are in a valid range for generic PCM.
            /// </summary>
            /// <typeparam name="T">The PCM sample type</typeparam>
            /// <returns>Returns true if the sample offset are in range of the size.</returns>
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            private bool IsSampleOffsetInRangeForPcm<T>() where T : unmanaged
            {
                uint dataTypeSize = (uint)Unsafe.SizeOf<T>();

                return StartSampleOffset * dataTypeSize <= Size &&
                       EndSampleOffset * dataTypeSize <= Size;
            }

            /// <summary>
            /// Check if the sample offsets are in a valid range for the given <see cref="SampleFormat"/>.
            /// </summary>
            /// <param name="format">The target <see cref="SampleFormat"/></param>
            /// <returns>Returns true if the sample offset are in range of the size.</returns>
            public bool IsSampleOffsetValid(SampleFormat format)
            {
                bool result;

                switch (format)
                {
                    case SampleFormat.PcmInt16:
                        result = IsSampleOffsetInRangeForPcm<ushort>();
                        break;
                    case SampleFormat.PcmFloat:
                        result = IsSampleOffsetInRangeForPcm<float>();
                        break;
                    case SampleFormat.Adpcm:
                        result = AdpcmHelper.GetAdpcmDataSize((int)StartSampleOffset) <= Size &&
                                 AdpcmHelper.GetAdpcmDataSize((int)EndSampleOffset) <= Size;
                        break;
                    default:
                        throw new NotImplementedException($"{format} not implemented!");
                }

                return result;
            }
        }

        /// <summary>
        /// Flag altering the behaviour of wavebuffer decoding.
        /// </summary>
        [Flags]
        public enum DecodingBehaviour : ushort
        {
            /// <summary>
            /// Default decoding behaviour.
            /// </summary>
            Default = 0,

            /// <summary>
            /// Reset the played samples accumulator when looping.
            /// </summary>
            PlayedSampleCountResetWhenLooping = 1,

            /// <summary>
            /// Skip pitch and Sample Rate Conversion (SRC).
            /// </summary>
            SkipPitchAndSampleRateConversion = 2
        }

        /// <summary>
        /// Specify the quality to use during Sample Rate Conversion (SRC) and pitch handling.
        /// </summary>
        /// <remarks>This was added in REV8.</remarks>
        public enum SampleRateConversionQuality : byte
        {
            /// <summary>
            /// Resample interpolating 4 samples per output sample.
            /// </summary>
            Default,

            /// <summary>
            /// Resample interpolating 8 samples per output sample.
            /// </summary>
            High,

            /// <summary>
            /// Resample interpolating 1 samples per output sample.
            /// </summary>
            Low
        }
    }
}