diff --git a/Ryujinx.Audio/IAalOutput.cs b/Ryujinx.Audio/IAalOutput.cs index 821c1ffb55..056b466549 100644 --- a/Ryujinx.Audio/IAalOutput.cs +++ b/Ryujinx.Audio/IAalOutput.cs @@ -45,9 +45,15 @@ namespace Ryujinx.Audio void Stop(int trackId); - float GetVolume(); + uint GetBufferCount(int trackId); - void SetVolume(float volume); + ulong GetPlayedSampleCount(int trackId); + + bool FlushBuffers(int trackId); + + float GetVolume(int trackId); + + void SetVolume(int trackId, float volume); PlaybackState GetState(int trackId); } diff --git a/Ryujinx.Audio/Renderers/DummyAudioOut.cs b/Ryujinx.Audio/Renderers/DummyAudioOut.cs index 2698b92819..cd197592f4 100644 --- a/Ryujinx.Audio/Renderers/DummyAudioOut.cs +++ b/Ryujinx.Audio/Renderers/DummyAudioOut.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Concurrent; +using System.Collections.Concurrent; using System.Collections.Generic; namespace Ryujinx.Audio @@ -15,6 +14,7 @@ namespace Ryujinx.Audio private ConcurrentQueue _trackIds; private ConcurrentQueue _buffers; private ConcurrentDictionary _releaseCallbacks; + private ulong _playedSampleCount; public DummyAudioOut() { @@ -76,6 +76,8 @@ namespace Ryujinx.Audio { _buffers.Enqueue(bufferTag); + _playedSampleCount += (ulong)buffer.Length; + if (_releaseCallbacks.TryGetValue(trackId, out var callback)) { callback?.Invoke(); @@ -86,9 +88,15 @@ namespace Ryujinx.Audio public void Stop(int trackId) { } - public float GetVolume() => _volume; + public uint GetBufferCount(int trackId) => (uint)_buffers.Count; - public void SetVolume(float volume) + public ulong GetPlayedSampleCount(int trackId) => _playedSampleCount; + + public bool FlushBuffers(int trackId) => false; + + public float GetVolume(int trackId) => _volume; + + public void SetVolume(int trackId, float volume) { _volume = volume; } diff --git a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs index fe82fced26..abad0f17fa 100644 --- a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs +++ b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs @@ -37,16 +37,6 @@ namespace Ryujinx.Audio /// private Thread _audioPollerThread; - /// - /// The volume of audio renderer - /// - 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 /// @@ -248,6 +238,8 @@ namespace Ryujinx.Audio AL.SourceQueueBuffer(track.SourceId, bufferId); StartPlaybackIfNeeded(track); + + track.PlayedSampleCount += (ulong)buffer.Length; } } } @@ -277,13 +269,6 @@ namespace Ryujinx.Audio if (State != ALSourceState.Playing && track.State == PlaybackState.Playing) { - if (_volumeChanged) - { - AL.Source(track.SourceId, ALSourcef.Gain, _volume); - - _volumeChanged = false; - } - AL.SourcePlay(track.SourceId); } } @@ -306,21 +291,87 @@ namespace Ryujinx.Audio } /// - /// Get playback volume + /// Get track buffer count /// - public float GetVolume() => _volume; + /// The ID of the track to get buffer count + public uint GetBufferCount(int trackId) + { + if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) + { + lock (track) + { + return track.BufferCount; + } + } + + return 0; + } /// - /// Set playback volume + /// Get track played sample count /// - /// The volume of the playback - public void SetVolume(float volume) + /// The ID of the track to get played sample count + public ulong GetPlayedSampleCount(int trackId) { - if (!_volumeChanged) + if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) { - _volume = volume; - _volumeChanged = true; + lock (track) + { + return track.PlayedSampleCount; + } } + + return 0; + } + + /// + /// Flush all track buffers + /// + /// The ID of the track to flush + public bool FlushBuffers(int trackId) + { + if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) + { + lock (track) + { + track.FlushBuffers(); + } + } + + return false; + } + + /// + /// Set track volume + /// + /// The ID of the track to set volume + /// The volume of the track + public void SetVolume(int trackId, float volume) + { + if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) + { + lock (track) + { + track.SetVolume(volume); + } + } + } + + /// + /// Get track volume + /// + /// The ID of the track to get volume + public float GetVolume(int trackId) + { + if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track)) + { + lock (track) + { + return track.Volume; + } + } + + return 1.0f; } /// diff --git a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs index 2f15099866..6e01671373 100644 --- a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs +++ b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs @@ -11,9 +11,12 @@ namespace Ryujinx.Audio public int SampleRate { get; private set; } public ALFormat Format { get; private set; } public PlaybackState State { get; set; } + public float Volume { get; private set; } public int HardwareChannels { get; } public int VirtualChannels { get; } + public uint BufferCount => (uint)_buffers.Count; + public ulong PlayedSampleCount { get; set; } private ReleaseCallback _callback; @@ -125,6 +128,34 @@ namespace Ryujinx.Audio } } + public bool FlushBuffers() + { + while (_queuedTagsQueue.TryDequeue(out long tag)) + { + _releasedTagsQueue.Enqueue(tag); + } + + _callback(); + + foreach (var buffer in _buffers) + { + AL.DeleteBuffer(buffer.Value); + } + + bool heldBuffers = _buffers.Count > 0; + + _buffers.Clear(); + + return heldBuffers; + } + + public void SetVolume(float volume) + { + Volume = volume; + + AL.Source(SourceId, ALSourcef.Gain, Volume); + } + public void Dispose() { Dispose(true); diff --git a/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioOut.cs b/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioOut.cs index fa3961e4b4..eb6caa6091 100644 --- a/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioOut.cs +++ b/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioOut.cs @@ -15,16 +15,6 @@ namespace Ryujinx.Audio /// private const int MaximumTracks = 256; - /// - /// The volume of audio renderer - /// - private float _volume = 1.0f; - - /// - /// True if the volume of audio renderer have changed - /// - private bool _volumeChanged; - /// /// The audio context /// @@ -155,14 +145,7 @@ namespace Ryujinx.Audio public void AppendBuffer(int trackId, long bufferTag, T[] buffer) where T : struct { if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) - { - if (_volumeChanged) - { - track.AudioStream.SetVolume(_volume); - - _volumeChanged = false; - } - + { track.AppendBuffer(bufferTag, buffer); } } @@ -192,23 +175,72 @@ namespace Ryujinx.Audio } /// - /// Get playback volume + /// Get track buffer count /// - public float GetVolume() => _volume; + /// The ID of the track to get buffer count + public uint GetBufferCount(int trackId) + { + if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) + { + return track.BufferCount; + } + + return 0; + } /// - /// Set playback volume + /// Get track played sample count + /// + /// The ID of the track to get played sample + public ulong GetPlayedSampleCount(int trackId) + { + if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) + { + return track.PlayedSampleCount; + } + + return 0; + } + + /// + /// Flush all track buffers + /// + /// The ID of the track to flush + public bool FlushBuffers(int trackId) + { + if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) + { + return track.FlushBuffers(); + } + + return false; + } + + /// + /// Set track volume /// /// The volume of the playback - public void SetVolume(float volume) + public void SetVolume(int trackId, float volume) { - if (!_volumeChanged) + if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) { - _volume = volume; - _volumeChanged = true; + track.AudioStream.SetVolume(volume); } } + /// + /// Get track volume + /// + public float GetVolume(int trackId) + { + if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) + { + return track.AudioStream.Volume; + } + + return 1.0f; + } + /// /// Gets the current playback state of the specified track /// diff --git a/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioTrack.cs b/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioTrack.cs index 6fdeb99148..52c4ebc9ae 100644 --- a/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioTrack.cs +++ b/Ryujinx.Audio/Renderers/SoundIo/SoundIoAudioTrack.cs @@ -54,6 +54,16 @@ namespace Ryujinx.Audio.SoundIo /// public ConcurrentQueue ReleasedBuffers { get; private set; } + /// + /// Buffer count of the track + /// + public uint BufferCount => (uint)m_ReservedBuffers.Count; + + /// + /// Played sample count of the track + /// + public ulong PlayedSampleCount { get; private set; } + private int _hardwareChannels; private int _virtualChannels; @@ -430,6 +440,8 @@ namespace Ryujinx.Audio.SoundIo AudioStream.EndWrite(); + PlayedSampleCount += (ulong)samples.Length; + UpdateReleasedBuffers(samples.Length); } @@ -571,6 +583,28 @@ namespace Ryujinx.Audio.SoundIo return m_ReservedBuffers.Any(x => x.Tag == bufferTag); } + /// + /// Flush all track buffers + /// + public bool FlushBuffers() + { + m_Buffer.Clear(); + + if (m_ReservedBuffers.Count > 0) + { + foreach (var buffer in m_ReservedBuffers) + { + ReleasedBuffers.Enqueue(buffer.Tag); + } + + OnBufferReleased(); + + return true; + } + + return false; + } + /// /// Closes the /// diff --git a/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs b/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs index d75fecf2a4..eaf644f6ce 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs @@ -149,17 +149,46 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager return ResultCode.Success; } + [Command(9)] // 4.0.0+ + // GetAudioOutBufferCount() -> u32 + public ResultCode GetAudioOutBufferCount(ServiceCtx context) + { + uint bufferCount = _audioOut.GetBufferCount(_track); + + context.ResponseData.Write(bufferCount); + + return ResultCode.Success; + } + + [Command(10)] // 4.0.0+ + // GetAudioOutPlayedSampleCount() -> u64 + public ResultCode GetAudioOutPlayedSampleCount(ServiceCtx context) + { + ulong playedSampleCount = _audioOut.GetPlayedSampleCount(_track); + + context.ResponseData.Write(playedSampleCount); + + return ResultCode.Success; + } + + [Command(11)] // 4.0.0+ + // FlushAudioOutBuffers() -> b8 + public ResultCode FlushAudioOutBuffers(ServiceCtx context) + { + bool heldBuffers = _audioOut.FlushBuffers(_track); + + context.ResponseData.Write(heldBuffers); + + return ResultCode.Success; + } + [Command(12)] // 6.0.0+ // SetAudioOutVolume(s32) public ResultCode SetAudioOutVolume(ServiceCtx context) { - // Games send a gain value here, so we need to apply it on the current volume value. + float volume = context.RequestData.ReadSingle(); - float gain = context.RequestData.ReadSingle(); - float currentVolume = _audioOut.GetVolume(); - float newVolume = Math.Clamp(currentVolume + gain, 0.0f, 1.0f); - - _audioOut.SetVolume(newVolume); + _audioOut.SetVolume(_track, volume); return ResultCode.Success; } @@ -168,7 +197,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager // GetAudioOutVolume() -> s32 public ResultCode GetAudioOutVolume(ServiceCtx context) { - float volume = _audioOut.GetVolume(); + float volume = _audioOut.GetVolume(_track); context.ResponseData.Write(volume);