Visions Of Afar
The Struggles of a College Student in the Gaming Industry

Wave File Format and C#

October 21, 2008 09:38 by Garrett Hoofman

I wanted to play around with a wave file in C# that way I could create my own procedure generated waves. So, I started searching around on Google to try and find a C# wave file generator. I couldn’t find anything. I decided to go about making my own and found a lot of information on the wave file format at  : ccrma.stanford.edu.

I created a C# library so that anybody else that wants to play with these wave files can. You can get the source code here : Wave Library

This is how you would create a wave file, with white noise, or a constant sound.

   1: WaveFile file;
   2:  
   3:         private void CreateWaveFile()
   4:         {
   5:             bool whiteNoise = true;
   6:             Random rand = new Random();
   7:  
   8:             file = new WaveFile(1, 8, 11025);
   9:  
  10:             int samples = 11025; //Creates 1 second of audio
  11:  
  12:             // Sound Data Array
  13:             // Size needs to be :
  14:             //          Number Of Channels - 1 for mono
  15:             //                               2 for stereo
  16:             //          Bits Per Sample    - How many bits are used to describe each Sample
  17:             //          Samples            - How many samples that are provided
  18:             //
  19:             // Sound Data Size = Number Of Channels * Bits Per Sample * Samples
  20:             byte[] data = new byte[file.NumChannels * (file.BitsPerSample / 8) * samples];
  21:  
  22:             if (whiteNoise)
  23:             {
  24:                 //Creates White Noise
  25:                 for (int i = 0; i < data.Length; i++)
  26:                 {
  27:                     data[i] = (byte)rand.Next(256); //256 is the max size of a byte
  28:                 }
  29:             }
  30:             else
  31:             {
  32:                 //Creates a Constant Sound
  33:                 for (int i = 0; i < data.Length; i++)
  34:                 {
  35:                     data[i] = (byte)(256 * Math.Sin(i));
  36:                 }
  37:             }
  38:  
  39:             file.SetData(data, samples);
  40:             file.WriteFile(@"C:\Test.wav"); //Writes the wave file out to the destination given
  41:         }

You can play with the for loops and create some neat sound effects :

   1: float push = 0;
   2: //Creates a Constant Sound
   3: for (int i = 0; i < data.Length; i++)
   4: {
   5:     push += 0.0001f;
   6:     data[i] = (byte)(256 * Math.Sin(i * push));
   7: }

That’ll create a nifty little whirl noise with very minimal effort.

The best part is you can then use the DirectSound framework to play your new wave file. This approach could be an easy way to limit your game size and create some neat procedurally generated sounds, all on the fly.

EDIT : The WaveLibrary had an include for LINQ, which it wasn't using. Having that in would throw an error if it were compiled in any Visual Studio below 2008. Thanks to Vulpes for catching that.


Comments

December 19. 2008 06:55

vertige

Great job, this is really "plug'n'play software". I put all your files ino a VS 2005 solution (I don' own the newer version), et voilà! ready to play and experiment: thanks alot!!!!

vertige

January 1. 2009 07:38

Chris

You are a life saver!
Every reference to MSDN's documentation on WAVE file has been moved lol.
Thanks!

Chris

Chris

January 1. 2009 10:54

Chris

Ok, I am stuck...
How can I change the sound of each byte? How can I play a sound at a specific pitch?

Chris

January 1. 2009 14:54

Chris

Sorry, to be more specific, can I set the frequency?

Chris

January 2. 2009 08:06

Chris

Never mind Laughing.
This place needs an edit button.

Thanks :]

Chris

September 1. 2009 09:16

Jesse

Hi, thanks for posting this info.  Is there a license associated with your library or is it public domain?  Thanks!

Jesse

September 13. 2009 16:00

Admin

It's public domain.

Admin

Comments are closed