Merge pull request #26110 from IvanFogel/fix/20261123/ngs2-compressor-key

fix(ngs2): route a native submixer compressor side-chain input
This commit is contained in:
Dean authored and GitHub committed 2026-11-26 23:36:54 +13:00
commit 9931d320d9
5 files changed
+56 -12

No files matched your search

@@ -160,6 +160,7 @@ struct Ngs2VoiceCustomChannelMixer {
struct Ngs2SubmixerCompressor {
bool configured = false;
Ngs2SubmixerVoiceCompressorParam param{};
std::vector<float> keySamples;
std::array<float, NGS2_MAX_CHANNELS> gain{};
};
+13 -7
View File
@@ -321,9 +321,8 @@ static std::vector<float> PortGains(const Ngs2Voice& voice, const Ngs2Voice& sou
return gains;
}
static void MixPort(Ngs2Voice& voice, const Ngs2Voice& source, const Ngs2Port& port, std::uint32_t sourceBuffer, const std::vector<float>& gains, std::uint32_t grain) {
static void MixPort(Ngs2Voice& voice, const Ngs2Voice& source, const Ngs2Port& port, std::uint32_t sourceBuffer, const std::vector<float>& gains, std::vector<float>& destSamples, std::uint32_t grain) {
const auto& sourceSamples = source.Samples(sourceBuffer);
auto& destSamples = voice.Samples(port.destInput);
if (sourceSamples.size() != static_cast<std::size_t>(grain) * source.channels ||
destSamples.size() != static_cast<std::size_t>(grain) * voice.InputChannels()) {
throw std::runtime_error("NGS2: channel layout changes during input rendering are not implemented");
@@ -349,6 +348,11 @@ static void MixPort(Ngs2Voice& voice, const Ngs2Voice& source, const Ngs2Port& p
}
static void MixInputs(Ngs2Voice& voice, const std::vector<Ngs2Voice*>& voices, std::uint32_t grain, std::uint32_t systemRate) {
auto& key = voice.compressor.keySamples;
const bool useKey = !PS4_LIBRARY && voice.rack->rackId == SCE_NGS2_RACK_ID_SUBMIXER &&
voice.rack->maxInputs >= 2 && (voice.compressor.param.flags & 0x9u) == 0x8u;
if (useKey) key.assign(voice.samples.size(), 0.0f);
else key.clear();
for (auto* source : voices) {
for (std::size_t p = 0; p < source->ports.size(); p++) {
auto& port = source->ports[p];
@@ -371,11 +375,12 @@ static void MixInputs(Ngs2Voice& voice, const std::vector<Ngs2Voice*>& voices, s
std::uint32_t outputs = 0;
auto gains = PortGains(voice, *source, port, outputs);
if (port.mixed && (port.mixedOutputs != outputs || port.mixedSources != source->channels)) port.mixed = false;
const bool unmixedInput = destInput != 0 && voice.rack->rackId == SCE_NGS2_RACK_ID_SUBMIXER;
if (source->hasSamples && !source->KilledForRender() && !unmixedInput) {
const bool secondaryInput = destInput != 0 && voice.rack->rackId == SCE_NGS2_RACK_ID_SUBMIXER;
if (source->hasSamples && !source->KilledForRender() && (!secondaryInput || (useKey && destInput == 1))) {
const auto sourceBuffer = source->rack->customPortBuffers.empty() ? 0u : source->rack->customPortBuffers[p];
MixPort(voice, *source, port, sourceBuffer, gains, grain);
voice.hasSamples = true;
auto& samples = secondaryInput ? key : voice.Samples(destInput);
MixPort(voice, *source, port, sourceBuffer, gains, samples, grain);
if (!secondaryInput) voice.hasSamples = true;
}
port.mixedGains = std::move(gains);
port.mixedOutputs = outputs;
@@ -407,6 +412,7 @@ static void ProcessCompressor(Ngs2Voice& voice, std::uint32_t grain, std::uint32
}
if (voice.userFxHandler != nullptr) throw std::runtime_error("NGS2: a submixer compressor on a voice with a UserFx handler is not implemented");
const auto channels = std::min<std::uint32_t>(voice.channels, NGS2_MAX_CHANNELS);
const auto& detector = compressor.keySamples.empty() ? voice.samples : compressor.keySamples;
const float attack = CompressorCoefficient(param.attack_time, sampleRate);
const float release = CompressorCoefficient(param.release_time, sampleRate);
const bool knee = CompressorKnee(param.knee);
@@ -414,7 +420,7 @@ static void ProcessCompressor(Ngs2Voice& voice, std::uint32_t grain, std::uint32
for (std::uint32_t start = 0; start < grain; start += 32) {
const auto count = std::min<std::uint32_t>(32, grain - start);
for (std::uint32_t channel = 0; channel < channels; channel++) {
const float* samples = voice.samples.data() + static_cast<std::size_t>(channel) * grain + start;
const float* samples = detector.data() + static_cast<std::size_t>(channel) * grain + start;
float value = 0.0f;
if ((param.flags & 2u) != 0) {
for (std::uint32_t i = 0; i < count; i++) value += samples[i] * samples[i];
@@ -578,7 +578,7 @@ static void CheckSubmixerFilter(const Ngs2Voice& voice, const Ngs2SamplerVoiceFi
if (param.type != 0) throw std::runtime_error("NGS2: submixer filter type " + std::to_string(param.type) + " is not implemented");
}
static void CheckSubmixerCompressor(const Ngs2Voice& voice, const Ngs2SubmixerVoiceCompressorParam& param) {
static void CheckSubmixerCompressor(const Ngs2SubmixerVoiceCompressorParam& param) {
const auto flags = param.flags;
if (PS4_LIBRARY && (flags & 1u) != 0) return;
if ((flags & ~0x3fu) != 0 || (PS4_LIBRARY ? (flags & 4u) != 0 : ((flags & 0x6u) == 0x6u || (flags & 0x30u) == 0x30u))) {
@@ -586,7 +586,6 @@ static void CheckSubmixerCompressor(const Ngs2Voice& voice, const Ngs2SubmixerVo
}
if ((flags & 1u) != 0) return;
if (!(param.knee >= 0.0f && param.knee <= 2.0f)) throw std::runtime_error("NGS2: a submixer compressor knee outside 0 to 2 is not implemented");
if (!PS4_LIBRARY && (flags & 8u) != 0 && voice.rack->maxInputs > 1) throw std::runtime_error("NGS2: a submixer compressor side-chain from a second input is not implemented");
if (!(param.threshold > 0.0f) || !std::isfinite(param.threshold) || !std::isfinite(param.ratio) || !std::isfinite(param.level) ||
!(param.attack_time >= 0.0f) || !std::isfinite(param.attack_time) || !(param.release_time >= 0.0f) || !std::isfinite(param.release_time)) {
throw std::runtime_error("NGS2: submixer compressor values outside the measured range are not implemented");
@@ -695,7 +694,7 @@ static Ngs2PendingControl CaptureParamControl(Ngs2Voice& voice, const Ngs2VoiceP
return CopyParam(param);
}
if (voice.rack->rackId == SCE_NGS2_RACK_ID_SUBMIXER && param.id == SCE_NGS2_SUBMIXER_VOICE_PARAM_COMPRESSOR) {
CheckSubmixerCompressor(voice, ParamAs<Ngs2SubmixerVoiceCompressorParam>(param));
CheckSubmixerCompressor(ParamAs<Ngs2SubmixerVoiceCompressorParam>(param));
return CopyParam(param);
}
if (voice.rack->rackId == SCE_NGS2_RACK_ID_SUBMIXER && param.id == SCE_NGS2_SUBMIXER_VOICE_PARAM_SETUP) {
@@ -1130,7 +1129,7 @@ static void ApplyParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param) {
}
if (param.id == SCE_NGS2_SUBMIXER_VOICE_PARAM_COMPRESSOR) {
const auto& compressor = ParamAs<Ngs2SubmixerVoiceCompressorParam>(param);
CheckSubmixerCompressor(voice, compressor);
CheckSubmixerCompressor(compressor);
SetVoiceCompressor(voice, compressor);
return;
}
@@ -262,7 +262,7 @@ static void TestSubmixerKeyInput() {
option.rack_option.max_ports = 1;
option.max_channels = 2;
option.max_envelope_points = 1;
option.max_inputs = 2;
option.max_inputs = 3;
option.num_peak_meter_blocks = 1;
Ngs2ContextBufferInfo query{};
Require(sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_SUBMIXER, &option.rack_option, &query) == SCE_NGS2_OK);
@@ -285,6 +285,35 @@ static void TestSubmixerKeyInput() {
Render(system, {-0.5f});
Route(input1, 0, submixer, 1);
Render(system, {0.25f});
#ifndef APS5_NGS2_PS4_LIBRARY
Ngs2SubmixerVoiceCompressorParam param{{}, 0x8, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0};
Control(submixer, SCE_NGS2_SUBMIXER_VOICE_PARAM_COMPRESSOR, param);
Render(system, {0.25f * 0.5f / 0.75f});
Route(input0, 1, submixer, 2);
Render(system, {0.25f * 0.5f / 0.75f});
param.flags = 0xa;
Control(submixer, SCE_NGS2_SUBMIXER_VOICE_PARAM_COMPRESSOR, param);
Render(system, {0.25f * 0.5f / 0.75f});
param.flags = 0;
Control(submixer, SCE_NGS2_SUBMIXER_VOICE_PARAM_COMPRESSOR, param);
Render(system, {0.25f});
param.flags = 0xa;
Control(submixer, SCE_NGS2_SUBMIXER_VOICE_PARAM_COMPRESSOR, param);
Route(input1, 0, 0);
Render(system, {0.25f});
Route(input1, 0, submixer, 1);
param.flags = 0;
Control(submixer, SCE_NGS2_SUBMIXER_VOICE_PARAM_COMPRESSOR, param);
Render(system, {0.25f});
param.flags = 0x8;
Control(submixer, SCE_NGS2_SUBMIXER_VOICE_PARAM_COMPRESSOR, param);
Route(input0, 1, submixer, 1);
Render(system, {0.25f});
Route(input0, 0, 0);
Render(system, {0.0f});
Route(input1, 0, submixer);
Render(system, {-0.75f});
#endif
Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK);
}
@@ -0,0 +1,9 @@
# libSceNgs2.native
## change [NGS2 patch destinations]
- [NGS2 patch destinations](../../../core/libs/prx/libSceNgs2.native/src/Voice.cpp) - a rack handle at input 0 selects its first voice (#4334). Multi-voice rack selection is unverified. Two-input custom submixers accept voice input 1. For standard native submixers, mapping patch input 1 to compressor buffer slot 1 is inferred from the [firmware's input-count and slot selection](https://github.com/boykopovar/AnyPS5/issues/15090#issuecomment-6643434499) and [PPSA35169's routing](https://github.com/boykopovar/AnyPS5/pull/15731). The PS4 library leaves input 1 unmixed. Nonzero rack-handle inputs, unsupported voice inputs, nonzero inputs on disconnected ports and routing changes during source render throw. GetPortInfo reports the resolved handle and input, with zero delay.
## change [Standard submixer compressor]
- [Standard submixer compressor](../../../core/libs/prx/libSceNgs2.native/src/Render.cpp) - console levels, module order, disabled pass-through and setup gain reset are unverified. It runs after mixing and refuses legacy UserFx, flags beyond `0x3f`, conflicting detector or link bits, and knees outside 0 to 2. The native `0x8` detector uses input 1 when available and input 0 otherwise. A [13.42 native run](https://github.com/boykopovar/AnyPS5/issues/21876#issuecomment-6950628991) confirmed the key controls gain on the program. The compressor curve follows the [unit measurements](https://github.com/boykopovar/AnyPS5/issues/15090#issuecomment-6643434499) and [gain and knee reading](https://github.com/boykopovar/AnyPS5/issues/15090#issuecomment-6672872206).