Address comments

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

View file

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

View file

@ -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()

View file

@ -42,6 +42,11 @@ namespace Ryujinx.Audio
/// </summary>
private float _volume = 1.0f;
/// <summary>
/// True if the volume of audio renderer have changed
/// </summary>
private bool _volumeChanged;
/// <summary>
/// True if OpenAL is supported on the device
/// </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);
}
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
/// <summary>
/// Set playback volume
/// </summary>
/// <param name="volume">The volume of the playback</param>
public void SetVolume(float volume)
/// <param name="gain">The gain of the playback</param>
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>

View file

@ -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.

View file

@ -20,6 +20,11 @@ namespace Ryujinx.Audio
/// </summary>
private float _volume = 1.0f;
/// <summary>
/// True if the volume of audio renderer have changed
/// </summary>
private bool _volumeChanged;
/// <summary>
/// The <see cref="SoundIO"/> audio context
/// </summary>
@ -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
/// <summary>
/// Set playback volume
/// </summary>
/// <param name="volume">The volume of the playback</param>
public void SetVolume(float volume)
/// <param name="gain">The gain of the playback</param>
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>
@ -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();