2015-10-17 10:55:27 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2015-12-22 19:44:42 +00:00
|
|
|
|
using System.Linq;
|
2015-10-17 10:55:27 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Local
|
|
|
|
|
{
|
2015-12-22 19:44:42 +00:00
|
|
|
|
internal static class VolumeMixerControl
|
2015-10-17 10:55:27 +01:00
|
|
|
|
{
|
2016-03-31 11:08:23 +01:00
|
|
|
|
private const string SpotifyProcessName = "spotify";
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
2015-10-23 20:25:13 +01:00
|
|
|
|
internal static float GetSpotifyVolume()
|
2015-10-17 10:55:27 +01:00
|
|
|
|
{
|
2016-07-13 16:45:15 +01:00
|
|
|
|
ISimpleAudioVolume volume = GetSpotifyVolumeObject();
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
if (volume == null)
|
2015-10-23 20:25:13 +01:00
|
|
|
|
{
|
|
|
|
|
throw new COMException("Volume object creation failed");
|
|
|
|
|
}
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
2018-03-11 23:13:38 +00:00
|
|
|
|
volume.GetMasterVolume(out float level);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
Marshal.ReleaseComObject(volume);
|
|
|
|
|
return level * 100;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-23 20:25:13 +01:00
|
|
|
|
internal static bool IsSpotifyMuted()
|
2015-10-17 10:55:27 +01:00
|
|
|
|
{
|
2016-07-13 16:45:15 +01:00
|
|
|
|
ISimpleAudioVolume volume = GetSpotifyVolumeObject();
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
if (volume == null)
|
2015-10-23 20:25:13 +01:00
|
|
|
|
{
|
|
|
|
|
throw new COMException("Volume object creation failed");
|
|
|
|
|
}
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
2018-03-11 23:13:38 +00:00
|
|
|
|
volume.GetMute(out bool mute);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
Marshal.ReleaseComObject(volume);
|
|
|
|
|
return mute;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void SetSpotifyVolume(float level)
|
|
|
|
|
{
|
2016-07-13 16:45:15 +01:00
|
|
|
|
ISimpleAudioVolume volume = GetSpotifyVolumeObject();
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
if (volume == null)
|
2015-10-23 20:25:13 +01:00
|
|
|
|
{
|
|
|
|
|
throw new COMException("Volume object creation failed");
|
|
|
|
|
}
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
Guid guid = Guid.Empty;
|
|
|
|
|
volume.SetMasterVolume(level / 100, ref guid);
|
|
|
|
|
Marshal.ReleaseComObject(volume);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void MuteSpotify(bool mute)
|
|
|
|
|
{
|
2016-07-13 16:45:15 +01:00
|
|
|
|
ISimpleAudioVolume volume = GetSpotifyVolumeObject();
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
if (volume == null)
|
2015-10-23 20:25:13 +01:00
|
|
|
|
{
|
|
|
|
|
throw new COMException("Volume object creation failed");
|
|
|
|
|
}
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
Guid guid = Guid.Empty;
|
|
|
|
|
volume.SetMute(mute, ref guid);
|
|
|
|
|
Marshal.ReleaseComObject(volume);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-11 23:08:37 +00:00
|
|
|
|
private static ISimpleAudioVolume GetSpotifyVolumeObject()
|
|
|
|
|
{
|
|
|
|
|
var audioVolumeObjects = from p in Process.GetProcessesByName(SpotifyProcessName)
|
|
|
|
|
let vol = GetVolumeObject(p.Id)
|
|
|
|
|
where vol != null
|
|
|
|
|
select vol;
|
|
|
|
|
return audioVolumeObjects.FirstOrDefault();
|
2015-12-22 19:44:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-17 10:55:27 +01:00
|
|
|
|
private static ISimpleAudioVolume GetVolumeObject(int pid)
|
|
|
|
|
{
|
|
|
|
|
// get the speakers (1st render + multimedia) device
|
2018-03-11 23:13:38 +00:00
|
|
|
|
IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator) new MMDeviceEnumerator();
|
|
|
|
|
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out IMmDevice speakers);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
2018-03-11 23:13:38 +00:00
|
|
|
|
speakers.GetId(out string defaultDeviceId);
|
2018-03-11 23:08:37 +00:00
|
|
|
|
|
|
|
|
|
ISimpleAudioVolume volumeControl = GetVolumeObject(pid, speakers);
|
|
|
|
|
Marshal.ReleaseComObject(speakers);
|
|
|
|
|
|
|
|
|
|
if (volumeControl == null)
|
|
|
|
|
{
|
|
|
|
|
// If volumeControl is null, then the process's volume object might be on a different device.
|
|
|
|
|
// This happens if the process doesn't use the default device.
|
|
|
|
|
//
|
|
|
|
|
// As far as Spotify is concerned, if using the "--enable-audio-graph" command line argument,
|
|
|
|
|
// a new option becomes available in the Settings that makes it possible to change the playback device.
|
|
|
|
|
|
2018-03-11 23:13:38 +00:00
|
|
|
|
deviceEnumerator.EnumAudioEndpoints(EDataFlow.ERender, EDeviceState.Active, out IMmDeviceCollection deviceCollection);
|
2018-03-11 23:08:37 +00:00
|
|
|
|
|
2018-03-11 23:13:38 +00:00
|
|
|
|
deviceCollection.GetCount(out int count);
|
2018-03-11 23:08:37 +00:00
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
2018-03-11 23:13:38 +00:00
|
|
|
|
deviceCollection.Item(i, out IMmDevice device);
|
|
|
|
|
device.GetId(out string deviceId);
|
2018-03-11 23:08:37 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (deviceId == defaultDeviceId)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
volumeControl = GetVolumeObject(pid, device);
|
|
|
|
|
if (volumeControl != null)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Marshal.ReleaseComObject(device);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Marshal.ReleaseComObject(deviceEnumerator);
|
|
|
|
|
return volumeControl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ISimpleAudioVolume GetVolumeObject(int pid, IMmDevice device)
|
|
|
|
|
{
|
2015-10-17 10:55:27 +01:00
|
|
|
|
// activate the session manager. we need the enumerator
|
2015-10-28 16:05:09 +00:00
|
|
|
|
Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
|
2018-03-11 23:13:38 +00:00
|
|
|
|
device.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out object o);
|
|
|
|
|
IAudioSessionManager2 mgr = (IAudioSessionManager2) o;
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
// enumerate sessions for on this device
|
2018-03-11 23:13:38 +00:00
|
|
|
|
mgr.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator);
|
|
|
|
|
sessionEnumerator.GetCount(out int count);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
// search for an audio session with the required name
|
|
|
|
|
// NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2)
|
|
|
|
|
ISimpleAudioVolume volumeControl = null;
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
2018-03-11 23:13:38 +00:00
|
|
|
|
sessionEnumerator.GetSession(i, out IAudioSessionControl2 ctl);
|
|
|
|
|
ctl.GetProcessId(out int cpid);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
if (cpid == pid)
|
|
|
|
|
{
|
2018-03-11 23:13:38 +00:00
|
|
|
|
volumeControl = (ISimpleAudioVolume) ctl;
|
2015-10-17 10:55:27 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Marshal.ReleaseComObject(ctl);
|
|
|
|
|
}
|
|
|
|
|
Marshal.ReleaseComObject(sessionEnumerator);
|
|
|
|
|
Marshal.ReleaseComObject(mgr);
|
|
|
|
|
return volumeControl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ComImport]
|
|
|
|
|
[Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
|
|
|
|
|
private class MMDeviceEnumerator
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private enum EDataFlow
|
|
|
|
|
{
|
2015-10-28 16:05:09 +00:00
|
|
|
|
ERender,
|
|
|
|
|
ECapture,
|
|
|
|
|
EAll,
|
|
|
|
|
EDataFlowEnumCount
|
2015-10-17 10:55:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private enum ERole
|
|
|
|
|
{
|
2015-10-28 16:05:09 +00:00
|
|
|
|
EConsole,
|
|
|
|
|
EMultimedia,
|
|
|
|
|
ECommunications,
|
|
|
|
|
ERoleEnumCount
|
2015-10-17 10:55:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-11 23:08:37 +00:00
|
|
|
|
[Flags]
|
|
|
|
|
private enum EDeviceState
|
|
|
|
|
{
|
|
|
|
|
Active = 0x00000001,
|
|
|
|
|
Disabled = 0x00000002,
|
|
|
|
|
NotPresent = 0x00000004,
|
|
|
|
|
UnPlugged = 0x00000008,
|
|
|
|
|
All = 0x0000000F
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-17 10:55:27 +01:00
|
|
|
|
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
private interface IMmDeviceEnumerator
|
2015-10-17 10:55:27 +01:00
|
|
|
|
{
|
2018-03-11 23:08:37 +00:00
|
|
|
|
[PreserveSig]
|
|
|
|
|
int EnumAudioEndpoints(EDataFlow dataFlow, EDeviceState stateMask, [Out] out IMmDeviceCollection deviceCollection);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMmDevice ppDevice);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
private interface IMmDevice
|
2015-10-17 10:55:27 +01:00
|
|
|
|
{
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int Activate(ref Guid iid, int dwClsCtx, IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);
|
2018-03-11 23:08:37 +00:00
|
|
|
|
|
|
|
|
|
int OpenPropertyStore_NotImpl();
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetId([Out, MarshalAs(UnmanagedType.LPWStr)] out string ppstrId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Guid("0BD7A1BE-7A1A-44DB-8397-CC5392387B5E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
|
|
|
|
private interface IMmDeviceCollection
|
|
|
|
|
{
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetCount(out int deviceCount);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int Item(int deviceIndex, [Out] out IMmDevice device);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Guid("77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
|
|
|
|
private interface IAudioSessionManager2
|
|
|
|
|
{
|
|
|
|
|
int NotImpl1();
|
2015-10-17 10:57:26 +01:00
|
|
|
|
|
2015-10-17 10:55:27 +01:00
|
|
|
|
int NotImpl2();
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int GetSessionEnumerator(out IAudioSessionEnumerator sessionEnum);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Guid("E2F5BB11-0570-40CA-ACDD-3AA01277DEE8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
|
|
|
|
private interface IAudioSessionEnumerator
|
|
|
|
|
{
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int GetCount(out int sessionCount);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int GetSession(int sessionCount, out IAudioSessionControl2 session);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Guid("87CE5498-68D6-44E5-9215-6DA47EF883D8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
|
|
|
|
private interface ISimpleAudioVolume
|
|
|
|
|
{
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int SetMasterVolume(float fLevel, ref Guid eventContext);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetMasterVolume(out float pfLevel);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int SetMute(bool bMute, ref Guid eventContext);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetMute(out bool pbMute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Guid("bfb7ff88-7239-4fc9-8fa2-07c950be9c6d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
|
|
|
|
private interface IAudioSessionControl2
|
|
|
|
|
{
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int NotImpl0();
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetDisplayName([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)]string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetIconPath([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetGroupingParam(out Guid pRetVal);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
2015-10-28 16:05:09 +00:00
|
|
|
|
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
|
2015-10-17 10:55:27 +01:00
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int NotImpl1();
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int NotImpl2();
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetSessionIdentifier([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetSessionInstanceIdentifier([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int GetProcessId(out int pRetVal);
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int IsSystemSoundsSession();
|
|
|
|
|
|
|
|
|
|
[PreserveSig]
|
|
|
|
|
int SetDuckingPreference(bool optOut);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-17 10:57:26 +01:00
|
|
|
|
}
|