Oct
21
2008

Wave File Format and C#

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 (15) -

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!!!!

Chris

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

Chris

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

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

Chris

Never mind Laughing.
This place needs an edit button.

Thanks :]

Jesse

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

Admin

It's public domain.

T&#252;rker &#214;zt&#252;rk

Good article, very clean code to understand. I will modify it for my needs. I must join little wave files for my personal project. Thank you for the code.

Kelly

Hey Garrett,

Thank you for this library.  I had a question on writing the wav's.  I have created some .wavs in mono, but what is the format of an array that would be passed to make a stereo wave?  More specifically, I have 2 arrays, of which I want to set one to the right channel and one to the left.  Is there another setData method which is used for stereo sounds? If not, how would you use setData to write the stereo .wav's?

This library is very cool and easy to use.  Thanks again for putting it out there.

-Kelly  

dlasd

The variable file is declared but never used, how can I fix this please

Kumar

Nice article.Thank You for sharing this.I have a question.Is it possible to convert the wav file to string.I would like to write the text from a wav file to a notepad.

I was trying by converting the wav file to byte arrays and then converting the byte arrays to string.But it is not working properly.Can you please give me an idea on this.

Thanks,
Kumar

mahesh

nice work thanks

pegah

thank you for this article
how can detect silence in wav file ?

Lluis

Dude, you're my hero! I was having lot of troubles creating .wav files.

Thank you!

Lluís.

http://gamesreviews.snappages.com/blog

Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

Pingbacks and trackbacks (2)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

Something about the author

Month List

Page List