From 52869f0112d2ccab6fab58eff42d6e403b095e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Tue, 6 Oct 2026 01:28:28 +0200 Subject: [PATCH] feat(ngs2): custom submixer rack with UserFx2 modules Co-Authored-By: Claude --- .../libs/prx/libSceNgs2.native/CMakeLists.txt | 3 +- .../libSceNgs2.native/include/Ngs2Types.hpp | 121 +++++++++ .../libs/prx/libSceNgs2.native/src/Custom.cpp | 109 +++++++++ .../libSceNgs2.native/src/Ngs2Internal.hpp | 23 ++ core/libs/prx/libSceNgs2.native/src/Rack.cpp | 7 + .../libs/prx/libSceNgs2.native/src/Render.cpp | 1 + core/libs/prx/libSceNgs2.native/src/Voice.cpp | 10 +- .../libSceNgs2.native/tests/Ngs2Custom.cpp | 231 ++++++++++++++++++ docs/dev/TechnicalDebt.md | 1 + 9 files changed, 499 insertions(+), 7 deletions(-) create mode 100644 core/libs/prx/libSceNgs2.native/src/Custom.cpp create mode 100644 core/libs/prx/libSceNgs2.native/tests/Ngs2Custom.cpp diff --git a/core/libs/prx/libSceNgs2.native/CMakeLists.txt b/core/libs/prx/libSceNgs2.native/CMakeLists.txt index 986014221..a2d50f9b8 100644 --- a/core/libs/prx/libSceNgs2.native/CMakeLists.txt +++ b/core/libs/prx/libSceNgs2.native/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(libSceNgs2.native SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/src/Atrac9.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/Custom.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Rack.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Render.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/System.cpp @@ -25,7 +26,7 @@ if(MINGW) endif() if(BUILD_TESTING) - foreach(ngs2Test IN ITEMS Render Atrac9 Filter) + foreach(ngs2Test IN ITEMS Render Atrac9 Filter Custom) string(TOLOWER ${ngs2Test} testName) add_test_executable(ngs2_${testName}_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/Ngs2${ngs2Test}.cpp) target_include_directories(ngs2_${testName}_tests PRIVATE ${LIBS_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/3rdparty/LibAtrac9/C/src) diff --git a/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp b/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp index 6c99f3f42..e7d5fd04c 100644 --- a/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp +++ b/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp @@ -18,6 +18,7 @@ static constexpr int SCE_NGS2_ERROR_UNKNOWN_WAVEFORM_FORMAT = static_cast(0 static constexpr std::uint32_t SCE_NGS2_RACK_ID_SAMPLER = 0x1000; static constexpr std::uint32_t SCE_NGS2_RACK_ID_SUBMIXER = 0x2000; static constexpr std::uint32_t SCE_NGS2_RACK_ID_MASTERING = 0x3000; +static constexpr std::uint32_t SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER = 0x4002; static constexpr std::uint32_t SCE_NGS2_WAVEFORM_TYPE_PCM_I16L = 0x12; static constexpr std::uint32_t SCE_NGS2_WAVEFORM_TYPE_PCM_F32L = 0x18; @@ -50,6 +51,12 @@ static constexpr std::uint32_t SCE_NGS2_SAMPLER_VOICE_PARAM_FILTER = 0x1000000a; static constexpr std::uint32_t SCE_NGS2_SUBMIXER_VOICE_PARAM_SETUP = 0x20000000; static constexpr std::uint32_t SCE_NGS2_MASTERING_VOICE_PARAM_SETUP = 0x30000000; static constexpr std::uint32_t SCE_NGS2_MASTERING_VOICE_PARAM_OUTPUT = 0x30000005; +static constexpr std::uint32_t SCE_NGS2_CUSTOM_SUBMIXER_VOICE_PARAM_SETUP = 0x40020000; +static constexpr std::uint32_t SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX2 = 0x40001f00; + +static constexpr std::uint32_t SCE_NGS2_CUSTOM_MAX_MODULES = 24; +static constexpr std::uint32_t SCE_NGS2_CUSTOM_MAX_PORTS = 16; +static constexpr std::uint32_t SCE_NGS2_CUSTOM_MODULE_ID_USER_FX2 = 0x1f; static constexpr std::uint32_t SCE_NGS2_WAVEFORM_BLOCKS_FLAG_CONTINUE = 1; static constexpr std::uint32_t SCE_NGS2_WAVEFORM_BLOCKS_FLAG_RESET = 4; @@ -149,6 +156,104 @@ struct Ngs2MasteringRackOption { std::uint32_t num_peak_meter_blocks; }; +struct Ngs2CustomModuleOption { + std::uint32_t size; +}; + +struct Ngs2UserFx2SetupContext { + void* common; + void* param; + void* work; + std::uintptr_t user_data; + std::uint32_t max_voices; + std::uint32_t voice_index; + std::uint64_t reserved[4]; +}; +static_assert(sizeof(Ngs2UserFx2SetupContext) == 72); + +using Ngs2UserFx2CleanupContext = Ngs2UserFx2SetupContext; + +struct Ngs2UserFx2ControlContext { + const void* data; + std::size_t data_size; + void* common; + void* param; + std::uintptr_t user_data; + std::uint64_t reserved[4]; +}; +static_assert(sizeof(Ngs2UserFx2ControlContext) == 72); + +struct Ngs2UserFx2ProcessContext { + float** channel_data; + void* common; + const void* param; + void* work; + void* state; + std::uintptr_t user_data; + std::uint32_t flags; + std::uint32_t num_input_channels; + std::uint32_t num_output_channels; + std::uint32_t num_grain_samples; + std::uint32_t sample_rate; + std::uint32_t reserved; + std::uint64_t reserved2[4]; +}; +static_assert(sizeof(Ngs2UserFx2ProcessContext) == 104); + +using Ngs2UserFx2SetupHandler = std::int32_t (APS5_VABI *)(Ngs2UserFx2SetupContext*); +using Ngs2UserFx2CleanupHandler = std::int32_t (APS5_VABI *)(Ngs2UserFx2CleanupContext*); +using Ngs2UserFx2ControlHandler = std::int32_t (APS5_VABI *)(Ngs2UserFx2ControlContext*); +using Ngs2UserFx2ProcessHandler = std::int32_t (APS5_VABI *)(Ngs2UserFx2ProcessContext*); + +struct Ngs2CustomUserFx2ModuleOption { + Ngs2CustomModuleOption custom_module_option; + Ngs2UserFx2SetupHandler setup_handler; + Ngs2UserFx2CleanupHandler cleanup_handler; + Ngs2UserFx2ControlHandler control_handler; + Ngs2UserFx2ProcessHandler process_handler; + std::size_t common_size; + std::size_t param_size; + std::size_t work_size; + std::uintptr_t user_data; +}; +static_assert(sizeof(Ngs2CustomUserFx2ModuleOption) == 72); + +struct Ngs2CustomRackModuleInfo { + const Ngs2CustomModuleOption* option; + std::uint32_t module_id; + std::uint32_t source_buffer_id; + std::uint32_t extra_buffer_id; + std::uint32_t dest_buffer_id; + std::uint32_t state_offset; + std::uint32_t state_size; + std::uint32_t reserved; + std::uint32_t reserved2; +}; +static_assert(sizeof(Ngs2CustomRackModuleInfo) == 40); + +struct Ngs2CustomRackPortInfo { + std::uint32_t source_buffer_id; + std::uint32_t reserved; +}; + +struct Ngs2CustomRackOption { + Ngs2RackOption rack_option; + std::uint32_t state_size; + std::uint32_t num_buffers; + std::uint32_t num_modules; + std::uint32_t reserved; + Ngs2CustomRackModuleInfo module[SCE_NGS2_CUSTOM_MAX_MODULES]; + Ngs2CustomRackPortInfo port[SCE_NGS2_CUSTOM_MAX_PORTS]; +}; +static_assert(sizeof(Ngs2CustomRackOption) == 1280); + +struct Ngs2CustomSubmixerRackOption { + Ngs2CustomRackOption custom_rack_option; + std::uint32_t max_channels; + std::uint32_t max_inputs; +}; +static_assert(sizeof(Ngs2CustomSubmixerRackOption) == 1288); + struct Ngs2VoiceParamHeader { std::uint16_t size; std::int16_t next; @@ -275,6 +380,22 @@ struct Ngs2SubmixerVoiceSetupParam { }; static_assert(sizeof(Ngs2SubmixerVoiceSetupParam) == 16); +struct Ngs2CustomSubmixerVoiceSetupParam { + Ngs2VoiceParamHeader header; + std::uint32_t num_input_channels; + std::uint32_t num_output_channels; + std::uint32_t flags; + std::uint32_t reserved; +}; +static_assert(sizeof(Ngs2CustomSubmixerVoiceSetupParam) == 24); + +struct Ngs2CustomVoiceUserFx2Param { + Ngs2VoiceParamHeader header; + const void* data; + std::size_t data_size; +}; +static_assert(sizeof(Ngs2CustomVoiceUserFx2Param) == 24); + struct Ngs2MasteringVoiceSetupParam { Ngs2VoiceParamHeader header; std::uint32_t num_io_channels; diff --git a/core/libs/prx/libSceNgs2.native/src/Custom.cpp b/core/libs/prx/libSceNgs2.native/src/Custom.cpp new file mode 100644 index 000000000..2ba302fb2 --- /dev/null +++ b/core/libs/prx/libSceNgs2.native/src/Custom.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include + +#include "prx/libc/include/General.hpp" +#include "Ngs2Internal.hpp" + +static constexpr std::uint32_t USER_FX2_FLAG_PARAM_CHANGED = 2; + +void Ngs2CheckCustomRack(const Ngs2CustomRackOption& option) { + if (option.num_buffers != 1) throw std::runtime_error("NGS2: custom racks with " + std::to_string(option.num_buffers) + " buffers are not implemented"); + if (option.num_modules > SCE_NGS2_CUSTOM_MAX_MODULES || option.rack_option.max_ports > SCE_NGS2_CUSTOM_MAX_PORTS) APS5_INVALID_ARG_EX; + for (std::uint32_t m = 0; m < option.num_modules; m++) { + const auto& info = option.module[m]; + if (info.module_id != SCE_NGS2_CUSTOM_MODULE_ID_USER_FX2) throw std::runtime_error("NGS2: custom module id " + Ngs2Hex(info.module_id) + " is not implemented"); + if (info.option == nullptr || info.option->size != sizeof(Ngs2CustomUserFx2ModuleOption)) APS5_INVALID_ARG_EX; + if (info.source_buffer_id != 0 || info.dest_buffer_id != 0) APS5_INVALID_ARG_EX; + if (reinterpret_cast(info.option)->process_handler == nullptr) APS5_INVALID_ARG_EX; + } + for (std::uint32_t p = 0; p < option.rack_option.max_ports; p++) { + if (option.port[p].source_buffer_id != 0) APS5_INVALID_ARG_EX; + } +} + +static Ngs2UserFx2SetupContext UserFxContext(Ngs2Rack& rack, std::size_t module, std::uint32_t voiceIndex) { + auto& fx = rack.voices[voiceIndex].userFx[module]; + return {rack.userFxCommon[module].data(), fx.param.data(), fx.work.data(), rack.userFx[module].user_data, + static_cast(rack.voices.size()), voiceIndex, {}}; +} + +void Ngs2SetupUserFx(Ngs2Rack& rack, const Ngs2CustomRackOption& option) { + rack.userFx.resize(option.num_modules); + rack.userFxCommon.resize(option.num_modules); + for (auto& voice : rack.voices) voice.userFx.resize(option.num_modules); + for (std::uint32_t m = 0; m < option.num_modules; m++) { + const auto& module = *reinterpret_cast(option.module[m].option); + rack.userFx[m] = module; + rack.userFxCommon[m].resize(module.common_size); + for (std::uint32_t i = 0; i < rack.voices.size(); i++) { + auto& fx = rack.voices[i].userFx[m]; + fx.param.resize(module.param_size); + fx.work.resize(module.work_size); + fx.state.resize(option.module[m].state_size); + if (module.setup_handler == nullptr) continue; + auto context = UserFxContext(rack, m, i); + const int result = module.setup_handler(&context); + if (result != SCE_NGS2_OK) throw std::runtime_error("NGS2: the UserFx2 setup handler failed with " + std::to_string(result)); + } + } +} + +void Ngs2CleanupUserFx(Ngs2Rack& rack) { + for (std::uint32_t i = 0; i < rack.voices.size(); i++) { + for (std::size_t m = 0; m < rack.userFx.size(); m++) { + if (rack.userFx[m].cleanup_handler == nullptr) continue; + auto context = UserFxContext(rack, m, i); + const int result = rack.userFx[m].cleanup_handler(&context); + if (result != SCE_NGS2_OK) throw std::runtime_error("NGS2: the UserFx2 cleanup handler failed with " + std::to_string(result)); + } + } +} + +static void SetupCustomSubmixer(Ngs2Voice& voice, const Ngs2CustomSubmixerVoiceSetupParam& setup) { + if (setup.flags != 0) throw std::runtime_error("NGS2: custom submixer setup flags are not implemented"); + if (setup.num_input_channels != setup.num_output_channels) throw std::runtime_error("NGS2: custom submixer channel conversion is not implemented"); + if (setup.num_input_channels == 0 || setup.num_input_channels > voice.rack->maxChannels) APS5_INVALID_ARG_EX; + voice.channels = setup.num_input_channels; +} + +static void SetUserFxParam(Ngs2Voice& voice, std::uint32_t index, const Ngs2CustomVoiceUserFx2Param& param) { + if (index >= voice.userFx.size()) APS5_INVALID_ARG_EX; + const auto& module = voice.rack->userFx[index]; + if (module.control_handler != nullptr) throw std::runtime_error("NGS2: the UserFx2 control handler is not implemented"); + auto& fx = voice.userFx[index]; + if (param.data_size != fx.param.size() || (param.data == nullptr && param.data_size != 0)) APS5_INVALID_ARG_EX; + if (param.data_size != 0) std::memcpy(fx.param.data(), param.data, param.data_size); + fx.flags |= USER_FX2_FLAG_PARAM_CHANGED; +} + +void Ngs2ApplyCustomParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param) { + if (voice.rack->rackId != SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) throw std::invalid_argument("NGS2: voice param " + Ngs2Hex(param.id) + " belongs to another rack"); + if (param.id == SCE_NGS2_CUSTOM_SUBMIXER_VOICE_PARAM_SETUP) { + SetupCustomSubmixer(voice, ParamAs(param)); + return; + } + if ((param.id & ~0x1fu) == SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX2) { + SetUserFxParam(voice, param.id & 0x1f, ParamAs(param)); + return; + } + throw std::runtime_error("NGS2: voice param " + Ngs2Hex(param.id) + " is not implemented"); +} + +void Ngs2ProcessUserFx(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t sampleRate) { + if (voice.userFx.empty()) return; + std::vector channels(voice.channels); + for (std::uint32_t c = 0; c < voice.channels; c++) channels[c] = voice.samples.data() + static_cast(c) * grain; + for (std::size_t m = 0; m < voice.userFx.size(); m++) { + auto& fx = voice.userFx[m]; + const auto& module = voice.rack->userFx[m]; + Ngs2UserFx2ProcessContext context{channels.data(), voice.rack->userFxCommon[m].data(), fx.param.data(), fx.work.data(), fx.state.data(), + module.user_data, fx.flags, voice.channels, voice.channels, grain, sampleRate, 0, {}}; + const int result = module.process_handler(&context); + if (result != SCE_NGS2_OK) throw std::runtime_error("NGS2: the UserFx2 process handler failed with " + std::to_string(result)); + fx.flags = 0; + } +} diff --git a/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp b/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp index 615380f59..022be9971 100644 --- a/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp +++ b/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -71,6 +72,13 @@ struct Ngs2Port { std::int32_t matrix = -1; }; +struct Ngs2UserFx2 { + std::vector param; + std::vector work; + std::vector state; + std::uint32_t flags = 0; +}; + struct Ngs2Voice { Ngs2Rack* rack = nullptr; Ngs2PlayState state = Ngs2PlayState::Empty; @@ -93,6 +101,7 @@ struct Ngs2Voice { std::vector> matrices; std::vector filters; std::uint32_t outputId = 0; + std::vector userFx; std::vector samples; bool rendering = false; bool rendered = false; @@ -111,6 +120,8 @@ struct Ngs2Rack { Ngs2ContextBufferInfo bufferInfo{}; Ngs2BufferAllocator allocator{}; std::vector voices; + std::vector userFx; + std::vector> userFxCommon; }; struct Ngs2System { @@ -125,6 +136,13 @@ struct Ngs2System { std::string Ngs2Hex(std::uint32_t value); std::recursive_mutex& Ngs2Mutex(); Ngs2System* Ngs2FindSystem(Ngs2Handle handle); + +template +const TParam& ParamAs(const Ngs2VoiceParamHeader& param) { + if (param.size < sizeof(TParam)) throw std::invalid_argument("NGS2: voice param " + Ngs2Hex(param.id) + " is too small"); + return reinterpret_cast(param); +} + Ngs2Rack* Ngs2FindRack(Ngs2Handle handle); Ngs2Voice* Ngs2FindVoice(Ngs2Handle handle); void* Ngs2Place(const Ngs2ContextBufferInfo* bufferInfo, std::size_t size, std::size_t alignment); @@ -134,6 +152,11 @@ void Ngs2SetupAtrac9(Ngs2Voice& voice, const Ngs2WaveformFormat& format); std::size_t Ngs2Atrac9BlockBytes(const Ngs2Voice& voice, const Ngs2WaveformBlock& block); void Ngs2RestartAtrac9(Ngs2Voice& voice); const float* Ngs2Atrac9Frame(Ngs2Voice& voice, Ngs2Block& block, std::uint32_t frame); +void Ngs2CheckCustomRack(const Ngs2CustomRackOption& option); +void Ngs2SetupUserFx(Ngs2Rack& rack, const Ngs2CustomRackOption& option); +void Ngs2CleanupUserFx(Ngs2Rack& rack); +void Ngs2ApplyCustomParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param); +void Ngs2ProcessUserFx(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t sampleRate); void Ngs2RenderSystem(Ngs2System& system, const Ngs2RenderBufferInfo* bufferInfo, std::uint32_t numBufferInfo); #endif diff --git a/core/libs/prx/libSceNgs2.native/src/Rack.cpp b/core/libs/prx/libSceNgs2.native/src/Rack.cpp index b98f15b96..1c0ce54f9 100644 --- a/core/libs/prx/libSceNgs2.native/src/Rack.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Rack.cpp @@ -16,6 +16,7 @@ union RackOptions { Ngs2SamplerRackOption sampler; Ngs2SubmixerRackOption submixer; Ngs2MasteringRackOption mastering; + Ngs2CustomSubmixerRackOption customSubmixer; }; static std::size_t RackOptionSize(std::uint32_t rackId) { @@ -23,11 +24,13 @@ static std::size_t RackOptionSize(std::uint32_t rackId) { case SCE_NGS2_RACK_ID_SAMPLER: return sizeof(Ngs2SamplerRackOption); case SCE_NGS2_RACK_ID_SUBMIXER: return sizeof(Ngs2SubmixerRackOption); case SCE_NGS2_RACK_ID_MASTERING: return sizeof(Ngs2MasteringRackOption); + case SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER: return sizeof(Ngs2CustomSubmixerRackOption); default: throw std::runtime_error("NGS2: rack id " + Ngs2Hex(rackId) + " is not implemented"); } } static RackOptions DefaultRackOption(std::uint32_t rackId) { + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) throw std::runtime_error("NGS2: default custom rack options are not implemented"); RackOptions options{}; auto& common = options.common; common.size = RackOptionSize(rackId); @@ -69,6 +72,7 @@ static RackOptions DefaultRackOption(std::uint32_t rackId) { static std::uint32_t RackMaxChannels(std::uint32_t rackId, const RackOptions& options) { if (rackId == SCE_NGS2_RACK_ID_SUBMIXER) return options.submixer.max_channels; if (rackId == SCE_NGS2_RACK_ID_MASTERING) return options.mastering.max_channels; + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) return options.customSubmixer.max_channels; return NGS2_MAX_CHANNELS; } @@ -82,6 +86,7 @@ static RackOptions CheckedRackOption(std::uint32_t rackId, const Ngs2RackOption* if (options.common.max_voices == 0 || maxChannels == 0 || maxChannels > NGS2_MAX_CHANNELS) { throw std::invalid_argument("NGS2: invalid rack voice or channel count"); } + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) Ngs2CheckCustomRack(options.customSubmixer.custom_rack_option); return options; } @@ -102,12 +107,14 @@ static int CreateRack(Ngs2Handle systemHandle, std::uint32_t rackId, const RackO voice.ports.resize(options.common.max_ports); voice.matrices.resize(options.common.max_matrices); } + if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) Ngs2SetupUserFx(*rack, options.customSubmixer.custom_rack_option); system->racks.push_back(rack); *handle = reinterpret_cast(rack); return SCE_NGS2_OK; } int Ngs2DestroyRack(Ngs2Rack& rack, Ngs2ContextBufferInfo* outBufferInfo) { + Ngs2CleanupUserFx(rack); auto& racks = rack.system->racks; std::erase(racks, &rack); for (auto* other : racks) { diff --git a/core/libs/prx/libSceNgs2.native/src/Render.cpp b/core/libs/prx/libSceNgs2.native/src/Render.cpp index ba5b520b3..4d0f5e394 100644 --- a/core/libs/prx/libSceNgs2.native/src/Render.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Render.cpp @@ -176,6 +176,7 @@ static void RenderVoice(Ngs2Voice& voice, const std::vector& voices, if (voice.state == Ngs2PlayState::Playing && voice.channels != 0) { if (voice.rack->rackId == SCE_NGS2_RACK_ID_SAMPLER) RenderSampler(voice, grain, systemRate); else MixInputs(voice, voices, grain, systemRate); + if (voice.hasSamples) Ngs2ProcessUserFx(voice, grain, systemRate); } voice.rendering = false; voice.rendered = true; diff --git a/core/libs/prx/libSceNgs2.native/src/Voice.cpp b/core/libs/prx/libSceNgs2.native/src/Voice.cpp index b92ece2f4..9afecf10c 100644 --- a/core/libs/prx/libSceNgs2.native/src/Voice.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Voice.cpp @@ -69,12 +69,6 @@ const std::uint8_t* Ngs2Voice::WaveformData() const { return block.data + (static_cast(block.info.num_skip_samples) + block.cursor) * channels * sizeof(std::int16_t); } -template -static const TParam& ParamAs(const Ngs2VoiceParamHeader& param) { - if (param.size < sizeof(TParam)) throw std::invalid_argument("NGS2: voice param " + Ngs2Hex(param.id) + " is too small"); - return reinterpret_cast(param); -} - static Ngs2Port& PortAt(Ngs2Voice& voice, std::uint32_t port) { if (port >= voice.ports.size()) throw std::invalid_argument("NGS2: voice port " + std::to_string(port) + " is out of range"); return voice.ports[port]; @@ -246,6 +240,10 @@ static void ApplyParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param) { ApplyCommonParam(voice, param); return; } + if (rackId == (SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX2 >> 16) || rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) { + Ngs2ApplyCustomParam(voice, param); + return; + } if (rackId != voice.rack->rackId) throw std::invalid_argument("NGS2: voice param " + Ngs2Hex(param.id) + " belongs to another rack"); switch (rackId) { case SCE_NGS2_RACK_ID_SAMPLER: ApplySamplerParam(voice, param); return; diff --git a/core/libs/prx/libSceNgs2.native/tests/Ngs2Custom.cpp b/core/libs/prx/libSceNgs2.native/tests/Ngs2Custom.cpp new file mode 100644 index 000000000..198b42fe2 --- /dev/null +++ b/core/libs/prx/libSceNgs2.native/tests/Ngs2Custom.cpp @@ -0,0 +1,231 @@ +#include "Ngs2Test.hpp" + +#include +#include +#include +#include + +struct Calls { + std::vector setupVoices; + std::vector cleanupVoices; + std::vector processFlags; + std::vector order; + int processResult = SCE_NGS2_OK; +}; +static Calls calls; + +static std::int32_t APS5_VABI Setup(Ngs2UserFx2SetupContext* context) { + Require(context->common != nullptr && context->param != nullptr && context->work != nullptr); + Require(context->user_data == 0x77 && context->max_voices == 2); + std::memset(context->work, 0x5a, 8); + *static_cast(context->param) = 1.0f; + *static_cast(context->common) = 0.25f; + calls.setupVoices.push_back(context->voice_index); + return SCE_NGS2_OK; +} + +static std::int32_t APS5_VABI Cleanup(Ngs2UserFx2CleanupContext* context) { + Require(context->user_data == 0x77 && context->max_voices == 2 && static_cast(context->work)[7] == 0x5a); + calls.cleanupVoices.push_back(context->voice_index); + return SCE_NGS2_OK; +} + +static std::int32_t APS5_VABI Gain(Ngs2UserFx2ProcessContext* context) { + Require(context->user_data == 0x77 && context->num_input_channels == 2 && context->num_output_channels == 2); + Require(context->num_grain_samples == Grain && context->sample_rate == 48000 && context->state != nullptr); + Require(static_cast(context->work)[0] == 0x5a); + const float gain = *static_cast(context->param); + for (std::uint32_t c = 0; c < 2; c++) { + for (std::uint32_t i = 0; i < Grain; i++) context->channel_data[c][i] *= gain; + } + *static_cast(context->common) = 0.125f; + calls.processFlags.push_back(context->flags); + calls.order.push_back(0); + return calls.processResult; +} + +static std::int32_t APS5_VABI Offset(Ngs2UserFx2ProcessContext* context) { + const float offset = *static_cast(context->common); + for (std::uint32_t i = 0; i < Grain; i++) context->channel_data[1][i] += offset; + calls.order.push_back(1); + return SCE_NGS2_OK; +} + +static std::int32_t APS5_VABI ControlHandler(Ngs2UserFx2ControlContext*) { + return SCE_NGS2_OK; +} + +static Ngs2CustomUserFx2ModuleOption Module(Ngs2UserFx2ProcessHandler process) { + return {{sizeof(Ngs2CustomUserFx2ModuleOption)}, Setup, Cleanup, nullptr, process, sizeof(float), sizeof(float), 8, 0x77}; +} + +static Ngs2CustomSubmixerRackOption RackOption(const Ngs2CustomUserFx2ModuleOption* modules, std::uint32_t count) { + Ngs2CustomSubmixerRackOption option{}; + auto& common = option.custom_rack_option.rack_option; + common.size = sizeof(option); + common.max_grain_samples = 512; + common.max_voices = 2; + common.max_matrices = 1; + common.max_ports = 1; + option.custom_rack_option.num_buffers = 1; + option.custom_rack_option.num_modules = count; + for (std::uint32_t m = 0; m < count; m++) { + option.custom_rack_option.module[m].option = &modules[m].custom_module_option; + option.custom_rack_option.module[m].module_id = SCE_NGS2_CUSTOM_MODULE_ID_USER_FX2; + option.custom_rack_option.module[m].state_size = 16; + } + option.max_channels = 2; + option.max_inputs = 1; + return option; +} + +template +static bool CreateThrows(const Ngs2CustomSubmixerRackOption& option) { + Ngs2ContextBufferInfo query{}; + try { + sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER, &option.custom_rack_option.rack_option, &query); + } catch (const TException&) { + return true; + } + return false; +} + +template +static bool ControlThrows(uintptr_t voice, std::uint32_t id, TParam param) { + param.header = {static_cast(sizeof(TParam)), 0, id}; + try { + sceNgs2VoiceControl(voice, ¶m.header); + } catch (const TException&) { + return true; + } + return false; +} + +static uintptr_t CustomRack(uintptr_t system, const Ngs2CustomSubmixerRackOption& option) { + Ngs2ContextBufferInfo query{}; + Require(sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER, &option.custom_rack_option.rack_option, &query) == SCE_NGS2_OK); + const auto info = Buffer(query); + uintptr_t rack = 0; + Require(sceNgs2RackCreate(system, SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER, &option.custom_rack_option.rack_option, &info, &rack) == SCE_NGS2_OK); + return rack; +} + +static uintptr_t Sampler(uintptr_t system, const std::vector& pcm) { + const auto voice = Voice(CreateRack(system, SCE_NGS2_RACK_ID_SAMPLER)); + Control(voice, SCE_NGS2_SAMPLER_VOICE_PARAM_SETUP, Ngs2SamplerVoiceSetupParam{{}, {SCE_NGS2_WAVEFORM_TYPE_PCM_I16L, 2, 48000, 0, 0, 0}}); + const Ngs2WaveformBlock block{0, pcm.size() * sizeof(std::int16_t), 0, 0, static_cast(pcm.size() / 2), 0, 0}; + Control(voice, SCE_NGS2_SAMPLER_VOICE_PARAM_ADD_WAVEFORM_BLOCKS, Ngs2SamplerVoiceWaveformBlocksParam{{}, pcm.data(), 0, 1, &block}); + return voice; +} + +static std::vector Render(uintptr_t system) { + std::vector 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); + return out; +} + +static void TestUserFxChain() { + calls = {}; + const auto system = CreateSystem(); + const auto master = Mastering(system, 2); + const Ngs2CustomUserFx2ModuleOption modules[2] = {Module(Gain), Module(Offset)}; + const auto rack = CustomRack(system, RackOption(modules, 2)); + Require(calls.setupVoices == (std::vector{0, 1, 0, 1})); + + const auto custom = Voice(rack); + Control(custom, SCE_NGS2_CUSTOM_SUBMIXER_VOICE_PARAM_SETUP, Ngs2CustomSubmixerVoiceSetupParam{{}, 2, 2, 0, 0}); + Patch(custom, master); + Event(custom, SCE_NGS2_VOICE_EVENT_PLAY); + + std::vector pcm(Grain * 2 * 8, 16384); + const auto sampler = Sampler(system, pcm); + Patch(sampler, custom); + Event(sampler, SCE_NGS2_VOICE_EVENT_PLAY); + + auto out = Render(system); + for (std::uint32_t i = 0; i < Grain; i++) Require(out[i * 2] == 0.5f && out[i * 2 + 1] == 0.75f); + Require(calls.processFlags == (std::vector{0}) && calls.order == (std::vector{0, 1})); + + const float gain = 0.5f; + Control(custom, SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX2 | 0, Ngs2CustomVoiceUserFx2Param{{}, &gain, sizeof(gain)}); + out = Render(system); + for (std::uint32_t i = 0; i < Grain; i++) Require(out[i * 2] == 0.25f && out[i * 2 + 1] == 0.5f); + Require(calls.processFlags == (std::vector{0, 2})); + out = Render(system); + Require(calls.processFlags == (std::vector{0, 2, 0})); + + Require(ControlThrows(custom, SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX2 | 0, Ngs2CustomVoiceUserFx2Param{{}, &gain, sizeof(gain) - 1})); + Require(ControlThrows(custom, SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX2 | 2, Ngs2CustomVoiceUserFx2Param{{}, &gain, sizeof(gain)})); + Require(ControlThrows(custom, SCE_NGS2_CUSTOM_SUBMIXER_VOICE_PARAM_SETUP, Ngs2CustomSubmixerVoiceSetupParam{{}, 2, 1, 0, 0})); + Require(ControlThrows(custom, SCE_NGS2_CUSTOM_SUBMIXER_VOICE_PARAM_SETUP, Ngs2CustomSubmixerVoiceSetupParam{{}, 2, 2, 1, 0})); + Require(ControlThrows(custom, SCE_NGS2_CUSTOM_SUBMIXER_VOICE_PARAM_SETUP, Ngs2CustomSubmixerVoiceSetupParam{{}, 3, 3, 0, 0})); + Require(ControlThrows(sampler, SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX2 | 0, Ngs2CustomVoiceUserFx2Param{{}, &gain, sizeof(gain)})); + + calls.processResult = -1; + bool failed = false; + try { + Render(system); + } catch (const std::runtime_error&) { + failed = true; + } + Require(failed); + calls.processResult = SCE_NGS2_OK; + + Require(sceNgs2RackDestroy(rack, nullptr) == SCE_NGS2_OK); + Require(calls.cleanupVoices == (std::vector{0, 0, 1, 1})); + Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); +} + +static void TestRackOptions() { + calls = {}; + const auto system = CreateSystem(); + const Ngs2CustomUserFx2ModuleOption modules[1] = {Module(Gain)}; + const auto good = RackOption(modules, 1); + Ngs2ContextBufferInfo query{}; + Require(sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER, &good.custom_rack_option.rack_option, &query) == SCE_NGS2_OK); + + auto option = good; + option.custom_rack_option.num_buffers = 2; + Require(CreateThrows(option)); + option = good; + option.custom_rack_option.module[0].module_id = 0x10; + Require(CreateThrows(option)); + option = good; + option.custom_rack_option.module[0].dest_buffer_id = 1; + Require(CreateThrows(option)); + option = good; + option.custom_rack_option.port[0].source_buffer_id = 1; + Require(CreateThrows(option)); + option = good; + option.custom_rack_option.num_modules = SCE_NGS2_CUSTOM_MAX_MODULES + 1; + Require(CreateThrows(option)); + auto noProcess = Module(nullptr); + option = RackOption(&noProcess, 1); + Require(CreateThrows(option)); + auto badSize = Module(Gain); + badSize.custom_module_option.size = 8; + option = RackOption(&badSize, 1); + Require(CreateThrows(option)); + bool failed = false; + try { + sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER, nullptr, &query); + } catch (const std::runtime_error&) { + failed = true; + } + Require(failed && calls.setupVoices.empty()); + + auto withControl = Module(Gain); + withControl.control_handler = ControlHandler; + const auto voice = Voice(CustomRack(system, RackOption(&withControl, 1))); + const float gain = 0.5f; + Require(ControlThrows(voice, SCE_NGS2_CUSTOM_VOICE_PARAM_USER_FX2 | 0, Ngs2CustomVoiceUserFx2Param{{}, &gain, sizeof(gain)})); + Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); + Require(calls.cleanupVoices == (std::vector{0, 1})); +} + +int main() { + TestUserFxChain(); + TestRackOptions(); + return 0; +} diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 4e605cf32..2cf3d0b50 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -103,6 +103,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceAjmDecMp3ParseFrame](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - signature, result layout and rate tables taken from the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation of the PS4 library, not confirmed on a PS5 title: the layer field is not checked and MPEG-2.5 stops at 64 kbps; the original file length (`parse_ofl`) layout (Xing/Info + LAME tag, VBRI, Fraunhofer `0xB4` block with its CRC, encoder delay = samples per frame + LAME delay + 529) also comes from shadPS4 only, and the Fraunhofer block has no other public description - [sceAjmBatchJobGetCodecInfo, sceAjmBatchJobGetGaplessDecode](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - the `RUN_GET_CODEC_INFO` flag (0x800), the ATRAC9 codec info layout and its next frame size are taken from the [shadPS4](https://github.com/shadps4-emu/shadPS4) and [RPCSX](https://github.com/RPCSX/rpcsx) reimplementations, not confirmed on a PS5 title; a run job without buffers returns result 0, as in shadPS4. The gapless sideband reports the total and skip counts the title set and does not count them down: RPCSX does the same, shadPS4 counts them down and BryKytyPS5 returns its current counts - [AJM Opus decoder](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native, codec 24) - the initialize parameter layout (`u32 channels, u32 sample rate, u32`, seen as `2, 48000, 0`) and the little-endian `u16` byte count before each packet come from one title (The Smurfs Dreams); only 48000 Hz and a zero third word are accepted +- [NGS2 custom submixer rack](../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native, rack 0x4002) - structure layouts from [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `ngs2_custom.h`; module id 0x1f for UserFx2, the voice parameter id `0x40001f00 | module index`, the process flag 2 after a parameter change and the call order (setup per voice at rack creation, process after the inputs are mixed, cleanup at destruction) come only from [KytyPS5](https://github.com/KytyPS5/KytyPS5). Only UserFx2 modules on a single buffer are accepted: other module ids, more buffers, a module control handler, different input and output channel counts, setup flags and default rack options throw. Each module's state is a separate buffer, not the `state_offset` slice of the voice state, and `sceNgs2VoiceGetState` still throws for this rack - [sceHttpParseResponseHeader](../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - signature follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Net/Http.cs); parsing and error codes follow [shadPS4's reverse-engineered PS4 library](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp), not confirmed on a PS5 title or hardware. Folded values retain their line breaks and whitespace; an empty first line starts the value scan after its line feed. A consumed byte count above `INT_MAX` throws instead of wrapping. - [sceHttpParseStatusLine](../../core/libs/prx/libSceHttp/src/Parse.cpp) (libSceHttp) - behaviour and error codes follow the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation of the PS4 library, not confirmed on a PS5 title; unlike it, no byte past `lineLen` is read - [sceHttpUriUnescape](../../core/libs/prx/libSceHttp/src/Uri.cpp) (libSceHttp) - signature follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Net/Http.cs); percent decoding, invalid sequences and error codes follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp), not confirmed on a PS5 title or hardware. Size includes the final NUL; malformed escapes and `+` remain unchanged, and decoding runs once.