Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / Win32

C# MP3 Sound Capturing/Recording Component

Rate me:
Please Sign up or sign in to vote.
4.75/5 (47 votes)
20 Oct 2009CPOL8 min read 465.6K   33.3K   186  
A .NET component capturing WAVE or MP3 sound from a sound card. LAME used for MP3 compression.
//
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER 
//  REMAINS UNCHANGED.
//
//  Email:  lukasz@istrib.org
//
//  Copyright (C) 2008-2009 Lukasz Kwiecinski. 
//

using System;
using System.Collections.Generic;

using System.Text;
using Microsoft.DirectX.DirectSound;

namespace Istrib.Sound
{
    /// <summary>
    /// Represents a capture device like a microphone or line-in.
    /// </summary>
    public struct SoundCaptureDevice
        : IEquatable<SoundCaptureDevice>
    {
        /// <summary>
        /// Not a specific device but a "choose OS current" special case.
        /// </summary>
        public static readonly SoundCaptureDevice Default = new SoundCaptureDevice("Default", Guid.Empty, "");

        /// <summary>
        /// Lists Sound Capture Devices which are currently available in the OS.
        /// </summary>
        public static IEnumerable<SoundCaptureDevice> AllAvailable
        {
            get
            {
                CaptureDevicesCollection devices = new CaptureDevicesCollection();
                foreach (DeviceInformation deviceInfo in devices)
                {
                    yield return new SoundCaptureDevice(
                        deviceInfo.Description,
                        deviceInfo.DriverGuid,
                        deviceInfo.ModuleName);
                }
            }
        }

        private string description;
        private Guid driverGuid;
        private string moduleName;

        /// <summary>
        /// Gets a textual description of the Microsoft DirectSound device.
        /// </summary>
        public string Description { get { return description; } }
        
        /// <summary>
        /// Gets the globally unique identifier (GUID) of a Microsoft DirectSound
        /// driver.
        /// </summary>
        public Guid DriverGuid { get { return driverGuid; } }

        /// <summary>
        /// Gets the module name of the Microsoft DirectSound driver corresponding
        /// to this device.
        /// </summary>
        public string ModuleName { get { return moduleName; } }

        public static bool operator ==(SoundCaptureDevice device1, SoundCaptureDevice device2)
        {
            return device1.Equals(device2);
        }

        public static bool operator !=(SoundCaptureDevice device1, SoundCaptureDevice device2)
        {
            return !(device1 == device2);
        }

        public bool Equals(SoundCaptureDevice other)
        {
            return driverGuid == other.driverGuid;
        }

        public override bool Equals(object obj)
        {
            if (!(obj is SoundCaptureDevice))
            {
                return false;
            }

            return base.Equals((SoundCaptureDevice)obj);
        }

        public override int GetHashCode()
        {
            return driverGuid.GetHashCode();
        }

        public override string ToString()
        {
            return Description;
        }

        public SoundCaptureDevice(string description, Guid driverGuid, string moduleName)
        {
            this.description = description;
            this.driverGuid = driverGuid;
            this.moduleName = moduleName;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
StaffPlan, Istrib
Poland Poland
I am a .NET developer, working currently in the UK, though still strongly associated with Poland. Primary interests: business application, server-side, enterprise-scale solutions, SOA, thin but rich internet clients (Silverlight).
Apart from my everyday work, I spend a lot of time validating design patterns for newer technologies (like Silverlight, WCF, LINQ). My warzone is www.nauka-slowek.org - a proof of concept for asynchrony, internet multimedia and command pattern in client-server apps. I also use that website myself as it is a great tool to effectively learn vocabulary (foreign languages), which aids my long being abroad.

Comments and Discussions