aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Audio/OpenAL/OpenALAudioOut.cs
blob: 48dcd19919de4292d42a8c39924211811497e183 (plain)
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
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace Ryujinx.Audio.OpenAL
{
    public class OpenALAudioOut : IAalOutput
    {
        private const int MaxTracks = 256;

        private const int MaxReleased = 32;

        private AudioContext Context;

        private class Track : IDisposable
        {
            public int SourceId { get; private set; }

            public int SampleRate { get; private set; }
            
            public ALFormat Format { get; private set; }

            public PlaybackState State { get; set; }

            private ConcurrentDictionary<long, int> Buffers;

            private Queue<long> QueuedTagsQueue;

            private Queue<long> ReleasedTagsQueue;

            private bool Disposed;

            public Track(int SampleRate, ALFormat Format)
            {
                this.SampleRate = SampleRate;
                this.Format     = Format;

                State = PlaybackState.Stopped;

                SourceId = AL.GenSource();

                Buffers = new ConcurrentDictionary<long, int>();

                QueuedTagsQueue = new Queue<long>();

                ReleasedTagsQueue = new Queue<long>();
            }

            public bool ContainsBuffer(long Tag)
            {
                SyncQueuedTags();

                foreach (long QueuedTag in QueuedTagsQueue)
                {
                    if (QueuedTag == Tag)
                    {
                        return true;
                    }
                }

                return false;
            }

            public long[] GetReleasedBuffers(int MaxCount)
            {
                ClearReleased();

                List<long> Tags = new List<long>();

                HashSet<long> Unique = new HashSet<long>();

                while (MaxCount-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
                {
                    if (Unique.Add(Tag))
                    {
                        Tags.Add(Tag);
                    }
                }

                return Tags.ToArray();
            }

            public int AppendBuffer(long Tag)
            {
                if (Disposed)
                {
                    throw new ObjectDisposedException(nameof(Track));
                }

                int Id = AL.GenBuffer();

                Buffers.AddOrUpdate(Tag, Id, (Key, OldId) =>
                {
                    AL.DeleteBuffer(OldId);

                    return Id;
                });

                QueuedTagsQueue.Enqueue(Tag);

                return Id;
            }

            public void ClearReleased()
            {
                SyncQueuedTags();

                AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);

                if (ReleasedCount > 0)
                {
                    AL.SourceUnqueueBuffers(SourceId, ReleasedCount);
                }
            }
            
            private void SyncQueuedTags()
            {
                AL.GetSource(SourceId, ALGetSourcei.BuffersQueued,    out int QueuedCount);
                AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);

                QueuedCount -= ReleasedCount;

                while (QueuedTagsQueue.Count > QueuedCount)
                {
                    ReleasedTagsQueue.Enqueue(QueuedTagsQueue.Dequeue());
                }

                while (ReleasedTagsQueue.Count > MaxReleased)
                {
                    ReleasedTagsQueue.Dequeue();
                }
            }

            public void Dispose()
            {
                Dispose(true);
            }

            protected virtual void Dispose(bool Disposing)
            {
                if (Disposing && !Disposed)
                {
                    Disposed = true;

                    AL.DeleteSource(SourceId);

                    foreach (int Id in Buffers.Values)
                    {
                        AL.DeleteBuffer(Id);
                    }
                }
            }
        }

        private ConcurrentDictionary<int, Track> Tracks;

        public OpenALAudioOut()
        {
            Context = new AudioContext();

            Tracks = new ConcurrentDictionary<int, Track>();
        }

        public int OpenTrack(int SampleRate, int Channels, out AudioFormat Format)
        {
            Format = AudioFormat.PcmInt16;

            Track Td = new Track(SampleRate, GetALFormat(Channels, Format));

            for (int Id = 0; Id < MaxTracks; Id++)
            {
                if (Tracks.TryAdd(Id, Td))
                {
                    return Id;
                }
            }

            return -1;
        }

        private ALFormat GetALFormat(int Channels, AudioFormat Format)
        {
            if (Channels < 1 || Channels > 2)
            {
                throw new ArgumentOutOfRangeException(nameof(Channels));
            }

            if (Channels == 1)
            {
                switch (Format)
                {
                    case AudioFormat.PcmInt8:  return ALFormat.Mono8;
                    case AudioFormat.PcmInt16: return ALFormat.Mono16;
                }
            }
            else /* if (Channels == 2) */
            {
                switch (Format)
                {
                    case AudioFormat.PcmInt8:  return ALFormat.Stereo8;
                    case AudioFormat.PcmInt16: return ALFormat.Stereo16;
                }
            }

            throw new ArgumentException(nameof(Format));
        }

        public void CloseTrack(int Track)
        {
            if (Tracks.TryRemove(Track, out Track Td))
            {
                Td.Dispose();
            }
        }

        public bool ContainsBuffer(int Track, long Tag)
        {
            if (Tracks.TryGetValue(Track, out Track Td))
            {
                return Td.ContainsBuffer(Tag);
            }
            
            return false;
        }

        public long[] GetReleasedBuffers(int Track, int MaxCount)
        {
            if (Tracks.TryGetValue(Track, out Track Td))
            {
                return Td.GetReleasedBuffers(MaxCount);
            }
            
            return null;
        }

        public void AppendBuffer(int Track, long Tag, byte[] Buffer)
        {
            if (Tracks.TryGetValue(Track, out Track Td))
            {
                int BufferId = Td.AppendBuffer(Tag);

                AL.BufferData(BufferId, Td.Format, Buffer, Buffer.Length, Td.SampleRate);

                AL.SourceQueueBuffer(Td.SourceId, BufferId);

                StartPlaybackIfNeeded(Td);
            }
        }

        public void Start(int Track)
        {
            if (Tracks.TryGetValue(Track, out Track Td))
            {
                Td.State = PlaybackState.Playing;

                StartPlaybackIfNeeded(Td);
            }
        }

        private void StartPlaybackIfNeeded(Track Td)
        {
            AL.GetSource(Td.SourceId, ALGetSourcei.SourceState, out int StateInt);

            ALSourceState State = (ALSourceState)StateInt;

            if (State != ALSourceState.Playing && Td.State == PlaybackState.Playing)
            {
                Td.ClearReleased();

                AL.SourcePlay(Td.SourceId);
            }
        }

        public void Stop(int Track)
        {
            if (Tracks.TryGetValue(Track, out Track Td))
            {
                Td.State = PlaybackState.Stopped;

                AL.SourceStop(Td.SourceId);
            }
        }

        public PlaybackState GetState(int Track)
        {
            if (Tracks.TryGetValue(Track, out Track Td))
            {
                return Td.State;
            }

            return PlaybackState.Stopped;
        }
    }
}