-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioInput.cs
132 lines (101 loc) · 3.93 KB
/
AudioInput.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using NAudio.Wave;
namespace Vision_Automix
{
class AudioInput
{
MMDeviceEnumerator masterEnumerator = new MMDeviceEnumerator();
private WaveIn recorder;
private int globalInterfaceIndex = 0;
private int liveLevel = 0;
private int liveThreshold = 50;
private double gain = 1.0;
private List<String> listedDevices;
private List<MMDevice> deviceList;
private int selectedDevice = 0;
private int selectedChannel = 0;
private bool messageFlag = false; //Flag for showing error message to stop flooding
public List<String> Initialize(int interfaceIndex)
{
globalInterfaceIndex = interfaceIndex;
Console.WriteLine("");
Console.WriteLine("Initializing Audio Input " + globalInterfaceIndex);
recorder = new WaveIn();
try { recorder.StartRecording(); }
catch { MessageBox.Show("No audio devices found. Application may not run correctly.", "AUDIO DEVICE ERROR"); }
var devices = masterEnumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
//Return list of devices to add to GUI list
List<String> tempList = new List<String>();
List<MMDevice> tempMMlist = new List<MMDevice>();
foreach (MMDevice n in devices)
{
tempList.Add(n.ToString());
tempMMlist.Add(n);
Console.WriteLine("Adding to devicelist: " + n.ToString());
}
listedDevices = tempList; //Add to object list
deviceList = tempMMlist;
return tempList;
}
//SET
public void SetDevice(int deviceIndex, int channelIndex)
{
selectedDevice = deviceIndex;
selectedChannel = channelIndex - 1;
Console.WriteLine("Audio Device " + globalInterfaceIndex + " updated.");
messageFlag = false; //Reset error-message flag
}
public void SetThreshold(int requestedThreshold)
{
liveThreshold = requestedThreshold;
messageFlag = false; //Reset error-message flag
}
public void SetGain (double newGain)
{
gain = (1+(newGain*0.04));
Console.WriteLine("New gain: " + gain);
}
//GET
public int GetVolume()
{
try
{
//Get peak level from audio device
var singledevice = (MMDevice)deviceList[selectedDevice];
liveLevel = (int)(singledevice.AudioMeterInformation.PeakValues[selectedChannel] * 100);
//Gain and clipping
liveLevel = (int)((double)liveLevel * gain);
if (liveLevel > 100 ) { liveLevel = 100; }
return liveLevel;
}
catch
{
ShowErrorMessage(("AUDIO IN for Speaker " + globalInterfaceIndex), "Error getting volume from device for speaker" + globalInterfaceIndex + ". Channel out of range?");
return 0;
}
}
public bool GetOpen()
{
return (liveLevel >= liveThreshold);
}
public double GetGain()
{
return gain;
}
private void ShowErrorMessage(string title, string message)
{
if (messageFlag == false)
{
Console.WriteLine(message);
messageFlag = true;
MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}