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 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)
{

View File

@ -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<byte> samplesOwner = SpanOwner<byte>.Rent(frameCount * _bytesPerFrame);
Span<byte> 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()

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