Address comments

This commit is contained in:
Ac_K 2019-09-29 19:49:51 +02:00
commit dd308c3494
5 changed files with 57 additions and 22 deletions

View file

@ -20,7 +20,7 @@ namespace Ryujinx.Audio
float GetVolume(); float GetVolume();
void SetVolume(float volume); void SetVolume(float gain);
PlaybackState GetState(int trackId); PlaybackState GetState(int trackId);
} }

View file

@ -83,9 +83,9 @@ namespace Ryujinx.Audio
public float GetVolume() => _volume; 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() public void Dispose()

View file

@ -42,6 +42,11 @@ namespace Ryujinx.Audio
/// </summary> /// </summary>
private float _volume = 1.0f; 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>
@ -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); Thread.Sleep(10);
} }
while (_keepPolling); while (_keepPolling);
@ -230,9 +235,14 @@ namespace Ryujinx.Audio
ALSourceState State = (ALSourceState)stateInt; ALSourceState State = (ALSourceState)stateInt;
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); AL.Source(track.SourceId, ALSourcef.Gain, _volume);
_volumeChanged = false;
}
AL.SourcePlay(track.SourceId); AL.SourcePlay(track.SourceId);
} }
} }
@ -262,10 +272,18 @@ namespace Ryujinx.Audio
/// <summary> /// <summary>
/// Set playback volume /// Set playback volume
/// </summary> /// </summary>
/// <param name="volume">The volume of the playback</param> /// <param name="gain">The gain of the playback</param>
public void SetVolume(float volume) 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;
}
} }
/// <summary> /// <summary>

View file

@ -104,7 +104,7 @@ namespace Ryujinx.Audio
if (releasedCount > 0) 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. // to return when GetReleasedBuffers is called.
// If playback needs to be re-started due to all buffers being processed, // 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. // then OpenAL zeros the counts (ReleasedCount), so we keep it on the queue.

View file

@ -20,6 +20,11 @@ namespace Ryujinx.Audio
/// </summary> /// </summary>
private float _volume = 1.0f; 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>
@ -74,7 +79,7 @@ namespace Ryujinx.Audio
return -1; 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); track.Open(sampleRate, channels, callback, SoundIOFormat.S16LE);
return track.TrackID; return track.TrackID;
@ -88,10 +93,10 @@ namespace Ryujinx.Audio
{ {
if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{ {
// NOTE: Close and dispose of the track // Close and dispose of the track
track.Close(); track.Close();
// NOTE: Recycle the track back into the pool // Recycle the track back into the pool
_trackPool.Put(track); _trackPool.Put(track);
} }
} }
@ -144,8 +149,14 @@ namespace Ryujinx.Audio
public void AppendBuffer<T>(int trackId, long bufferTag, T[] buffer) where T : struct public void AppendBuffer<T>(int trackId, long bufferTag, T[] buffer) where T : struct
{ {
if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track)) if (_trackPool.TryGet(trackId, out SoundIoAudioTrack track))
{
if (_volumeChanged)
{ {
track.AudioStream.SetVolume(_volume); track.AudioStream.SetVolume(_volume);
_volumeChanged = false;
}
track.AppendBuffer(bufferTag, buffer); track.AppendBuffer(bufferTag, buffer);
} }
} }
@ -182,10 +193,18 @@ namespace Ryujinx.Audio
/// <summary> /// <summary>
/// Set playback volume /// Set playback volume
/// </summary> /// </summary>
/// <param name="volume">The volume of the playback</param> /// <param name="gain">The gain of the playback</param>
public void SetVolume(float volume) 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;
}
} }
/// <summary> /// <summary>
@ -253,12 +272,10 @@ namespace Ryujinx.Audio
try try
{ {
context = new SoundIO context = new SoundIO();
{
OnBackendDisconnect = (i) => context.OnBackendDisconnect = (i) => {
{
backendDisconnected = true; backendDisconnected = true;
}
}; };
context.Connect(); context.Connect();