forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeMixerControl.cs
More file actions
302 lines (241 loc) · 10.2 KB
/
VolumeMixerControl.cs
File metadata and controls
302 lines (241 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
namespace SpotifyAPI.Local
{
internal static class VolumeMixerControl
{
private const string SpotifyProcessName = "spotify";
internal static float GetSpotifyVolume()
{
ISimpleAudioVolume volume = GetSpotifyVolumeObject();
if (volume == null)
{
throw new COMException("Volume object creation failed");
}
volume.GetMasterVolume(out float level);
Marshal.ReleaseComObject(volume);
return level * 100;
}
internal static bool IsSpotifyMuted()
{
ISimpleAudioVolume volume = GetSpotifyVolumeObject();
if (volume == null)
{
throw new COMException("Volume object creation failed");
}
volume.GetMute(out bool mute);
Marshal.ReleaseComObject(volume);
return mute;
}
internal static void SetSpotifyVolume(float level)
{
ISimpleAudioVolume volume = GetSpotifyVolumeObject();
if (volume == null)
{
throw new COMException("Volume object creation failed");
}
Guid guid = Guid.Empty;
volume.SetMasterVolume(level / 100, ref guid);
Marshal.ReleaseComObject(volume);
}
internal static void MuteSpotify(bool mute)
{
ISimpleAudioVolume volume = GetSpotifyVolumeObject();
if (volume == null)
{
throw new COMException("Volume object creation failed");
}
Guid guid = Guid.Empty;
volume.SetMute(mute, ref guid);
Marshal.ReleaseComObject(volume);
}
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();
}
private static ISimpleAudioVolume GetVolumeObject(int pid)
{
// get the speakers (1st render + multimedia) device
IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator) new MMDeviceEnumerator();
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out IMmDevice speakers);
speakers.GetId(out string defaultDeviceId);
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.
deviceEnumerator.EnumAudioEndpoints(EDataFlow.ERender, EDeviceState.Active, out IMmDeviceCollection deviceCollection);
deviceCollection.GetCount(out int count);
for (int i = 0; i < count; i++)
{
deviceCollection.Item(i, out IMmDevice device);
device.GetId(out string deviceId);
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)
{
// activate the session manager. we need the enumerator
Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
device.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out object o);
IAudioSessionManager2 mgr = (IAudioSessionManager2) o;
// enumerate sessions for on this device
mgr.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator);
sessionEnumerator.GetCount(out int count);
// 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++)
{
sessionEnumerator.GetSession(i, out IAudioSessionControl2 ctl);
ctl.GetProcessId(out int cpid);
if (cpid == pid)
{
volumeControl = (ISimpleAudioVolume) ctl;
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
{
ERender,
ECapture,
EAll,
EDataFlowEnumCount
}
private enum ERole
{
EConsole,
EMultimedia,
ECommunications,
ERoleEnumCount
}
[Flags]
private enum EDeviceState
{
Active = 0x00000001,
Disabled = 0x00000002,
NotPresent = 0x00000004,
UnPlugged = 0x00000008,
All = 0x0000000F
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IMmDeviceEnumerator
{
[PreserveSig]
int EnumAudioEndpoints(EDataFlow dataFlow, EDeviceState stateMask, [Out] out IMmDeviceCollection deviceCollection);
[PreserveSig]
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMmDevice ppDevice);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IMmDevice
{
[PreserveSig]
int Activate(ref Guid iid, int dwClsCtx, IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);
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);
}
[Guid("77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IAudioSessionManager2
{
int NotImpl1();
int NotImpl2();
[PreserveSig]
int GetSessionEnumerator(out IAudioSessionEnumerator sessionEnum);
}
[Guid("E2F5BB11-0570-40CA-ACDD-3AA01277DEE8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IAudioSessionEnumerator
{
[PreserveSig]
int GetCount(out int sessionCount);
[PreserveSig]
int GetSession(int sessionCount, out IAudioSessionControl2 session);
}
[Guid("87CE5498-68D6-44E5-9215-6DA47EF883D8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ISimpleAudioVolume
{
[PreserveSig]
int SetMasterVolume(float fLevel, ref Guid eventContext);
[PreserveSig]
int GetMasterVolume(out float pfLevel);
[PreserveSig]
int SetMute(bool bMute, ref Guid eventContext);
[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]
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)]string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int GetIconPath([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
[PreserveSig]
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int GetGroupingParam(out Guid pRetVal);
[PreserveSig]
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[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);
}
}
}