From 8761b13b3477f778319b87be35038b48d348b1d4 Mon Sep 17 00:00:00 2001 From: madwind Date: Thu, 6 Feb 2025 10:21:43 +0800 Subject: [PATCH] Optimized the way SetVolume works --- .../SDL3HardwareDeviceDriver.cs | 17 ++++++++++++-- .../SDL3HardwareDeviceSession.cs | 22 ++++++++----------- src/Ryujinx.SDL3-CS/external/SDL | 2 +- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Ryujinx.Audio.Backends.SDL3/SDL3HardwareDeviceDriver.cs b/src/Ryujinx.Audio.Backends.SDL3/SDL3HardwareDeviceDriver.cs index 02ea0b25e..3ec02ade1 100644 --- a/src/Ryujinx.Audio.Backends.SDL3/SDL3HardwareDeviceDriver.cs +++ b/src/Ryujinx.Audio.Backends.SDL3/SDL3HardwareDeviceDriver.cs @@ -18,8 +18,20 @@ namespace Ryujinx.Audio.Backends.SDL3 private readonly ConcurrentDictionary _sessions; private readonly bool _supportSurroundConfiguration; + private float _volume; - public float Volume { get; set; } + public float Volume + { + get => _volume; + set + { + _volume = value; + foreach (SDL3HardwareDeviceSession session in _sessions.Keys) + { + session.SetVolume(_volume); + } + } + } public SDL3HardwareDeviceDriver() { @@ -48,7 +60,8 @@ namespace Ryujinx.Audio.Backends.SDL3 private static bool IsSupportedInternal() { - nint device = OpenStream(SampleFormat.PcmInt16, Constants.TargetSampleRate, Constants.ChannelCountMax, null); + nint device = OpenStream(SampleFormat.PcmInt16, Constants.TargetSampleRate, Constants.ChannelCountMax, + null); if (device != 0) { diff --git a/src/Ryujinx.Audio.Backends.SDL3/SDL3HardwareDeviceSession.cs b/src/Ryujinx.Audio.Backends.SDL3/SDL3HardwareDeviceSession.cs index 845908a7c..8fc305586 100644 --- a/src/Ryujinx.Audio.Backends.SDL3/SDL3HardwareDeviceSession.cs +++ b/src/Ryujinx.Audio.Backends.SDL3/SDL3HardwareDeviceSession.cs @@ -1,6 +1,5 @@ using Ryujinx.Audio.Backends.Common; using Ryujinx.Audio.Common; -using Ryujinx.Common.Logging; using Ryujinx.Common.Memory; using Ryujinx.Memory; using System; @@ -40,11 +39,9 @@ namespace Ryujinx.Audio.Backends.SDL3 _volume = 1f; } - private void EnsureAudioStreamSetup(AudioBuffer buffer) + private void EnsureAudioStreamSetup() { - uint bufferSampleCount = (uint)GetSampleCount(buffer); - bool needAudioSetup = (_outputStream == 0 && !_hasSetupError) || - (bufferSampleCount >= Constants.TargetSampleCount); + bool needAudioSetup = _outputStream == 0 && !_hasSetupError; if (needAudioSetup) { @@ -87,19 +84,14 @@ namespace Ryujinx.Audio.Backends.SDL3 } using SpanOwner samplesOwner = SpanOwner.Rent(frameCount * _bytesPerFrame); - Span samples = samplesOwner.Span; int samplesLength = samples.Length; _ringBuffer.Read(samples, 0, samplesLength); fixed (byte* p = samples) { - nint pStreamSrc = (nint)p; - nint pStreamDst = SDL_calloc(1,samplesLength); - // Apply volume to written data - SDL_MixAudio(pStreamDst, pStreamSrc, _nativeSampleFormat, (uint)samplesLength, _driver.Volume); - SDL_PutAudioStreamData(stream, pStreamDst, samplesLength); - SDL_free(pStreamDst); + nint pBuffer = (nint)p; + SDL_PutAudioStreamData(stream, pBuffer, samplesLength); } ulong sampleCount = GetSampleCount(samplesLength); @@ -148,7 +140,7 @@ namespace Ryujinx.Audio.Backends.SDL3 public override void QueueBuffer(AudioBuffer buffer) { - EnsureAudioStreamSetup(buffer); + EnsureAudioStreamSetup(); if (_outputStream != 0) { @@ -169,6 +161,10 @@ namespace Ryujinx.Audio.Backends.SDL3 public override void SetVolume(float volume) { _volume = volume; + if (_outputStream != 0) + { + SDL_SetAudioStreamGain(_outputStream, _volume); + } } public override void Start() diff --git a/src/Ryujinx.SDL3-CS/external/SDL b/src/Ryujinx.SDL3-CS/external/SDL index 2fa1e7258..fa8c0f055 160000 --- a/src/Ryujinx.SDL3-CS/external/SDL +++ b/src/Ryujinx.SDL3-CS/external/SDL @@ -1 +1 @@ -Subproject commit 2fa1e7258a1fd9e3a7a546218b5ed1564953ad39 +Subproject commit fa8c0f0552cc232ab454b4b5ce787e63617ef7ab