0
0
Fork 0

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
This commit is contained in:
Ac_K 2020-11-20 21:59:01 +01:00 committed by GitHub
parent 9493cdfe55
commit 57c4e6ef21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 254 additions and 63 deletions

View file

@ -45,9 +45,15 @@ namespace Ryujinx.Audio
void Stop(int trackId); 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); PlaybackState GetState(int trackId);
} }

View file

@ -1,5 +1,4 @@
using System; using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
namespace Ryujinx.Audio namespace Ryujinx.Audio
@ -15,6 +14,7 @@ namespace Ryujinx.Audio
private ConcurrentQueue<int> _trackIds; private ConcurrentQueue<int> _trackIds;
private ConcurrentQueue<long> _buffers; private ConcurrentQueue<long> _buffers;
private ConcurrentDictionary<int, ReleaseCallback> _releaseCallbacks; private ConcurrentDictionary<int, ReleaseCallback> _releaseCallbacks;
private ulong _playedSampleCount;
public DummyAudioOut() public DummyAudioOut()
{ {
@ -76,6 +76,8 @@ namespace Ryujinx.Audio
{ {
_buffers.Enqueue(bufferTag); _buffers.Enqueue(bufferTag);
_playedSampleCount += (ulong)buffer.Length;
if (_releaseCallbacks.TryGetValue(trackId, out var callback)) if (_releaseCallbacks.TryGetValue(trackId, out var callback))
{ {
callback?.Invoke(); callback?.Invoke();
@ -86,9 +88,15 @@ namespace Ryujinx.Audio
public void Stop(int trackId) { } 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; _volume = volume;
} }

View file

@ -37,16 +37,6 @@ namespace Ryujinx.Audio
/// </summary> /// </summary>
private Thread _audioPollerThread; private Thread _audioPollerThread;
/// <summary>
/// The volume of audio renderer
/// </summary>
private float _volume = 1.0f;
/// <summary>
/// True if the volume of audio renderer have changed
/// </summary>
private bool _volumeChanged;
/// <summary> /// <summary>
/// True if OpenAL is supported on the device /// True if OpenAL is supported on the device
/// </summary> /// </summary>
@ -248,6 +238,8 @@ namespace Ryujinx.Audio
AL.SourceQueueBuffer(track.SourceId, bufferId); AL.SourceQueueBuffer(track.SourceId, bufferId);
StartPlaybackIfNeeded(track); StartPlaybackIfNeeded(track);
track.PlayedSampleCount += (ulong)buffer.Length;
} }
} }
} }
@ -277,13 +269,6 @@ namespace Ryujinx.Audio
if (State != ALSourceState.Playing && track.State == PlaybackState.Playing) if (State != ALSourceState.Playing && track.State == PlaybackState.Playing)
{ {
if (_volumeChanged)
{
AL.Source(track.SourceId, ALSourcef.Gain, _volume);
_volumeChanged = false;
}
AL.SourcePlay(track.SourceId); AL.SourcePlay(track.SourceId);
} }
} }
@ -306,23 +291,89 @@ namespace Ryujinx.Audio
} }
/// <summary> /// <summary>
/// Get playback volume /// Get track buffer count
/// </summary> /// </summary>
public float GetVolume() => _volume; /// <param name="trackId">The ID of the track to get buffer count</param>
public uint GetBufferCount(int trackId)
{
if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track))
{
lock (track)
{
return track.BufferCount;
}
}
return 0;
}
/// <summary> /// <summary>
/// Set playback volume /// Get track played sample count
/// </summary> /// </summary>
/// <param name="volume">The volume of the playback</param> /// <param name="trackId">The ID of the track to get played sample count</param>
public void SetVolume(float volume) public ulong GetPlayedSampleCount(int trackId)
{ {
if (!_volumeChanged) if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track))
{ {
_volume = volume; lock (track)
_volumeChanged = true; {
return track.PlayedSampleCount;
} }
} }
return 0;
}
/// <summary>
/// Flush all track buffers
/// </summary>
/// <param name="trackId">The ID of the track to flush</param>
public bool FlushBuffers(int trackId)
{
if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track))
{
lock (track)
{
track.FlushBuffers();
}
}
return false;
}
/// <summary>
/// Set track volume
/// </summary>
/// <param name="trackId">The ID of the track to set volume</param>
/// <param name="volume">The volume of the track</param>
public void SetVolume(int trackId, float volume)
{
if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track))
{
lock (track)
{
track.SetVolume(volume);
}
}
}
/// <summary>
/// Get track volume
/// </summary>
/// <param name="trackId">The ID of the track to get volume</param>
public float GetVolume(int trackId)
{
if (_tracks.TryGetValue(trackId, out OpenALAudioTrack track))
{
lock (track)
{
return track.Volume;
}
}
return 1.0f;
}
/// <summary> /// <summary>
/// Gets the current playback state of the specified track /// Gets the current playback state of the specified track
/// </summary> /// </summary>

View file

