feat(libSceNgs2.native): implement sceNgs2RackGetInfo, sceNgs2VoiceGetPortInfo and sceNgs2VoiceQueryInfo (#958)

RackGetInfo reports the rack's handles, buffer, id, uid, option limits,
render count and active voices. VoiceGetPortInfo reports a port's matrix,
volume and destination. VoiceQueryInfo answers info id 0x4001 with the
voice channel count and throws for other ids. Assumptions are recorded in
TechnicalDebt.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
JAB
2026-10-07 00:10:30 +02:00
committed by GitHub
parent ebad6db846
commit e289b56e6f
8 changed files with 176 additions and 3 deletions
@@ -255,6 +255,30 @@ struct Ngs2CustomSubmixerRackOption {
};
static_assert(sizeof(Ngs2CustomSubmixerRackOption) == 1288);
struct Ngs2RackInfo {
char name[64];
Ngs2Handle rack_handle;
Ngs2ContextBufferInfo buffer_info;
Ngs2Handle owner_system_handle;
std::uint32_t type;
std::uint32_t rack_id;
std::uint32_t uid;
std::uint32_t min_grain_samples;
std::uint32_t max_grain_samples;
std::uint32_t max_voices;
std::uint32_t max_channel_works;
std::uint32_t max_inputs;
std::uint32_t max_matrices;
std::uint32_t max_ports;
std::uint32_t state_flags;
float last_process_ratio;
std::uint64_t last_processed_tick;
std::uint64_t render_count;
std::uint32_t active_voice_count;
std::uint32_t active_channel_work_count;
};
static_assert(sizeof(Ngs2RackInfo) == 216);
struct Ngs2VoiceParamHeader {
std::uint16_t size;
std::int16_t next;
@@ -437,6 +461,23 @@ struct Ngs2RenderBufferInfo {
std::uint32_t num_channels;
};
static constexpr std::uint32_t SCE_NGS2_VOICE_INFO_CHANNELS = 0x4001;
struct Ngs2VoiceChannelsInfo {
std::uint32_t num_channels;
std::uint32_t reserved;
};
static_assert(sizeof(Ngs2VoiceChannelsInfo) == 8);
struct Ngs2VoicePortInfo {
std::int32_t matrix_id;
float volume;
std::uint32_t num_delay_samples;
std::uint32_t dest_input_id;
Ngs2Handle dest_handle;
};
static_assert(sizeof(Ngs2VoicePortInfo) == 24);
struct Ngs2VoiceState {
std::uint32_t state_flags;
std::int32_t error_code;
@@ -13,6 +13,7 @@
#include "prx/libSceNgs2.native/include/Ngs2Types.hpp"
static constexpr std::uint32_t NGS2_MAX_CHANNELS = 8;
static constexpr std::uint32_t MIN_GRAIN_SAMPLES = 64;
struct Ngs2System;
struct Ngs2Rack;
@@ -117,8 +118,13 @@ struct Ngs2Voice {
struct Ngs2Rack {
Ngs2System* system = nullptr;
std::uint32_t rackId = 0;
std::uint32_t uid = 0;
char name[64] = {};
std::uint32_t maxChannels = 0;
std::uint32_t maxFilters = 0;
std::uint32_t maxGrainSamples = 0;
std::uint32_t maxChannelWorks = 0;
std::uint32_t maxInputs = 0;
Ngs2ContextBufferInfo bufferInfo{};
Ngs2BufferAllocator allocator{};
std::vector<Ngs2Voice> voices;
@@ -92,6 +92,7 @@ static RackOptions CheckedRackOption(std::uint32_t rackId, const Ngs2RackOption*
static int CreateRack(Ngs2Handle systemHandle, std::uint32_t rackId, const RackOptions& options, const Ngs2ContextBufferInfo& bufferInfo,
const Ngs2BufferAllocator& allocator, Ngs2Handle* handle) {
static std::uint32_t nextUid = 1;
auto* system = Ngs2FindSystem(systemHandle);
if (system == nullptr) return SCE_NGS2_ERROR_INVALID_SYSTEM_HANDLE;
auto* rack = new (Ngs2Place(&bufferInfo, sizeof(Ngs2Rack), alignof(Ngs2Rack))) Ngs2Rack{};
@@ -99,6 +100,13 @@ static int CreateRack(Ngs2Handle systemHandle, std::uint32_t rackId, const RackO
rack->rackId = rackId;
rack->maxChannels = RackMaxChannels(rackId, options);
rack->maxFilters = rackId == SCE_NGS2_RACK_ID_SAMPLER ? options.sampler.max_filters : 0;
rack->uid = nextUid++;
std::memcpy(rack->name, options.common.name, sizeof(rack->name));
rack->name[sizeof(rack->name) - 1] = '\0';
rack->maxGrainSamples = options.common.max_grain_samples;
if (rackId == SCE_NGS2_RACK_ID_SAMPLER) rack->maxChannelWorks = options.sampler.max_channel_works;
if (rackId == SCE_NGS2_RACK_ID_SUBMIXER) rack->maxInputs = options.submixer.max_inputs;
if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) rack->maxInputs = options.customSubmixer.max_inputs;
rack->bufferInfo = bufferInfo;
rack->allocator = allocator;
rack->voices.resize(options.common.max_voices);
@@ -182,6 +190,34 @@ int APS5_VABI sceNgs2RackGetVoiceHandle(uintptr_t rack_handle, uint32_t voice_id
return SCE_NGS2_OK;
}
int APS5_VABI sceNgs2RackGetInfo(uintptr_t rack_handle, Ngs2RackInfo* info, size_t info_size) {
if (info == nullptr) return SCE_NGS2_ERROR_INVALID_OUT_ADDRESS;
if (info_size != sizeof(Ngs2RackInfo)) return SCE_NGS2_ERROR_INVALID_OUT_SIZE;
std::lock_guard lock(Ngs2Mutex());
const auto* rack = Ngs2FindRack(rack_handle);
if (rack == nullptr) return SCE_NGS2_ERROR_INVALID_RACK_HANDLE;
*info = {};
std::memcpy(info->name, rack->name, sizeof(info->name));
info->rack_handle = rack_handle;
info->buffer_info = rack->bufferInfo;
info->owner_system_handle = reinterpret_cast<Ngs2Handle>(rack->system);
info->type = rack->rackId >> 12;
info->rack_id = rack->rackId;
info->uid = rack->uid;
info->min_grain_samples = MIN_GRAIN_SAMPLES;
info->max_grain_samples = rack->maxGrainSamples;
info->max_voices = static_cast<std::uint32_t>(rack->voices.size());
info->max_channel_works = rack->maxChannelWorks;
info->max_inputs = rack->maxInputs;
info->max_matrices = static_cast<std::uint32_t>(rack->voices.front().matrices.size());
info->max_ports = static_cast<std::uint32_t>(rack->voices.front().ports.size());
info->state_flags = 1;
info->render_count = static_cast<std::uint64_t>(rack->system->renderCount);
info->active_voice_count = static_cast<std::uint32_t>(std::count_if(rack->voices.begin(), rack->voices.end(),
[](const Ngs2Voice& voice) { return voice.state != Ngs2PlayState::Empty; }));
return SCE_NGS2_OK;
}
int APS5_VABI sceNgs2RackLock(uintptr_t rack_handle) {
Ngs2Mutex().lock();
if (Ngs2FindRack(rack_handle) != nullptr) return SCE_NGS2_OK;
@@ -13,7 +13,6 @@
#include "prx/libc/include/General.hpp"
#include "Ngs2Internal.hpp"
static constexpr std::uint32_t MIN_GRAIN_SAMPLES = 64;
static std::vector<Ngs2System*>& Systems() {
static std::vector<Ngs2System*> systems;
@@ -347,6 +347,33 @@ int APS5_VABI sceNgs2VoiceGetState(uintptr_t voice_handle, Ngs2VoiceState* state
}
}
int APS5_VABI sceNgs2VoiceGetPortInfo(uintptr_t voice_handle, uint32_t port, Ngs2VoicePortInfo* info, size_t info_size) {
if (info == nullptr) return SCE_NGS2_ERROR_INVALID_OUT_ADDRESS;
if (info_size != sizeof(Ngs2VoicePortInfo)) return SCE_NGS2_ERROR_INVALID_OUT_SIZE;
std::lock_guard lock(Ngs2Mutex());
auto& voice = CheckedVoice(voice_handle);
const auto& source = PortAt(voice, port);
*info = {};
info->matrix_id = source.matrix;
info->volume = source.volume;
info->dest_handle = reinterpret_cast<Ngs2Handle>(source.dest);
return SCE_NGS2_OK;
}
int APS5_VABI sceNgs2VoiceQueryInfo(uintptr_t voice_handle, uint32_t info_id, void* info, size_t info_size) {
if (info == nullptr) return SCE_NGS2_ERROR_INVALID_OUT_ADDRESS;
std::lock_guard lock(Ngs2Mutex());
const auto& voice = CheckedVoice(voice_handle);
switch (info_id) {
case SCE_NGS2_VOICE_INFO_CHANNELS: {
if (info_size != sizeof(Ngs2VoiceChannelsInfo)) return SCE_NGS2_ERROR_INVALID_OUT_SIZE;
*static_cast<Ngs2VoiceChannelsInfo*>(info) = {voice.channels, 0};
return SCE_NGS2_OK;
}
default: throw std::runtime_error("NGS2: voice info " + Ngs2Hex(info_id) + " is not implemented");
}
}
int APS5_VABI sceNgs2VoiceGetStateFlags(uintptr_t voice_handle, uint32_t* state_flags) {
if (state_flags == nullptr) return SCE_NGS2_ERROR_INVALID_OUT_ADDRESS;
std::lock_guard lock(Ngs2Mutex());
@@ -42,11 +42,46 @@ static void TestErrorsAndInfo() {
Require(sceNgs2RackDestroy(0x1234, nullptr) == static_cast<int>(0x804A0261u));
const auto system = CreateSystem();
CreateRack(system, SCE_NGS2_RACK_ID_SAMPLER);
const auto rack = CreateRack(system, SCE_NGS2_RACK_ID_SAMPLER);
Ngs2RackInfo rackInfo{};
Require(sceNgs2RackGetInfo(0x1234, &rackInfo, sizeof(rackInfo)) == SCE_NGS2_ERROR_INVALID_RACK_HANDLE);
Require(sceNgs2RackGetInfo(rack, nullptr, sizeof(rackInfo)) == SCE_NGS2_ERROR_INVALID_OUT_ADDRESS);
Require(sceNgs2RackGetInfo(rack, &rackInfo, sizeof(rackInfo) - 1) == SCE_NGS2_ERROR_INVALID_OUT_SIZE);
Require(sceNgs2RackGetInfo(rack, &rackInfo, sizeof(rackInfo)) == SCE_NGS2_OK);
Require(rackInfo.rack_handle == rack && rackInfo.owner_system_handle == system && rackInfo.rack_id == SCE_NGS2_RACK_ID_SAMPLER && rackInfo.type == 1);
Require(rackInfo.uid != 0 && rackInfo.max_voices == 256 && rackInfo.max_channel_works == 256 && rackInfo.max_grain_samples == 512);
Require(rackInfo.max_ports == 8 && rackInfo.max_matrices == 1 && rackInfo.active_voice_count == 0 && rackInfo.name[0] == '\0');
Ngs2SubmixerRackOption named{};
named.rack_option.size = sizeof(named);
for (auto& c : named.rack_option.name) c = 'n';
named.rack_option.max_grain_samples = 256;
named.rack_option.max_voices = 2;
named.rack_option.max_matrices = 1;
named.rack_option.max_ports = 2;
named.max_channels = 2;
named.max_inputs = 3;
Ngs2ContextBufferInfo namedQuery{};
Require(sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_SUBMIXER, &named.rack_option, &namedQuery) == SCE_NGS2_OK);
const auto namedBuffer = Buffer(namedQuery);
uintptr_t namedRack = 0;
Require(sceNgs2RackCreate(system, SCE_NGS2_RACK_ID_SUBMIXER, &named.rack_option, &namedBuffer, &namedRack) == SCE_NGS2_OK);
Ngs2RackInfo namedInfo{};
Require(sceNgs2RackGetInfo(namedRack, &namedInfo, sizeof(namedInfo)) == SCE_NGS2_OK);
Require(namedInfo.name[0] == 'n' && namedInfo.name[62] == 'n' && namedInfo.name[63] == '\0');
Require(namedInfo.type == 2 && namedInfo.rack_id == SCE_NGS2_RACK_ID_SUBMIXER && namedInfo.uid != rackInfo.uid && namedInfo.max_grain_samples == 256);
Require(namedInfo.max_voices == 2 && namedInfo.max_ports == 2 && namedInfo.max_inputs == 3 && namedInfo.max_channel_works == 0);
Require(namedInfo.buffer_info.host_buffer == namedBuffer.host_buffer && namedInfo.render_count == 0);
uintptr_t named0 = 0;
Require(sceNgs2RackGetVoiceHandle(namedRack, 0, &named0) == SCE_NGS2_OK);
Control(named0, SCE_NGS2_SUBMIXER_VOICE_PARAM_SETUP, Ngs2SubmixerVoiceSetupParam{{}, 2, 0});
Event(named0, SCE_NGS2_VOICE_EVENT_PLAY);
Require(sceNgs2RackGetInfo(namedRack, &namedInfo, sizeof(namedInfo)) == SCE_NGS2_OK && namedInfo.active_voice_count == 1);
Ngs2VoiceChannelsInfo stereoInfo{};
Require(sceNgs2VoiceQueryInfo(named0, SCE_NGS2_VOICE_INFO_CHANNELS, &stereoInfo, sizeof(stereoInfo)) == SCE_NGS2_OK && stereoInfo.num_channels == 2);
Require(sceNgs2SystemGetInfo(system, nullptr, sizeof(info)) == SCE_NGS2_ERROR_INVALID_OUT_ADDRESS);
Require(sceNgs2SystemGetInfo(system, &info, sizeof(info) - 1) == SCE_NGS2_ERROR_INVALID_OUT_SIZE);
Require(sceNgs2SystemGetInfo(system, &info, sizeof(info)) == SCE_NGS2_OK);
Require(info.system_handle == system && info.uid != 0 && info.rack_count == 1 && info.sample_rate == 48000);
Require(info.system_handle == system && info.uid != 0 && info.rack_count == 2 && info.sample_rate == 48000);
Require(info.num_grain_samples == Grain && info.max_grain_samples == 512 && info.render_count == 0);
Ngs2ContextBufferInfo released{};
@@ -61,6 +96,29 @@ static void TestPcmBlockEnd() {
for (int i = 0; i < 12; i++) pcm.push_back(static_cast<std::int16_t>(i * 1000 - 4000));
const auto sampler = Sampler(system, pcm, 0);
Patch(sampler, master);
Ngs2VoicePortInfo portInfo{};
Require(sceNgs2VoiceGetPortInfo(sampler, 0, nullptr, sizeof(portInfo)) == SCE_NGS2_ERROR_INVALID_OUT_ADDRESS);
Require(sceNgs2VoiceGetPortInfo(sampler, 0, &portInfo, sizeof(portInfo) - 1) == SCE_NGS2_ERROR_INVALID_OUT_SIZE);
Require(sceNgs2VoiceGetPortInfo(sampler, 0, &portInfo, sizeof(portInfo)) == SCE_NGS2_OK);
Require(portInfo.dest_handle == master && portInfo.matrix_id == -1 && portInfo.volume == 1.0f);
Control(sampler, SCE_NGS2_VOICE_PARAM_PORT_VOLUME, Ngs2VoicePortVolumeParam{{}, 0, 0.5f});
Control(sampler, SCE_NGS2_VOICE_PARAM_PORT_MATRIX, Ngs2VoicePortMatrixParam{{}, 0, 0});
Require(sceNgs2VoiceGetPortInfo(sampler, 0, &portInfo, sizeof(portInfo)) == SCE_NGS2_OK);
Require(portInfo.dest_handle == master && portInfo.matrix_id == 0 && portInfo.volume == 0.5f);
Control(sampler, SCE_NGS2_VOICE_PARAM_PORT_VOLUME, Ngs2VoicePortVolumeParam{{}, 0, 1.0f});
Control(sampler, SCE_NGS2_VOICE_PARAM_PORT_MATRIX, Ngs2VoicePortMatrixParam{{}, 0, -1});
Require(sceNgs2VoiceGetPortInfo(sampler, 1, &portInfo, sizeof(portInfo)) == SCE_NGS2_OK && portInfo.dest_handle == 0);
bool portOutOfRange = false;
try { sceNgs2VoiceGetPortInfo(sampler, 8, &portInfo, sizeof(portInfo)); } catch (const std::invalid_argument&) { portOutOfRange = true; }
Require(portOutOfRange);
Ngs2VoiceChannelsInfo channelsInfo{};
Require(sceNgs2VoiceQueryInfo(sampler, SCE_NGS2_VOICE_INFO_CHANNELS, nullptr, sizeof(channelsInfo)) == SCE_NGS2_ERROR_INVALID_OUT_ADDRESS);
Require(sceNgs2VoiceQueryInfo(sampler, SCE_NGS2_VOICE_INFO_CHANNELS, &channelsInfo, sizeof(channelsInfo) - 1) == SCE_NGS2_ERROR_INVALID_OUT_SIZE);
Require(sceNgs2VoiceQueryInfo(sampler, SCE_NGS2_VOICE_INFO_CHANNELS, &channelsInfo, sizeof(channelsInfo)) == SCE_NGS2_OK);
Require(channelsInfo.num_channels == 1 && channelsInfo.reserved == 0);
bool unknownInfo = false;
try { sceNgs2VoiceQueryInfo(sampler, 0x4000, &channelsInfo, sizeof(channelsInfo)); } catch (const std::runtime_error&) { unknownInfo = true; }
Require(unknownInfo);
Control(sampler, SCE_NGS2_VOICE_PARAM_CALLBACK, Ngs2VoiceCallbackParam{{}, OnBlock, 7, SCE_NGS2_VOICE_CALLBACK_FLAG_BLOCK_END, 0});
Require(Flags(sampler) == 0);
Event(sampler, SCE_NGS2_VOICE_EVENT_PLAY);
@@ -29,10 +29,13 @@ int APS5_VABI sceNgs2RackCreate(uintptr_t, uint32_t, const Ngs2RackOption*, cons
int APS5_VABI sceNgs2RackCreateWithAllocator(uintptr_t, uint32_t, const Ngs2RackOption*, const Ngs2BufferAllocator*, uintptr_t*);
int APS5_VABI sceNgs2RackDestroy(uintptr_t, Ngs2ContextBufferInfo*);
int APS5_VABI sceNgs2RackGetVoiceHandle(uintptr_t, uint32_t, uintptr_t*);
int APS5_VABI sceNgs2RackGetInfo(uintptr_t, Ngs2RackInfo*, size_t);
int APS5_VABI sceNgs2VoiceControl(uintptr_t, const Ngs2VoiceParamHeader*);
int APS5_VABI sceNgs2VoiceRunCommands(uintptr_t, const Ngs2VoiceCommand*, size_t);
int APS5_VABI sceNgs2VoiceGetState(uintptr_t, Ngs2VoiceState*, size_t);
int APS5_VABI sceNgs2VoiceGetStateFlags(uintptr_t, uint32_t*);
int APS5_VABI sceNgs2VoiceGetPortInfo(uintptr_t, uint32_t, Ngs2VoicePortInfo*, size_t);
int APS5_VABI sceNgs2VoiceQueryInfo(uintptr_t, uint32_t, void*, size_t);
}
inline void Check(bool value, int line) {
+3
View File
@@ -140,6 +140,9 @@ Throughout the project, every function at every stage either **does exactly what
- [sceSystemServiceReenableMediaPlay](../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - unknown signature
- [sceSystemServiceParamGetString](../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - the system name (parameter 6) is the fixed `PS5`, not the console's name; other parameters and buffers of 1 to 64 bytes throw
- [sceNgs2SystemSetSampleRate, sceNgs2SystemSetUserData, sceNgs2SystemGetUserData, sceNgs2SystemLock, sceNgs2SystemUnlock](../../core/libs/prx/libSceNgs2.native/src/System.cpp) (libSceNgs2.native) - shadPS4 only checks the system handle (`INVALID_SYSTEM_HANDLE`) and SharpProspero names what each call does; neither gives the other limits. A sample rate of 0 and a null user data pointer throw, any other rate is accepted, and changing the rate while a sampler filter is enabled throws because the filter coefficients were computed for the old rate. Lock and unlock take the same lock as `sceNgs2RackLock`
- [sceNgs2RackGetInfo](../../core/libs/prx/libSceNgs2.native/src/Rack.cpp) (libSceNgs2.native) - field order from [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `OrbisNgs2RackInfo` with the 64-byte name of the other info structures (216 bytes); `type` is assumed to be the rack id divided by 0x1000, `min_grain_samples` 64 (the system minimum) and `state_flags` 1; `uid` comes from a per-process rack counter, `render_count` is the owning system's render count and `active_voice_count` counts voices that are not empty (paused and stopped voices included). The last process ratio and tick and `active_channel_work_count` are left zero
- [sceNgs2VoiceQueryInfo](../../core/libs/prx/libSceNgs2.native/src/Voice.cpp) (libSceNgs2.native) - only info id 0x4001 is known: PPSA22520 passes an 8-byte buffer and branches on the first word being 1 or 2, so it is assumed to be the voice channel count (0 before the voice is set up); the second word is zeroed and other ids throw
- [sceNgs2VoiceGetPortInfo](../../core/libs/prx/libSceNgs2.native/src/Voice.cpp) (libSceNgs2.native) - layout from shadPS4; the delay and destination input are zero because ports have neither yet
- [sceSaveDataInitialize3](../../core/libs/prx/libSceSaveData.native/Export.cpp) (libSceSaveData.native) - a repeated initialize succeeds: PPSA12544's executable and its Unity SaveData plugin both initialize, and the plugin fails on any error. Initializations are counted and `sceSaveDataTerminate` ends the session at the last one; how the console pairs them is unknown
- [sceVoiceSetMuteFlag](../../core/libs/prx/libSceVoice/Export.cpp) (libSceVoice) - unknown signature
- [sceRtcFormatRFC2822, sceRtcFormatRFC2822LocalTime, sceRtcFormatRFC3339LocalTime](../../core/libs/prx/libSceRtc/Export.cpp) (libSceRtc) - only shadPS4 (PS4) has bodies. A null tick returns `INVALID_POINTER` like the existing `sceRtcFormatRFC3339`, where shadPS4 formats the current time instead; offsets outside ±1439 minutes return `INVALID_VALUE` like `sceRtcFormatRFC3339`; the local-time variants use the host's offset at the given instant (shadPS4 uses `sceKernelGettimezone`, the current offset)