mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(libSceNgs2.native): clamp matrix levels to -4..4 as the PS4 libSceNgs2 does
The PS4 firmware 11.00 libSceNgs2.sprx stores matrix levels clamped to -4..4, keeps NaN, and rejects a level count of 0. Levels were stored unclamped, so a level of 8 played twice as loud as on the console, and a count of 0 cleared the matrix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,7 @@ static constexpr std::uint8_t COMMAND_TYPE_FLOAT = 1;
|
||||
static constexpr std::uint8_t COMMAND_TYPE_INT = 3;
|
||||
static constexpr std::uint8_t COMMAND_TYPE_UINT = 4;
|
||||
static constexpr std::uint8_t COMMAND_TYPE_FLOAT_ARRAY = 0x11;
|
||||
static constexpr float MAX_MATRIX_LEVEL = 4.0f;
|
||||
|
||||
void Ngs2Voice::SetEvent(std::uint32_t eventId) {
|
||||
switch (eventId) {
|
||||
@@ -92,10 +93,12 @@ static Ngs2Port& PortAt(Ngs2Voice& voice, std::uint32_t port) {
|
||||
}
|
||||
|
||||
static void SetMatrixLevels(Ngs2Voice& voice, std::uint32_t matrixId, const float* levels, std::uint32_t numLevels) {
|
||||
if (matrixId >= voice.matrices.size() || numLevels > NGS2_MAX_CHANNELS * NGS2_MAX_CHANNELS || (levels == nullptr && numLevels != 0)) {
|
||||
if (matrixId >= voice.matrices.size() || numLevels == 0 || numLevels > NGS2_MAX_CHANNELS * NGS2_MAX_CHANNELS || levels == nullptr) {
|
||||
APS5_INVALID_ARG_EX;
|
||||
}
|
||||
voice.matrices[matrixId].assign(levels, levels + numLevels);
|
||||
auto& matrix = voice.matrices[matrixId];
|
||||
matrix.resize(numLevels);
|
||||
for (std::uint32_t i = 0; i < numLevels; i++) matrix[i] = std::clamp(levels[i], -MAX_MATRIX_LEVEL, MAX_MATRIX_LEVEL);
|
||||
}
|
||||
|
||||
static void SetPortMatrix(Ngs2Voice& voice, std::uint32_t port, std::int32_t matrixId) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
@@ -201,6 +202,49 @@ static void TestSubmixerMatrix() {
|
||||
Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK);
|
||||
}
|
||||
|
||||
static std::vector<float> MatrixLevelFrame(const std::vector<float>& levels, bool command) {
|
||||
const auto system = CreateSystem();
|
||||
const auto master = Mastering(system, 2);
|
||||
const std::vector<std::int16_t> pcm(Grain, 16384);
|
||||
const auto sampler = Sampler(system, pcm, 0);
|
||||
Patch(sampler, master);
|
||||
if (command) {
|
||||
const Ngs2VoiceCommand set{5, 0, 0x11, static_cast<std::uint16_t>(levels.size()), {.levels = levels.data()}};
|
||||
Require(sceNgs2VoiceRunCommands(sampler, &set, 1) == SCE_NGS2_OK);
|
||||
} else {
|
||||
Control(sampler, SCE_NGS2_VOICE_PARAM_MATRIX_LEVELS, Ngs2VoiceMatrixLevelsParam{{}, 0, static_cast<std::uint32_t>(levels.size()), levels.data()});
|
||||
}
|
||||
Control(sampler, SCE_NGS2_VOICE_PARAM_PORT_MATRIX, Ngs2VoicePortMatrixParam{{}, 0, 0});
|
||||
Event(sampler, SCE_NGS2_VOICE_EVENT_PLAY);
|
||||
std::vector<float> out(Grain * 2, -1.0f);
|
||||
const Ngs2RenderBufferInfo info{out.data(), out.size() * sizeof(float), SCE_NGS2_WAVEFORM_TYPE_PCM_F32L, 2};
|
||||
Require(sceNgs2SystemRender(system, &info, 1) == SCE_NGS2_OK);
|
||||
Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK);
|
||||
return {out[0], out[1]};
|
||||
}
|
||||
|
||||
static void TestMatrixLevelClamp() {
|
||||
const float inf = std::numeric_limits<float>::infinity();
|
||||
for (bool command : {false, true}) {
|
||||
Require(MatrixLevelFrame({4.0f, -4.0f}, command) == (std::vector<float>{2.0f, -2.0f}));
|
||||
Require(MatrixLevelFrame({4.0001f, -4.5f}, command) == (std::vector<float>{2.0f, -2.0f}));
|
||||
Require(MatrixLevelFrame({100.0f, -100.0f}, command) == (std::vector<float>{2.0f, -2.0f}));
|
||||
Require(MatrixLevelFrame({inf, -inf}, command) == (std::vector<float>{2.0f, -2.0f}));
|
||||
Require(MatrixLevelFrame({3.5f, -0.25f}, command) == (std::vector<float>{1.75f, -0.125f}));
|
||||
const auto nan = MatrixLevelFrame({std::numeric_limits<float>::quiet_NaN(), 0.5f}, command);
|
||||
Require(std::isnan(nan[0]) && nan[1] == 0.25f);
|
||||
}
|
||||
|
||||
const auto system = CreateSystem();
|
||||
const std::vector<std::int16_t> silence(Grain, 0);
|
||||
const auto sampler = Sampler(system, silence, 0);
|
||||
const float levels[2] = {1.0f, 1.0f};
|
||||
bool empty = false;
|
||||
try { Control(sampler, SCE_NGS2_VOICE_PARAM_MATRIX_LEVELS, Ngs2VoiceMatrixLevelsParam{{}, 0, 0, levels}); } catch (const std::invalid_argument&) { empty = true; }
|
||||
Require(empty);
|
||||
Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK);
|
||||
}
|
||||
|
||||
static int allocations = 0;
|
||||
static std::int32_t APS5_VABI Allocate(Ngs2ContextBufferInfo* info) {
|
||||
Require(info->host_buffer == nullptr && info->host_buffer_size != 0 && info->user_data == 9);
|
||||
@@ -450,6 +494,7 @@ int main() {
|
||||
TestPcmBlockEnd();
|
||||
TestPitchAndRepeat();
|
||||
TestSubmixerMatrix();
|
||||
TestMatrixLevelClamp();
|
||||
TestSampleRate();
|
||||
TestUserData();
|
||||
TestMasteringGain();
|
||||
|
||||
@@ -229,6 +229,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
- [Resource registration](../../core/libs/prx/libSceAgcDriver/Resource/src/Registration.cpp) (libSceAgcDriver) - registration always fails with `RESOURCE_REGISTRATION_UNAVAILABLE`, so the owner and resource queries, `sceAgcDriverSetResourceUserData`, `sceAgcDriverFindResourcesPublic` and `sceAgcDriverUnregisterAllResourcesForOwner` return the same error without reading their arguments. Their parameter lists follow the libSceGnmDriver counterparts and are unverified. `sceAgcDriverGetResourceRegistrationMaxNameLength` writes 0xfc and returns 0, as [prosper](https://github.com/mattias800/prosper) does from a title's registration configuration; the `uint32_t*` parameter matches the SDK wrapper `sce::Agc::ResourceRegistration::getMaxNameLength(unsigned int*)`. sharpemu writes 256 and KytyPS5 returns `RESOURCE_REGISTRATION_UNAVAILABLE`, so the value is unverified; a null output throws
|
||||
- [Shader registration](../../core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp) (libSceAgcDriver) - a shader header is accepted at any address: PPSA26344 passes headers 4 and 1 bytes past an 8-byte boundary; whether the console refuses an alignment is unknown
|
||||
- [sceNgs2VoiceControl sampler filter](../../core/libs/prx/libSceNgs2.native/src/Voice.cpp) (libSceNgs2.native) - param `0x1000000a`, its layout and meaning follow KytyPS5 only: location 1 type 1 is an RBJ low-pass biquad at the system sample rate after resampling, scaled by `level`, a set `channel_mask` bit bypasses that channel, type 0 turns the filter off. Other locations and types throw. The voice keeps playing a filter tail after its last block until the history is zero, with outputs below 1e-20 flushed to zero (a KytyPS5 choice); the console's tail length is unknown
|
||||
- [NGS2 matrix levels](../../core/libs/prx/libSceNgs2.native/src/Voice.cpp) (libSceNgs2.native) - levels are clamped to -4..4 and NaN is kept, as the PS4 firmware 11.00 `libSceNgs2.sprx` stores them; not yet confirmed on the PS5 library. That library rejects a level count of 0 and keeps the matrix; the PS5 error code is unknown, so a count of 0 throws. `sceNgs2VoiceRunCommands` is not in that library, so its matrix levels command is assumed to clamp the same way
|
||||
- [sceRtcGetDosTime, sceRtcSetDosTime](../../core/libs/prx/libSceRtc/Export.cpp) (libSceRtc) - bit layout from shadPS4 (PS4, the standard FAT date/time). A year outside 1980-2107 makes `GetDosTime` return `INVALID_YEAR` after writing 0 for an earlier one and `0xff9fbf7d` (2107-12-31 23:59:58, the largest encodable time) for a later one, and `SetDosTime` decodes the year as the unsigned 7-bit FAT field (1980-2107) without validating the fields, as the disassembly-derived [fpPS4](https://github.com/red-prig/fpPS4) reimplementation of the PS4 library does; not confirmed on a PS5 title, and shadPS4 instead masks out-of-range years into the packed value and sign-extends the 16-bit date
|
||||
- [sce::Json::InitParameter2](../../core/libs/prx/libSceJson2/Export.cpp) (libSceJson2) - layout inferred, not known: assumed to start with the allocator, its user data and the file buffer size, in that order, and the whole struct is assumed to fit in 40 bytes; `Initializer::initialize(InitParameter2)` ignores them as `initialize(InitParameter)` does
|
||||
- [sceAjmBatchJobSetResampleParameters, sceAjmBatchJobGetResampleInfo](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm) - the ratio is taken as decoded samples per output sample and num_samples as the decoded samples the resampler holds, both inferred from FMOD (Hades II); the console's interpolation is unknown, a 4-point Catmull-Rom is used. The flags argument is ignored, as in shadPS4 and Kyty
|
||||
|
||||
Reference in New Issue
Block a user