-
Notifications
You must be signed in to change notification settings - Fork 0
/
Microphone.cs
111 lines (102 loc) · 3.18 KB
/
Microphone.cs
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
using NAudio.CoreAudioApi;
using System;
using System.IO;
using System.Reflection;
namespace Microphone
{
/// <summary>
/// Allows for the manipulation of a given microphone device.
/// </summary>
public class Mic
{
private readonly MMDevice micDevice;
/// <summary>
/// Creates a new <see cref="Microphone"/> object to access a specific microphone.
/// </summary>
/// <param name="MicIndex">The 0-based index position of the microphone device to access from the list of microphone devices.</param>
public Mic(int MicIndex)
{
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
MMDeviceCollection devices = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
if (devices.Count > 0)
{
if (MicIndex <= devices.Count - 1 || MicIndex >= 0)
{
micDevice = devices[MicIndex];
}
else throw new ArgumentOutOfRangeException("MicIndex", "The index position was outside the bounds of the array.");
}
else throw new Exception("Unable to retrieve microphone device(s) because there are no devices to retrieve.");
}
/// <summary>
/// Creates a new <see cref="Microphone"/> object to access a specific microphone.
/// </summary>
/// <param name="DeviceName">Can either be the full name, basic name, or a substring of your microphone's device name.</param>
public Mic(string DeviceName)
{
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
MMDeviceCollection devices = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
if (devices.Count > 0)
{
foreach (MMDevice device in devices)
{
if (device.FriendlyName.Contains(DeviceName))
{
micDevice = device;
}
else throw new ArgumentException("No microphones similar to, or containing, the name '" + DeviceName + "' were found.", "DeviceName");
}
}
else throw new Exception("Unable to retrieve microphone device(s) because there are no devices to retrieve.");
}
/// <summary>
/// The audio device that represents the current <strong>microphone</strong>.
/// </summary>
public MMDevice Device => micDevice;
/// <summary>
/// The full name of the device.
/// </summary>
public string Name => micDevice.FriendlyName;
/// <summary>
/// The basic name of the device.
/// </summary>
public string FriendlyName => micDevice.DeviceFriendlyName;
/// <summary>
/// The volume (in whole numbers only) of the microphone.
/// </summary>
public int Volume
{
get { return (int)(micDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100); }
set { micDevice.AudioEndpointVolume.MasterVolumeLevelScalar = value / 100.0f; }
}
/// <summary>
/// The volume (including decimals) of the microphone.
/// </summary>
public float VolumeF
{
get { return micDevice.AudioEndpointVolume.MasterVolumeLevelScalar; }
set
{
if (value > 1)
{
value = 1.0f;
}
else if (value < 0)
{
value = 0.0f;
}
else
{
if (value.ToString().Length > 4)
{
string num = value.ToString();
num = num.Remove(4);
num += "f";
value = float.Parse(num);
}
}
micDevice.AudioEndpointVolume.MasterVolumeLevelScalar = value;
}
}
}
}