Skip to content

Commit

Permalink
Modified WaveOut and WaveOutEvent to read the actual volume
Browse files Browse the repository at this point in the history
Added additional code to WaveOut and WaveOutEvent to read the actual volume from the system rather than returning a cached value.
  • Loading branch information
neilt6 committed Jun 20, 2018
1 parent 902b589 commit 6aedf49
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
21 changes: 17 additions & 4 deletions NAudio/Wave/WaveOutputs/WaveOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class WaveOut : IWavePlayer, IWavePosition
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;
Expand Down Expand Up @@ -276,14 +275,28 @@ public long GetPosition()
/// </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");
Expand Down
9 changes: 5 additions & 4 deletions NAudio/Wave/WaveOutputs/WaveOutEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class WaveOutEvent : IWavePlayer, IWavePosition
private IWaveProvider waveStream;
private volatile PlaybackState playbackState;
private AutoResetEvent callbackEvent;
private float volume = 1.0f;

/// <summary>
/// Indicates playback has stopped automatically
Expand Down Expand Up @@ -281,15 +280,17 @@ public PlaybackState PlaybackState
}

/// <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;
}
}

Expand Down

0 comments on commit 6aedf49

Please sign in to comment.