From e15f1644e384ab055f721347a4d31cdff02e9a47 Mon Sep 17 00:00:00 2001 From: JAB-dev <68183649+JAB-dev@users.noreply.github.com> Date: Wed, 7 Oct 2026 01:26:11 +0200 Subject: [PATCH] fix(libSceNgs2.native): reverb as the PS4 libSceNgs2 does The reverb was a Freeverb-style approximation. It now follows the PS4 firmware 11.00 libSceNgs2.sprx, read and checked against its output: the input mixed to mono with the library's fixed gains, a 14400-sample buffer with four reflection taps from the reflection pattern tables and a late feed, sixteen damped comb filters with a pre-emphasis stage, a signed selection of combs and four allpasses per output channel, a two-pole room HF filter, and the library's parameter clamps, crossfades and setup defaults. The test values are the PS4 library's own output. Co-Authored-By: Claude Opus 5.5 --- .../libSceNgs2.native/src/Ngs2Internal.hpp | 1 + .../libs/prx/libSceNgs2.native/src/Reverb.cpp | 452 ++++++++++++------ core/libs/prx/libSceNgs2.native/src/Voice.cpp | 2 + .../libSceNgs2.native/tests/Ngs2Render.cpp | 249 +++++----- docs/dev/TechnicalDebt.md | 2 +- 5 files changed, 448 insertions(+), 258 deletions(-) diff --git a/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp b/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp index 506160b4d..1d8841542 100644 --- a/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp +++ b/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp @@ -166,6 +166,7 @@ void Ngs2CleanupUserFx(Ngs2Rack& rack); void Ngs2ApplyCustomParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param); void Ngs2ProcessUserFx(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t sampleRate); void Ngs2SetReverbParams(Ngs2Voice& voice, const Ngs2ReverbI3DL2Param& params); +void Ngs2SetupReverb(Ngs2Voice& voice); void Ngs2ClearReverb(Ngs2Voice& voice); bool Ngs2ProcessReverb(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t sampleRate); void Ngs2RenderSystem(Ngs2System& system, const Ngs2RenderBufferInfo* bufferInfo, std::uint32_t numBufferInfo); diff --git a/core/libs/prx/libSceNgs2.native/src/Reverb.cpp b/core/libs/prx/libSceNgs2.native/src/Reverb.cpp index 548881da6..06e2beae0 100644 --- a/core/libs/prx/libSceNgs2.native/src/Reverb.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Reverb.cpp @@ -9,192 +9,372 @@ #include "prx/libc/include/General.hpp" #include "Ngs2Internal.hpp" -static constexpr std::array COMB_TUNING = {1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617}; -static constexpr std::array ALLPASS_TUNING = {556, 441, 341, 225}; -static constexpr std::array EARLY_TAPS = {1.0f, 1.31f, 1.73f, 2.19f}; -static constexpr std::array EARLY_GAINS = {0.6f, 0.45f, 0.35f, 0.25f}; -static constexpr std::uint32_t STEREO_SPREAD = 23; -static constexpr float TUNING_RATE = 44100.0f; -static constexpr float INPUT_GAIN = 0.015f; -static constexpr float OUTPUT_GAIN = 3.0f; -static constexpr float MAX_REFLECTIONS_DELAY = 0.3f; -static constexpr float MAX_REVERB_DELAY = 0.1f; -static constexpr float SILENCE = 1e-20f; +static constexpr std::uint32_t TAP_RING_SAMPLES = 14400; +static constexpr std::uint32_t EARLY_TAPS = 4; +static constexpr std::uint32_t COMBS = 16; +static constexpr std::uint32_t ALLPASSES = 4; +static constexpr std::uint32_t LFE_CHANNEL = 3; -static float Flush(float value) { - return std::fabs(value) < SILENCE ? 0.0f : value; -} +static constexpr float TAP_TIMES[8][EARLY_TAPS] = { + {0, 11, 23, 35}, {0, 14.75f, 26.5499992f, 38.0299988f}, {0, 6, 17, 29}, {0, 8, 20, 53}, + {0, 11, 17, 30}, {0, 16, 30, 55}, {0, 20, 29, 40}, {0, 37, 47, 62}, +}; +static constexpr float TAP_GAINS[8][EARLY_TAPS] = { + {0.7f, 0.4f, 0.134f, 0.1f}, {0.7f, 0.4f, 0.3f, 0.1f}, {0.7f, 0.45f, -0.6f, 0.2f}, {0.7f, 0.55f, -0.6f, 0.2f}, + {0.7f, -0.45f, 0.6f, 0.3f}, {0.7f, 0.7f, 0.7f, 0.4f}, {0.7f, 0.7f, -0.4f, 0.3f}, {0.7f, 0.7f, -0.4f, 0.4f}, +}; +static constexpr float COMB_TIMES[COMBS] = { + 22.0100002f, 23.25f, 24.75f, 26.6000004f, 28.25f, 29.6299992f, 31.0599995f, 32.4399986f, + 33.6899986f, 34.9500008f, 36.2900009f, 37.6500015f, 38.9900017f, 40.3499985f, 41.6699982f, 43.0499992f, +}; +static constexpr float ALLPASS_TIMES[ALLPASSES] = {11.5799999f, 9.18999958f, 7.0999999f, 4.69000006f}; +static constexpr std::int32_t ALLPASS_OFFSETS[8][ALLPASSES] = { + {0, -32, -6, -11}, {-10, -5, 0, -34}, {-8, -3, -33, -5}, {-7, -9, -5, -8}, + {-33, -8, -8, 0}, {-11, -21, -10, -7}, {-9, -10, -27, -3}, {-6, -11, -9, -24}, +}; +static constexpr std::uint16_t CHANNEL_COMBS[8] = {0xaaaa, 0x5555, 0xcccc, 0x3333, 0x0ff0, 0x9ab2, 0x3c3c, 0x399c}; +static constexpr Ngs2ReverbI3DL2Param SETUP_PARAMS{1.0f, 0.0f, -10000, 0, 0, 1.0f, 0.5f, -10000, 0.02f, -10000, 0.04f, 100.0f, 100.0f, 5000.0f, {}}; -struct Ngs2DelayLine { - std::vector buffer; - std::size_t cursor = 0; - - float Tap(std::size_t delay) const { - delay = std::min(delay, buffer.size() - 1); - return buffer[(cursor + buffer.size() - delay) % buffer.size()]; - } - void Push(float value) { - cursor = (cursor + 1) % buffer.size(); - buffer[cursor] = value; - } +struct Ngs2ReverbTaps { + std::array delays{}; + std::array gains{}; }; struct Ngs2Comb { std::vector buffer; - std::size_t cursor = 0; + std::uint32_t cursor = 0; float feedback = 0.0f; - float damp = 0.0f; - float store = 0.0f; - - float Process(float input) { - const float output = buffer[cursor]; - store = Flush(output * (1.0f - damp) + store * damp); - buffer[cursor] = Flush(input + store * feedback); - cursor = (cursor + 1) % buffer.size(); - return output; - } + float lowpassIn = 0.0f; + float lowpassBack = 0.0f; + float direct = 0.0f; + float filtered = 0.0f; + float emphasis = 0.0f; + float emphasisBack = 0.0f; + float previous = 0.0f; + float lowpass = 0.0f; }; struct Ngs2Allpass { std::vector buffer; - std::size_t cursor = 0; - - float Process(float input, float feedback) { - const float delayed = buffer[cursor]; - buffer[cursor] = Flush(input + delayed * feedback); - cursor = (cursor + 1) % buffer.size(); - return delayed - input; - } + std::uint32_t cursor = 0; }; struct Ngs2ReverbChannel { - float roomLowpass = 0.0f; - Ngs2DelayLine preDelay; - std::array combs; - std::array allpasses; + std::array allpasses; + float y1 = 0.0f; + float y2 = 0.0f; + float wet = 0.0f; + float dry = 0.0f; }; struct Ngs2ReverbState { Ngs2ReverbI3DL2Param params{}; - bool configured = false; + bool reset = true; + bool changed = true; std::uint32_t sampleRate = 0; - std::uint32_t channels = 0; - float dry = 0.0f; - float wet = 0.0f; - float roomHf = 1.0f; - float roomCoefficient = 0.0f; + std::vector ring; + std::uint32_t ringCursor = 0; + Ngs2ReverbTaps taps; float earlyGain = 0.0f; float lateGain = 0.0f; - float allpassFeedback = 0.5f; - std::size_t earlyDelay = 0; - std::size_t lateDelay = 0; - std::vector lines; + float density = NAN; + std::array combs; + std::array channels; + bool roomFilter = false; + float roomB0 = 0.0f; + float roomA1 = 0.0f; + float roomA2 = 0.0f; }; void Ngs2ReverbDeleter::operator()(Ngs2ReverbState* reverb) const { delete reverb; } -static float MillibelGain(std::int32_t millibels) { - return std::pow(10.0f, static_cast(millibels) / 2000.0f); +static float Millibels(float millibels) { + if (millibels < -10000.0f) return 0.0f; + return static_cast(std::pow(10.0, static_cast(millibels * 0.0005f))); } -static std::size_t ScaledLength(std::uint32_t tuning, std::uint32_t spread, float rate) { - return std::max(1, static_cast((tuning + spread) * rate / TUNING_RATE)); +static float Clamp(float value, float low, float high) { + return value < low ? low : std::min(high, value); } -static void Allocate(Ngs2ReverbState& reverb, std::uint32_t sampleRate, std::uint32_t channels) { - const float rate = static_cast(sampleRate); - const auto maxDelay = static_cast(std::ceil(std::max(MAX_REFLECTIONS_DELAY * EARLY_TAPS.back(), MAX_REFLECTIONS_DELAY + MAX_REVERB_DELAY) * rate)); - reverb.sampleRate = sampleRate; - reverb.channels = channels; - reverb.lines.assign(channels, {}); - for (std::uint32_t channel = 0; channel < channels; channel++) { - auto& line = reverb.lines[channel]; - line.preDelay.buffer.assign(maxDelay + 1, 0.0f); - const std::uint32_t spread = channel * STEREO_SPREAD; - for (std::size_t i = 0; i < COMB_TUNING.size(); i++) line.combs[i].buffer.assign(ScaledLength(COMB_TUNING[i], spread, rate), 0.0f); - for (std::size_t i = 0; i < ALLPASS_TUNING.size(); i++) line.allpasses[i].buffer.assign(ScaledLength(ALLPASS_TUNING[i], spread, rate), 0.0f); +static std::int32_t ClampMillibels(std::int32_t value, std::int32_t high) { + return std::max(-10000, std::min(value, high)); +} + +static std::uint32_t Samples(float milliseconds, float rate) { + return static_cast(rate * milliseconds * 0.001f + 0.5f); +} + +static void Ramp(float* samples, float from, float to, std::uint32_t grain, const float* source, bool accumulate) { + const float step = (to - from) / static_cast(grain); + float gain = from; + for (std::uint32_t i = 0; i < grain; i++) { + const float value = source[i] * (from == to ? to : gain); + samples[i] = accumulate ? samples[i] + value : value; + gain += step; } } -static void UpdateCoefficients(Ngs2ReverbState& reverb) { - const auto& params = reverb.params; - const float rate = static_cast(reverb.sampleRate); - const float hfReference = std::min(params.hf_reference, rate * 0.45f); - reverb.dry = params.dry; - reverb.wet = params.wet * MillibelGain(params.room); - reverb.roomHf = MillibelGain(params.room_hf); - reverb.roomCoefficient = 1.0f - std::exp(-2.0f * std::numbers::pi_v * hfReference / rate); - reverb.earlyGain = MillibelGain(params.reflections); - reverb.lateGain = MillibelGain(params.reverb); - reverb.allpassFeedback = 0.5f * params.diffusion / 100.0f; - reverb.earlyDelay = static_cast(params.reflections_delay * rate); - reverb.lateDelay = static_cast((params.reflections_delay + params.reverb_delay) * rate); - for (auto& line : reverb.lines) { - for (auto& comb : line.combs) { - const float seconds = static_cast(comb.buffer.size()) / rate; - const float low = std::pow(10.0f, -3.0f * seconds / params.decay_time); - const float high = std::pow(10.0f, -3.0f * seconds / (params.decay_time * params.decay_hf_ratio)); - const float ratio = std::min(high / low, 1.0f); - comb.feedback = low; - comb.damp = (1.0f - ratio) / (1.0f + ratio); +static void RunTaps(const Ngs2ReverbState& reverb, const Ngs2ReverbTaps& taps, const float* input, std::uint32_t grain, float* early, float* late) { + const auto size = static_cast(reverb.ring.size()); + std::fill(early, early + grain, 0.0f); + std::fill(late, late + grain, 0.0f); + for (std::uint32_t tap = 0; tap <= EARLY_TAPS; tap++) { + float* out = tap < EARLY_TAPS ? early : late; + const float gain = taps.gains[tap]; + const std::uint32_t delay = taps.delays[tap]; + std::uint32_t read = (reverb.ringCursor + size - delay) % size; + for (std::uint32_t i = 0; i < grain; i++) { + if (i < delay) { + out[i] += reverb.ring[read] * gain; + read = read + 1 == size ? 0 : read + 1; + } else { + out[i] += input[i - delay] * gain; + } } } } -static bool InRange(float value, float low, float high) { - return std::isfinite(value) && value >= low && value <= high; -} - -void Ngs2SetReverbParams(Ngs2Voice& voice, const Ngs2ReverbI3DL2Param& params) { - if (!std::isfinite(params.wet) || !std::isfinite(params.dry) || params.room < -10000 || params.room > 0 || params.room_hf < -10000 || params.room_hf > 0 || - !InRange(params.decay_time, 0.1f, 20.0f) || !InRange(params.decay_hf_ratio, 0.1f, 2.0f) || params.reflections < -10000 || params.reflections > 1000 || - !InRange(params.reflections_delay, 0.0f, MAX_REFLECTIONS_DELAY) || params.reverb < -10000 || params.reverb > 2000 || - !InRange(params.reverb_delay, 0.0f, MAX_REVERB_DELAY) || !InRange(params.diffusion, 0.0f, 100.0f) || !InRange(params.density, 0.0f, 100.0f) || - !InRange(params.hf_reference, 20.0f, 20000.0f)) { - APS5_INVALID_ARG_EX; +static Ngs2ReverbTaps TapsFor(const Ngs2ReverbI3DL2Param& params, std::uint32_t size, float rate) { + Ngs2ReverbTaps taps; + const std::uint32_t pattern = params.reflection_pattern < 8 ? params.reflection_pattern : params.reflection_pattern - 8; + const auto early = std::min(static_cast(params.reflections_delay * rate * 1000.0f * 0.001f + 0.5f), size); + const auto late = std::min(static_cast((params.reflections_delay + params.reverb_delay) * rate * 1000.0f * 0.001f + 0.5f), size); + const auto last = static_cast(rate * TAP_TIMES[pattern][EARLY_TAPS - 1] * 0.001f + 0.5f); + const std::uint32_t limit = params.reflection_pattern > 7 ? size : late; + const float scale = early + last > limit ? static_cast(limit - early) / static_cast(last) : 1.0f; + taps.delays[0] = early; + taps.gains[0] = TAP_GAINS[pattern][0]; + for (std::uint32_t tap = 1; tap < EARLY_TAPS; tap++) { + taps.delays[tap] = static_cast(scale * TAP_TIMES[pattern][tap] * rate * 0.001f + 0.5f) + early; + taps.gains[tap] = taps.delays[tap] == taps.delays[tap - 1] ? 0.0f : TAP_GAINS[pattern][tap]; } - if (!voice.reverb) voice.reverb.reset(new Ngs2ReverbState{}); - voice.reverb->params = params; - voice.reverb->configured = false; + taps.delays[EARLY_TAPS] = std::min(early + static_cast(params.reverb_delay * rate * 1000.0f * 0.001f + 0.5f), size); + taps.gains[EARLY_TAPS] = 1.0f; + return taps; } -void Ngs2ClearReverb(Ngs2Voice& voice) { - if (voice.reverb) voice.reverb->sampleRate = 0; +static void SetupComb(Ngs2Comb& comb, std::uint32_t length, const Ngs2ReverbI3DL2Param& params, float rate, bool clear) { + if (clear || comb.buffer.size() != length) { + comb.buffer.assign(length, 0.0f); + if (comb.cursor >= length) comb.cursor = 0; + } + comb.feedback = std::pow(10.0f, static_cast(length) / rate / params.decay_time * -3.0f); + const float ratio = params.decay_hf_ratio; + if (ratio > 1.0f) { + const float high = std::pow(10.0f, (ratio - 1.0f) * std::log10(comb.feedback)); + comb.lowpassIn = 0.0506209731f; + comb.lowpassBack = 0.949379027f; + comb.direct = 1.0f; + comb.filtered = 1.0f - high; + } else { + float shaped = ratio * ratio; + if (0.8f > ratio) { + shaped = (0.8f - ratio) * -0.857142925f + 0.8f; + shaped *= shaped; + } + const float back = std::min(0.99977994f, std::log10(comb.feedback) * 2.30258512f * 0.25f * (1.0f - 1.0f / shaped)); + comb.lowpassIn = 1.0f - back; + comb.lowpassBack = back; + comb.direct = 0.0f; + comb.filtered = 1.0f; + } + const float root = std::sqrt(ratio); + const float tilt = (1.0f - root) / (root + 1.0f); + comb.emphasis = tilt / (1.0f - tilt) + 1.0f; + comb.emphasisBack = -tilt / (1.0f - tilt); +} + +static void Reset(Ngs2ReverbState& reverb) { + reverb.ring.assign(TAP_RING_SAMPLES, 0.0f); + reverb.ringCursor = 0; + reverb.taps = {}; + reverb.earlyGain = 1.0f; + reverb.lateGain = 1.0f; + reverb.density = NAN; + reverb.combs = {}; + reverb.channels = {}; +} + +static void ApplyParams(Ngs2ReverbState& reverb, float rate) { + const auto& params = reverb.params; + reverb.taps = TapsFor(params, static_cast(reverb.ring.size()), rate); + const float room = std::min(1.0f, Millibels(static_cast(params.room))); + reverb.earlyGain = Millibels(static_cast(ClampMillibels(params.reflections, 1000))) * room; + reverb.lateGain = Millibels(static_cast(ClampMillibels(params.reverb, 2000) - 2400)) * room; + const bool densityChanged = !(reverb.density == params.density); + reverb.density = params.density; + const float densityScale = params.density * -3.0f / 100.0f + 4.0f; + for (std::uint32_t i = 0; i < COMBS; i++) { + auto length = static_cast(rate * COMB_TIMES[i] / densityScale * 0.001f + 0.5f); + length += ~length & 1; + SetupComb(reverb.combs[i], length, params, rate, densityChanged); + } + for (std::uint32_t channel = 0; channel < 8; channel++) { + for (std::uint32_t stage = 0; stage < ALLPASSES; stage++) { + auto& allpass = reverb.channels[channel].allpasses[stage]; + const auto length = static_cast(static_cast(Samples(ALLPASS_TIMES[stage], rate)) + ALLPASS_OFFSETS[channel][stage]); + if (allpass.buffer.size() != length) allpass.buffer.assign(length, 0.0f); + if (allpass.cursor >= length) allpass.cursor = 0; + } + } +} + +static void UpdateRoomFilter(Ngs2ReverbState& reverb, float rate) { + const auto& params = reverb.params; + const float roomHf = Millibels(static_cast(ClampMillibels(params.room_hf, 0))); + reverb.roomFilter = 1.0f > roomHf; + if (!reverb.roomFilter) return; + const float cosine = std::cos(params.hf_reference * 6.28318548f / rate); + const float root = std::sqrt((roomHf + roomHf) * (1.0f - cosine) - roomHf * roomHf * (1.0f - cosine * cosine)); + const float pole = ((1.0f - roomHf * cosine) - root) / (1.0f - roomHf); + reverb.roomB0 = (1.0f - pole) * (1.0f - pole); + reverb.roomA1 = pole + pole; + reverb.roomA2 = pole * -pole; +} + +static void RunComb(Ngs2Comb& comb, const float* input, float* output, std::uint32_t grain) { + const auto length = static_cast(comb.buffer.size()); + for (std::uint32_t i = 0; i < grain; i++) { + const float emphasized = input[i] * comb.emphasis + comb.previous * comb.emphasisBack; + comb.previous = input[i]; + const float delayed = comb.buffer[comb.cursor]; + comb.lowpass = delayed * comb.lowpassIn + comb.lowpass * comb.lowpassBack; + const float back = (delayed * comb.direct + comb.lowpass * comb.filtered) * comb.feedback; + comb.buffer[comb.cursor] = emphasized + back; + output[i] = back; + comb.cursor = comb.cursor + 1 == length ? 0 : comb.cursor + 1; + } +} + +static void RunAllpass(Ngs2Allpass& allpass, float gain, float* samples, std::uint32_t grain) { + const auto length = static_cast(allpass.buffer.size()); + for (std::uint32_t i = 0; i < grain; i++) { + const float delayed = allpass.buffer[allpass.cursor]; + const float stored = delayed * gain + samples[i]; + allpass.buffer[allpass.cursor] = stored; + samples[i] = delayed - gain * stored; + allpass.cursor = allpass.cursor + 1 == length ? 0 : allpass.cursor + 1; + } +} + +static void MixDown(const float* input, std::uint32_t channels, std::uint32_t grain, float* mono) { + const float* channel[8]; + for (std::uint32_t c = 0; c < channels; c++) channel[c] = input + static_cast(c) * grain; + if (channels == 1) { + std::copy(channel[0], channel[0] + grain, mono); + return; + } + const float gain = channels == 2 ? 0.707000017f : channels == 6 ? 0.316000015f : 0.26699999f; + for (std::uint32_t i = 0; i < grain; i++) mono[i] = channel[0][i] * gain; + for (std::uint32_t c = 1; c < channels; c++) { + if (c == LFE_CHANNEL) continue; + for (std::uint32_t i = 0; i < grain; i++) mono[i] += channel[c][i] * gain; + } } bool Ngs2ProcessReverb(Ngs2Voice& voice, std::uint32_t grain, std::uint32_t sampleRate) { auto& reverb = *voice.reverb; - if (reverb.sampleRate != sampleRate || reverb.channels != voice.channels) { - Allocate(reverb, sampleRate, voice.channels); - reverb.configured = false; + const float rate = static_cast(sampleRate); + const std::uint32_t channels = voice.channels; + if (reverb.sampleRate != sampleRate) { + reverb.sampleRate = sampleRate; + reverb.reset = true; } - if (!reverb.configured) { - UpdateCoefficients(reverb); - reverb.configured = true; + const bool reset = reverb.reset; + if (reset) { + Reset(reverb); + reverb.changed = true; } + const std::vector input = voice.samples; + std::vector mono(grain), early(grain), late(grain); + MixDown(input.data(), channels, grain, mono.data()); + if (reverb.changed) { + std::vector oldEarly(grain), oldLate(grain); + RunTaps(reverb, reverb.taps, mono.data(), grain, oldEarly.data(), oldLate.data()); + ApplyParams(reverb, rate); + RunTaps(reverb, reverb.taps, mono.data(), grain, early.data(), late.data()); + Ramp(early.data(), 0.0f, 1.0f, grain, early.data(), false); + Ramp(early.data(), 1.0f, 0.0f, grain, oldEarly.data(), true); + Ramp(late.data(), 0.0f, 1.0f, grain, late.data(), false); + Ramp(late.data(), 1.0f, 0.0f, grain, oldLate.data(), true); + } else { + RunTaps(reverb, reverb.taps, mono.data(), grain, early.data(), late.data()); + } + for (std::uint32_t i = 0; i < grain; i++) { + reverb.ring[reverb.ringCursor] = mono[i]; + reverb.ringCursor = reverb.ringCursor + 1 == reverb.ring.size() ? 0 : reverb.ringCursor + 1; + } + std::vector combs(static_cast(COMBS) * grain); + for (std::uint32_t i = 0; i < COMBS; i++) RunComb(reverb.combs[i], late.data(), combs.data() + static_cast(i) * grain, grain); + UpdateRoomFilter(reverb, rate); + const auto& params = reverb.params; bool audible = false; - for (std::uint32_t channel = 0; channel < voice.channels; channel++) { - auto& line = reverb.lines[channel]; - float* samples = voice.samples.data() + static_cast(channel) * grain; - for (std::uint32_t i = 0; i < grain; i++) { - const float input = samples[i]; - line.roomLowpass = Flush(line.roomLowpass + reverb.roomCoefficient * (input - line.roomLowpass)); - line.preDelay.Push(line.roomLowpass + reverb.roomHf * (input - line.roomLowpass)); - float early = 0.0f; - if (reverb.earlyDelay != 0) { - for (std::size_t tap = 0; tap < EARLY_TAPS.size(); tap++) { - early += line.preDelay.Tap(static_cast(reverb.earlyDelay * EARLY_TAPS[tap])) * EARLY_GAINS[tap]; - } + for (std::uint32_t channel = 0; channel < channels; channel++) { + auto& state = reverb.channels[channel]; + float* out = voice.samples.data() + static_cast(channel) * grain; + const float* in = input.data() + static_cast(channel) * grain; + if (channel == LFE_CHANNEL) { + std::copy(in, in + grain, out); + } else { + std::vector tail(grain, 0.0f); + std::uint32_t selected = 0; + for (std::uint32_t i = 0; i < COMBS; i++) { + if ((CHANNEL_COMBS[channel] >> i & 1) == 0) continue; + const float weight = (selected & 1) != 0 ? 1.0f : 1.0f / (static_cast(selected) * 0.5f * 20.0f + 40.0f) * (params.density + -100.0f) + 1.0f; + const float* comb = combs.data() + static_cast(i) * grain; + for (std::uint32_t n = 0; n < grain; n++) tail[n] += comb[n] * weight; + selected++; + } + for (auto& allpass : state.allpasses) RunAllpass(allpass, params.diffusion * 0.005f, tail.data(), grain); + for (std::uint32_t n = 0; n < grain; n++) out[n] = early[n] * reverb.earlyGain + tail[n] * reverb.lateGain; + for (std::uint32_t n = 0; n < grain; n++) { + const float y = reverb.roomFilter ? reverb.roomB0 * out[n] + reverb.roomA1 * state.y1 + reverb.roomA2 * state.y2 : out[n]; + state.y2 = state.y1; + state.y1 = y; + out[n] = y; } - const float lateInput = line.preDelay.Tap(reverb.lateDelay) * INPUT_GAIN; - float late = 0.0f; - for (auto& comb : line.combs) late += comb.Process(lateInput); - for (auto& allpass : line.allpasses) late = allpass.Process(late, reverb.allpassFeedback); - samples[i] = Flush(reverb.dry * input + reverb.wet * (reverb.earlyGain * early + reverb.lateGain * late * OUTPUT_GAIN)); - audible = audible || samples[i] != 0.0f; } + const float wet = channel == LFE_CHANNEL ? 0.0f : params.wet; + Ramp(out, reset ? wet : state.wet, wet, grain, out, false); + Ramp(out, reset ? params.dry : state.dry, params.dry, grain, in, true); + state.wet = wet; + state.dry = params.dry; + for (std::uint32_t n = 0; n < grain && !audible; n++) audible = out[n] != 0.0f; } + reverb.reset = false; + reverb.changed = false; return audible; } + +void Ngs2SetReverbParams(Ngs2Voice& voice, const Ngs2ReverbI3DL2Param& params) { + if (std::isnan(params.wet) || std::isnan(params.dry) || std::isnan(params.decay_time) || std::isnan(params.decay_hf_ratio) || std::isnan(params.reflections_delay) || + std::isnan(params.reverb_delay) || std::isnan(params.diffusion) || std::isnan(params.density) || std::isnan(params.hf_reference)) { + APS5_INVALID_ARG_EX; + } + if (!voice.reverb) voice.reverb.reset(new Ngs2ReverbState{}); + auto& stored = voice.reverb->params; + stored = params; + stored.wet = Clamp(params.wet, 0.0f, 32.0f); + stored.dry = Clamp(params.dry, 0.0f, 32.0f); + stored.reflection_pattern = params.reflection_pattern < 16 ? params.reflection_pattern : 0; + stored.decay_time = Clamp(params.decay_time, 0.1f, 20.0f); + stored.decay_hf_ratio = Clamp(params.decay_hf_ratio, 0.00999999978f, 2.0f); + stored.reflections_delay = Clamp(params.reflections_delay, 0.0f, 0.300000012f); + stored.reverb_delay = Clamp(params.reverb_delay, 0.0f, 0.100000001f); + stored.diffusion = Clamp(params.diffusion, 0.0f, 100.0f); + stored.density = Clamp(params.density, 0.0f, 100.0f); + stored.hf_reference = Clamp(params.hf_reference, 20.0f, 20000.0f); + voice.reverb->changed = true; +} + +void Ngs2SetupReverb(Ngs2Voice& voice) { + voice.reverb.reset(new Ngs2ReverbState{}); + Ngs2SetReverbParams(voice, SETUP_PARAMS); +} + +void Ngs2ClearReverb(Ngs2Voice& voice) { + if (voice.reverb) voice.reverb->reset = true; +} diff --git a/core/libs/prx/libSceNgs2.native/src/Voice.cpp b/core/libs/prx/libSceNgs2.native/src/Voice.cpp index 8e5e9910e..c43e9c513 100644 --- a/core/libs/prx/libSceNgs2.native/src/Voice.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Voice.cpp @@ -258,7 +258,9 @@ static void ApplyParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param) { const auto& setup = ParamAs(param); if (setup.flags != 0) throw std::runtime_error("NGS2: reverb setup flags are not implemented"); if (setup.num_input_channels != setup.num_output_channels) throw std::runtime_error("NGS2: reverb channel conversion is not implemented"); + if (setup.num_output_channels != 1 && setup.num_output_channels != 2 && setup.num_output_channels != 6 && setup.num_output_channels != 8) APS5_INVALID_ARG_EX; SetupMixer(voice, setup.num_output_channels); + Ngs2SetupReverb(voice); return; } if (param.id == SCE_NGS2_REVERB_VOICE_PARAM_I3DL2) { diff --git a/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp b/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp index 81f613548..08612a639 100644 --- a/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp +++ b/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp @@ -1,5 +1,6 @@ #include "Ngs2Test.hpp" +#include #include #include #include @@ -157,153 +158,160 @@ static std::int32_t APS5_VABI Release(Ngs2ContextBufferInfo* info) { return SCE_NGS2_OK; } -static std::uint32_t FirstReverbSample(const Ngs2ReverbI3DL2Param& params) { +struct ReverbFrame { + std::uint32_t frame; + std::vector samples; +}; + +struct ReverbCase { + std::uint32_t channels; + float amplitude; + bool noise; + Ngs2ReverbI3DL2Param params; + std::int32_t changeGrain; + Ngs2ReverbI3DL2Param change; + std::vector expected; +}; + +static std::vector RenderReverb(const ReverbCase& reverbCase, std::uint32_t frames) { + const auto firstBuffer = usedBuffers; const auto system = CreateSystem(); - const auto master = Mastering(system, 1); + Require(sceNgs2SystemSetGrainSamples(system, 256) == SCE_NGS2_OK); + const auto channels = reverbCase.channels; + const auto master = Mastering(system, channels); const auto reverb = Voice(CreateRack(system, SCE_NGS2_RACK_ID_REVERB)); - Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 1, 1, 0, 0}); - Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, params}); + Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, channels, channels, 0, 0}); + Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, reverbCase.params}); Patch(reverb, master); Event(reverb, SCE_NGS2_VOICE_EVENT_PLAY); - std::vector pcm(Grain, 0); - pcm[0] = 32767; - const auto sampler = Sampler(system, pcm, 0); + std::vector pcm(4096 * channels, 0); + std::uint32_t seed = 1; + for (std::uint32_t i = 0; reverbCase.noise && i + 3 * channels < pcm.size(); i++) { + seed = seed * 1664525u + 1013904223u; + if (i < 3000 * channels) pcm[i + 3 * channels] = static_cast(static_cast((seed >> 16) & 0x3fff) - 0x2000); + } + for (std::uint32_t c = 0; !reverbCase.noise && c < channels; c++) pcm[3 * channels + c] = static_cast((16384 - 4000 * static_cast(c)) * reverbCase.amplitude); + const auto sampler = Voice(CreateRack(system, SCE_NGS2_RACK_ID_SAMPLER)); + Control(sampler, SCE_NGS2_SAMPLER_VOICE_PARAM_SETUP, Ngs2SamplerVoiceSetupParam{{}, {SCE_NGS2_WAVEFORM_TYPE_PCM_I16L, channels, 48000, 0, 0, 0}}); + const Ngs2WaveformBlock block{0, pcm.size() * sizeof(std::int16_t), 0, 0, 4096, 0, 0}; + Control(sampler, SCE_NGS2_SAMPLER_VOICE_PARAM_ADD_WAVEFORM_BLOCKS, Ngs2SamplerVoiceWaveformBlocksParam{{}, pcm.data(), 0, 1, &block}); Patch(sampler, reverb); + std::vector identity(channels * channels, 0.0f); + for (std::uint32_t c = 0; c < channels; c++) identity[c * channels + c] = 1.0f; + Control(sampler, SCE_NGS2_VOICE_PARAM_MATRIX_LEVELS, Ngs2VoiceMatrixLevelsParam{{}, 0, channels * channels, identity.data()}); + Control(sampler, SCE_NGS2_VOICE_PARAM_PORT_MATRIX, Ngs2VoicePortMatrixParam{{}, 0, 0}); Event(sampler, SCE_NGS2_VOICE_EVENT_PLAY); - std::vector out(Grain); - const Ngs2RenderBufferInfo info{out.data(), out.size() * sizeof(float), SCE_NGS2_WAVEFORM_TYPE_PCM_F32L, 1}; - std::uint32_t first = 0; - for (std::uint32_t grain = 0; grain < 48000 / Grain && first == 0; grain++) { + std::vector all; + std::vector out(256 * channels); + for (std::uint32_t grain = 0; grain * 256 < frames; grain++) { + if (static_cast(grain) == reverbCase.changeGrain) Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, reverbCase.change}); + const Ngs2RenderBufferInfo info{out.data(), out.size() * sizeof(float), SCE_NGS2_WAVEFORM_TYPE_PCM_F32L, channels}; Require(sceNgs2SystemRender(system, &info, 1) == SCE_NGS2_OK); - for (std::uint32_t i = 0; i < Grain && first == 0; i++) { - if (out[i] != 0.0f) first = grain * Grain + i; - } + all.insert(all.end(), out.begin(), out.end()); } Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); - return first; -} - -static void TestReverbDelays() { - const Ngs2ReverbI3DL2Param lateOnly{1.0f, 0.0f, 0, 0, 0, 1.0f, 1.0f, 0, 0.0f, 0, 0.05f, 100.0f, 100.0f, 5000.0f, {}}; - Require(FirstReverbSample(lateOnly) > 2400); - const Ngs2ReverbI3DL2Param withReflections{1.0f, 0.0f, 0, 0, 0, 1.0f, 1.0f, 0, 0.01f, 0, 0.05f, 100.0f, 100.0f, 5000.0f, {}}; - const auto first = FirstReverbSample(withReflections); - Require(first >= 480 && first < 600); -} - -static bool ReverbParamThrows(uintptr_t reverb, Ngs2ReverbI3DL2Param params) { - try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, params}); } catch (const std::invalid_argument&) { return true; } - return false; -} - -static float RenderPeak(uintptr_t system, std::vector& out, const Ngs2RenderBufferInfo& info) { - Require(sceNgs2SystemRender(system, &info, 1) == SCE_NGS2_OK); - float peak = 0.0f; - for (float sample : out) peak = std::max(peak, std::fabs(sample)); - return peak; -} - -static void TestReverbLifetime() { - const auto system = CreateSystem(); - const auto master = Mastering(system, 1); - const auto reverb = Voice(CreateRack(system, SCE_NGS2_RACK_ID_REVERB)); - bool flags = false; - try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 1, 1, 1, 0}); } catch (const std::runtime_error&) { flags = true; } - Require(flags); - Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 6, 6, 0, 0}); - Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 1, 1, 0, 0}); - const Ngs2ReverbI3DL2Param room{1.0f, 0.0f, 0, 0, 0, 0.1f, 1.0f, -10000, 0.0f, 0, 0.0f, 100.0f, 100.0f, 5000.0f, {}}; - auto bad = room; - bad.decay_time = 0.05f; - Require(ReverbParamThrows(reverb, bad)); - bad = room; - bad.wet = NAN; - Require(ReverbParamThrows(reverb, bad)); - bad = room; - bad.room = 100; - Require(ReverbParamThrows(reverb, bad)); - Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, room}); - Patch(reverb, master); - Event(reverb, SCE_NGS2_VOICE_EVENT_PLAY); - - std::vector pcm(Grain, 0); - pcm[0] = 32767; - const auto sampler = Sampler(system, pcm, 0); - Patch(sampler, reverb); - Event(sampler, SCE_NGS2_VOICE_EVENT_PLAY); - std::vector out(Grain); - const Ngs2RenderBufferInfo info{out.data(), out.size() * sizeof(float), SCE_NGS2_WAVEFORM_TYPE_PCM_F32L, 1}; - float before = 0.0f; - for (std::uint32_t grain = 0; grain < 2400 / Grain; grain++) before = std::max(before, RenderPeak(system, out, info)); - Require(before > 0.0f); - Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, room}); - float after = 0.0f; - for (std::uint32_t grain = 0; grain < 1200 / Grain; grain++) after = std::max(after, RenderPeak(system, out, info)); - Require(after > 0.0f); - - Event(reverb, SCE_NGS2_VOICE_EVENT_KILL); - Event(reverb, SCE_NGS2_VOICE_EVENT_PLAY); - Require(RenderPeak(system, out, info) == 0.0f); - - const auto second = Sampler(system, pcm, 0); - Patch(second, reverb); - Event(second, SCE_NGS2_VOICE_EVENT_PLAY); - float peak = 0.0f; - for (std::uint32_t grain = 0; grain < 4800 / Grain; grain++) peak = std::max(peak, RenderPeak(system, out, info)); - Require(peak > 0.0f); - for (std::uint32_t grain = 0; grain < 48000 / Grain; grain++) RenderPeak(system, out, info); - Require(RenderPeak(system, out, info) == 0.0f); - Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); + usedBuffers = firstBuffer; + return all; } static void TestReverb() { + const Ngs2ReverbI3DL2Param cave{1.0f, 0.0f, -1000, 0, 0, 2.91f, 1.3f, -602, 0.015f, -302, 0.022f, 100.0f, 100.0f, 5000.0f, {}}; + auto changed = cave; + changed.reflection_pattern = 5; + changed.decay_time = 0.7f; + changed.decay_hf_ratio = 0.6f; + changed.reflections_delay = 0.004f; + changed.reverb_delay = 0.03f; + changed.room_hf = -900; + changed.wet = 0.6f; + changed.dry = 0.4f; + auto denser = cave; + denser.density = 99.999f; + const std::vector cases = { + {2, 1.0f, false, cave, -1, {}, {{100, {0, 0}}, {723, {0.0687032491f, 0.0687032491f}}, {1055, {0.0392590016f, 0.0392590016f}}, {1417, {0.0131517649f, 0.0131517649f}}, + {1779, {0.0098147504f, 0.0098147504f}}, {2500, {0, 0}}, {4000, {1.45101382e-07f, -1.15403088e-06f}}, {6000, {0.000655628915f, 3.03641864e-05f}}, + {9000, {0.000400578661f, -0.000684358703f}}, {12000, {0.000191548112f, 0.000220489033f}}, {16000, {0.000210488462f, 0.000146183695f}}, + {20000, {2.41150738e-05f, -0.000864534522f}}, {26000, {0.000125532999f, -0.000234790568f}}, {32000, {-0.000462158496f, 4.56995949e-05f}}}}, + {2, 1.0f, false, {1.0f, 0.0f, -500, -800, 3, 1.2f, 0.5f, -300, 0.01f, 0, 0.03f, 80.0f, 60.0f, 3000.0f, {}}, -1, {}, + {{100, {0, 0}}, {723, {0.000166983227f, 0.000166983227f}}, {1055, {-3.41919585e-05f, -3.41919585e-05f}}, {1417, {0, 0}}, {1779, {0, 0}}, + {2500, {0, 1.55222626e-08f}}, {4000, {-0.000106364969f, 9.90879998e-05f}}, {6000, {0.00013573296f, 7.22560799e-06f}}, + {9000, {-7.91876082e-05f, 0.000225067721f}}, {12000, {0.000150469248f, 0.00036288987f}}, {16000, {-7.86288219e-05f, -0.000195921995f}}, + {20000, {1.01910318e-05f, 5.79599109e-05f}}, {26000, {2.14232614e-06f, -2.05506512e-05f}}, {32000, {-4.31467561e-06f, 2.34100444e-05f}}}}, + {6, 1.0f, false, {1.0f, 0.2f, -200, -300, 12, 1.0f, 0.9f, -100, 0.03f, -100, 0.04f, 70.0f, 90.0f, 4000.0f, {}}, -1, {}, + {{6000, {-0.000167909253f, -0.000179917421f, -0.000647808949f, 0, 3.22720189e-05f, 0.000213803389f}}, + {9000, {8.47857882e-05f, 8.62322413e-05f, -0.000100753634f, 0, 0.000109430795f, -0.000122895101f}}, + {12000, {4.37544441e-05f, 3.58103216e-06f, -0.00013746327f, 0, 9.3237948e-05f, -0.00013529828f}}, + {16000, {8.63046807e-08f, 1.62214419e-05f, 8.89167641e-05f, 0, 1.02178528e-05f, 0.000201705057f}}, + {20000, {-9.6526619e-06f, -2.73812566e-05f, -1.92096536e-07f, 0, -0.000116049101f, 6.94858772e-06f}}, + {26000, {1.27750207e-06f, -1.59169917e-06f, 5.30674515e-05f, 0, -3.59285696e-05f, 5.16466662e-06f}}, + {32000, {3.5737969e-06f, 4.19655044e-06f, -2.30460955e-07f, 0, 1.16596939e-05f, 1.14889262e-05f}}}}, + {2, 1.0f, false, cave, 6, changed, {{723, {0.0687032491f, 0.0687032491f}}, {1055, {0.0392590016f, 0.0392590016f}}, {1417, {0.0131517649f, 0.0131517649f}}, + {1779, {4.38156894e-05f, 4.38156894e-05f}}, {2500, {0, 0}}, {4000, {-1.60370582e-05f, -4.44955149e-05f}}, {6000, {1.02755057e-05f, 8.34046841e-06f}}, + {9000, {1.1641363e-05f, -4.69685165e-06f}}, {12000, {3.57917179e-06f, -2.34550089e-06f}}, {16000, {-8.00212234e-08f, 3.19533115e-06f}}, + {20000, {5.66619519e-07f, 4.26969848e-07f}}, {26000, {1.46052287e-07f, -2.44572629e-08f}}, {32000, {-4.45668853e-08f, 1.51349102e-08f}}}}, + {2, 0.02f, false, {40.0f, -3.0f, 5000, 500, 20, 50.0f, 5.0f, 3000, 0.9f, 5000, 0.9f, 300.0f, 300.0f, 50000.0f, {}}, -1, {}, + {{14403, {0.877262115f, 0.877262115f}}, {14500, {0, 0}}, {15000, {0, 0}}, {15500, {0, 7.50572099e-07f}}, {16000, {4.95207075e-07f, 6.37215771e-06f}}, + {20000, {0.00482429564f, -0.00149071764f}}, {26000, {-0.0096597122f, 0.00718426611f}}, {32000, {0.0107425917f, -0.000738018774f}}}}, + {2, 1.0f, false, {1.0f, 0.3f, 0, -200, 15, 0.8f, 1.0f, 0, 0.002f, -500, 0.004f, 60.0f, 80.0f, 6000.0f, {}}, -1, {}, + {{60, {0, 0}}, {99, {0.0947210863f, 0.0947210863f}}, {100, {0.0472047739f, 0.0472047739f}}, {150, {0, 0}}, {200, {0, 0}}, {300, {0, 0}}, + {2000, {-9.86956729e-06f, 7.96923473e-07f}}, {3000, {-0.000152820328f, -4.59704825e-06f}}, {5000, {0.000203833857f, -0.000208990226f}}, + {8000, {-3.69777154e-05f, -0.000259475899f}}}}, + {2, 1.0f, true, cave, 6, changed, {{1540, {0.0222691782f, 0.0242232569f}}, {1600, {0.0201471522f, 0.0186609477f}}, {1700, {-0.0211425349f, -0.0235042125f}}, + {1780, {-0.0168117173f, 0.00417749817f}}, {1800, {-0.0866222829f, -0.0945812687f}}, {2000, {0.0131965755f, -0.0707511827f}}, + {2500, {0.0942443311f, -0.100531064f}}, {3500, {0.000985965831f, 0.000946713379f}}, {8000, {-0.000968122098f, 0.000556072395f}}, + {15000, {0.000227724231f, 0.000151095577f}}}}, + {2, 1.0f, true, cave, 20, denser, {{5200, {-0.00942458585f, -0.0064059021f}}, {6000, {0.00248076022f, 0.00317249796f}}, + {8000, {0.00102439919f, 0.00105210941f}}, {12000, {1.63059376e-05f, 6.10309735e-06f}}, {20000, {-1.08853078e-06f, -2.99162899e-08f}}}}, + }; + for (const auto& reverbCase : cases) { + const auto out = RenderReverb(reverbCase, 32256); + for (const auto& expected : reverbCase.expected) { + for (std::uint32_t c = 0; c < reverbCase.channels; c++) { + if (c == 3) continue; + const float value = out[expected.frame * reverbCase.channels + c]; + Require(std::fabs(value - expected.samples[c]) <= 2e-7f + std::fabs(expected.samples[c]) * 1e-5f); + } + } + } +} + +static void TestReverbSetup() { const auto system = CreateSystem(); const auto master = Mastering(system, 2); const auto reverb = Voice(CreateRack(system, SCE_NGS2_RACK_ID_REVERB)); + for (std::uint32_t channels : {3u, 4u, 5u, 7u}) { + bool rejected = false; + try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, channels, channels, 0, 0}); } catch (const std::invalid_argument&) { rejected = true; } + Require(rejected); + } bool conversion = false; try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 1, 2, 0, 0}); } catch (const std::runtime_error&) { conversion = true; } Require(conversion); + bool flags = false; + try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 2, 2, 1, 0}); } catch (const std::runtime_error&) { flags = true; } + Require(flags); Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_SETUP, Ngs2ReverbVoiceSetupParam{{}, 2, 2, 0, 0}); - const Ngs2ReverbI3DL2Param cave{1.0f, 0.0f, -1000, 0, 0, 2.91f, 1.3f, -602, 0.015f, -302, 0.022f, 100.0f, 100.0f, 5000.0f, {}}; - Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, cave}); + Ngs2ReverbI3DL2Param invalid{1.0f, 0.0f, 0, 0, 0, 1.0f, 1.0f, 0, 0.0f, 0, 0.0f, 100.0f, 100.0f, 5000.0f, {}}; + invalid.decay_time = NAN; + bool nan = false; + try { Control(reverb, SCE_NGS2_REVERB_VOICE_PARAM_I3DL2, Ngs2ReverbVoiceI3DL2Param{{}, invalid}); } catch (const std::invalid_argument&) { nan = true; } + Require(nan); Patch(reverb, master); Event(reverb, SCE_NGS2_VOICE_EVENT_PLAY); - Ngs2VoiceState state{}; - Require(sceNgs2VoiceGetState(reverb, &state, sizeof(state) + 1) == SCE_NGS2_ERROR_INVALID_OUT_SIZE); - Require(sceNgs2VoiceGetState(reverb, &state, sizeof(state)) == SCE_NGS2_OK && (state.state_flags & SCE_NGS2_VOICE_STATE_FLAG_INUSE) != 0); - - std::vector pcm(Grain, 0); - pcm[0] = 32767; + const std::vector pcm(Grain * 64, 16384); const auto sampler = Sampler(system, pcm, 0); - const float levels[2] = {1.0f, 1.0f}; - Control(sampler, SCE_NGS2_VOICE_PARAM_MATRIX_LEVELS, Ngs2VoiceMatrixLevelsParam{{}, 0, 2, levels}); - Control(sampler, SCE_NGS2_VOICE_PARAM_PORT_MATRIX, Ngs2VoicePortMatrixParam{{}, 0, 0}); Patch(sampler, reverb); Event(sampler, SCE_NGS2_VOICE_EVENT_PLAY); - std::vector out(Grain * 2); const Ngs2RenderBufferInfo info{out.data(), out.size() * sizeof(float), SCE_NGS2_WAVEFORM_TYPE_PCM_F32L, 2}; - Require(sceNgs2SystemRender(system, &info, 1) == SCE_NGS2_OK); - for (float sample : out) Require(sample == 0.0f); - float early = 0.0f; - float late = 0.0f; - float lastSecond = 0.0f; - bool stereo = false; - for (std::uint32_t grain = 1; grain < 2 * 48000 / Grain; grain++) { + float loudest = 0.0f; + for (std::uint32_t grain = 0; grain < 600; grain++) { Require(sceNgs2SystemRender(system, &info, 1) == SCE_NGS2_OK); - for (std::uint32_t i = 0; i < Grain; i++) { - const float left = out[i * 2]; - const float right = out[i * 2 + 1]; - Require(std::isfinite(left) && std::isfinite(right) && std::fabs(left) < 1.0f && std::fabs(right) < 1.0f); - if (left != right) stereo = true; - const float level = std::max(std::fabs(left), std::fabs(right)); - if (grain * Grain < 2400) early = std::max(early, level); - else if (grain * Grain < 48000) late = std::max(late, level); - else lastSecond = std::max(lastSecond, level); - } + for (float sample : out) loudest = std::max(loudest, std::fabs(sample)); } - Require(early > 0.0f && late > 0.0f && stereo); - Require(lastSecond < late * 0.5f); + Require(loudest > 0.0f && loudest < 1e-8f); + Ngs2VoiceState state{}; + Require(sceNgs2VoiceGetState(reverb, &state, sizeof(state)) == SCE_NGS2_OK && (state.state_flags & SCE_NGS2_VOICE_STATE_FLAG_INUSE) != 0); Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); } @@ -392,8 +400,7 @@ int main() { TestPitchAndRepeat(); TestSubmixerMatrix(); TestReverb(); - TestReverbDelays(); - TestReverbLifetime(); + TestReverbSetup(); TestSampleRate(); TestUserData(); TestLock(); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index d6c661817..8ded0ea70 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -107,7 +107,7 @@ Throughout the project, every function at every stage either **does exactly what - [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 flags (1 on the first process call after setup, 2 after a parameter change, 0 otherwise) 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. As in KytyPS5, process runs only on grains where an input produced samples, so an effect tail stops with the input and a pending flag 2 waits for the next audible grain -- [NGS2 reverb rack](../../core/libs/prx/libSceNgs2.native/src/Reverb.cpp) (libSceNgs2.native, rack 0x2001) - structure layouts from [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `ngs2_reverb.h` and the default options (8 channels, `reverb_size` 1) from [KytyPS5](https://github.com/KytyPS5/KytyPS5); the param ids 0x20010000 (setup) and 0x20010001 (I3DL2) are taken from PPSA22520. The console's algorithm is unknown, so the I3DL2 parameters drive an approximation, a Freeverb-style network (8 damped combs and 4 allpasses per channel, 4 early taps): `wet` and `dry` are linear factors, `room` scales the wet output, `room_hf` is a one-pole high cut at `hf_reference`, `decay_hf_ratio` damps the combs towards Nyquist and never raises high frequencies, a zero `reflections_delay` disables the early taps, and `density`, `reflection_pattern` and the rack's `reverb_size` are not used. I3DL2 values outside the standard ranges throw. Before any I3DL2 param the voice passes its mixed input through unchanged. Changing the params keeps the tail; a stop or kill clears it. Setup flags and differing input and output channel counts throw +- [NGS2 reverb rack](../../core/libs/prx/libSceNgs2.native/src/Reverb.cpp) (libSceNgs2.native, rack 0x2001) - follows the PS4 firmware 11.00 `libSceNgs2.sprx`, read and checked against its output at 11.025 to 48 kHz, including parameter changes during playback (differences at float rounding level); not yet compared with the PS5 library. Structure layouts come from [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `ngs2_reverb.h` and the param ids 0x20010000 (setup) and 0x20010001 (I3DL2) from PPSA22520. Only the default rack options are handled: their 14400-sample reflection buffer is used whatever `reverb_size` says, and the library's half-rate mode is not implemented. Setup only accepts equal input and output channel counts - [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.