diff --git a/core/libs/prx/libSceNgs2.native/Ngs2Library.cmake b/core/libs/prx/libSceNgs2.native/Ngs2Library.cmake index 453e4d6a1..bc9254bb0 100644 --- a/core/libs/prx/libSceNgs2.native/Ngs2Library.cmake +++ b/core/libs/prx/libSceNgs2.native/Ngs2Library.cmake @@ -5,6 +5,7 @@ function(add_sce_ngs2_library target) ${ngs2Dir}/src/Custom.cpp ${ngs2Dir}/src/Rack.cpp ${ngs2Dir}/src/Render.cpp + ${ngs2Dir}/src/Reverb.cpp ${ngs2Dir}/src/System.cpp ${ngs2Dir}/src/Unimplemented.cpp ${ngs2Dir}/src/Voice.cpp diff --git a/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp b/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp index 3496f022f..3c0d9ea6b 100644 --- a/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp +++ b/core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp @@ -17,6 +17,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_REVERB = 0x2001; static constexpr std::uint32_t SCE_NGS2_RACK_ID_MASTERING = 0x3000; static constexpr std::uint32_t SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER = 0x4002; @@ -50,6 +51,8 @@ static constexpr std::uint32_t SCE_NGS2_SAMPLER_VOICE_PARAM_PITCH = 0x10000005; 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_SUBMIXER_VOICE_PARAM_USER_FX = 0x20000004; +static constexpr std::uint32_t SCE_NGS2_REVERB_VOICE_PARAM_SETUP = 0x20010000; +static constexpr std::uint32_t SCE_NGS2_REVERB_VOICE_PARAM_I3DL2 = 0x20010001; static constexpr std::uint32_t SCE_NGS2_MASTERING_VOICE_PARAM_SETUP = 0x30000000; static constexpr std::uint32_t SCE_NGS2_MASTERING_VOICE_PARAM_GAIN = 0x30000004; static constexpr std::uint32_t SCE_NGS2_MASTERING_VOICE_PARAM_OUTPUT = 0x30000005; @@ -159,6 +162,12 @@ struct Ngs2MasteringRackOption { std::uint32_t num_peak_meter_blocks; }; +struct Ngs2ReverbRackOption { + Ngs2RackOption rack_option; + std::uint32_t max_channels; + std::uint32_t reverb_size; +}; + struct Ngs2CustomModuleOption { std::uint32_t size; }; @@ -421,6 +430,40 @@ struct Ngs2SubmixerVoiceSetupParam { }; static_assert(sizeof(Ngs2SubmixerVoiceSetupParam) == 16); +struct Ngs2ReverbVoiceSetupParam { + 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(Ngs2ReverbVoiceSetupParam) == 24); + +struct Ngs2ReverbI3DL2Param { + float wet; + float dry; + std::int32_t room; + std::int32_t room_hf; + std::uint32_t reflection_pattern; + float decay_time; + float decay_hf_ratio; + std::int32_t reflections; + float reflections_delay; + std::int32_t reverb; + float reverb_delay; + float diffusion; + float density; + float hf_reference; + std::uint32_t reserved[8]; +}; +static_assert(sizeof(Ngs2ReverbI3DL2Param) == 88); + +struct Ngs2ReverbVoiceI3DL2Param { + Ngs2VoiceParamHeader header; + Ngs2ReverbI3DL2Param i3dl2; +}; +static_assert(sizeof(Ngs2ReverbVoiceI3DL2Param) == 96); + struct Ngs2SubmixerVoiceUserFxParam { Ngs2VoiceParamHeader header; Ngs2UserFxProcessHandler handler; diff --git a/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp b/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp index ab1486207..b70a94949 100644 --- a/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp +++ b/core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp @@ -57,6 +57,12 @@ struct Ngs2Atrac9 { std::uint32_t windowStart = 0; }; +struct Ngs2ReverbState; + +struct Ngs2ReverbDeleter { + void operator()(Ngs2ReverbState* reverb) const; +}; + struct Ngs2Voice; struct Ngs2FilterHistory { @@ -98,6 +104,7 @@ struct Ngs2Voice { std::uint32_t sampleRate = 0; std::uint32_t waveformType = 0; Ngs2Atrac9 atrac9; + std::unique_ptr reverb; float pitch = 1.0f; std::uint64_t phase = 0; std::deque blocks; @@ -180,6 +187,10 @@ 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 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 Ngs2ProcessLegacyUserFx(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/Rack.cpp b/core/libs/prx/libSceNgs2.native/src/Rack.cpp index 04d922208..1acc40d6b 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; + Ngs2ReverbRackOption reverb; Ngs2CustomSubmixerRackOption customSubmixer; }; @@ -24,6 +25,7 @@ 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_REVERB: return sizeof(Ngs2ReverbRackOption); case SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER: return sizeof(Ngs2CustomSubmixerRackOption); default: throw std::runtime_error("NGS2: rack id " + Ngs2Hex(rackId) + " is not implemented"); } @@ -59,6 +61,10 @@ static RackOptions DefaultRackOption(std::uint32_t rackId) { options.submixer.max_inputs = 1; options.submixer.num_peak_meter_blocks = 8; break; + case SCE_NGS2_RACK_ID_REVERB: + options.reverb.max_channels = 8; + options.reverb.reverb_size = 1; + break; default: common.max_matrices = 0; common.max_ports = 0; @@ -72,6 +78,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_REVERB) return options.reverb.max_channels; if (rackId == SCE_NGS2_RACK_ID_CUSTOM_SUBMIXER) return options.customSubmixer.max_channels; return NGS2_MAX_CHANNELS; } @@ -87,6 +94,9 @@ static RackOptions CheckedRackOption(std::uint32_t rackId, const Ngs2RackOption* 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); + if (rackId == SCE_NGS2_RACK_ID_REVERB && options.reverb.reverb_size != 1) { + throw std::runtime_error("NGS2: reverb rack size " + std::to_string(options.reverb.reverb_size) + " is not implemented"); + } return options; } diff --git a/core/libs/prx/libSceNgs2.native/src/Render.cpp b/core/libs/prx/libSceNgs2.native/src/Render.cpp index b0133ed2f..dfdf4583c 100644 --- a/core/libs/prx/libSceNgs2.native/src/Render.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Render.cpp @@ -197,6 +197,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.rack->rackId == SCE_NGS2_RACK_ID_REVERB && voice.reverb) voice.hasSamples = Ngs2ProcessReverb(voice, grain, systemRate); Ngs2ProcessLegacyUserFx(voice, grain, systemRate); if (voice.hasSamples) Ngs2ProcessUserFx(voice, grain, systemRate); } diff --git a/core/libs/prx/libSceNgs2.native/src/Reverb.cpp b/core/libs/prx/libSceNgs2.native/src/Reverb.cpp new file mode 100644 index 000000000..131fb321f --- /dev/null +++ b/core/libs/prx/libSceNgs2.native/src/Reverb.cpp @@ -0,0 +1,384 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "prx/libc/include/General.hpp" +#include "Ngs2Internal.hpp" + +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 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 Ngs2ReverbTaps { + std::array delays{}; + std::array gains{}; +}; + +struct Ngs2Comb { + std::vector buffer; + std::uint32_t cursor = 0; + float feedback = 0.0f; + 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::uint32_t cursor = 0; +}; + +struct Ngs2ReverbChannel { + std::array allpasses; + float y1 = 0.0f; + float y2 = 0.0f; + float wet = 0.0f; + float dry = 0.0f; +}; + +struct Ngs2ReverbState { + Ngs2ReverbI3DL2Param params{}; + bool reset = true; + bool changed = true; + std::uint32_t sampleRate = 0; + std::vector ring; + std::uint32_t ringCursor = 0; + Ngs2ReverbTaps taps; + float earlyGain = 0.0f; + float lateGain = 0.0f; + 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 Millibels(float millibels) { + if (millibels < -10000.0f) return 0.0f; + return static_cast(std::pow(10.0, static_cast(millibels * 0.0005f))); +} + +static float Clamp(float value, float low, float high) { + return value < low ? low : std::min(high, value); +} + +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 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 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]; + } + 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; +} + +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 signedLength = static_cast(Samples(ALLPASS_TIMES[stage], rate)) + ALLPASS_OFFSETS[channel][stage]; + if (signedLength < 1) throw std::runtime_error("NGS2: reverb at a sample rate of " + std::to_string(reverb.sampleRate) + " Hz is not implemented"); + const auto length = static_cast(signedLength); + 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; + const float rate = static_cast(sampleRate); + const std::uint32_t channels = voice.channels; + if (reverb.sampleRate != sampleRate) { + reverb.sampleRate = sampleRate; + reverb.reset = 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 < 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 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 69de14213..acd3003b0 100644 --- a/core/libs/prx/libSceNgs2.native/src/Voice.cpp +++ b/core/libs/prx/libSceNgs2.native/src/Voice.cpp @@ -35,6 +35,7 @@ void Ngs2Voice::SetEvent(std::uint32_t eventId) { case SCE_NGS2_VOICE_EVENT_STOP_IMM: case SCE_NGS2_VOICE_EVENT_KILL: state = Ngs2PlayState::Empty; + Ngs2ClearReverb(*this); break; case SCE_NGS2_VOICE_EVENT_PAUSE: if (state == Ngs2PlayState::Playing) state = Ngs2PlayState::Paused; @@ -307,6 +308,21 @@ static void ApplyParam(Ngs2Voice& voice, const Ngs2VoiceParamHeader& param) { if (ParamAs(param).flags != 0) throw std::runtime_error("NGS2: submixer setup flags are not implemented"); SetupMixer(voice, ParamAs(param).num_io_channels); return; + case SCE_NGS2_RACK_ID_REVERB: + if (param.id == SCE_NGS2_REVERB_VOICE_PARAM_SETUP) { + 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) { + Ngs2SetReverbParams(voice, ParamAs(param).i3dl2); + return; + } + break; case SCE_NGS2_RACK_ID_MASTERING: if (param.id == SCE_NGS2_MASTERING_VOICE_PARAM_SETUP) { SetupMixer(voice, ParamAs(param).num_io_channels); @@ -389,6 +405,12 @@ int APS5_VABI sceNgs2VoiceGetState(uintptr_t voice_handle, Ngs2VoiceState* state sampler.waveform_data = voice.WaveformData(); return SCE_NGS2_OK; } + case SCE_NGS2_RACK_ID_REVERB: { + if (state_size != sizeof(Ngs2VoiceState)) return SCE_NGS2_ERROR_INVALID_OUT_SIZE; + *state = {}; + state->state_flags = voice.stateFlags; + return SCE_NGS2_OK; + } case SCE_NGS2_RACK_ID_SUBMIXER: { if (state_size != sizeof(Ngs2SubmixerVoiceState)) return SCE_NGS2_ERROR_INVALID_OUT_SIZE; auto& submixer = *reinterpret_cast(state); diff --git a/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp b/core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp index 4b691d1a8..eabf182c0 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 @@ -258,6 +259,185 @@ static std::int32_t APS5_VABI Release(Ngs2ContextBufferInfo* info) { return SCE_NGS2_OK; } +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(); + 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{{}, 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(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 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); + all.insert(all.end(), out.begin(), out.end()); + } + 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}); + 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); + const std::vector pcm(Grain * 64, 16384); + const auto sampler = Sampler(system, pcm, 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}; + float loudest = 0.0f; + for (std::uint32_t grain = 0; grain < 600; grain++) { + Require(sceNgs2SystemRender(system, &info, 1) == SCE_NGS2_OK); + for (float sample : out) loudest = std::max(loudest, std::fabs(sample)); + } + 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(sceNgs2SystemSetSampleRate(system, 7000) == SCE_NGS2_OK); + bool lowRate = false; + try { sceNgs2SystemRender(system, &info, 1); } catch (const std::runtime_error&) { lowRate = true; } + Require(lowRate); + Require(sceNgs2SystemDestroy(system, nullptr) == SCE_NGS2_OK); + + Ngs2ReverbRackOption option{}; + option.rack_option.size = sizeof(option); + option.rack_option.max_grain_samples = 512; + option.rack_option.max_voices = 1; + option.rack_option.max_input_delay_blocks = 1; + option.rack_option.max_matrices = 1; + option.rack_option.max_ports = 8; + option.max_channels = 8; + option.reverb_size = 1; + Ngs2ContextBufferInfo query{}; + Require(sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_REVERB, &option.rack_option, &query) == SCE_NGS2_OK); + for (std::uint32_t size : {0u, 2u}) { + option.reverb_size = size; + bool unsupported = false; + try { sceNgs2RackQueryBufferSize(SCE_NGS2_RACK_ID_REVERB, &option.rack_option, &query); } catch (const std::runtime_error&) { unsupported = true; } + Require(unsupported); + } +} + static void TestSampleRate() { const auto system = CreateSystem(); Require(sceNgs2SystemSetSampleRate(0x1234, 96000) == SCE_NGS2_ERROR_INVALID_SYSTEM_HANDLE); @@ -493,6 +673,8 @@ int main() { TestPcmBlockEnd(); TestPitchAndRepeat(); TestSubmixerMatrix(); + TestReverb(); + TestReverbSetup(); TestSampleRate(); TestUserData(); TestMasteringGain(); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index f4f234d93..fa1c4985f 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -195,6 +195,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceAjmBatchJobControl](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - initialize parameters are 8 bytes for ATRAC9, 12 for Opus and none for MP3, as one title passes them; other codecs and sizes, and an output other than the 8-byte result, throw - [sceAjmBatchJobControl](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) - SIDEBAND_GAPLESS_DECODE with RESET overwrites both counts as in [RPCSX](https://github.com/RPCSX/rpcsx); shadPS4 ignores a zero count when the other is nonzero. Without RESET they differ, so it throws - [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) - 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 `reverb_size` of 1 is handled, with its 14400-sample reflection buffer; other sizes and the library's half-rate mode throw. System sample rates below about 7.5 kHz, where an allpass delay would have no samples, throw. 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 - [sceHttpUriMerge](../../core/libs/prx/libSceHttp/src/Uri.cpp) (libSceHttp) - signature follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Net/Http.cs); validation, size formula, absolute/relative split and path join follow the disassembly-like body in [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp), not confirmed on a PS5 title or hardware. The base URL goes through our `sceHttpUriParse`, which removes dot segments from the base path, while the relative part is appended unswept; base query and fragment are dropped. The relative reference is only classified by its scheme and a leading `//`, and only references with `//` are validated; as in shadPS4, `//host/x` is returned unchanged instead of taking the base scheme, a reference with a scheme but no `//` (`mailto:x`) is joined onto the base directory, and `?q` and `#f` are appended to the base directory instead of the base path.