Repository navigation
Expand file tree
/
Copy pathExport.cpp
More file actions
370 lines (312 loc) · 11.2 KB
/
Copy pathExport.cpp
File metadata and controls
370 lines (312 loc) · 11.2 KB
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
369
370
#include "prx/libSceAudioIn/include/AudioIn.hpp"
#include "prx/libSceAudioIn/AudioInCapture.hpp"
#include <cstdint>
#include <cstddef>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/PreciseWait.hpp"
#include "SDL.h"
#include <algorithm>
#include <array>
#include <chrono>
#include <cstring>
#include <mutex>
#include <stdexcept>
namespace {
constexpr int AUDIO_IN_ERROR_INVALID_HANDLE = static_cast<int>(0x80260101);
constexpr int AUDIO_IN_ERROR_INVALID_SIZE = static_cast<int>(0x80260102);
constexpr int AUDIO_IN_ERROR_INVALID_FREQ = static_cast<int>(0x80260103);
constexpr int AUDIO_IN_ERROR_INVALID_TYPE = static_cast<int>(0x80260104);
constexpr int AUDIO_IN_ERROR_INVALID_POINTER = static_cast<int>(0x80260105);
constexpr int AUDIO_IN_ERROR_INVALID_PARAM = static_cast<int>(0x80260106);
constexpr int AUDIO_IN_ERROR_PORT_FULL = static_cast<int>(0x80260107);
constexpr int AUDIO_IN_ERROR_BUSY = static_cast<int>(0x8026010A);
constexpr int AUDIO_IN_SILENT_STATE_DEVICE_NONE = 1;
constexpr std::uint32_t MAX_QUEUED_BLOCKS = 4;
using Clock = std::chrono::steady_clock;
struct Format {
SDL_AudioFormat format;
std::uint8_t channels;
};
struct Port {
bool used = false;
bool busy = false;
std::uint32_t samples = 0;
std::uint32_t frameBytes = 0;
SDL_AudioDeviceID device = 0;
Clock::duration grain{};
Clock::time_point next{};
};
std::mutex g_mutex;
std::array<Port, 8> g_ports{};
bool g_sdlAudio = false;
bool formatOf(std::uint32_t param, Format& format) {
switch (param) {
case 1: format = {AUDIO_S16SYS, 1}; return true;
case 2: format = {AUDIO_S16SYS, 2}; return true;
case 0x10:
case 0x11: format = {AUDIO_F32SYS, 1}; return true;
case 0x12: format = {AUDIO_F32SYS, 2}; return true;
default: return false;
}
}
SDL_AudioDeviceID openDevice(std::uint32_t freq, std::uint32_t samples, const Format& format) {
if (!g_sdlAudio) {
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) return 0;
g_sdlAudio = true;
}
SDL_AudioSpec desired{};
desired.freq = static_cast<int>(freq);
desired.format = format.format;
desired.channels = format.channels;
desired.samples = static_cast<Uint16>(samples);
SDL_AudioSpec obtained{};
const SDL_AudioDeviceID device = SDL_OpenAudioDevice(nullptr, 1, &desired, &obtained, 0);
if (device != 0) SDL_PauseAudioDevice(device, 0);
return device;
}
Port* find(int handle) {
if (handle <= 0 || static_cast<std::size_t>(handle) > g_ports.size()) return nullptr;
Port& port = g_ports[static_cast<std::size_t>(handle - 1)];
return port.used ? &port : nullptr;
}
int checkArguments(std::uint32_t type, std::uint32_t index, std::uint32_t len, std::uint32_t freq) {
if (type != 0 && type != 1) return AUDIO_IN_ERROR_INVALID_TYPE;
if (index != 0) return AUDIO_IN_ERROR_INVALID_PARAM;
if (len != 128 && len != 256) return AUDIO_IN_ERROR_INVALID_SIZE;
if (freq != 48000 && freq != 16000) return AUDIO_IN_ERROR_INVALID_FREQ;
return 0;
}
int openPort(std::uint32_t len, std::uint32_t freq, std::uint32_t param) {
Format format{};
if (!formatOf(param, format)) return AUDIO_IN_ERROR_INVALID_PARAM;
std::lock_guard lock(g_mutex);
for (std::size_t i = 0; i < g_ports.size(); ++i) {
if (g_ports[i].used) continue;
const auto frameBytes = static_cast<std::uint32_t>(SDL_AUDIO_BITSIZE(format.format) / 8 * format.channels);
const auto grain = std::chrono::duration_cast<Clock::duration>(std::chrono::microseconds(1000000ull * len / freq));
g_ports[i] = {true, false, len, frameBytes, openDevice(freq, len, format), grain, {}};
return static_cast<int>(i + 1);
}
return AUDIO_IN_ERROR_PORT_FULL;
}
std::size_t capture(SDL_AudioDeviceID device, std::uint8_t* dest, std::size_t bytes, Clock::time_point deadline) {
while (SDL_GetQueuedAudioSize(device) < bytes && Clock::now() < deadline) PreciseSleepUs(1000);
const std::size_t queued = SDL_GetQueuedAudioSize(device);
std::array<std::uint8_t, 4096> discard;
const std::size_t excess = queued > bytes * MAX_QUEUED_BLOCKS ? queued - bytes * MAX_QUEUED_BLOCKS : 0;
audioin::detail::discardQueuedAudio(excess, discard.data(), discard.size(),
[device](std::uint8_t* dest, std::uint32_t chunk) { return SDL_DequeueAudio(device, dest, chunk); });
return SDL_DequeueAudio(device, dest, static_cast<Uint32>(bytes));
}
}
extern "C" {
int APS5_VABI sceAudioInInit() {
return 0;
}
int APS5_VABI sceAudioInGetSilentState(int handle) {
std::lock_guard lock(g_mutex);
const Port* port = find(handle);
if (!port) return AUDIO_IN_ERROR_INVALID_HANDLE;
return port->device != 0 ? 0 : AUDIO_IN_SILENT_STATE_DEVICE_NONE;
}
int APS5_VABI sceAudioInInput(int handle, void* dest) {
std::unique_lock lock(g_mutex);
Port* port = find(handle);
if (!port) return AUDIO_IN_ERROR_INVALID_HANDLE;
if (port->busy) return AUDIO_IN_ERROR_BUSY;
if (!dest) {
if (port->device != 0) SDL_ClearQueuedAudio(port->device);
port->next = {};
return 0;
}
const std::size_t bytes = static_cast<std::size_t>(port->samples) * port->frameBytes;
const SDL_AudioDeviceID device = port->device;
const auto now = Clock::now();
const auto wake = port->next > now ? port->next : now;
port->next = wake + port->grain;
const auto deadline = now + port->grain * MAX_QUEUED_BLOCKS;
const int samples = static_cast<int>(port->samples);
port->busy = true;
lock.unlock();
auto* out = static_cast<std::uint8_t*>(dest);
std::size_t captured = 0;
if (device != 0) {
captured = capture(device, out, bytes, deadline);
} else {
PreciseSleepUntil(wake);
}
std::memset(out + captured, 0, bytes - captured);
lock.lock();
port->busy = false;
return samples;
}
int APS5_VABI sceAudioInOpen(int user_id, uint32_t type, uint32_t index, uint32_t len, uint32_t freq, uint32_t param) {
(void)user_id;
if (const int error = checkArguments(type, index, len, freq); error != 0) return error;
return openPort(len, freq, param);
}
int32_t APS5_VABI sceAudioInClose(int32_t handle) {
std::lock_guard lock(g_mutex);
Port* port = find(handle);
if (!port) return AUDIO_IN_ERROR_INVALID_HANDLE;
if (port->busy) return AUDIO_IN_ERROR_BUSY;
if (port->device != 0) SDL_CloseAudioDevice(port->device);
*port = {};
return 0;
}
int32_t APS5_VABI sceAudioInHqOpen(int32_t userId, uint32_t type, uint32_t index, uint32_t len, uint32_t freq, uint32_t param) {
if (freq != 48000) return AUDIO_IN_ERROR_INVALID_FREQ;
if (len != 128) return AUDIO_IN_ERROR_INVALID_SIZE;
if (param != 2) return AUDIO_IN_ERROR_INVALID_PARAM;
if (const int error = checkArguments(type, index, len, freq); error != 0) return error;
return openPort(len, freq, param);
}
int32_t APS5_VABI sceAudioInAsyncOpen(int32_t userId, uint32_t type, uint32_t index, uint32_t len, uint32_t freq, uint32_t param) {
if (freq != 48000) return AUDIO_IN_ERROR_INVALID_FREQ;
if (len != 128) return AUDIO_IN_ERROR_INVALID_SIZE;
if (const int error = checkArguments(type, index, len, freq); error != 0) return error;
return openPort(len, freq, param);
}
int32_t APS5_VABI sceAudioInIsPadEmbeddedMic(int32_t handle) {
std::lock_guard lock(g_mutex);
if (!find(handle)) return AUDIO_IN_ERROR_INVALID_HANDLE;
return 0;
}
int32_t APS5_VABI sceAudioInGetGain(int32_t handle, float* gain) {
if (!gain) return AUDIO_IN_ERROR_INVALID_POINTER;
*gain = 1.0f;
std::lock_guard lock(g_mutex);
return find(handle) ? 0 : AUDIO_IN_ERROR_INVALID_HANDLE;
}
int32_t APS5_VABI sceAudioInGetRerouteCount(int32_t handle) {
std::lock_guard lock(g_mutex);
return find(handle) ? 0 : AUDIO_IN_ERROR_INVALID_HANDLE;
}
int APS5_VABI sceAudioInDeviceIdOpen(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInDeviceIdAsyncOpen(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetUsbGain(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInIsSharedDevice(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInDeviceIdHqOpen(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInDeviceHqOpen(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetConnections(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetPortConnections(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetPortStatuses(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInGetHandleStatusInfo(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInOpenEx(int user_id, uint32_t type, uint32_t index, uint32_t extra, uint32_t len, uint32_t freq, uint32_t param) {
(void)extra;
return sceAudioInOpen(user_id, type, index, len, freq, param);
}
int APS5_VABI sceAudioInHqOpenEx(int user_id, uint32_t type, uint32_t index, uint32_t extra, uint32_t len, uint32_t freq, uint32_t param) {
(void)extra;
return sceAudioInOpen(user_id, type, index, len, freq, param);
}
int APS5_VABI sceAudioInDeviceOpenEx(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInChangeAppModuleState(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetSparkParam(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetSparkSideTone(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetCompressorPreGain(void) {
return 0;
}
int APS5_VABI sceAudioInSetNoiseGateThreshold(void) {
return 0;
}
int APS5_VABI sceAudioInSetMicLevel(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetDeviceMuteStatus(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetDevConnection(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInCountPorts(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetUserMute(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInVmicCreate(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInVmicDestroy(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInVmicWrite(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInVmicGetStatus(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInVmicSetMute(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetMode(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInSetMode2(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInExtCtrl(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAudioInInputs(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
}