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
|
using OpenTK.Audio.OpenAL;
using Ryujinx.Audio.Backends.Common;
using Ryujinx.Audio.Common;
using Ryujinx.Memory;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Ryujinx.Audio.Backends.OpenAL
{
class OpenALHardwareDeviceSession : HardwareDeviceSessionOutputBase
{
private readonly OpenALHardwareDeviceDriver _driver;
private readonly int _sourceId;
private readonly ALFormat _targetFormat;
private bool _isActive;
private readonly Queue<OpenALAudioBuffer> _queuedBuffers;
private ulong _playedSampleCount;
private float _volume;
private readonly object _lock = new();
public OpenALHardwareDeviceSession(OpenALHardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount)
{
_driver = driver;
_queuedBuffers = new Queue<OpenALAudioBuffer>();
_sourceId = AL.GenSource();
_targetFormat = GetALFormat();
_isActive = false;
_playedSampleCount = 0;
SetVolume(1f);
}
private ALFormat GetALFormat()
{
return RequestedSampleFormat switch
{
SampleFormat.PcmInt16 => RequestedChannelCount switch
{
1 => ALFormat.Mono16,
2 => ALFormat.Stereo16,
6 => ALFormat.Multi51Chn16Ext,
_ => throw new NotImplementedException($"Unsupported channel config {RequestedChannelCount}"),
},
_ => throw new NotImplementedException($"Unsupported sample format {RequestedSampleFormat}"),
};
}
public override void PrepareToClose() { }
private void StartIfNotPlaying()
{
AL.GetSource(_sourceId, ALGetSourcei.SourceState, out int stateInt);
ALSourceState State = (ALSourceState)stateInt;
if (State != ALSourceState.Playing)
{
AL.SourcePlay(_sourceId);
}
}
public override void QueueBuffer(AudioBuffer buffer)
{
lock (_lock)
{
OpenALAudioBuffer driverBuffer = new()
{
DriverIdentifier = buffer.DataPointer,
BufferId = AL.GenBuffer(),
SampleCount = GetSampleCount(buffer),
};
AL.BufferData(driverBuffer.BufferId, _targetFormat, buffer.Data, (int)RequestedSampleRate);
_queuedBuffers.Enqueue(driverBuffer);
AL.SourceQueueBuffer(_sourceId, driverBuffer.BufferId);
if (_isActive)
{
StartIfNotPlaying();
}
}
}
public override void SetVolume(float volume)
{
_volume = volume;
UpdateMasterVolume(_driver.Volume);
}
public override float GetVolume()
{
return _volume;
}
public void UpdateMasterVolume(float newVolume)
{
lock (_lock)
{
AL.Source(_sourceId, ALSourcef.Gain, newVolume * _volume);
}
}
public override void Start()
{
lock (_lock)
{
_isActive = true;
StartIfNotPlaying();
}
}
public override void Stop()
{
lock (_lock)
{
SetVolume(0.0f);
AL.SourceStop(_sourceId);
_isActive = false;
}
}
public override void UnregisterBuffer(AudioBuffer buffer) { }
public override bool WasBufferFullyConsumed(AudioBuffer buffer)
{
lock (_lock)
{
if (!_queuedBuffers.TryPeek(out OpenALAudioBuffer driverBuffer))
{
return true;
}
return driverBuffer.DriverIdentifier != buffer.DataPointer;
}
}
public override ulong GetPlayedSampleCount()
{
lock (_lock)
{
return _playedSampleCount;
}
}
public bool Update()
{
lock (_lock)
{
if (_isActive)
{
AL.GetSource(_sourceId, ALGetSourcei.BuffersProcessed, out int releasedCount);
if (releasedCount > 0)
{
int[] bufferIds = new int[releasedCount];
AL.SourceUnqueueBuffers(_sourceId, releasedCount, bufferIds);
int i = 0;
while (_queuedBuffers.TryPeek(out OpenALAudioBuffer buffer) && i < bufferIds.Length)
{
if (buffer.BufferId == bufferIds[i])
{
_playedSampleCount += buffer.SampleCount;
_queuedBuffers.TryDequeue(out _);
i++;
}
}
Debug.Assert(i == bufferIds.Length, "Unknown buffer ids found!");
AL.DeleteBuffers(bufferIds);
}
return releasedCount > 0;
}
return false;
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing && _driver.Unregister(this))
{
lock (_lock)
{
PrepareToClose();
Stop();
AL.DeleteSource(_sourceId);
}
}
}
public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
|