-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAudioManager.cs
More file actions
256 lines (217 loc) · 7.85 KB
/
Copy pathAudioManager.cs
File metadata and controls
256 lines (217 loc) · 7.85 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
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
// ReSharper disable SuspiciousTypeConversion.Global
// ReSharper disable InconsistentNaming
//Copied from https://gist.github.com/sverrirs/d099b34b7f72bb4fb386
namespace MediaControls_Plugin
{
internal enum Mode
{
Microphone,
Speakers
}
/// <summary>
/// Controls audio using the Windows CoreAudio API
/// from: http://stackoverflow.com/questions/14306048/controling-volume-mixer
/// and: http://netcoreaudio.codeplex.com/
/// </summary>
public class AudioManager
{
#region Master Volume Manipulation
private Direction direction;
internal AudioManager(Mode m)
{
switch (m)
{
case Mode.Microphone:
direction = new Microphone();
break;
case Mode.Speakers:
direction = new Speakers();
break;
}
}
/// <summary>
/// Gets the current master volume in scalar values (percentage)
/// </summary>
/// <returns>-1 in case of an error, if successful the value will be between 0 and 100</returns>
public float GetMasterVolume()
{
IAudioEndpointVolume masterVol = null;
try
{
masterVol = CoreAudioAPI.GetMasterVolumeObject(direction);
if (masterVol == null)
return -1;
float volumeLevel;
masterVol.GetMasterVolumeLevelScalar(out volumeLevel);
return volumeLevel * 100;
}
finally
{
if (masterVol != null)
Marshal.ReleaseComObject(masterVol);
}
}
/// <summary>
/// Gets the mute state of the master volume.
/// While the volume can be muted the <see cref="GetMasterVolume"/> will still return the pre-muted volume value.
/// </summary>
/// <returns>false if not muted, true if volume is muted</returns>
public bool GetMasterVolumeMute()
{
IAudioEndpointVolume masterVol = null;
try
{
masterVol = CoreAudioAPI.GetMasterVolumeObject(direction);
if (masterVol == null)
return false;
bool isMuted;
masterVol.GetMute(out isMuted);
return isMuted;
}
finally
{
if (masterVol != null)
Marshal.ReleaseComObject(masterVol);
}
}
/// <summary>
/// Sets the master volume to a specific level
/// </summary>
/// <param name="newLevel">Value between 0 and 100 indicating the desired scalar value of the volume</param>
public void SetMasterVolume(float newLevel)
{
IAudioEndpointVolume masterVol = null;
try
{
masterVol = CoreAudioAPI.GetMasterVolumeObject(direction);
if (masterVol == null)
return;
masterVol.SetMasterVolumeLevelScalar(newLevel / 100, Guid.Empty);
}
finally
{
if (masterVol != null)
Marshal.ReleaseComObject(masterVol);
}
}
/// <summary>
/// Increments or decrements the current volume level by the <see cref="stepAmount"/>.
/// </summary>
/// <param name="stepAmount">Value between -100 and 100 indicating the desired step amount. Use negative numbers to decrease
/// the volume and positive numbers to increase it.</param>
/// <returns>the new volume level assigned</returns>
public float StepMasterVolume(float stepAmount)
{
IAudioEndpointVolume masterVol = null;
try
{
masterVol = CoreAudioAPI.GetMasterVolumeObject(direction);
if (masterVol == null)
return -1;
float stepAmountScaled = stepAmount / 100;
// Get the level
float volumeLevel;
masterVol.GetMasterVolumeLevelScalar(out volumeLevel);
// Calculate the new level
float newLevel = volumeLevel + stepAmountScaled;
newLevel = Math.Min(1, newLevel);
newLevel = Math.Max(0, newLevel);
masterVol.SetMasterVolumeLevelScalar(newLevel, Guid.Empty);
// Return the new volume level that was set
return newLevel * 100;
}
finally
{
if (masterVol != null)
Marshal.ReleaseComObject(masterVol);
}
}
/// <summary>
/// Mute or unmute the master volume
/// </summary>
/// <param name="isMuted">true to mute the master volume, false to unmute</param>
public void SetMasterVolumeMute(bool isMuted)
{
IAudioEndpointVolume masterVol = null;
try
{
masterVol = CoreAudioAPI.GetMasterVolumeObject(direction);
if (masterVol == null)
return;
masterVol.SetMute(isMuted, Guid.Empty);
}
finally
{
if (masterVol != null)
Marshal.ReleaseComObject(masterVol);
}
}
/// <summary>
/// Switches between the master volume mute states depending on the current state
/// </summary>
/// <returns>the current mute state, true if the volume was muted, false if unmuted</returns>
public bool ToggleMasterVolumeMute()
{
IAudioEndpointVolume masterVol = null;
try
{
masterVol = CoreAudioAPI.GetMasterVolumeObject(direction);
if (masterVol == null)
return false;
bool isMuted;
masterVol.GetMute(out isMuted);
masterVol.SetMute(!isMuted, Guid.Empty);
return !isMuted;
}
finally
{
if (masterVol != null)
Marshal.ReleaseComObject(masterVol);
}
}
#endregion
#region Individual Application Volume Manipulation
public float? GetApplicationVolume(int pid)
{
ISimpleAudioVolume volume = CoreAudioAPI.GetVolumeObject(direction, pid);
if (volume == null)
return null;
float level;
volume.GetMasterVolume(out level);
Marshal.ReleaseComObject(volume);
return level * 100;
}
public bool? GetApplicationMute(int pid)
{
ISimpleAudioVolume volume = CoreAudioAPI.GetVolumeObject(direction, pid);
if (volume == null)
return null;
bool mute;
volume.GetMute(out mute);
Marshal.ReleaseComObject(volume);
return mute;
}
public void SetApplicationVolume(int pid, float level)
{
ISimpleAudioVolume volume = CoreAudioAPI.GetVolumeObject(direction, pid);
if (volume == null)
return;
Guid guid = Guid.Empty;
volume.SetMasterVolume(level / 100, ref guid);
Marshal.ReleaseComObject(volume);
}
public void SetApplicationMute(int pid, bool mute)
{
ISimpleAudioVolume volume = CoreAudioAPI.GetVolumeObject(direction, pid);
if (volume == null)
return;
Guid guid = Guid.Empty;
volume.SetMute(mute, ref guid);
Marshal.ReleaseComObject(volume);
}
#endregion
}
}