From 1c836c10ca6622f0afd41b034a3250b2efcd481e Mon Sep 17 00:00:00 2001 From: Lib Date: Fri, 8 May 2026 12:58:10 +0000 Subject: [PATCH] audio: devirtualize AudioStream, fold NullAudioStream into LibretroAudioStream The audio path used to be: - common/audio_stream.h declared an abstract AudioStream base with one pure virtual (FramesAvailable) and shared non-virtual BeginWrite/EndWrite/Reconfigure/Shutdown. - libretro/libretro_audio_stream.{h,cpp} overrode FramesAvailable with the real libretro frontend drain. - common/null_audio_stream.{h,cpp} overrode FramesAvailable to discard, and was constructed only to feed the SPU during internal-runahead replay so audio from those frames never reached the frontend. The SPU pointer-swapped between the host stream and the null stream around DoRunahead's replay loop. The base class buys nothing here. There is exactly one runtime audio stream in swanstation - LibretroAudioStream - and the second subclass exists only to express 'discard everything' behaviour, which is trivially representable as a flag on the real one. EndWrite ran a virtual call to FramesAvailable on every SPU update (~150-300 times per frame), the indirection resolving to the same place every time. Drop the base class and the null subclass: - Delete common/audio_stream.{h,cpp} and common/null_audio_stream.{h,cpp}. - LibretroAudioStream becomes a plain class with no inheritance, absorbing the BeginWrite/EndWrite/Reconfigure/Shutdown bodies and the SampleType/MaxSamples/etc. constants from the old base. - FramesAvailable's contents are inlined into EndWrite (it was the only caller), with an early-return for fast-hook mode that matches the previous behaviour exactly. - The 'discard' role of NullAudioStream becomes a m_silent_mode flag and a SetSilentMode() setter on LibretroAudioStream. EndWrite drains the FIFO immediately when silent, so the SPU keeps ticking state-machines (envelope counters, ADPCM phase, etc.) for correctness, the FIFO does not build up backpressure across the runahead window, and nothing reaches the frontend. - System::DoRunahead replaces the SPU pointer-swap with a SetSilentMode(true)/SetSilentMode(false) toggle on the host's existing LibretroAudioStream. s_runahead_audio_stream (std::unique_ptr) and the matching reset() in System::Shutdown go away entirely. - HostInterface drops the virtual CreateAudioStream() factory method; its single override in LibretroHostInterface returned std::make_unique() which is now inlined at the only call site (HostInterface::BootSystem). - HostInterface::m_audio_stream and HostInterface::GetAudioStream become typed as LibretroAudioStream (forward-declared in host_interface.h, the libretro header included from the cpp). SPU::SetAudioStream and SPU::m_audio_stream similarly get the concrete type. The static_cast in retro_run_frame's UploadToFrontend call goes away. - Makefile.libretro and src/common/CMakeLists.txt drop the deleted source files from their lists. Linux x64 build still warning-clean. .so size drops from 8047800 to 8046184 bytes (-1616) - the AudioStream and NullAudioStream vtables/typeinfo, the virtual destructor and FramesAvailable, the indirect-call sites in EndWrite, and the second runahead audio stream instance and its construction code are all gone. 25 retro_* exports unchanged. Behaviour is identical: the SetSilentMode toggle produces exactly the same observable result as the swap-to-NullAudioStream pattern (the SPU's frame output is discarded during the replay window in both cases). --- Makefile.libretro | 4 +- src/common/CMakeLists.txt | 4 - src/common/audio_stream.cpp | 66 ----------- src/common/audio_stream.h | 54 --------- src/common/null_audio_stream.cpp | 21 ---- src/common/null_audio_stream.h | 12 -- src/core/host_interface.cpp | 9 +- src/core/host_interface.h | 7 +- src/core/spu.cpp | 2 +- src/core/spu.h | 5 +- src/core/system.cpp | 36 +++--- src/libretro/libretro_audio_stream.cpp | 138 ++++++++++++++++------- src/libretro/libretro_audio_stream.h | 54 ++++++++- src/libretro/libretro_host_interface.cpp | 13 +-- src/libretro/libretro_host_interface.h | 1 - 15 files changed, 178 insertions(+), 248 deletions(-) delete mode 100644 src/common/audio_stream.cpp delete mode 100644 src/common/audio_stream.h delete mode 100644 src/common/null_audio_stream.cpp delete mode 100644 src/common/null_audio_stream.h diff --git a/Makefile.libretro b/Makefile.libretro index db831b92..2ad85ad9 100644 --- a/Makefile.libretro +++ b/Makefile.libretro @@ -225,7 +225,7 @@ VIXL_DIR := $(DEPS_DIR)/vixl # Notes: # - We only add `-Isrc`. Source files inside src/{common,core,libretro} # use forms like `#include "common/byte_stream.h"` and same-directory -# `#include "audio_stream.h"`; the latter resolves source-relative +# `#include "byte_stream.h"`; the latter resolves source-relative # automatically. Adding `-Isrc/common` would shadow the system # `` with `src/common/string.h` and break ``. INCFLAGS := \ @@ -271,7 +271,6 @@ endif # --------------------------------------------------------------------- COMMON_DIR := $(SRC_DIR)/common SOURCES_CXX += \ - $(COMMON_DIR)/audio_stream.cpp \ $(COMMON_DIR)/byte_stream.cpp \ $(COMMON_DIR)/cd_image.cpp \ $(COMMON_DIR)/cd_image_bin.cpp \ @@ -294,7 +293,6 @@ SOURCES_CXX += \ $(COMMON_DIR)/jit_code_buffer.cpp \ $(COMMON_DIR)/log.cpp \ $(COMMON_DIR)/md5_digest.cpp \ - $(COMMON_DIR)/null_audio_stream.cpp \ $(COMMON_DIR)/memory_arena.cpp \ $(COMMON_DIR)/page_fault_handler.cpp \ $(COMMON_DIR)/progress_callback.cpp \ diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 47d8e4e7..33673183 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -1,7 +1,5 @@ add_library(common align.h - audio_stream.cpp - audio_stream.h bitfield.h bitutils.h byte_stream.cpp @@ -52,8 +50,6 @@ add_library(common make_array.h md5_digest.cpp md5_digest.h - null_audio_stream.cpp - null_audio_stream.h memory_arena.cpp memory_arena.h page_fault_handler.cpp diff --git a/src/common/audio_stream.cpp b/src/common/audio_stream.cpp deleted file mode 100644 index 6d1565fd..00000000 --- a/src/common/audio_stream.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "audio_stream.h" -#include -#include - -#ifndef AUDIO_CHANNELS -#define AUDIO_CHANNELS 2 -#endif - -AudioStream::AudioStream() = default; - -AudioStream::~AudioStream() -{ -} - -bool AudioStream::Reconfigure(u32 input_sample_rate , - u32 output_sample_rate , - u32 channels, - u32 buffer_size) -{ - m_buffer_size = buffer_size; - - return SetBufferSize(buffer_size); -} - -void AudioStream::Shutdown() -{ - m_buffer.Clear(); - m_buffer_size = 0; -} - -void AudioStream::BeginWrite(SampleType** buffer_ptr, u32* num_frames) -{ - // The libretro core is single-threaded; producer (SPU) and consumer - // (UploadToFrontend) run on the same thread. No synchronization - // primitive is required around the FIFO; see the comment in - // audio_stream.h for the previous mutex's rationale. - // - // BeginWrite is non-blocking: the SPU is told what contiguous space - // is currently available, capped at m_buffer_size, and EndWrite - // records the count the caller actually filled. The SPU's loop in - // spu.cpp Execute() handles short returns by re-issuing - // BeginWrite/EndWrite until all generated frames are consumed. - *buffer_ptr = m_buffer.GetWritePointer(); - *num_frames = std::min(m_buffer_size, m_buffer.GetContiguousSpace() / AUDIO_CHANNELS); -} - -void AudioStream::EndWrite(u32 num_frames) -{ - m_buffer.AdvanceTail(num_frames * AUDIO_CHANNELS); - FramesAvailable(); -} - -bool AudioStream::SetBufferSize(u32 buffer_size) -{ - const u32 buffer_size_in_samples = buffer_size * AUDIO_CHANNELS; - // The FIFO has fixed capacity; reject configurations that would not - // fit two batches' worth of samples (the historical 2x headroom that - // the now-removed back-pressure code relied on). The check is kept - // because games exposed via core options can request larger - // audio_buffer_size values. - if ((buffer_size_in_samples * 2u) > m_buffer.GetCapacity()) - return false; - - m_buffer_size = buffer_size; - return true; -} diff --git a/src/common/audio_stream.h b/src/common/audio_stream.h deleted file mode 100644 index bd9163a2..00000000 --- a/src/common/audio_stream.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once -#include "fifo_queue.h" -#include "types.h" -#include -#include -#include - -// Uses signed 16-bits samples. - -class AudioStream -{ -public: - using SampleType = s16; - - static constexpr u32 DefaultInputSampleRate = 44100, DefaultOutputSampleRate = 44100, DefaultBufferSize = 2048, - MaxSamples = 32768, FullVolume = 100; - - AudioStream(); - virtual ~AudioStream(); - - u32 GetBufferSize() const { return m_buffer_size; } - - bool Reconfigure(u32 input_sample_rate, u32 output_sample_rate, - u32 channels, u32 buffer_size); - - void Shutdown(); - - void BeginWrite(SampleType** buffer_ptr, u32* num_frames); - void EndWrite(u32 num_frames); - - static std::unique_ptr CreateNullAudioStream(); - -protected: - virtual void FramesAvailable() = 0; - - ALWAYS_INLINE static SampleType ApplyVolume(SampleType sample, u32 volume) - { - return s16((s32(sample) * s32(volume)) / 100); - } - - bool SetBufferSize(u32 buffer_size); - - u32 m_buffer_size = 0; - - // The libretro core is single-threaded by design: the SPU runs on - // the same thread (driven by TimingEvents) that eventually calls - // UploadToFrontend() at end-of-retro_run. Both subclasses - // (LibretroAudioStream, NullAudioStream) are libretro-only. No - // synchronization primitive is needed around this FIFO; the - // previously-present mutex was always uncontended and only added - // per-write atomic-CAS overhead (~50ns x ~300 SPU updates per - // frame). - HeapFIFOQueue m_buffer; -}; diff --git a/src/common/null_audio_stream.cpp b/src/common/null_audio_stream.cpp deleted file mode 100644 index 366b7615..00000000 --- a/src/common/null_audio_stream.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "null_audio_stream.h" - -#ifndef AUDIO_CHANNELS -#define AUDIO_CHANNELS 2 -#endif - -NullAudioStream::NullAudioStream() = default; - -NullAudioStream::~NullAudioStream() = default; - -void NullAudioStream::FramesAvailable() -{ - // Drop everything that's available as soon as it arrives. - // The base AudioStream is single-threaded; no lock needed. - m_buffer.Remove(m_buffer.GetSize()); -} - -std::unique_ptr AudioStream::CreateNullAudioStream() -{ - return std::make_unique(); -} diff --git a/src/common/null_audio_stream.h b/src/common/null_audio_stream.h deleted file mode 100644 index 82e0fb8b..00000000 --- a/src/common/null_audio_stream.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "audio_stream.h" - -class NullAudioStream final : public AudioStream -{ -public: - NullAudioStream(); - ~NullAudioStream(); - -protected: - void FramesAvailable() override; -}; diff --git a/src/core/host_interface.cpp b/src/core/host_interface.cpp index 98481081..c5b39a81 100644 --- a/src/core/host_interface.cpp +++ b/src/core/host_interface.cpp @@ -1,7 +1,7 @@ #include "host_interface.h" #include "bios.h" #include "cdrom.h" -#include "common/audio_stream.h" +#include "libretro/libretro_audio_stream.h" #include "common/byte_stream.h" #include "common/file_system.h" #include "common/image.h" @@ -53,8 +53,11 @@ bool HostInterface::BootSystem(std::shared_ptr parameters) { AcquireHostDisplay(); - // create the audio stream. this will never fail, since we'll just fall back to null - m_audio_stream = CreateAudioStream(); + // Construct the libretro audio stream directly. There used to be a + // virtual CreateAudioStream() hook to abstract the construction, but + // since the libretro core has only ever had one audio stream type + // (LibretroAudioStream), the indirection bought nothing. + m_audio_stream = std::make_unique(); m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, g_settings.audio_buffer_size); if (System::IsValid()) diff --git a/src/core/host_interface.h b/src/core/host_interface.h index f85bf3ad..c7aae81f 100644 --- a/src/core/host_interface.h +++ b/src/core/host_interface.h @@ -14,7 +14,7 @@ enum class LogLevel : u8; -class AudioStream; +class LibretroAudioStream; class ByteStream; class CDImage; class HostDisplay; @@ -38,7 +38,7 @@ public: ALWAYS_INLINE HostDisplay* GetDisplay() const { return m_display.get(); } /// Access to host audio stream. - ALWAYS_INLINE AudioStream* GetAudioStream() const { return m_audio_stream.get(); } + ALWAYS_INLINE LibretroAudioStream* GetAudioStream() const { return m_audio_stream.get(); } /// Initializes the emulator frontend. virtual bool Initialize(); @@ -114,7 +114,6 @@ public: protected: virtual void AcquireHostDisplay() = 0; virtual void ReleaseHostDisplay() = 0; - virtual std::unique_ptr CreateAudioStream() = 0; virtual void OnControllerTypeChanged(u32 slot) = 0; @@ -137,7 +136,7 @@ protected: void UpdateSoftwareCursor(); std::unique_ptr m_display; - std::unique_ptr m_audio_stream; + std::unique_ptr m_audio_stream; std::string m_user_directory; }; diff --git a/src/core/spu.cpp b/src/core/spu.cpp index f3e2c964..f473dfd2 100644 --- a/src/core/spu.cpp +++ b/src/core/spu.cpp @@ -1,6 +1,6 @@ #include "spu.h" #include "cdrom.h" -#include "common/audio_stream.h" +#include "libretro/libretro_audio_stream.h" #include "common/state_wrapper.h" #include "dma.h" #include "host_interface.h" diff --git a/src/core/spu.h b/src/core/spu.h index 7dd8a616..0a7dca82 100644 --- a/src/core/spu.h +++ b/src/core/spu.h @@ -12,6 +12,7 @@ namespace Common { } class TimingEvent; +class LibretroAudioStream; class SPU { @@ -41,7 +42,7 @@ public: std::array& GetRAM() { return m_ram; } /// Change output stream - used for runahead. - ALWAYS_INLINE void SetAudioStream(AudioStream* stream) { m_audio_stream = stream; } + ALWAYS_INLINE void SetAudioStream(LibretroAudioStream* stream) { m_audio_stream = stream; } private: static constexpr u32 SPU_BASE = 0x1F801C00; @@ -357,7 +358,7 @@ private: std::unique_ptr m_tick_event; std::unique_ptr m_transfer_event; - AudioStream* m_audio_stream = nullptr; + LibretroAudioStream* m_audio_stream = nullptr; TickCount m_ticks_carry = 0; TickCount m_cpu_ticks_per_spu_tick = 0; TickCount m_cpu_tick_divider = 0; diff --git a/src/core/system.cpp b/src/core/system.cpp index 5879dd61..896d59bb 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -3,7 +3,7 @@ #include "bus.h" #include "cdrom.h" #include "cheats.h" -#include "common/audio_stream.h" +#include "libretro/libretro_audio_stream.h" #include "common/error.h" #include "common/file_system.h" #include "common/iso_reader.h" @@ -120,7 +120,6 @@ static s32 s_rewind_save_counter = -1; static bool s_rewinding_first_save = false; static std::deque s_runahead_states; -static std::unique_ptr s_runahead_audio_stream; static bool s_runahead_replay_pending = false; static u32 s_runahead_frames = 0; @@ -779,7 +778,6 @@ void Shutdown() return; ClearMemorySaveStates(); - s_runahead_audio_stream.reset(); g_texture_replacements.Shutdown(); @@ -1821,21 +1819,11 @@ void UpdateMemorySaveStateSettings() if (s_runahead_frames > 0) { Log_InfoPrintf("Runahead is active with %u frames", s_runahead_frames); - - if (!s_runahead_audio_stream) - { - // doesn't matter if it's not resampled here since it eats everything anyway, nom nom nom. - s_runahead_audio_stream = AudioStream::CreateNullAudioStream(); - s_runahead_audio_stream->Reconfigure( - HostInterface::AUDIO_SAMPLE_RATE, - HostInterface::AUDIO_SAMPLE_RATE, - HostInterface::AUDIO_CHANNELS, - AudioStream::DefaultBufferSize); - } - } - else - { - s_runahead_audio_stream.reset(); + // The replay path used to allocate a separate NullAudioStream and + // pointer-swap it onto the SPU; that has been replaced with a + // SetSilentMode toggle on the host's existing audio stream + // (see DoRunahead) so the indirection and the second instance go + // away. } } @@ -1989,7 +1977,13 @@ void DoRunahead() s32 frames_to_run = static_cast(s_runahead_frames) - static_cast(s_runahead_states.size()); if (frames_to_run > 0) { - g_spu.SetAudioStream(s_runahead_audio_stream.get()); + // Switch the host audio stream into silent mode for the replay + // window: the SPU keeps producing samples for state correctness, + // but the FIFO is drained inside EndWrite so nothing reaches the + // libretro frontend and back-pressure does not build up. This + // replaces the previous pointer-swap-to-NullAudioStream pattern. + LibretroAudioStream* const audio_stream = g_host_interface->GetAudioStream(); + audio_stream->SetSilentMode(true); while (frames_to_run > 0) { @@ -1998,15 +1992,13 @@ void DoRunahead() frames_to_run--; } - g_spu.SetAudioStream(g_host_interface->GetAudioStream()); - + audio_stream->SetSilentMode(false); } else { // save this frame SaveRunaheadState(); } - } void DoMemorySaveStates() diff --git a/src/libretro/libretro_audio_stream.cpp b/src/libretro/libretro_audio_stream.cpp index 5d0ee0f9..f8cadfab 100644 --- a/src/libretro/libretro_audio_stream.cpp +++ b/src/libretro/libretro_audio_stream.cpp @@ -10,52 +10,64 @@ LibretroAudioStream::LibretroAudioStream() = default; LibretroAudioStream::~LibretroAudioStream() = default; -void LibretroAudioStream::UploadToFrontend() +bool LibretroAudioStream::Reconfigure(u32 input_sample_rate, + u32 output_sample_rate, + u32 channels, + u32 buffer_size) { - // Drain the FIFO straight into the libretro batch callback. The FIFO is - // a circular buffer, so up to two contiguous regions may be present - // when the producer wrapped around since the last drain. Most frames - // are one region (one callback); wraparound costs one extra callback. - // - // Previously this routine bounce-copied every sample through a 32 KiB - // (65 KiB after the s16 promotion) std::array on the stack and made - // exactly one callback. The intermediate copy is unnecessary - the - // libretro frontend reads samples synchronously from the buffer we - // pass it, and the FIFO storage stays valid until Remove() is called - // below. - // - // When the frontend has cleared RETRO_AV_ENABLE_AUDIO for this frame - // (single-instance runahead skip frame, fast-forward, etc.), we still - // need to drain the FIFO so the SPU side doesn't keep accumulating - // backpressure across the runahead window - we just don't forward - // anything to the frontend. - while (true) - { - const u32 num_samples = m_buffer.GetContiguousSize(); - if (num_samples == 0) - break; - - if (!g_retro_skip_audio_this_frame) - g_retro_audio_sample_batch_callback(m_buffer.GetReadPointer(), - num_samples / AUDIO_CHANNELS); - m_buffer.Remove(num_samples); - } + m_buffer_size = buffer_size; + return SetBufferSize(buffer_size); } -void LibretroAudioStream::FramesAvailable() +void LibretroAudioStream::Shutdown() { - // No-op in fast-hook mode (the default). The frontend is fed once per - // retro_run_frame() via UploadToFrontend(), which keeps audio batched - // and predictable. Calling the frontend per SPU update (the slow-hook - // path) is preserved here for parity but should be considered a - // legacy / debugging mode: it produces dozens to hundreds of tiny - // batch calls per frame, increases callback overhead, and the - // resulting audio chunking is sensitive to host scheduling jitter - // on the libretro side. + m_buffer.Clear(); + m_buffer_size = 0; +} + +void LibretroAudioStream::BeginWrite(SampleType** buffer_ptr, u32* num_frames) +{ + // The libretro core is single-threaded; producer (SPU) and consumer + // (UploadToFrontend) run on the same thread. No synchronisation + // primitive is required around the FIFO; see the comment in + // libretro_audio_stream.h for the previous mutex's rationale. + // + // BeginWrite is non-blocking: the SPU is told what contiguous space + // is currently available, capped at m_buffer_size, and EndWrite + // records the count the caller actually filled. The SPU's loop in + // spu.cpp Execute() handles short returns by re-issuing + // BeginWrite/EndWrite until all generated frames are consumed. + *buffer_ptr = m_buffer.GetWritePointer(); + *num_frames = std::min(m_buffer_size, m_buffer.GetContiguousSpace() / AUDIO_CHANNELS); +} + +void LibretroAudioStream::EndWrite(u32 num_frames) +{ + m_buffer.AdvanceTail(num_frames * AUDIO_CHANNELS); + + if (m_silent_mode) + { + // Internal-runahead replay: drop everything immediately so the + // FIFO doesn't fill up across the replay window. We're still + // ticking the SPU for state correctness, just discarding the + // resulting audio. + m_buffer.Remove(m_buffer.GetSize()); + return; + } + + // No-op in fast-hook mode (the default). The frontend is fed once + // per retro_run_frame() via UploadToFrontend(), which keeps audio + // batched and predictable. Calling the frontend per SPU update (the + // slow-hook path below) is preserved for parity but should be + // considered a legacy / debugging mode: it produces dozens to + // hundreds of tiny batch calls per frame, increases callback + // overhead, and the resulting audio chunking is sensitive to host + // scheduling jitter on the libretro side. if (g_settings.audio_fast_hook) return; - // Same skip-but-still-drain rule as UploadToFrontend; see comment there. + // Slow-hook drain. Same skip-but-still-drain rule as + // UploadToFrontend; see comment there. while (true) { const u32 num_samples = m_buffer.GetContiguousSize(); @@ -68,3 +80,51 @@ void LibretroAudioStream::FramesAvailable() m_buffer.Remove(num_samples); } } + +void LibretroAudioStream::UploadToFrontend() +{ + // Drain the FIFO straight into the libretro batch callback. The FIFO + // is a circular buffer, so up to two contiguous regions may be + // present when the producer wrapped around since the last drain. + // Most frames are one region (one callback); wraparound costs one + // extra callback. + // + // Previously this routine bounce-copied every sample through a + // 32 KiB (65 KiB after the s16 promotion) std::array on the stack + // and made exactly one callback. The intermediate copy is + // unnecessary - the libretro frontend reads samples synchronously + // from the buffer we pass it, and the FIFO storage stays valid until + // Remove() is called below. + // + // When the frontend has cleared RETRO_AV_ENABLE_AUDIO for this frame + // (single-instance runahead skip frame, fast-forward, etc.), we + // still need to drain the FIFO so the SPU side doesn't keep + // accumulating backpressure across the runahead window - we just + // don't forward anything to the frontend. + while (true) + { + const u32 num_samples = m_buffer.GetContiguousSize(); + if (num_samples == 0) + break; + + if (!g_retro_skip_audio_this_frame) + g_retro_audio_sample_batch_callback(m_buffer.GetReadPointer(), + num_samples / AUDIO_CHANNELS); + m_buffer.Remove(num_samples); + } +} + +bool LibretroAudioStream::SetBufferSize(u32 buffer_size) +{ + const u32 buffer_size_in_samples = buffer_size * AUDIO_CHANNELS; + // The FIFO has fixed capacity; reject configurations that would not + // fit two batches' worth of samples (the historical 2x headroom that + // the now-removed back-pressure code relied on). The check is kept + // because games exposed via core options can request larger + // audio_buffer_size values. + if ((buffer_size_in_samples * 2u) > m_buffer.GetCapacity()) + return false; + + m_buffer_size = buffer_size; + return true; +} diff --git a/src/libretro/libretro_audio_stream.h b/src/libretro/libretro_audio_stream.h index 489f9eb3..c3903464 100644 --- a/src/libretro/libretro_audio_stream.h +++ b/src/libretro/libretro_audio_stream.h @@ -1,16 +1,62 @@ #pragma once -#include "common/audio_stream.h" +#include "common/fifo_queue.h" +#include "core/types.h" #include #include -class LibretroAudioStream final : public AudioStream +// 16-bit signed audio. The libretro core is single-threaded by design +// (the SPU runs on the same thread driven by TimingEvents that +// eventually calls UploadToFrontend() at end-of-retro_run), so no +// synchronisation primitive is needed around the FIFO. The previously +// present mutex was always uncontended and only added per-write +// atomic-CAS overhead (~50ns x ~300 SPU updates per frame). +// +// This class used to derive from a generic AudioStream base. There has +// only ever been one runtime audio stream in swanstation - this one - +// and the second subclass (NullAudioStream) was only used to discard +// audio during internal-runahead replay. The "discard" behaviour is +// folded in here behind SetSilentMode() instead, dropping the vtable +// dispatch from FramesAvailable / EndWrite hot paths. +class LibretroAudioStream { public: + using SampleType = s16; + + static constexpr u32 DefaultInputSampleRate = 44100, DefaultOutputSampleRate = 44100, DefaultBufferSize = 2048, + MaxSamples = 32768, FullVolume = 100; + LibretroAudioStream(); ~LibretroAudioStream(); + u32 GetBufferSize() const { return m_buffer_size; } + + // input_sample_rate / output_sample_rate / channels are accepted for + // signature compatibility with the historical AudioStream base; the + // libretro frontend does its own resampling, so only buffer_size + // affects this object. + bool Reconfigure(u32 input_sample_rate, u32 output_sample_rate, + u32 channels, u32 buffer_size); + + void Shutdown(); + + void BeginWrite(SampleType** buffer_ptr, u32* num_frames); + void EndWrite(u32 num_frames); + void UploadToFrontend(); -protected: - void FramesAvailable() override; + // Switch the FIFO into a "drop everything" mode used by the internal + // runahead replay path: the SPU keeps producing samples for state + // correctness, but nothing reaches the libretro frontend and the + // FIFO is drained inside EndWrite so backpressure does not build up + // across the runahead window. + void SetSilentMode(bool silent) { m_silent_mode = silent; } + bool IsSilentMode() const { return m_silent_mode; } + +private: + bool SetBufferSize(u32 buffer_size); + + u32 m_buffer_size = 0; + bool m_silent_mode = false; + + HeapFIFOQueue m_buffer; }; diff --git a/src/libretro/libretro_host_interface.cpp b/src/libretro/libretro_host_interface.cpp index 236c506b..b00a6665 100644 --- a/src/libretro/libretro_host_interface.cpp +++ b/src/libretro/libretro_host_interface.cpp @@ -772,14 +772,7 @@ void LibretroHostInterface::retro_run_frame() m_display->Render(); if (g_settings.audio_fast_hook) - { - // The audio stream is constructed by CreateAudioStream() below as a - // LibretroAudioStream; the type is invariant for the lifetime of - // the host interface. dynamic_cast adds RTTI lookup on every frame - // for no gain. - auto* const audio_stream = static_cast(m_audio_stream.get()); - audio_stream->UploadToFrontend(); - } + m_audio_stream->UploadToFrontend(); } unsigned LibretroHostInterface::retro_get_region() @@ -915,10 +908,6 @@ void LibretroHostInterface::ReleaseHostDisplay() m_display.reset(); } -std::unique_ptr LibretroHostInterface::CreateAudioStream() -{ - return std::make_unique(); -} void LibretroHostInterface::OnControllerTypeChanged(u32 slot) {} void LibretroHostInterface::SetMouseMode(bool relative, bool hide_cursor) {} diff --git a/src/libretro/libretro_host_interface.h b/src/libretro/libretro_host_interface.h index b6ebdeb8..07bdaa24 100644 --- a/src/libretro/libretro_host_interface.h +++ b/src/libretro/libretro_host_interface.h @@ -72,7 +72,6 @@ public: protected: void AcquireHostDisplay() override; void ReleaseHostDisplay() override; - std::unique_ptr CreateAudioStream() override; void CheckForSettingsChanges(const Settings& old_settings) override; void OnRunningGameChanged(const std::string& path, CDImage* image, const std::string& game_code, const std::string& game_title) override;