diff --git a/Ryujinx.Audio/IAalOutput.cs b/Ryujinx.Audio/IAalOutput.cs index 489f90028e..756c4003f8 100644 --- a/Ryujinx.Audio/IAalOutput.cs +++ b/Ryujinx.Audio/IAalOutput.cs @@ -20,7 +20,7 @@ namespace Ryujinx.Audio float GetVolume(); - void SetVolume(float volume); + void SetVolume(float gain); PlaybackState GetState(int trackId); } diff --git a/Ryujinx.Audio/Renderers/DummyAudioOut.cs b/Ryujinx.Audio/Renderers/DummyAudioOut.cs index be1c101ea8..2180d0c746 100644 --- a/Ryujinx.Audio/Renderers/DummyAudioOut.cs +++ b/Ryujinx.Audio/Renderers/DummyAudioOut.cs @@ -83,9 +83,9 @@ namespace Ryujinx.Audio public float GetVolume() => _volume; - public void SetVolume(float volume) + public void SetVolume(float gain) { - _volume = Math.Clamp(_volume - (_volume * volume), 0.0f, 1.0f); + _volume = Math.Clamp(_volume - (_volume * gain), 0.0f, 1.0f); } public void Dispose() diff --git a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs index 1e0c170a15..01cd84c656 100644 --- a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs +++ b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs @@ -42,6 +42,11 @@ namespace Ryujinx.Audio /// private float _volume = 1.0f; + /// + /// True if the volume of audio renderer have changed + /// + private bool _volumeChanged; + /// /// True if OpenAL is supported on the device /// @@ -82,7 +87,7 @@ namespace Ryujinx.Audio } } - // NOTE: If it's not slept it will waste cycles. + // If it's not slept it will waste cycles. Thread.Sleep(10); } while (_keepPolling); @@ -231,7 +236,12 @@ namespace Ryujinx.Audio if (State != ALSourceState.Playing && track.State == PlaybackState.Playing) { - AL.Source(track.SourceId, ALSourcef.Gain, _volume); + if (_volumeChanged) + { + AL.Source(track.SourceId, ALSourcef.Gain, _volume); + + _volumeChanged = false; + } AL.SourcePlay(track.SourceId); } @@ -262,10 +272,18 @@ namespace Ryujinx.Audio /// /// Set playback volume /// - /// The volume of the playback - public void SetVolume(float volume) + /// The gain of the playback + public void SetVolume(float gain) { - _volume = Math.Clamp(_volume - (_volume * volume), 0.0f, 1.0f); + if (!_volumeChanged) + { + // Games send a gain value here, so we need to apply it on the current volume value. + // In that way we have to multiply the gain by the volume to get the real gain value. + // And then we substract the real gain value to our current volume value. + _volume = Math.Clamp(_volume - (_volume * gain), 0.0f, 1.0f); + + _volumeChanged = true; + } } /// diff --git a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs index bf350db0a1..8629dc969c 100644 --- a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs +++ b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs @@ -104,7 +104,7 @@ namespace Ryujinx.Audio if (releasedCount > 0) { - // NOTE: If we signal, then we also need to have released buffers available + // If we signal, then we also need to have released buffers available // to return when GetReleasedBuffers is called. // If playback needs to be re-started due to all buffers being processed, // then OpenAL zeros the counts (ReleasedCount), so we keep it on the queue. diff --git a/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioOut.cs b/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioOut.cs index 751d7c9f16..2e4f04d780 100644 --- a/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioOut.cs +++ b/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioOut.cs @@ -20,6 +20,11 @@ namespace Ryujinx.Audio /// private float _volume = 1.0f; + /// + /// True if the volume of audio renderer have changed + /// + private bool _volumeChanged; + /// /// The audio context /// @@ -74,7 +79,7 @@ namespace Ryujinx.Audio return -1; } - // NOTE: Open the output. We currently only support 16-bit signed LE + // Open the output. We currently only support 16-bit signed LE track.Open(sampleRate, channels, callback, SoundIOFormat.S16LE); return track.TrackID; @@ -88,10 +93,10 @@ namespace Ryujinx.Audio { if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) { - // NOTE: Close and dispose of the track + // Close and dispose of the track track.Close(); - // NOTE: Recycle the track back into the pool + // Recycle the track back into the pool _trackPool.Put(track); } } @@ -145,7 +150,13 @@ namespace Ryujinx.Audio { if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) { - track.AudioStream.SetVolume(_volume); + if (_volumeChanged) + { + track.AudioStream.SetVolume(_volume); + + _volumeChanged = false; + } + track.AppendBuffer(bufferTag, buffer); } } @@ -182,10 +193,18 @@ namespace Ryujinx.Audio /// /// Set playback volume /// - /// The volume of the playback - public void SetVolume(float volume) + /// The gain of the playback + public void SetVolume(float gain) { - _volume = Math.Clamp(_volume - (_volume * volume), 0.0f, 1.0f); + if (!_volumeChanged) + { + // Games send a gain value here, so we need to apply it on the current volume value. + // In that way we have to multiply the gain by the volume to get the real gain value. + // And then we substract the real gain value to our current volume value. + _volume = Math.Clamp(_volume - (_volume * gain), 0.0f, 1.0f); + + _volumeChanged = true; + } } /// @@ -253,12 +272,10 @@ namespace Ryujinx.Audio try { - context = new SoundIO - { - OnBackendDisconnect = (i) => - { - backendDisconnected = true; - } + context = new SoundIO(); + + context.OnBackendDisconnect = (i) => { + backendDisconnected = true; }; context.Connect();