Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#

Compose sounds from frequencies and visualize them

Rate me:
Please Sign up or sign in to vote.
4.87/5 (53 votes)
17 Apr 2006CPOL6 min read 190.6K   3.5K   162  
What you never wanted to know about PCM.
/* This class has been written by
 * Corinna John (Hannover, Germany)
 * cj@binary-universe.net
 * 
 * You may do with this code whatever you like,
 * except selling it or claiming any rights/ownership.
 * 
 * Please send me a little feedback about what you're
 * using this code for and what changes you'd like to
 * see in later versions.
 */

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace WaveMixer {

    /// <summary>Format header information of a RIFF Wave file.</summary>
	[StructLayout(LayoutKind.Sequential)]
	public class WaveFormat {
        private short wFormatTag;
        private short nChannels;
        private int nSamplesPerSec;
        private int nAvgBytesPerSec;
        private short nBlockAlign;
        private short wBitsPerSample;
        private short cbSize;

        /// <summary>wFormatTag header field.</summary>
        public short FormatTag
        {
            get { return wFormatTag; }
            set { wFormatTag = value; }
        }

        /// <summary>nChannels header field.</summary>
        public short Channels
        {
            get { return nChannels; }
            set { nChannels = value; }
        }

        /// <summary>nSamplesPerSec header field.</summary>
        public int SamplesPerSec
        {
            get { return nSamplesPerSec; }
            set { nSamplesPerSec = value; }
        }

        /// <summary>nAvgBytesPerSec header field.</summary>
        public int AvgBytesPerSec
        {
            get { return nAvgBytesPerSec; }
            set { nAvgBytesPerSec = value; }
        }

        /// <summary>nBlockAlign header field.</summary>
        public short BlockAlign
        {
            get{ return nBlockAlign; }
            set { nBlockAlign = value; }
        }

        /// <summary>wBitsPerSample header field.</summary>
        public short BitsPerSample
        {
            get { return wBitsPerSample; }
            set { wBitsPerSample = value; }
        }

        /// <summary>cbSize header field.</summary>
        public short Size
		{
			get { return cbSize; }
			set { cbSize = value; }
		}

        /// <summary>Constructor.</summary>
		public WaveFormat()
		{
			//default constructor
		}

        /// <summary>Constructor.</summary>
        /// <param name="samplesPerSec">Number of samples per second.</param>
        /// <param name="bitsPerSample">Number of bits per sample.</param>
        /// <param name="channels">Number of channels.</param>
		public WaveFormat(int samplesPerSec, short bitsPerSample, short channels)
		{
			wFormatTag = (short)WaveFormats.Pcm;
			nChannels = channels;
			nSamplesPerSec = samplesPerSec;
			wBitsPerSample = bitsPerSample;
			cbSize = 0;

			nBlockAlign = (short)(channels * (bitsPerSample / 8));
			nAvgBytesPerSec = samplesPerSec * nBlockAlign;
		}
    }
}

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
Software Developer
Germany Germany
Corinna lives in Hanover/Germany and works as a C# developer.

Comments and Discussions