示例#1
0
        /// <summary>
        ///     Get the available audio input devices.
        ///     Thought that would be trivial shit, but this wasn't.
        ///     - We use NAudio to record the audio
        ///     - NAudio has its own audio device index
        ///     - NAudio can't get the full device name because of a M$ limitation
        ///     - So we use MMDevice instead to enumerate them devices
        ///     - However their order/index is different
        ///     - But they have a full device name
        ///     - And also they have the proper number of audio channels
        ///     - And basically MMDevice has more stuff in there
        ///     - So we use a MMDevice list of the available devices
        ///     - But we order them using the NAudio index
        ///     Also note that the use of MMDevice is only okay for Win Vista+ OSes.
        ///     No XP here because of that...
        /// </summary>
        /// <returns>A list of the available audio devices ordered by NAudio device index.</returns>
        private static IList <MMDevice> GetAudioInputDevices()
        {
            Info("Detected input devices: {0}", WaveIn.DeviceCount);

            // Enumerates audio input devices using MMDevice
            var devices    = new MMDevice[WaveIn.DeviceCount];
            var enumerator = new MMDeviceEnumerator();

            foreach (var deviceInfo in enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active))
            {
                // MMDevice has the full product name in that property
                var fullProductName = deviceInfo.FriendlyName;

                // Find this device's Index for NAudio's WaveIn
                var naudioIndex = 0;
                for (var waveInDeviceIndex = 0; waveInDeviceIndex < WaveIn.DeviceCount; waveInDeviceIndex++)
                {
                    // WaveIn only has 32bits product name lenght
                    var partialProductName = WaveIn.GetCapabilities(waveInDeviceIndex).ProductName;

                    // Okay, same device, so we grab NAudio's index
                    if (fullProductName.StartsWith(partialProductName))
                    {
                        naudioIndex = waveInDeviceIndex;
                        break;
                    }
                }

                Info("{0}:{1}, {2} channels", naudioIndex, deviceInfo.FriendlyName,
                     deviceInfo.AudioEndpointVolume.Channels.Count);
                devices[naudioIndex] = deviceInfo;
            }

            return(devices.ToList());
        }