I'm using the NAudio library to capture Audio Input in C#:
int _deviceID;
double _previewVal;
private void PreviewData()
{
WaveIn waveIn = new WaveIn();
waveIn.DeviceNumber = _deviceID;
waveIn.DataAvailable += waveIn_DataAvailable;
int sampleRate = 8000; // 8 kHz
int channels = 1; // mono
waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
waveIn.StartRecording();
}
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((e.Buffer[index + 1] << 8) |
e.Buffer[index + 0]);
_previewVal = sample / 327.68f;
}
}
The function PreviewData is called simultaneously for each audio input device (which are 4 in my system). When I only call the method for one device it seems to be working but if I call it once more I get the exception "AlreadyAllocated calling waveInOpen". Does someone know how to work around this?