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
|
using ChocolArm64.Memory;
using Ryujinx.Audio;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.HLE.HOS.Services.Aud.AudioOut;
using System.Collections.Generic;
using System.Text;
using static Ryujinx.HLE.HOS.ErrorCode;
namespace Ryujinx.HLE.HOS.Services.Aud
{
class IAudioOutManager : IpcService
{
private const string DefaultAudioOutput = "DeviceOut";
private const int DefaultSampleRate = 48000;
private const int DefaultChannelsCount = 2;
private Dictionary<int, ServiceProcessRequest> _commands;
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
public IAudioOutManager()
{
_commands = new Dictionary<int, ServiceProcessRequest>
{
{ 0, ListAudioOuts },
{ 1, OpenAudioOut },
{ 2, ListAudioOutsAuto },
{ 3, OpenAudioOutAuto }
};
}
public long ListAudioOuts(ServiceCtx context)
{
return ListAudioOutsImpl(
context,
context.Request.ReceiveBuff[0].Position,
context.Request.ReceiveBuff[0].Size);
}
public long OpenAudioOut(ServiceCtx context)
{
return OpenAudioOutImpl(
context,
context.Request.SendBuff[0].Position,
context.Request.SendBuff[0].Size,
context.Request.ReceiveBuff[0].Position,
context.Request.ReceiveBuff[0].Size);
}
public long ListAudioOutsAuto(ServiceCtx context)
{
(long recvPosition, long recvSize) = context.Request.GetBufferType0x22();
return ListAudioOutsImpl(context, recvPosition, recvSize);
}
public long OpenAudioOutAuto(ServiceCtx context)
{
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
(long recvPosition, long recvSize) = context.Request.GetBufferType0x22();
return OpenAudioOutImpl(
context,
sendPosition,
sendSize,
recvPosition,
recvSize);
}
private long ListAudioOutsImpl(ServiceCtx context, long position, long size)
{
int nameCount = 0;
byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(DefaultAudioOutput + "\0");
if ((ulong)deviceNameBuffer.Length <= (ulong)size)
{
context.Memory.WriteBytes(position, deviceNameBuffer);
nameCount++;
}
else
{
Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
}
context.ResponseData.Write(nameCount);
return 0;
}
private long OpenAudioOutImpl(ServiceCtx context, long sendPosition, long sendSize, long receivePosition, long receiveSize)
{
string deviceName = MemoryHelper.ReadAsciiString(
context.Memory,
sendPosition,
sendSize);
if (deviceName == string.Empty)
{
deviceName = DefaultAudioOutput;
}
if (deviceName != DefaultAudioOutput)
{
Logger.PrintWarning(LogClass.Audio, "Invalid device name!");
return MakeError(ErrorModule.Audio, AudErr.DeviceNotFound);
}
byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(deviceName + "\0");
if ((ulong)deviceNameBuffer.Length <= (ulong)receiveSize)
{
context.Memory.WriteBytes(receivePosition, deviceNameBuffer);
}
else
{
Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {receiveSize} too small!");
}
int sampleRate = context.RequestData.ReadInt32();
int channels = context.RequestData.ReadInt32();
if (sampleRate == 0)
{
sampleRate = DefaultSampleRate;
}
if (sampleRate != DefaultSampleRate)
{
Logger.PrintWarning(LogClass.Audio, "Invalid sample rate!");
return MakeError(ErrorModule.Audio, AudErr.UnsupportedSampleRate);
}
channels = (ushort)channels;
if (channels == 0)
{
channels = DefaultChannelsCount;
}
KEvent releaseEvent = new KEvent(context.Device.System);
ReleaseCallback callback = () =>
{
releaseEvent.ReadableEvent.Signal();
};
IAalOutput audioOut = context.Device.AudioOut;
int track = audioOut.OpenTrack(sampleRate, channels, callback);
MakeObject(context, new IAudioOut(audioOut, releaseEvent, track));
context.ResponseData.Write(sampleRate);
context.ResponseData.Write(channels);
context.ResponseData.Write((int)SampleFormat.PcmInt16);
context.ResponseData.Write((int)PlaybackState.Stopped);
return 0;
}
}
}
|