Optimized the way SetVolume works

This commit is contained in:
madwind 2025-02-06 10:21:43 +08:00
parent 0c94b7c0d5
commit 8761b13b34
3 changed files with 25 additions and 16 deletions

View File

@ -18,8 +18,20 @@ namespace Ryujinx.Audio.Backends.SDL3
private readonly ConcurrentDictionary<SDL3HardwareDeviceSession, byte> _sessions; private readonly ConcurrentDictionary<SDL3HardwareDeviceSession, byte> _sessions;
private readonly bool _supportSurroundConfiguration; 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() public SDL3HardwareDeviceDriver()
{ {
@ -48,7 +60,8 @@ namespace Ryujinx.Audio.Backends.SDL3
private static bool IsSupportedInternal() 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) if (device != 0)
{ {

View File

@ -1,6 +1,5 @@
using Ryujinx.Audio.Backends.Common; using Ryujinx.Audio.Backends.Common;
using Ryujinx.Audio.Common; using Ryujinx.Audio.Common;
using Ryujinx.Common.Logging;
using Ryujinx.Common.Memory; using Ryujinx.Common.Memory;
using Ryujinx.Memory; using Ryujinx.Memory;
using System; using System;
@ -40,11 +39,9 @@ namespace Ryujinx.Audio.Backends.SDL3
_volume = 1f; _volume = 1f;
} }
private void EnsureAudioStreamSetup(AudioBuffer buffer) private void EnsureAudioStreamSetup()
{ {
uint bufferSampleCount = (uint)GetSampleCount(buffer); bool needAudioSetup = _outputStream == 0 && !_hasSetupError;
bool needAudioSetup = (_outputStream == 0 && !_hasSetupError) ||
(bufferSampleCount >= Constants.TargetSampleCount);
if (needAudioSetup) if (needAudioSetup)
{ {
@ -87,19 +84,14 @@ namespace Ryujinx.Audio.Backends.SDL3
} }
using SpanOwner<byte> samplesOwner = SpanOwner<byte>.Rent(frameCount * _bytesPerFrame); using SpanOwner<byte> samplesOwner = SpanOwner<byte>.Rent(frameCount * _bytesPerFrame);
Span<byte> samples = samplesOwner.Span; Span<byte> samples = samplesOwner.Span;
int samplesLength = samples.Length; int samplesLength = samples.Length;
_ringBuffer.Read(samples, 0, samplesLength); _ringBuffer.Read(samples, 0, samplesLength);
fixed (byte* p = samples) fixed (byte* p = samples)
{ {
nint pStreamSrc = (nint)p; nint pBuffer = (nint)p;
nint pStreamDst = SDL_calloc(1,samplesLength); SDL_PutAudioStreamData(stream, pBuffer, samplesLength);
// Apply volume to written data
SDL_MixAudio(pStreamDst, pStreamSrc, _nativeSampleFormat, (uint)samplesLength, _driver.Volume);
SDL_PutAudioStreamData(stream, pStreamDst, samplesLength);
SDL_free(pStreamDst);
} }
ulong sampleCount = GetSampleCount(samplesLength); ulong sampleCount = GetSampleCount(samplesLength);
@ -148,7 +140,7 @@ namespace Ryujinx.Audio.Backends.SDL3
public override void QueueBuffer(AudioBuffer buffer) public override void QueueBuffer(AudioBuffer buffer)
{ {
EnsureAudioStreamSetup(buffer); EnsureAudioStreamSetup();
if (_outputStream != 0) if (_outputStream != 0)
{ {
@ -169,6 +161,10 @@ namespace Ryujinx.Audio.Backends.SDL3
public override void SetVolume(float volume) public override void SetVolume(float volume)
{ {
_volume = volume; _volume = volume;
if (_outputStream != 0)
{
SDL_SetAudioStreamGain(_outputStream, _volume);
}
} }
public override void Start() public override void Start()

@ -1 +1 @@
Subproject commit 2fa1e7258a1fd9e3a7a546218b5ed1564953ad39 Subproject commit fa8c0f0552cc232ab454b4b5ce787e63617ef7ab