Use event callback to get audio endpoint volume

This commit is contained in:
Travis Nickles 2017-12-10 02:05:56 -06:00
parent bc3b042bcf
commit 132d411b24

View File

@ -4,7 +4,7 @@ using DS4Windows.DS4Library.CoreAudio;
namespace DS4Windows.DS4Library namespace DS4Windows.DS4Library
{ {
public class DS4Audio public class DS4Audio : IAudioEndpointVolumeCallback
{ {
private IAudioEndpointVolume endpointVolume; private IAudioEndpointVolume endpointVolume;
@ -12,27 +12,32 @@ namespace DS4Windows.DS4Library
private static readonly PropertyKey PKEY_Device_FriendlyName = private static readonly PropertyKey PKEY_Device_FriendlyName =
new PropertyKey(new Guid(unchecked((int)0xa45c254e), unchecked((short)0xdf1c), 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0), 14); new PropertyKey(new Guid(unchecked((int)0xa45c254e), unchecked((short)0xdf1c), 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0), 14);
public uint vol;
public uint Volume public uint Volume
{ {
get get
{ {
float pfLevel = 0; return vol;
if (endpointVolume != null)
endpointVolume.GetMasterVolumeLevelScalar(out pfLevel);
return Convert.ToUInt32(pfLevel * 100);
} }
} }
public uint getVolume() public uint getVolume()
{
return vol;
}
public void RefreshVolume()
{ {
float pfLevel = 0; float pfLevel = 0;
if (endpointVolume != null) if (endpointVolume != null)
endpointVolume.GetMasterVolumeLevelScalar(out pfLevel); endpointVolume.GetMasterVolumeLevelScalar(out pfLevel);
vol = Convert.ToUInt32(pfLevel * 100);
}
return Convert.ToUInt32(pfLevel * 100); public void OnNotify(IntPtr pNotify)
{
RefreshVolume();
} }
public DS4Audio(DataFlow audioFlags = DataFlow.Render) public DS4Audio(DataFlow audioFlags = DataFlow.Render)
@ -55,20 +60,24 @@ namespace DS4Windows.DS4Library
object interfacePointer; object interfacePointer;
Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer)); Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer));
endpointVolume = interfacePointer as IAudioEndpointVolume; endpointVolume = interfacePointer as IAudioEndpointVolume;
endpointVolume.RegisterControlChangeNotify(this);
} }
else if (deviceName.Contains("Headset Earphone (Wireless Controller)")) else if (deviceName.Contains("Headset Earphone (Wireless Controller)"))
{ {
object interfacePointer; object interfacePointer;
Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer)); Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer));
endpointVolume = interfacePointer as IAudioEndpointVolume; endpointVolume = interfacePointer as IAudioEndpointVolume;
endpointVolume.RegisterControlChangeNotify(this);
} }
else if (deviceName.Contains("Headset Microphone (Wireless Controller)")) else if (deviceName.Contains("Headset Microphone (Wireless Controller)"))
{ {
object interfacePointer; object interfacePointer;
Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer)); Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer));
endpointVolume = interfacePointer as IAudioEndpointVolume; endpointVolume = interfacePointer as IAudioEndpointVolume;
endpointVolume.RegisterControlChangeNotify(this);
} }
RefreshVolume();
Marshal.ReleaseComObject(audioDevice); Marshal.ReleaseComObject(audioDevice);
} }
@ -80,6 +89,7 @@ namespace DS4Windows.DS4Library
{ {
if (endpointVolume != null) if (endpointVolume != null)
{ {
endpointVolume.UnregisterControlChangeNotify(this);
Marshal.ReleaseComObject(endpointVolume); Marshal.ReleaseComObject(endpointVolume);
endpointVolume = null; endpointVolume = null;
} }