From 57c4e6ef21d1f281b172aedcfd993a2ac43456ef Mon Sep 17 00:00:00 2001 From: Ac_K Date: Fri, 20 Nov 2020 21:59:01 +0100 Subject: audout: Implement and fix some calls (#1725) * audout: Implement GetAudioOutBufferCount, GetAudioOutPlayedSampleCount and FlushAudioOutBuffers This PR implement audout service calls: - GetAudioOutBufferCount - GetAudioOutPlayedSampleCount - FlushAudioOutBuffers The RE calls just give some hints about no extra checks. Since we use a totally different implementation because of our backend, I can't do something better for now. SetAudioOutVolume and GetAudioOutVolume are fixed too by set/get the volume of the current opened track, previous implementation was wrong. This fix #1133, fix #1258 and fix #1519. Thanks to @jduncanator for this help during the implementation and all his precious advices. * Fix some debug leftovers * Address jD feedback --- Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs | 101 ++++++++++++++++----- Ryujinx.Audio/Renderers/OpenAL/OpenALAudioTrack.cs | 31 +++++++ 2 files changed, 107 insertions(+), 25 deletions(-) (limited to 'Ryujinx.Audio/Renderers/OpenAL') diff --git a/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs b/Ryujinx.Audio/Renderers/OpenAL/OpenALAudioOut.cs index fe82fced..abad0f17 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 2f150998..6e016713 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); -- cgit v1.2.3