Click here to Skip to main content
15,886,693 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.9K   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;

namespace Istrib.Sound.Formats
{
    /// <summary>
    /// Raw wave PCM format - without any headers, just sample sequence.
    /// </summary>
    [SoundFormatClass("WAV-PCM")]
    public class PcmSoundFormat
        : SoundFormatBase, IEquatable<PcmSoundFormat>
    {
        public static readonly PcmSoundFormat Pcm8kHz8bitMono = new PcmSoundFormat(8000, 8, 1);
        public static readonly PcmSoundFormat Pcm8kHz8bitStereo = new PcmSoundFormat(8000, 8, 2);
        public static readonly PcmSoundFormat Pcm8kHz16bitMono = new PcmSoundFormat(8000, 16, 1);
        public static readonly PcmSoundFormat Pcm8kHz16bitStereo = new PcmSoundFormat(8000, 16, 2);

        public static readonly PcmSoundFormat Pcm11kHz8bitMono = new PcmSoundFormat(11025, 8, 1);
        public static readonly PcmSoundFormat Pcm11kHz8bitStereo = new PcmSoundFormat(11025, 8, 2);
        public static readonly PcmSoundFormat Pcm11kHz16bitMono = new PcmSoundFormat(11025, 16, 1);
        public static readonly PcmSoundFormat Pcm11kHz16bitStereo = new PcmSoundFormat(11025, 16, 2);

        public static readonly PcmSoundFormat Pcm22kHz8bitMono = new PcmSoundFormat(22050, 8, 1);
        public static readonly PcmSoundFormat Pcm22kHz8bitStereo = new PcmSoundFormat(22050, 8, 2);
        public static readonly PcmSoundFormat Pcm22kHz16bitMono = new PcmSoundFormat(22050, 16, 1);
        public static readonly PcmSoundFormat Pcm22kHz16bitStereo = new PcmSoundFormat(22050, 16, 2);

        public static readonly PcmSoundFormat Pcm44kHz8bitMono = new PcmSoundFormat(44100, 8, 1);
        public static readonly PcmSoundFormat Pcm44kHz8bitStereo = new PcmSoundFormat(44100, 8, 2);
        public static readonly PcmSoundFormat Pcm44kHz16bitMono = new PcmSoundFormat(44100, 16, 1);
        public static readonly PcmSoundFormat Pcm44kHz16bitStereo = new PcmSoundFormat(44100, 16, 2);

        public static readonly PcmSoundFormat Pcm48kHz8bitMono = new PcmSoundFormat(48000, 8, 1);
        public static readonly PcmSoundFormat Pcm48kHz8bitStereo = new PcmSoundFormat(48000, 8, 2);
        public static readonly PcmSoundFormat Pcm48kHz16bitMono = new PcmSoundFormat(48000, 16, 1);
        public static readonly PcmSoundFormat Pcm48kHz16bitStereo = new PcmSoundFormat(48000, 16, 2);

        /// <summary>
        /// List of most popular Pcm formats.
        /// </summary>
        public static readonly IEnumerable<PcmSoundFormat> StandardFormats = new PcmSoundFormat[]
        {
            Pcm8kHz8bitMono,
            Pcm8kHz8bitStereo,
            Pcm8kHz16bitMono,
            Pcm8kHz16bitStereo,

            Pcm11kHz8bitMono,
            Pcm11kHz8bitStereo,
            Pcm11kHz16bitMono,
            Pcm11kHz16bitStereo,

            Pcm22kHz8bitMono,
            Pcm22kHz8bitStereo,
            Pcm22kHz16bitMono,
            Pcm22kHz16bitStereo,

            Pcm44kHz8bitMono,
            Pcm44kHz8bitStereo,
            Pcm44kHz16bitMono,
            Pcm44kHz16bitStereo,

            Pcm48kHz8bitMono,
            Pcm48kHz8bitStereo,
            Pcm48kHz16bitMono,
            Pcm48kHz16bitStereo,
        };

        private int sampleRate;
        private short bitsPerSample;
        private short channels;

        /// <summary>
        /// Samples per second
        /// </summary>
        public int SampleRate
        {
            get
            {
                return sampleRate;
            }
        }

        public short BitsPerSample
        {
            get
            {
                return bitsPerSample;
            }
        }

        /// <summary>
        /// 1 - mono, 2 - stereo.
        /// </summary>
        public short Channels
        {
            get
            {
                return channels;
            }
        }

        public short BlockAlign
        {
            get
            {
                return (short)(Channels * (BitsPerSample / 8));
            }
        }

        public int AverageBytesPerSecond
        {
            get
            {
                return BlockAlign * SampleRate;
            }
        }

        public override string Description
        {
            get
            {
                return ToString();
            }
        }

        public static bool operator ==(PcmSoundFormat format1, PcmSoundFormat format2)
        {
            return format1.Equals(format2);
        }

        public static bool operator !=(PcmSoundFormat format1, PcmSoundFormat format2)
        {
            return !(format1 == format2);
        }

        public bool Equals(PcmSoundFormat other)
        {
            return ReferenceEquals(other, this)
                || (!ReferenceEquals(other, null)
                    && this.bitsPerSample == other.bitsPerSample
                    && this.channels == other.channels
                    && this.sampleRate == other.sampleRate);
        }

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

            return Equals((PcmSoundFormat)obj);
        }

        public override int GetHashCode()
        {
            return bitsPerSample * channels * sampleRate;
        }

        public override string ToString()
        {
            string channelsText;
            switch (channels)
            {
                case 1:
                    channelsText = "Mono";
                    break;
                case 2:
                    channelsText = "Stereo";
                    break;
                default:
                    channelsText = channels + " channels";
                    break;
            };

            return string.Format("{0} Hz, {1} bit, {2}", sampleRate, bitsPerSample, channelsText);
        }

        public PcmSoundFormat(int sampleRate, short bitsPerSample, short channels)
        {
            this.sampleRate = sampleRate;
            this.bitsPerSample = bitsPerSample;
            this.channels = channels;
        }
    }
}

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