-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputDevices.cs
More file actions
100 lines (86 loc) · 2.53 KB
/
Copy pathInputDevices.cs
File metadata and controls
100 lines (86 loc) · 2.53 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
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
namespace StealthNotes
{
internal class InputDevices
{
public InputDevices()
{
ActiveInputs = GetActiveInputs();
}
private Dictionary<string, MMDevice> ActiveInputs { get; }
public bool IsMutedByName(string name)
{
var device = GetInputByName(name);
return device.AudioEndpointVolume.Mute;
}
public List<string> DeviceNames
{
get
{
return ActiveInputs.Keys.OrderBy(k => k).ToList();
}
}
/// <summary>
/// Mutes a given input if it is in active use
/// </summary>
/// <param name="name"></param>
/// <param name="mute"></param>
public void MuteInputByName(string name, bool mute = true)
{
var device = GetInputByName(name);
if (device == null)
return;
if (device.AudioEndpointVolume.Mute != mute)
device.AudioEndpointVolume.Mute = mute;
}
/// <summary>
/// Whether the device is currently in active use
/// </summary>
/// <param name="device"></param>
/// <param name="application"></param>
/// <returns>True if the input is currently active</returns>
public bool IsActiveInApplication(string name, IEnumerable<string> applications)
{
var device = GetInputByName(name);
if (device == null)
return false;
int count = device.AudioSessionManager.Sessions.Count;
for (var i = 0; i < count; i++)
{
var session = device.AudioSessionManager.Sessions[i];
var sessionIdentifier = session.GetSessionIdentifier;
if (session.State == NAudio.CoreAudioApi.Interfaces.AudioSessionState.AudioSessionStateActive && applications.Any(a => sessionIdentifier.Contains(a)))
return true;
}
return false;
}
private Dictionary<string, MMDevice> GetActiveInputs()
{
var enumerator = new MMDeviceEnumerator();
var enabledDevices = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active)
.Where(device => device.State == DeviceState.Active)
.ToDictionary(device => device.FriendlyName, device => device);
return enabledDevices;
}
public void RemoveActiveInput(string name)
{
ActiveInputs.Remove(name);
}
public MMDevice GetInputByName(string name)
{
var x = ActiveInputs.TryGetValue(name, out var tmp) ? tmp : null;
int sessionCount = x.AudioSessionManager.Sessions.Count;
for (var i = 0; i < sessionCount; i++)
{
var session = x.AudioSessionManager.Sessions[i];
var id = session.GetSessionInstanceIdentifier;
Debug.WriteLine(id);
}
return x;
}
}
}