Browse Source

Modified WaveOut and WaveOutEvent to read the actual volume

Added additional code to WaveOut and WaveOutEvent to read the actual volume from the system rather than returning a cached value.
pull/349/head
Neil Thiessen 7 years ago
parent
commit
6aedf4901c
  1. 21
      NAudio/Wave/WaveOutputs/WaveOut.cs
  2. 9
      NAudio/Wave/WaveOutputs/WaveOutEvent.cs

21
NAudio/Wave/WaveOutputs/WaveOut.cs

@ -15,7 +15,6 @@ namespace NAudio.Wave
private IWaveProvider waveStream;
private volatile PlaybackState playbackState;
private readonly WaveInterop.WaveCallback callback;
private float volume = 1;
private readonly WaveCallbackInfo callbackInfo;
private readonly object waveOutLock;
private int queuedBuffers;
@ -276,14 +275,28 @@ namespace NAudio.Wave
/// </summary>
public float Volume
{
get => volume;
set
get
{
return GetWaveOutVolume(hWaveOut, waveOutLock);
}
set
{
SetWaveOutVolume(value, hWaveOut, waveOutLock);
volume = value;
}
}
internal static float GetWaveOutVolume(IntPtr hWaveOut, object lockObject)
{
int stereoVolume;
MmResult result;
lock (lockObject)
{
result = WaveInterop.waveOutGetVolume(hWaveOut, out stereoVolume);
}
MmException.Try(result, "waveOutGetVolume");
return (stereoVolume & 0xFFFF) / (float)0xFFFF;
}
internal static void SetWaveOutVolume(float value, IntPtr hWaveOut, object lockObject)
{
if (value < 0) throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0");

9
NAudio/Wave/WaveOutputs/WaveOutEvent.cs

@ -18,7 +18,6 @@ namespace NAudio.Wave
private IWaveProvider waveStream;
private volatile PlaybackState playbackState;
private AutoResetEvent callbackEvent;
private float volume = 1.0f;
/// <summary>
/// Indicates playback has stopped automatically
@ -281,15 +280,17 @@ namespace NAudio.Wave
}
/// <summary>
/// Obsolete property
/// Volume for this device 1.0 is full scale
/// </summary>
public float Volume
{
get { return volume; }
get
{
return WaveOut.GetWaveOutVolume(hWaveOut, waveOutLock);
}
set
{
WaveOut.SetWaveOutVolume(value, hWaveOut, waveOutLock);
volume = value;
}
}

Loading…
Cancel
Save