feat(libSceAudioIn): implement sceAudioInClose and throw for the unverified 0x10 format

Close releases the port and its host device; Input and Close on a port
that is capturing return BUSY. Format codes follow the PS5 SDK bindings
(SharpProspero) and KytyPS5: 1/2 S16 mono/stereo, 0x11/0x12 float.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Adrià Franch
2026-10-04 08:39:33 +02:00
parent 29aa4f3cdf
commit 7e5ce945e1
2 changed files with 25 additions and 5 deletions
+18 -5
View File
@@ -18,6 +18,7 @@ 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_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;
@@ -30,6 +31,7 @@ struct Format {
struct Port {
bool used = false;
bool busy = false;
std::uint32_t samples = 0;
std::uint32_t frameBytes = 0;
SDL_AudioDeviceID device = 0;
@@ -45,7 +47,6 @@ 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;
@@ -100,6 +101,7 @@ 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 = {};
@@ -112,6 +114,7 @@ int APS5_VABI sceAudioInInput(int handle, void* dest) {
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);
@@ -122,6 +125,8 @@ int APS5_VABI sceAudioInInput(int handle, void* dest) {
std::this_thread::sleep_until(wake);
}
std::memset(out + captured, 0, bytes - captured);
lock.lock();
port->busy = false;
return samples;
}
@@ -131,6 +136,10 @@ int APS5_VABI sceAudioInOpen(int user_id, uint32_t type, uint32_t index, uint32_
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;
if (param == 0x10) {
NotImplemented_nid_no_patch(__func__);
return 0;
}
Format format{};
if (!formatOf(param, format)) return AUDIO_IN_ERROR_INVALID_PARAM;
std::lock_guard lock(g_mutex);
@@ -138,16 +147,20 @@ int APS5_VABI sceAudioInOpen(int user_id, uint32_t type, uint32_t index, uint32_
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, len, frameBytes, openDevice(freq, len, format), grain, {}};
g_ports[i] = {true, false, len, frameBytes, openDevice(freq, len, format), grain, {}};
return static_cast<int>(i + 1);
}
return AUDIO_IN_ERROR_PORT_FULL;
}
int32_t APS5_VABI sceAudioInClose(int32_t handle) {
(void)handle;
NotImplemented_nid_no_patch(__func__);
return 0;
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 user_id, uint32_t type, uint32_t index, uint32_t len, uint32_t freq, uint32_t param) {
+7
View File
@@ -12,6 +12,7 @@ extern "C" {
int APS5_VABI sceAudioInOpen(int, std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t);
int APS5_VABI sceAudioInInput(int, void*);
int APS5_VABI sceAudioInGetSilentState(int);
int APS5_VABI sceAudioInClose(int);
}
static void Require(bool value) { if (!value) std::abort(); }
@@ -38,6 +39,7 @@ void TestValidation() {
Require(sceAudioInOpen(user, 0, 0, 512, 48000, 2) == invalidSize);
Require(sceAudioInOpen(user, 0, 0, 256, 44100, 2) == invalidFreq);
Require(sceAudioInOpen(user, 0, 0, 256, 48000, 3) == invalidParam);
Require(sceAudioInOpen(user, 0, 0, 256, 48000, 0) == invalidParam);
}
void TestCapture() {
@@ -61,6 +63,7 @@ void TestCapture() {
Require(captured == recorded);
Require(sceAudioInInput(handle, block.data()) == 256);
for (std::size_t i = 0; i < 256 * 2; ++i) Require(block[i] == 0);
Require(sceAudioInClose(handle) == 0);
std::filesystem::remove(path);
}
@@ -84,6 +87,10 @@ void TestNoDevice() {
Require(sceAudioInInput(handle, nullptr) == 0);
for (int port = 1; port < 8; ++port) Require(sceAudioInOpen(user, 1, 0, 128, 48000, 1) > 0);
Require(sceAudioInOpen(user, 1, 0, 128, 48000, 1) == portFull);
Require(sceAudioInClose(handle) == 0);
Require(sceAudioInClose(handle) == invalidHandle);
Require(sceAudioInInput(handle, buffer.data()) == invalidHandle);
Require(sceAudioInOpen(user, 1, 0, 128, 48000, 0x11) == handle);
}
}