@ -11,9 +11,12 @@ namespace Ryujinx.Audio
public int SampleRate { get; private set; } public int SampleRate { get; private set; }
public ALFormat Format { get; private set; } public ALFormat Format { get; private set; }
public PlaybackState State { get; set; } public PlaybackState State { get; set; }
public float Volume { get; private set; }
public int HardwareChannels { get; } public int HardwareChannels { get; }
public int VirtualChannels { get; } public int VirtualChannels { get; }
public uint BufferCount => (uint)_buffers.Count;
public ulong PlayedSampleCount { get; set; }
private ReleaseCallback _callback; 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() public void Dispose()
{ {
Dispose(true); Dispose(true);

View file

@ -15,16 +15,6 @@ namespace Ryujinx.Audio
/// </summary> /// </summary>
private const int MaximumTracks = 256; private const int MaximumTracks = 256;
/// <summary>
/// The volume of audio renderer
/// </summary>
private float _volume = 1.0f;
/// <summary>
/// True if the volume of audio renderer have changed
/// </summary>
private bool _volumeChanged;
/// <summary> /// <summary>
/// The <see cref="SoundIO"/> audio context /// The <see cref="SoundIO"/> audio context
/// </summary> /// </summary>
@ -156,13 +146,6 @@ namespace Ryujinx.Audio
{ {
if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{ {
if (_volumeChanged)
{
track.AudioStream.SetVolume(_volume);
_volumeChanged = false;
}
track.AppendBuffer(bufferTag, buffer); track.AppendBuffer(bufferTag, buffer);
} }
} }
@ -192,23 +175,72 @@ namespace Ryujinx.Audio
} }
/// <summary> /// <summary>
/// Get playback volume /// Get track buffer count
/// </summary> /// </summary>
public float GetVolume() => _volume; /// <param name="trackId">The ID of the track to get buffer count</param>
public uint GetBufferCount(int trackId)
{
if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
return track.BufferCount;
}
return 0;
}
/// <summary> /// <summary>
/// Set playback volume /// Get track played sample count
/// </summary>
/// <param name="trackId">The ID of the track to get played sample</param>
public ulong GetPlayedSampleCount(int trackId)
{
if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
return track.PlayedSampleCount;
}
return 0;
}
/// <summary>
/// Flush all track buffers
/// </summary>
/// <param name="trackId">The ID of the track to flush</param>
public bool FlushBuffers(int trackId)
{
if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
return track.FlushBuffers();
}
return false;
}
/// <summary>
/// Set track volume
/// </summary> /// </summary>
/// <param name="volume">The volume of the playback</param> /// <param name="volume">The volume of the playback</param>
public void SetVolume(float volume) public void SetVolume(int trackId, float volume)
{ {
if (!_volumeChanged) if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{ {
_volume = volume; track.AudioStream.SetVolume(volume);
_volumeChanged = true;
} }
} }
/// <summary>
/// Get track volume
/// </summary>
public float GetVolume(int trackId)
{
if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
return track.AudioStream.Volume;
}
return 1.0f;
}
/// <summary> /// <summary>
/// Gets the current playback state of the specified track /// Gets the current playback state of the specified track
/// </summary> /// </summary>

View file

@ -54,6 +54,16 @@ namespace Ryujinx.Audio.SoundIo
/// </summary> /// </summary>
public ConcurrentQueue<long> ReleasedBuffers { get; private set; } public ConcurrentQueue<long> ReleasedBuffers { get; private set; }
/// <summary>
/// Buffer count of the track
/// </summary>
public uint BufferCount => (uint)m_ReservedBuffers.Count;
/// <summary>
/// Played sample count of the track
/// </summary>
public ulong PlayedSampleCount { get; private set; }
private int _hardwareChannels; private int _hardwareChannels;
private int _virtualChannels; private int _virtualChannels;
@ -430,6 +440,8 @@ namespace Ryujinx.Audio.SoundIo
AudioStream.EndWrite(); AudioStream.EndWrite();
PlayedSampleCount += (ulong)samples.Length;
UpdateReleasedBuffers(samples.Length); UpdateReleasedBuffers(samples.Length);
} }
@ -571,6 +583,28 @@ namespace Ryujinx.Audio.SoundIo
return m_ReservedBuffers.Any(x => x.Tag == bufferTag); return m_ReservedBuffers.Any(x => x.Tag == bufferTag);
} }
/// <summary>
/// Flush all track buffers
/// </summary>
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;
}
/// <summary> /// <summary>
/// Closes the <see cref="SoundIoAudioTrack"/> /// Closes the <see cref="SoundIoAudioTrack"/>
/// </summary> /// </summary>

View file

@ -149,17 +149,46 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager
return ResultCode.Success; 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+ [Command(12)] // 6.0.0+
// SetAudioOutVolume(s32) // SetAudioOutVolume(s32)
public ResultCode SetAudioOutVolume(ServiceCtx context) 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(); _audioOut.SetVolume(_track, volume);
float currentVolume = _audioOut.GetVolume();
float newVolume = Math.Clamp(currentVolume + gain, 0.0f, 1.0f);
_audioOut.SetVolume(newVolume);
return ResultCode.Success; return ResultCode.Success;
} }
@ -168,7 +197,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager
// GetAudioOutVolume() -> s32 // GetAudioOutVolume() -> s32
public ResultCode GetAudioOutVolume(ServiceCtx context) public ResultCode GetAudioOutVolume(ServiceCtx context)
{ {
float volume = _audioOut.GetVolume(); float volume = _audioOut.GetVolume(_track);
context.ResponseData.Write(volume); context.ResponseData.Write(volume);