Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerate Recording Devices in NAudio

Tags:

c#

naudio

How can you get a list of all the recording devices on a computer using NAudio? When you want to record, you have to give it the index of the device you want to use, but there's no way of knowing what device that is. I'd like to be able to select from Mic, Stereo Mix, etc.

like image 862
Adam Haile Avatar asked Sep 19 '09 18:09

Adam Haile


2 Answers

To get full device names, i use this...

using NAudio.CoreAudioApi;
using NAudio.Wave;

For getting all recording devices:

//create enumerator
var enumerator = new MMDeviceEnumerator();
//cycle through all audio devices
for (int i = 0; i < WaveIn.DeviceCount; i++)
    Console.WriteLine(enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active)[i]);
//clean up
enumerator.Dispose();

For getting all capture devices:

//create enumerator
var enumerator = new MMDeviceEnumerator();
//cyckle trough all audio devices
for (int i = 0; i < WaveOut.DeviceCount; i++)
    Console.WriteLine(enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)[i]);
//clean up
enumerator.Dispose();
like image 103
IntegratedHen Avatar answered Nov 03 '22 11:11

IntegratedHen


For WaveIn, you can use the static WaveIn.GetCapabilities method. This will give you a device name, but with the annoying limitation that it is a maximum of 31 characters. I am still looking for the way to get the full name (see my question here).

int waveInDevices = WaveIn.DeviceCount;
for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
{
    WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
    Console.WriteLine("Device {0}: {1}, {2} channels", waveInDevice, deviceInfo.ProductName, deviceInfo.Channels);
}

For WASAPI (Vista and above), you can use the MMDeviceEnumerator:

MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
foreach (MMDevice device in enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All))
{
    Console.WriteLine("{0}, {1}", device.FriendlyName, device.State);
}

I tend to recommend WaveIn, as it is more widely supported, and allows more flexibility over recording sample rates.

like image 35
Mark Heath Avatar answered Nov 03 '22 11:11

Mark Heath