From f81425ec8ed913e269bcd20e49a4145dadd57223 Mon Sep 17 00:00:00 2001 From: Merijn Date: Tue, 6 Oct 2026 00:22:05 +0200 Subject: [PATCH 1/3] fix(audioout2): read grains at the next push and play them behind a cushion A push mixed the ports' buffers at once and queued the mix on SDL, and the queue level left out a 40 ms cushion that the SDL queue rarely exceeded, so titles saw an empty queue. PPSA12544's FMOD output fills a 256-frame buffer after pushing it, reuses it two pushes later, and its mixer thread can still be writing a buffer when the next push follows within microseconds: a grain boundary broke about every 5 ms (an audible crackle). A push now records the buffers the ports point at and reads the previous grain at the next push, 100 us after it. The mixed frames are played by the SDL device callback, which waits for a 40 ms cushion before it plays (again after running dry), and the queue level counts the unplayed grains beyond that cushion. Recording the four buffers at every push showed 27% of boundaries broken when read at the push, 59% when read two pushes later and none when read at the next push; the title's output went from 4-9% to none of its grain boundaries broken over 6,000 loud grains. AI-assisted: yes Co-Authored-By: Claude Opus 5.5 --- .../libSceAudioOut/src/AudioOut2Context.cpp | 130 +++++++++++------- .../libSceAudioOut/src/AudioOut2Internal.hpp | 15 +- .../prx/libSceAudioOut/src/AudioOut2Port.cpp | 34 +++-- docs/dev/TechnicalDebt.md | 1 + 4 files changed, 118 insertions(+), 62 deletions(-) diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp index fc6408d62..d82a9ae67 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp @@ -8,16 +8,19 @@ #include #include #include +#include #include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" #include "AudioOut2Internal.hpp" -// An AudioOut2 context is the hardware output queue: every push mixes the ports' current grain -// (num_grains samples) and appends it to a queue of queue_depth grains that plays in real time. The -// title paces its mixer on that queue (Demon's Souls polls GetQueueLevel and pushes non-blocking -// whenever a slot is free), so the level must follow the output clock: with an SDL device open the -// device's own queue is the hardware queue, otherwise a wall-clock model of it stands in. +// An AudioOut2 context is the hardware output queue: every push records the buffers the ports point +// at and appends that grain (num_grains samples) to a queue of queue_depth grains that plays in real +// time. The title paces its mixer on that queue (Demon's Souls polls GetQueueLevel and pushes +// non-blocking whenever a slot is free), so the level must follow the output clock. With an SDL device +// open, the device callback plays the mixed grains behind a cushion and the queue level counts the +// grains beyond that cushion; otherwise a wall-clock model of the queue stands in. A grain's buffers +// are read at the next push: PPSA12544 fills a buffer after pushing it and reuses it two pushes later. using Clock = std::chrono::steady_clock; @@ -25,15 +28,18 @@ static constexpr std::size_t CONTEXT_MEMORY = 0x10000; static constexpr std::uint32_t DEFAULT_MAX_PORTS = 16; // Pushes, polls and advances traced individually before the trace falls back to the per-second summary. static constexpr std::uint64_t CALL_TRACE_FULL = 16; -// Silence queued ahead of the first grain (and again whenever the SDL queue ran dry) so scheduling -// jitter of the pushing thread does not starve the device; the queue level reported to the title -// counts only what lies beyond it. +// Grains the device waits for before it starts playing (and again whenever it ran dry), so the +// pushing thread's scheduling jitter does not starve it; the pad device queues as much silence ahead +// of its first grain. static constexpr std::uint32_t CUSHION_MS = 40; -// The SDL queue is not allowed to run further ahead than this; grains beyond it are dropped. +// Grains the device has not played are not allowed to run further ahead than this; newer ones are dropped. static constexpr std::uint32_t MAX_QUEUED_MS = 250; // A blocking push on a full queue gives up after this long. static constexpr std::chrono::milliseconds FULL_WAIT_TIMEOUT{200}; static constexpr std::chrono::milliseconds FULL_WAIT_STEP{1}; +// How long a push waits before it reads the previous grain's buffers: PPSA12544's mixer thread can +// still be writing them when the next push follows within microseconds. +static constexpr std::chrono::microseconds READ_DELAY{100}; static constexpr std::uint16_t DEVICE_SAMPLES = 512; // The summed ports (a 7.1 bed folded to stereo plus the object ports) peak above full scale in the // intro cutscene (2.1 measured); the device takes float and clips hard, so the mix is attenuated @@ -66,8 +72,17 @@ static std::uint32_t GrainBytes(const AudioOut2Context& context) { return static_cast(context.grain * OUTPUT_FRAME_BYTES); } -static std::uint32_t SdlQueuedMs(const AudioOut2Context& context) { - return context.device ? SDL_GetQueuedAudioSize(context.device) / OUTPUT_BYTES_PER_MS : 0; +static std::uint32_t CushionGrains(const AudioOut2Context& context) { + return (CUSHION_MS * AUDIO_OUT2_SAMPLE_RATE / 1000 + context.grain - 1) / context.grain; +} + +static std::uint32_t UnplayedGrains(const AudioOut2Context& context) { + const auto frames = context.output.size() / AUDIO_OUT2_OUTPUT_CHANNELS; + return static_cast(context.pendingGrains.size() + (frames + context.grain - 1) / context.grain); +} + +static std::uint32_t PendingMs(const AudioOut2Context& context) { + return static_cast(static_cast(UnplayedGrains(context)) * context.grain * 1000 / AUDIO_OUT2_SAMPLE_RATE); } // Retires the modelled grains whose playback finished by now. The caller holds the context lock. @@ -85,13 +100,13 @@ static void Drain(AudioOut2Context& context, Clock::time_point now) { static std::uint32_t QueueLevel(AudioOut2Context& context, Clock::time_point now) { Drain(context, now); if (context.device == 0) return context.queued; - const auto queuedBytes = SDL_GetQueuedAudioSize(context.device); - const auto cushionBytes = CUSHION_MS * OUTPUT_BYTES_PER_MS; - const auto pending = queuedBytes > cushionBytes ? queuedBytes - cushionBytes : 0; - const auto grainBytes = GrainBytes(context); - return std::min(context.queueDepth, (pending + grainBytes - 1) / grainBytes); + const auto unplayed = UnplayedGrains(context); + const auto cushion = CushionGrains(context); + return unplayed > cushion ? std::min(context.queueDepth, unplayed - cushion) : 0; } +static void DeviceCallback(void* userdata, Uint8* stream, int length); + static void OpenDevice(AudioOut2Context& context) { if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { AUDIOOUT2_TRACE("SDL audio init failed: %s\n", SDL_GetError()); @@ -102,10 +117,11 @@ static void OpenDevice(AudioOut2Context& context) { desired.format = AUDIO_F32SYS; desired.channels = AUDIO_OUT2_OUTPUT_CHANNELS; desired.samples = DEVICE_SAMPLES; - desired.callback = nullptr; + desired.callback = DeviceCallback; + desired.userdata = &context; SDL_AudioSpec obtained{}; - // No format change is allowed: SDL converts to the device's native format itself, so the queue - // always takes stereo float at 48 kHz. + // No format change is allowed: SDL converts to the device's native format itself, so the callback + // always writes stereo float at 48 kHz. context.device = SDL_OpenAudioDevice(nullptr, 0, &desired, &obtained, 0); if (context.device == 0) { AUDIOOUT2_TRACE("SDL_OpenAudioDevice failed: %s\n", SDL_GetError()); @@ -115,12 +131,8 @@ static void OpenDevice(AudioOut2Context& context) { AUDIOOUT2_TRACE("SDL device %u opened: %d Hz, format 0x%x, %u channels, %u samples\n", context.device, obtained.freq, obtained.format, obtained.channels, obtained.samples); } -static void CloseDevice(AudioOut2Context& context) { - if (context.device != 0 && SDL_WasInit(SDL_INIT_AUDIO) != 0) { - SDL_ClearQueuedAudio(context.device); - SDL_CloseAudioDevice(context.device); - } - context.device = 0; +static void CloseDevice(SDL_AudioDeviceID device) { + if (device != 0 && SDL_WasInit(SDL_INIT_AUDIO) != 0) SDL_CloseAudioDevice(device); } static void OpenPadDevice(AudioOut2Context& context) { @@ -178,7 +190,7 @@ static void QueuePadGrain(AudioOut2Context& context) { const auto frameBytes = static_cast(context.padLayout.channels * sizeof(float)); const auto grainBytes = context.grain * frameBytes; const auto cushionBytes = CUSHION_MS * (AUDIO_OUT2_SAMPLE_RATE / 1000) * frameBytes; - const auto mainQueuedBytes = context.device != 0 ? SDL_GetQueuedAudioSize(context.device) / OUTPUT_FRAME_BYTES * frameBytes : cushionBytes + context.queueDepth * grainBytes; + const auto mainQueuedBytes = cushionBytes + context.queueDepth * grainBytes; const auto queuedBytes = SDL_GetQueuedAudioSize(context.padDevice); if (queuedBytes > mainQueuedBytes + PAD_SLACK_GRAINS * grainBytes) return; if (queuedBytes == 0) { @@ -188,15 +200,14 @@ static void QueuePadGrain(AudioOut2Context& context) { SDL_QueueAudio(context.padDevice, context.padFrames.data(), grainBytes); } -static std::uint32_t Render(AudioOut2Context& context, Clock::time_point now) { - UpdatePadDevice(context, now); +static void Render(AudioOut2Context& context, const AudioOut2Grain& grain) { float* pad = nullptr; if (context.padDevice != 0) { std::fill(context.padMix.begin(), context.padMix.end(), 0.0f); pad = context.padMix.data(); } std::fill(context.mix.begin(), context.mix.end(), 0.0f); - const auto mixed = AudioOut2MixPorts(context, context.mix.data(), pad, context.grain); + AudioOut2MixPorts(context, grain, context.mix.data(), pad, context.grain); for (float& sample : context.mix) { sample = std::clamp(sample * MASTER_GAIN, -1.0f, 1.0f); if (AudioOut2TraceEnabled()) context.summaryPeak = std::max(context.summaryPeak, std::abs(sample)); @@ -205,19 +216,23 @@ static std::uint32_t Render(AudioOut2Context& context, Clock::time_point now) { AudioOut2FinishPadMix(pad, context.grain); QueuePadGrain(context); } - if (context.device == 0) return mixed; - const auto queuedBytes = SDL_GetQueuedAudioSize(context.device); - if (queuedBytes > MAX_QUEUED_MS * OUTPUT_BYTES_PER_MS) { - context.dropped++; - return mixed; - } - if (queuedBytes == 0) { - static const std::vector silence(static_cast(CUSHION_MS) * AUDIO_OUT2_SAMPLE_RATE / 1000 * AUDIO_OUT2_OUTPUT_CHANNELS, 0.0f); +} + +static void DeviceCallback(void* userdata, Uint8* stream, int length) { + auto& context = *static_cast(userdata); + std::lock_guard lock(context.lock); + auto* out = reinterpret_cast(stream); + const auto samples = static_cast(length) / sizeof(float); + const auto cushionSamples = static_cast(CushionGrains(context)) * context.grain * AUDIO_OUT2_OUTPUT_CHANNELS; + if (context.priming && context.output.size() >= cushionSamples) context.priming = false; + const auto count = context.priming ? 0 : std::min(samples, context.output.size()); + std::copy_n(context.output.begin(), count, out); + context.output.erase(context.output.begin(), context.output.begin() + static_cast(count)); + std::fill(out + count, out + samples, 0.0f); + if (count < samples && !context.priming) { + context.priming = true; context.primes++; - SDL_QueueAudio(context.device, silence.data(), static_cast(silence.size() * sizeof(float))); } - SDL_QueueAudio(context.device, context.mix.data(), GrainBytes(context)); - return mixed; } static void TraceSummary(AudioOut2Context& context, Clock::time_point now) { @@ -228,9 +243,9 @@ static void TraceSummary(AudioOut2Context& context, Clock::time_point now) { } const auto elapsed = std::chrono::duration(now - context.summaryStart).count(); if (elapsed < 1.0) return; - std::fprintf(stderr, "[audioout2] t=%.3f ctx %p: %.1f pushes/s, %.1f advances/s, %.1f queue polls/s, hw queue %u/%u, sdl queue %u ms, mix peak %.3f; totals: pushes %llu (%llu blocking, %llu queue-full rejects), primes %llu, dropped %llu\n", + std::fprintf(stderr, "[audioout2] t=%.3f ctx %p: %.1f pushes/s, %.1f advances/s, %.1f queue polls/s, hw queue %u/%u, unread %u ms, mix peak %.3f; totals: pushes %llu (%llu blocking, %llu queue-full rejects), primes %llu, dropped %llu\n", AudioOut2TraceSeconds(), static_cast(&context), static_cast(context.summaryPushes) / elapsed, static_cast(context.summaryAdvances) / elapsed, - static_cast(context.summaryPolls) / elapsed, QueueLevel(context, now), context.queueDepth, SdlQueuedMs(context), static_cast(context.summaryPeak), + static_cast(context.summaryPolls) / elapsed, QueueLevel(context, now), context.queueDepth, PendingMs(context), static_cast(context.summaryPeak), static_cast(context.pushes), static_cast(context.blockingPushes), static_cast(context.fullRejects), static_cast(context.primes), static_cast(context.dropped)); context.summaryStart = now; @@ -242,8 +257,8 @@ static void TraceSummary(AudioOut2Context& context, Clock::time_point now) { extern "C" { -// The title's per-tick step between setting the ports' data and pushing. The ports are mixed at push -// time from the buffers they currently point at, and the push paces the clock, so nothing is due here. +// The title's per-tick step between setting the ports' data and pushing. A push records the buffers the +// ports point at and the next push reads them, and the push paces the clock, so nothing is due here. int APS5_VABI sceAudioOut2ContextAdvance(AudioOut2ContextHandle ctx) { auto* context = FromHandle(ctx); if (!context) return SCE_AUDIO_OUT2_ERROR_INVALID_HANDLE; @@ -273,11 +288,13 @@ int APS5_VABI sceAudioOut2ContextDestroy(AudioOut2ContextHandle ctx) { auto* context = FromHandle(ctx); if (!context) return SCE_AUDIO_OUT2_ERROR_INVALID_HANDLE; AUDIOOUT2_TRACE("t=%.3f ContextDestroy ctx %p after %llu pushes\n", AudioOut2TraceSeconds(), static_cast(context), static_cast(context->pushes)); + SDL_AudioDeviceID device = 0; { std::lock_guard lock(context->lock); - CloseDevice(*context); + device = std::exchange(context->device, 0); ClosePadDevice(*context); } + CloseDevice(device); AudioOut2ReleasePorts(*context); delete context; return 0; @@ -316,13 +333,30 @@ int APS5_VABI sceAudioOut2ContextPush(AudioOut2ContextHandle ctx, uint32_t block } context->queued++; context->playHead += GrainDuration(*context); - const auto mixed = Render(*context, now); + context->pendingGrains.push_back(AudioOut2CaptureGrain(*context)); + UpdatePadDevice(*context, now); + const auto mixed = static_cast(context->pendingGrains.back().size()); + const std::size_t unread = context->device == 0 ? context->queueDepth : 2; + if (context->pendingGrains.size() >= unread) { + const auto readAt = Clock::now() + READ_DELAY; + while (Clock::now() < readAt) std::this_thread::yield(); + } + while (context->pendingGrains.size() >= unread) { + Render(*context, context->pendingGrains.front()); + context->pendingGrains.pop_front(); + if (context->device == 0) continue; + if (PendingMs(*context) > MAX_QUEUED_MS) { + context->dropped++; + continue; + } + context->output.insert(context->output.end(), context->mix.begin(), context->mix.end()); + } context->pushes++; context->summaryPushes++; if (blocking) context->blockingPushes++; if (context->pushes <= CALL_TRACE_FULL) { - AUDIOOUT2_TRACE("t=%.3f Push ctx %p blocking=%u: grain from %u ports, hw queue %u/%u, sdl queue %u ms\n", AudioOut2TraceSeconds(), static_cast(context), blocking, - mixed, QueueLevel(*context, now), context->queueDepth, SdlQueuedMs(*context)); + AUDIOOUT2_TRACE("t=%.3f Push ctx %p blocking=%u: grain from %u ports, hw queue %u/%u, unread %u ms\n", AudioOut2TraceSeconds(), static_cast(context), blocking, + mixed, QueueLevel(*context, now), context->queueDepth, PendingMs(*context)); } TraceSummary(*context, now); return 0; diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp index 57f75a165..f5e38e833 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include #include #include "SDL.h" @@ -41,6 +43,8 @@ static constexpr std::uint32_t AUDIO_OUT2_PORT_CHANNELS_MAX = 12; struct AudioOut2Context; struct AudioOut2StereoFold; +using AudioOut2Grain = std::vector>; + struct AudioOut2Port { bool used = false; AudioOut2Context* context = nullptr; @@ -63,12 +67,16 @@ struct AudioOut2Context { std::uint32_t queueDepth = 1; // Fallback hardware queue model for a context without an SDL device: pushes that have not // finished playing, and the time the last of them finishes; playback is real time, so the queue - // drains as the wall clock advances. With a device the SDL queue is the hardware queue. + // drains as the wall clock advances. With a device the unplayed grains are the hardware queue. std::uint32_t queued = 0; std::chrono::steady_clock::time_point playHead; SDL_AudioDeviceID device = 0; - // Stereo float mix of the ports for one push. + // Stereo float mix of the ports for one grain. std::vector mix; + // Mixed stereo frames the device has not played yet; it plays silence until the cushion is mixed. + std::deque output; + bool priming = true; + std::deque pendingGrains; SDL_AudioDeviceID padDevice = 0; std::chrono::steady_clock::time_point nextPadProbe; AudioOut2PadLayout padLayout; @@ -92,7 +100,8 @@ struct AudioOut2Context { // Mixes every port of the context that carries PCM data into out (stereo float, frames frames), summing // onto the zeroed buffer. Returns the number of ports mixed. -std::uint32_t AudioOut2MixPorts(const AudioOut2Context& context, float* out, float* padOut, std::uint32_t frames); +std::uint32_t AudioOut2MixPorts(const AudioOut2Context& context, const AudioOut2Grain& grain, float* out, float* padOut, std::uint32_t frames); +AudioOut2Grain AudioOut2CaptureGrain(const AudioOut2Context& context); bool AudioOut2HasPadPorts(const AudioOut2Context& context); // Forgets the ports of a context being destroyed. void AudioOut2ReleasePorts(const AudioOut2Context& context); diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp index 1eda180e4..fa8b8aba9 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp @@ -62,26 +62,26 @@ static const AudioOut2StereoFold* StereoFoldFor(std::uint32_t channels) { return found == std::end(STEREO_FOLDS) ? nullptr : found; } -static void ReadFrame(const AudioOut2Port& port, std::uint32_t frame, float* in) { +static void ReadFrame(const AudioOut2Port& port, const void* data, std::uint32_t frame, float* in) { const auto first = static_cast(frame) * port.channels; for (std::uint32_t c = 0; c < port.channels; c++) { - in[c] = port.int16 ? static_cast(port.data)[first + c] / 32768.0f : static_cast(port.data)[first + c]; + in[c] = port.int16 ? static_cast(data)[first + c] / 32768.0f : static_cast(data)[first + c]; } } -static void AccumulatePadPort(const AudioOut2Port& port, AudioOut2Route route, float* out, std::uint32_t frames) { +static void AccumulatePadPort(const AudioOut2Port& port, const void* data, AudioOut2Route route, float* out, std::uint32_t frames) { float in[AUDIO_OUT2_PORT_CHANNELS_MAX]; for (std::uint32_t frame = 0; frame < frames; frame++) { - ReadFrame(port, frame, in); + ReadFrame(port, data, frame, in); AudioOut2AccumulatePadFrame(route, in, port.channels, port.volume, out + static_cast(frame) * AUDIO_OUT2_PAD_CHANNELS); } } -static void AccumulatePort(const AudioOut2Port& port, float* out, std::uint32_t frames) { +static void AccumulatePort(const AudioOut2Port& port, const void* data, float* out, std::uint32_t frames) { const auto& fold = *port.fold; float in[AUDIO_OUT2_PORT_CHANNELS_MAX]; for (std::uint32_t frame = 0; frame < frames; frame++) { - ReadFrame(port, frame, in); + ReadFrame(port, data, frame, in); float left = 0.0f; float right = 0.0f; for (std::uint32_t c = 0; c < port.channels; c++) { @@ -94,14 +94,26 @@ static void AccumulatePort(const AudioOut2Port& port, float* out, std::uint32_t } } -std::uint32_t AudioOut2MixPorts(const AudioOut2Context& context, float* out, float* padOut, std::uint32_t frames) { +AudioOut2Grain AudioOut2CaptureGrain(const AudioOut2Context& context) { + std::lock_guard lock(g_portsLock); + AudioOut2Grain grain; + for (std::size_t index = 0; index < g_ports.size(); index++) { + const auto& port = g_ports[index]; + if (port.used && port.context == &context && port.data != nullptr && port.channels != 0) grain.emplace_back(index, port.data); + } + return grain; +} + +std::uint32_t AudioOut2MixPorts(const AudioOut2Context& context, const AudioOut2Grain& grain, float* out, float* padOut, std::uint32_t frames) { std::lock_guard lock(g_portsLock); std::uint32_t mixed = 0; - for (const auto& port : g_ports) { - if (!port.used || port.context != &context || port.data == nullptr || port.channels == 0) continue; + for (const auto& [index, data] : grain) { + if (index >= g_ports.size()) continue; + const auto& port = g_ports[index]; + if (!port.used || port.context != &context || port.channels == 0) continue; const auto route = padOut != nullptr ? AudioOut2RouteForPort(port.type, port.channels) : AudioOut2Route::Main; - if (route == AudioOut2Route::Main) AccumulatePort(port, out, frames); - else AccumulatePadPort(port, route, padOut, frames); + if (route == AudioOut2Route::Main) AccumulatePort(port, data, out, frames); + else AccumulatePadPort(port, data, route, padOut, frames); mixed++; } return mixed; diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 4e605cf32..6a9221f01 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -156,6 +156,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceKeyboardGetKey2Char](../../core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp) (libSceKeyboard) translates only the 101-key (US) arrangement and throws for the 106-key (Japanese) one; Ctrl and Alt do not change the character. - [sceAudioOut2Set3DLatency](../../core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp) and [sceAudioOut2MasteringInit](../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) have no effect: there is no 3D rendering or mastering stage. - [sceImeKeyboardGetInfo](../../core/libs/prx/libSceIme/Export.cpp) (libSceIme) returns `0x80bc0023` (no resource id) for every id while a keyboard is open, since the IME keyboard never reports a connected device. [sceImeKeyboardSetMode](../../core/libs/prx/libSceIme/Export.cpp) validates the mode bits but the mode has no effect, as no IME keyboard events are delivered. +- [AudioOut2 contexts](../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) read a grain's port buffers at the next push, 100 us after it, and play the mix behind a 40 ms cushion that the queue level leaves out. Both follow PPSA12544's FMOD output, measured by recording its four 256-frame buffers at every push: it fills a buffer after pushing it, reuses it two pushes later, and its mixer thread can still be writing when the next push follows within microseconds. Reading at the push, or two pushes later, breaks 27% or 59% of grain boundaries. When the hardware reads the buffers is not known. - [libSceAudiodec](../../core/libs/prx/libSceAudiodec/Export.cpp) throws for the 24-bit PCM word size (`iBwPcm` 0): its sample layout is unknown. ATRAC9 decoding is covered only by configuration and error tests, as no ATRAC9 encoder is available for a fixture. - [sceAjmDecMp3ParseFrame](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) throws when the original file length is requested (`parse_ofl` non-zero) for a layer other than III, for a CRC-protected frame, or for a VBRI header outside MPEG-1 stereo: shadPS4 looks for the tag after the CRC and after the side information, while LAME and FFmpeg place it without the CRC and VBRI always at byte 36. - The [output modifier](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) (`mul:2`, `mul:4`, `div:2`) follows the hardware with the IEEE mode off and f32 denormals flushed, the LLVM graphics default; the shader's MODE register is not read, and the hardware ignores the modifier in the other modes. On f16 results it is ignored, as on the hardware while f16 denormals are enabled; with them flushed the hardware applies it. From 9a8a85ebd02cfb0618010b28a4f704925ca37d43 Mon Sep 17 00:00:00 2001 From: Merijn Date: Wed, 7 Oct 2026 21:20:27 +0200 Subject: [PATCH 2/3] test(audioout): cover AudioOut2 grain timing with the SDL disk driver Address review: read a grain's buffers at the next push without spinning under the context lock, keep the no-device model mixing at push time, size the pad queue from the unplayed main grains again, use generic names in comments and TechnicalDebt, and add audio_out2_timing (next-push read, queue level beyond the cushion, priming after running dry). Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAudioOut/CMakeLists.txt | 9 + .../libSceAudioOut/src/AudioOut2Context.cpp | 55 +++--- .../libSceAudioOut/src/AudioOut2Internal.hpp | 1 + .../libSceAudioOut/tests/AudioOut2Timing.cpp | 172 ++++++++++++++++++ docs/dev/TechnicalDebt.md | 2 +- 5 files changed, 213 insertions(+), 26 deletions(-) create mode 100644 core/libs/prx/libSceAudioOut/tests/AudioOut2Timing.cpp diff --git a/core/libs/prx/libSceAudioOut/CMakeLists.txt b/core/libs/prx/libSceAudioOut/CMakeLists.txt index e5e5a0685..144dab991 100644 --- a/core/libs/prx/libSceAudioOut/CMakeLists.txt +++ b/core/libs/prx/libSceAudioOut/CMakeLists.txt @@ -72,4 +72,13 @@ if(BUILD_TESTING) set_tests_properties(audio_out2_port_layouts PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(audio_out2_timing_tests tests/AudioOut2Timing.cpp) + target_include_directories(audio_out2_timing_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(audio_out2_timing_tests PRIVATE libSceAudioOut libc) + configure_windows_unwind(audio_out2_timing_tests) + add_test(NAME audio_out2_timing COMMAND audio_out2_timing_tests) + if(WIN32) + set_tests_properties(audio_out2_timing PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() endif() diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp index d82a9ae67..9dc8f44d6 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp @@ -14,13 +14,12 @@ #include "prx/libc/include/General.hpp" #include "AudioOut2Internal.hpp" -// An AudioOut2 context is the hardware output queue: every push records the buffers the ports point -// at and appends that grain (num_grains samples) to a queue of queue_depth grains that plays in real -// time. The title paces its mixer on that queue (Demon's Souls polls GetQueueLevel and pushes -// non-blocking whenever a slot is free), so the level must follow the output clock. With an SDL device -// open, the device callback plays the mixed grains behind a cushion and the queue level counts the -// grains beyond that cushion; otherwise a wall-clock model of the queue stands in. A grain's buffers -// are read at the next push: PPSA12544 fills a buffer after pushing it and reuses it two pushes later. +// An AudioOut2 context is the hardware output queue: every push appends a grain (num_grains samples) +// to a queue of queue_depth grains that plays in real time. Titles pace their mixer on that queue, so +// the level must follow the output clock. With an SDL device open, a grain's port buffers are read at +// the next push, the device callback plays the mixed grains behind a cushion and the level counts the +// grains beyond that cushion; otherwise each push is mixed at once and a wall-clock model of the +// queue stands in. using Clock = std::chrono::steady_clock; @@ -37,8 +36,6 @@ static constexpr std::uint32_t MAX_QUEUED_MS = 250; // A blocking push on a full queue gives up after this long. static constexpr std::chrono::milliseconds FULL_WAIT_TIMEOUT{200}; static constexpr std::chrono::milliseconds FULL_WAIT_STEP{1}; -// How long a push waits before it reads the previous grain's buffers: PPSA12544's mixer thread can -// still be writing them when the next push follows within microseconds. static constexpr std::chrono::microseconds READ_DELAY{100}; static constexpr std::uint16_t DEVICE_SAMPLES = 512; // The summed ports (a 7.1 bed folded to stereo plus the object ports) peak above full scale in the @@ -190,7 +187,7 @@ static void QueuePadGrain(AudioOut2Context& context) { const auto frameBytes = static_cast(context.padLayout.channels * sizeof(float)); const auto grainBytes = context.grain * frameBytes; const auto cushionBytes = CUSHION_MS * (AUDIO_OUT2_SAMPLE_RATE / 1000) * frameBytes; - const auto mainQueuedBytes = cushionBytes + context.queueDepth * grainBytes; + const auto mainQueuedBytes = context.device != 0 ? UnplayedGrains(context) * grainBytes : cushionBytes + context.queueDepth * grainBytes; const auto queuedBytes = SDL_GetQueuedAudioSize(context.padDevice); if (queuedBytes > mainQueuedBytes + PAD_SLACK_GRAINS * grainBytes) return; if (queuedBytes == 0) { @@ -333,23 +330,31 @@ int APS5_VABI sceAudioOut2ContextPush(AudioOut2ContextHandle ctx, uint32_t block } context->queued++; context->playHead += GrainDuration(*context); - context->pendingGrains.push_back(AudioOut2CaptureGrain(*context)); UpdatePadDevice(*context, now); - const auto mixed = static_cast(context->pendingGrains.back().size()); - const std::size_t unread = context->device == 0 ? context->queueDepth : 2; - if (context->pendingGrains.size() >= unread) { - const auto readAt = Clock::now() + READ_DELAY; - while (Clock::now() < readAt) std::this_thread::yield(); - } - while (context->pendingGrains.size() >= unread) { - Render(*context, context->pendingGrains.front()); - context->pendingGrains.pop_front(); - if (context->device == 0) continue; - if (PendingMs(*context) > MAX_QUEUED_MS) { - context->dropped++; - continue; + auto grain = AudioOut2CaptureGrain(*context); + const auto mixed = static_cast(grain.size()); + if (context->device == 0) { + Render(*context, grain); + } else { + context->pendingGrains.push_back(std::move(grain)); + context->pendingSince.push_back(Clock::now()); + while (context->pendingGrains.size() >= 2) { + const auto readAt = context->pendingSince.front() + READ_DELAY; + if (Clock::now() < readAt) { + lock.unlock(); + while (Clock::now() < readAt) std::this_thread::yield(); + lock.lock(); + continue; + } + Render(*context, context->pendingGrains.front()); + context->pendingGrains.pop_front(); + context->pendingSince.pop_front(); + if (PendingMs(*context) > MAX_QUEUED_MS) { + context->dropped++; + continue; + } + context->output.insert(context->output.end(), context->mix.begin(), context->mix.end()); } - context->output.insert(context->output.end(), context->mix.begin(), context->mix.end()); } context->pushes++; context->summaryPushes++; diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp index f5e38e833..2b7a8aa0a 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp @@ -77,6 +77,7 @@ struct AudioOut2Context { std::deque output; bool priming = true; std::deque pendingGrains; + std::deque pendingSince; SDL_AudioDeviceID padDevice = 0; std::chrono::steady_clock::time_point nextPadProbe; AudioOut2PadLayout padLayout; diff --git a/core/libs/prx/libSceAudioOut/tests/AudioOut2Timing.cpp b/core/libs/prx/libSceAudioOut/tests/AudioOut2Timing.cpp new file mode 100644 index 000000000..14e3f1744 --- /dev/null +++ b/core/libs/prx/libSceAudioOut/tests/AudioOut2Timing.cpp @@ -0,0 +1,172 @@ +#include "SceTypes.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI sceAudioOut2ContextResetParam(AudioOut2ContextParam*); +int APS5_VABI sceAudioOut2ContextCreate(const AudioOut2ContextParam*, void*, std::size_t, AudioOut2ContextHandle*); +int APS5_VABI sceAudioOut2ContextDestroy(AudioOut2ContextHandle); +int APS5_VABI sceAudioOut2ContextPush(AudioOut2ContextHandle, std::uint32_t); +int APS5_VABI sceAudioOut2ContextGetQueueLevel(AudioOut2ContextHandle, std::uint32_t*, std::uint32_t*); +int APS5_VABI sceAudioOut2PortCreate(AudioOut2ContextHandle, const AudioOut2PortParam*, AudioOut2PortHandle*); +int APS5_VABI sceAudioOut2PortDestroy(AudioOut2PortHandle); +int APS5_VABI sceAudioOut2PortSetAttributes(AudioOut2PortHandle, const AudioOut2Attribute*, std::uint32_t); +} + +static void Require(bool value) { if (!value) std::abort(); } + +static void SetEnvironment(const char* name, const std::string& value) { +#ifdef _WIN32 + _putenv_s(name, value.c_str()); +#else + setenv(name, value.c_str(), 1); +#endif +} + +namespace { + +constexpr std::uint32_t grain = 256; +constexpr std::uint32_t queueDepth = 2; +constexpr std::uint32_t cushionGrains = 8; +constexpr std::uint32_t frequency = 48000; +constexpr std::uint32_t formatStereoFloat = 2u << 8; +constexpr std::uint32_t attributeData = 0; +constexpr int queueFull = static_cast(0x80260507); +constexpr float masterGain = 0.5f; +constexpr float tolerance = 1e-5f; + +struct Session { + std::filesystem::path path; + AudioOut2ContextHandle context = 0; + AudioOut2PortHandle port = 0; + std::vector buffer = std::vector(static_cast(grain) * 2, 0.0f); + + explicit Session(const char* name) : path(std::filesystem::temp_directory_path() / name) { + std::filesystem::remove(path); + SetEnvironment("SDL_DISKAUDIOFILE", path.string()); + AudioOut2ContextParam params{}; + Require(sceAudioOut2ContextResetParam(¶ms) == 0); + params.num_grains = grain; + params.queue_depth = queueDepth; + Require(sceAudioOut2ContextCreate(¶ms, nullptr, 0, &context) == 0); + AudioOut2PortParam port{}; + port.port_type = 0; + port.data_format = formatStereoFloat; + port.sampling_freq = frequency; + Require(sceAudioOut2PortCreate(context, &port, &this->port) == 0); + } + + void Point(const void* data) { + const AudioOut2Attribute attribute{attributeData, 0, &data, sizeof(data)}; + Require(sceAudioOut2PortSetAttributes(port, &attribute, 1) == 0); + } + + void Fill(float value) { + for (float& sample : buffer) sample = value; + } + + std::vector Close() { + Require(sceAudioOut2PortDestroy(port) == 0); + Require(sceAudioOut2ContextDestroy(context) == 0); + std::ifstream file(path, std::ios::binary); + const std::vector bytes{std::istreambuf_iterator(file), std::istreambuf_iterator()}; + file.close(); + std::filesystem::remove(path); + Require(bytes.size() % sizeof(float) == 0); + const auto* samples = reinterpret_cast(bytes.data()); + return {samples, samples + bytes.size() / sizeof(float)}; + } +}; + +std::size_t FirstNonZero(const std::vector& played, std::size_t from = 0) { + while (from < played.size() && played[from] == 0.0f) from++; + return from; +} + +bool Constant(const std::vector& played, std::size_t first, std::size_t count, float value) { + if (first + count > played.size()) return false; + for (std::size_t index = first; index < first + count; index++) { + if (std::fabs(played[index] - value) > tolerance) return false; + } + return true; +} + +void TestReadAtNextPush() { + Session session("anyps5_audio_out2_next_push.raw"); + session.Fill(0.25f); + session.Point(session.buffer.data()); + Require(sceAudioOut2ContextPush(session.context, 1) == 0); + session.Fill(0.5f); + Require(sceAudioOut2ContextPush(session.context, 1) == 0); + session.Fill(0.75f); + Require(sceAudioOut2ContextPush(session.context, 1) == 0); + session.Fill(0.0f); + session.Point(nullptr); + for (std::uint32_t push = 0; push < cushionGrains * 4; push++) Require(sceAudioOut2ContextPush(session.context, 1) == 0); + const auto played = session.Close(); + const auto first = FirstNonZero(played); + const std::size_t samples = static_cast(grain) * 2; + Require(Constant(played, first, samples, 0.5f * masterGain)); + Require(Constant(played, first + samples, samples, 0.75f * masterGain)); + Require(FirstNonZero(played, first + 2 * samples) == played.size()); +} + +void TestLevelExcludesCushion() { + Session session("anyps5_audio_out2_level.raw"); + session.Point(nullptr); + std::uint32_t level = 99; + std::uint32_t available = 99; + Require(sceAudioOut2ContextGetQueueLevel(session.context, &level, &available) == 0); + Require(level == 0 && available == queueDepth); + std::uint32_t accepted = 0; + int result = 0; + while ((result = sceAudioOut2ContextPush(session.context, 0)) == 0) { + accepted++; + Require(accepted < 1000); + } + Require(result == queueFull); + Require(accepted >= cushionGrains + queueDepth); + session.Close(); +} + +void TestPrimingAfterRunningDry() { + Session session("anyps5_audio_out2_priming.raw"); + session.Fill(0.5f); + session.Point(session.buffer.data()); + for (std::uint32_t push = 0; push < 12; push++) Require(sceAudioOut2ContextPush(session.context, 1) == 0); + session.Point(nullptr); + Require(sceAudioOut2ContextPush(session.context, 1) == 0); + std::this_thread::sleep_for(std::chrono::milliseconds(300)); + session.Fill(-0.5f); + session.Point(session.buffer.data()); + for (std::uint32_t push = 0; push < 12; push++) Require(sceAudioOut2ContextPush(session.context, 1) == 0); + session.Point(nullptr); + for (std::uint32_t push = 0; push < cushionGrains * 4; push++) Require(sceAudioOut2ContextPush(session.context, 1) == 0); + const auto played = session.Close(); + const std::size_t burst = static_cast(grain) * 2 * 12; + const auto first = FirstNonZero(played); + Require(Constant(played, first, burst, 0.5f * masterGain)); + const auto second = FirstNonZero(played, first + burst); + Require(second < played.size() && second - (first + burst) >= static_cast(grain) * 2); + Require(Constant(played, second, burst, -0.5f * masterGain)); +} + +} + +int main() { + SetEnvironment("SDL_AUDIODRIVER", "disk"); + TestReadAtNextPush(); + TestLevelExcludesCushion(); + TestPrimingAfterRunningDry(); + return 0; +} diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index b8b01fa24..43239a6cc 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -249,7 +249,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceKeyboardGetKey2Char](../../core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp) (libSceKeyboard) translates the 101-key (US) arrangement and the alphanumeric layer of the 106-key (Japanese) one, which follows the JIS layout of the HID usages: the Yen (0x89) and Ro (0x87) keys return `\` (0x5C) unshifted, as JIS X 0201 places the yen sign there; whether the console returns U+00A5 instead is unverified. With the Kana LED (bit 4, the HID position) set the 106-key arrangement throws, as the kana the console returns are unknown. Ctrl and Alt do not change the character. - [sceAudioOut2Set3DLatency](../../core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp) and [sceAudioOut2MasteringInit](../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) have no effect: there is no 3D rendering or mastering stage. - [sceImeKeyboardGetInfo](../../core/libs/prx/libSceIme/Export.cpp) (libSceIme) returns `0x80bc0023` (no resource id) for every id while a keyboard is open, since the IME keyboard never reports a connected device. [sceImeKeyboardSetMode](../../core/libs/prx/libSceIme/Export.cpp) validates the mode bits but the mode has no effect, as no IME keyboard events are delivered. -- [AudioOut2 contexts](../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) read a grain's port buffers at the next push, 100 us after it, and play the mix behind a 40 ms cushion that the queue level leaves out. Both follow PPSA12544's FMOD output, measured by recording its four 256-frame buffers at every push: it fills a buffer after pushing it, reuses it two pushes later, and its mixer thread can still be writing when the next push follows within microseconds. Reading at the push, or two pushes later, breaks 27% or 59% of grain boundaries. When the hardware reads the buffers is not known. +- [AudioOut2 contexts](../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) with an SDL device read a grain's port buffers at the next push, at least 100 us after the grain was pushed, and play the mix behind a 40 ms cushion that the queue level leaves out. When the hardware reads the buffers is not known. The timing follows a title whose mixer fills a buffer after pushing it, reuses it two pushes later, and may still be writing it when the next push follows within microseconds - [libSceAudiodec](../../core/libs/prx/libSceAudiodec/Export.cpp) throws for the 24-bit PCM word size (`iBwPcm` 0): its sample layout is unknown. ATRAC9 decoding is covered only by configuration and error tests, as no ATRAC9 encoder is available for a fixture. - [sceAjmDecMp3ParseFrame](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) throws when the original file length is requested (`parse_ofl` non-zero) for a layer other than III, for a CRC-protected frame, or for a VBRI header outside MPEG-1 stereo: shadPS4 looks for the tag after the CRC and after the side information, while LAME and FFmpeg place it without the CRC and VBRI always at byte 36. - The [output modifier](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) (`mul:2`, `mul:4`, `div:2`) follows the hardware with the IEEE mode off and f32 denormals flushed, the LLVM graphics default; the shader's MODE register is not read, and the hardware ignores the modifier in the other modes. On f16 results it is ignored, as on the hardware while f16 denormals are enabled; with them flushed the hardware applies it. From cd7735ceab138600fcd345056217d0662a7cbbe9 Mon Sep 17 00:00:00 2001 From: Merijn Date: Wed, 7 Oct 2026 22:13:30 +0200 Subject: [PATCH 3/3] fix(audioout): drop captured buffers of a port whose slot was reused A captured grain stored only the port slot and data pointer, so a port created in a destroyed port's slot before the next push mixed the old pointer with its own format. Ports now carry a generation that the capture records. TechnicalDebt notes that the last grain pushed before a title stops is never played. Co-Authored-By: Claude Opus 5.5 --- .../libSceAudioOut/src/AudioOut2Internal.hpp | 9 ++++++- .../prx/libSceAudioOut/src/AudioOut2Port.cpp | 8 +++--- .../libSceAudioOut/tests/AudioOut2Timing.cpp | 27 +++++++++++++++++++ docs/dev/TechnicalDebt.md | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp index 2b7a8aa0a..96b9ed331 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Internal.hpp @@ -43,10 +43,17 @@ static constexpr std::uint32_t AUDIO_OUT2_PORT_CHANNELS_MAX = 12; struct AudioOut2Context; struct AudioOut2StereoFold; -using AudioOut2Grain = std::vector>; +struct AudioOut2CapturedPort { + std::size_t index = 0; + std::uint64_t generation = 0; + const void* data = nullptr; +}; + +using AudioOut2Grain = std::vector; struct AudioOut2Port { bool used = false; + std::uint64_t generation = 0; AudioOut2Context* context = nullptr; std::uint16_t type = 0; std::uint32_t dataFormat = 0; diff --git a/core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp b/core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp index fa8b8aba9..5db475551 100644 --- a/core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp +++ b/core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp @@ -32,6 +32,7 @@ static constexpr std::uint32_t FORMAT_FIELDS_MASK = 0xFFFu; static std::mutex g_portsLock; // Grows on demand: the title opens its bed ports plus max_object_ports object ports at once. static std::vector g_ports; +static std::uint64_t g_portGeneration = 0; static AudioOut2Port* FromHandle(AudioOut2PortHandle handle) { const auto index = handle - 1; @@ -99,7 +100,7 @@ AudioOut2Grain AudioOut2CaptureGrain(const AudioOut2Context& context) { AudioOut2Grain grain; for (std::size_t index = 0; index < g_ports.size(); index++) { const auto& port = g_ports[index]; - if (port.used && port.context == &context && port.data != nullptr && port.channels != 0) grain.emplace_back(index, port.data); + if (port.used && port.context == &context && port.data != nullptr && port.channels != 0) grain.push_back({index, port.generation, port.data}); } return grain; } @@ -107,10 +108,10 @@ AudioOut2Grain AudioOut2CaptureGrain(const AudioOut2Context& context) { std::uint32_t AudioOut2MixPorts(const AudioOut2Context& context, const AudioOut2Grain& grain, float* out, float* padOut, std::uint32_t frames) { std::lock_guard lock(g_portsLock); std::uint32_t mixed = 0; - for (const auto& [index, data] : grain) { + for (const auto& [index, generation, data] : grain) { if (index >= g_ports.size()) continue; const auto& port = g_ports[index]; - if (!port.used || port.context != &context || port.channels == 0) continue; + if (!port.used || port.generation != generation || port.context != &context || port.channels == 0) continue; const auto route = padOut != nullptr ? AudioOut2RouteForPort(port.type, port.channels) : AudioOut2Route::Main; if (route == AudioOut2Route::Main) AccumulatePort(port, data, out, frames); else AccumulatePadPort(port, data, route, padOut, frames); @@ -164,6 +165,7 @@ int APS5_VABI sceAudioOut2PortCreate(AudioOut2ContextHandle ctx, const AudioOut2 auto& entry = g_ports[index]; entry = AudioOut2Port{}; entry.used = true; + entry.generation = ++g_portGeneration; entry.context = reinterpret_cast(ctx); entry.type = params->port_type; entry.dataFormat = params->data_format; diff --git a/core/libs/prx/libSceAudioOut/tests/AudioOut2Timing.cpp b/core/libs/prx/libSceAudioOut/tests/AudioOut2Timing.cpp index 14e3f1744..b3f670c35 100644 --- a/core/libs/prx/libSceAudioOut/tests/AudioOut2Timing.cpp +++ b/core/libs/prx/libSceAudioOut/tests/AudioOut2Timing.cpp @@ -39,6 +39,7 @@ constexpr std::uint32_t grain = 256; constexpr std::uint32_t queueDepth = 2; constexpr std::uint32_t cushionGrains = 8; constexpr std::uint32_t frequency = 48000; +constexpr std::uint32_t formatMonoFloat = 1u << 8; constexpr std::uint32_t formatStereoFloat = 2u << 8; constexpr std::uint32_t attributeData = 0; constexpr int queueFull = static_cast(0x80260507); @@ -121,6 +122,31 @@ void TestReadAtNextPush() { Require(FirstNonZero(played, first + 2 * samples) == played.size()); } +void TestRecreatedPort() { + Session session("anyps5_audio_out2_recreated_port.raw"); + session.Fill(0.5f); + session.Point(session.buffer.data()); + Require(sceAudioOut2ContextPush(session.context, 1) == 0); + const auto first = session.port; + Require(sceAudioOut2PortDestroy(session.port) == 0); + AudioOut2PortParam mono{}; + mono.port_type = 0; + mono.data_format = formatMonoFloat; + mono.sampling_freq = frequency; + Require(sceAudioOut2PortCreate(session.context, &mono, &session.port) == 0); + Require(session.port == first); + const std::vector monoBuffer(grain, 0.75f); + session.Point(monoBuffer.data()); + Require(sceAudioOut2ContextPush(session.context, 1) == 0); + session.Point(nullptr); + for (std::uint32_t push = 0; push < cushionGrains * 4; push++) Require(sceAudioOut2ContextPush(session.context, 1) == 0); + const auto played = session.Close(); + const auto start = FirstNonZero(played); + const std::size_t samples = static_cast(grain) * 2; + Require(Constant(played, start, samples, 0.75f * masterGain)); + Require(FirstNonZero(played, start + samples) == played.size()); +} + void TestLevelExcludesCushion() { Session session("anyps5_audio_out2_level.raw"); session.Point(nullptr); @@ -166,6 +192,7 @@ void TestPrimingAfterRunningDry() { int main() { SetEnvironment("SDL_AUDIODRIVER", "disk"); TestReadAtNextPush(); + TestRecreatedPort(); TestLevelExcludesCushion(); TestPrimingAfterRunningDry(); return 0; diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 43239a6cc..db27ab47a 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -249,7 +249,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceKeyboardGetKey2Char](../../core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp) (libSceKeyboard) translates the 101-key (US) arrangement and the alphanumeric layer of the 106-key (Japanese) one, which follows the JIS layout of the HID usages: the Yen (0x89) and Ro (0x87) keys return `\` (0x5C) unshifted, as JIS X 0201 places the yen sign there; whether the console returns U+00A5 instead is unverified. With the Kana LED (bit 4, the HID position) set the 106-key arrangement throws, as the kana the console returns are unknown. Ctrl and Alt do not change the character. - [sceAudioOut2Set3DLatency](../../core/libs/prx/libSceAudioOut/src/AudioOut2System.cpp) and [sceAudioOut2MasteringInit](../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) have no effect: there is no 3D rendering or mastering stage. - [sceImeKeyboardGetInfo](../../core/libs/prx/libSceIme/Export.cpp) (libSceIme) returns `0x80bc0023` (no resource id) for every id while a keyboard is open, since the IME keyboard never reports a connected device. [sceImeKeyboardSetMode](../../core/libs/prx/libSceIme/Export.cpp) validates the mode bits but the mode has no effect, as no IME keyboard events are delivered. -- [AudioOut2 contexts](../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) with an SDL device read a grain's port buffers at the next push, at least 100 us after the grain was pushed, and play the mix behind a 40 ms cushion that the queue level leaves out. When the hardware reads the buffers is not known. The timing follows a title whose mixer fills a buffer after pushing it, reuses it two pushes later, and may still be writing it when the next push follows within microseconds +- [AudioOut2 contexts](../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) with an SDL device read a grain's port buffers at the next push, at least 100 us after the grain was pushed, and play the mix behind a 40 ms cushion that the queue level leaves out. When the hardware reads the buffers is not known. The timing follows a title whose mixer fills a buffer after pushing it, reuses it two pushes later, and may still be writing it when the next push follows within microseconds. The last grain pushed before a title stops pushing is never read or played. - [libSceAudiodec](../../core/libs/prx/libSceAudiodec/Export.cpp) throws for the 24-bit PCM word size (`iBwPcm` 0): its sample layout is unknown. ATRAC9 decoding is covered only by configuration and error tests, as no ATRAC9 encoder is available for a fixture. - [sceAjmDecMp3ParseFrame](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) throws when the original file length is requested (`parse_ofl` non-zero) for a layer other than III, for a CRC-protected frame, or for a VBRI header outside MPEG-1 stereo: shadPS4 looks for the tag after the CRC and after the side information, while LAME and FFmpeg place it without the CRC and VBRI always at byte 36. - The [output modifier](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) (`mul:2`, `mul:4`, `div:2`) follows the hardware with the IEEE mode off and f32 denormals flushed, the LLVM graphics default; the shader's MODE register is not read, and the hardware ignores the modifier in the other modes. On f16 results it is ignored, as on the hardware while f16 denormals are enabled; with them flushed the hardware applies it.