If you’ve been following this series, you’re probably thinking, “Finally! He is going to show us some code!”
Well, I hate to disappoint you. So I’ll go ahead and show some code.
WaveFun! Wave Generator
The app we will be building is really not all that flashy. It just generates a simple waveform with 1 second of audio and plays it back for you. Nothing configurable or anything like that. Trust me, though, the flashy stuff is coming.
If one giant button on a form isn’t the pinnacle of UI design, I have no idea what to do in this world.
Anyway, this is what the structure of the app looks like:
Chunk Wrappers (Chunks.cs)
The first thing we care about is Chunks.cs, which contains wrappers for the header and two chunks that we learned about in the last article.
Let’s look at the code for the WaveHeader wrapper class. Note the data types we use instead of just “int.” The strings will be converted to character arrays later when we write the file. If you don’t convert them, you get end-of-string characters that ruin your file. dwFileLength is initialized to zero, but is determined later (retroactively) after we have written the stream and we know how long the file is.
Next up is the code for the Format chunk wrapper class. Again, note that the datatypes are consistent with the wave file format spec. Also note that we can explicitly set the chunk size in the constructor to 16 bytes, because the size of this chunk never changes (just add up the number of bytes taken up by each field, you get 16).
Finally, let’s have a look at the wrapper for the Data chunk. Here, we use an array of shorts because we have 16-bit samples as specified in the format block. If you want to change to 8-bit audio, use an array of bytes. If you want to use 32-bit audio, use an array of floats. dwChunkSize is initialized to zero and is determined after the wave data is generated, when we know how long the array is and what the bit depth is.
Now we have all the tools we need to assemble a wave file!
The Wave Generator (WaveGenerator.cs)
This class does two things. It has a constructor, which instantiates all these chunks and then uses a very simple algorithm to generate sample data for a sine wave oscillating at 440Hz. This results in an audible pitch known as Concert A.
In this file, we have an enum called WaveExampleType, which is used to identify what kind of wave we want to create. Feel free to create your own and modify the “big switch statement” to add different sound wave options.
public enum WaveExampleType
ExampleSineWave = 0
The WaveGenerator class only has three members, and they are all chunks.
public class WaveGenerator
// Header, Format, Data chunks
The constructor of the WaveGenerator class takes in an argument of type WaveExampleType, which we switch on to determine what kind of wave to generate. Lots of stuff happens in the constructor, so I’ll use line numbers here to refer to after the jump.
Lines 4-6 instantiate the chunks.
On line 9, we switch on the wave type. This gives us an opportunity to try different things without breaking stuff that works, which I encourage you to do.
On line 14, we calculate the size of the data array. This is calculated by multiplying the sample rate and channel count together. In our case, we have 44100 samples and 2 channels of data , giving us an array of length 88,200.
Line 19 specifies an important value: 32760 is the max amplitude for 16-bit audio. I discussed this in the second article. As an aside, the samples will range from -32760 to 32760; the negative values are provided by the fact that the sine function’s output ranges from -1.0 to 1.0. For other nonperiodic functions you may have to specify -32760 as your lower bound instead of zero – we’ll see this in action in a future article.
Line 20 specifies the frequency of the sound. 440Hz is concert A. You can use any other pitch you want – check out this awesome table for a handy reference.
On line 24, we are doing a little fun trig. See this article if you want to understand the math, otherwise just use this formula and love it.
Line 26 is where the magic happens! The structure of this nested for loop can change. It works for 1 or 2 channels – anything beyond that and you would need to change the condition in the topmost loop (i < numSamples – 1) lest you get an out of memory error.
It’s important to note how multichannel data is written. For WAV files, data is written in an interleaved manner. The sample at each time point is written to all the channels first before advancing to the next time. So shortArray would be the sample in channel 1, and shortArray would be the exact same sample in channel 2. That’s why we have a nested loop.
On line 31, we use Math.Sin to generate the sample data based on the “angle” (t) and the current time (i). This value is written once for each channel before “i” is incremented.
On line 36, we set the chunk size of the data chunk. Most other chunks know how to do this themselves, but because the chunks are independent, the data chunk does not know what the bitrate is (it’s stored in the format chunk). So we set that value directly. The reason we need the bit rate is that the chunk size is stored in bytes, and each sample takes two bytes. Therefore we are setting the data chunk size to the array length times the number of bytes in a sample (2).
At this point, all of our chunks have the correct values and we are ready to write the chunks to a stream. This is where the Save method comes in.
Again, I’ll use line numbers to refer to the Save method below.
Save takes one argument – a file path. Lines 4-7 set up our file stream and binary writer associated with that stream. The order in which values are written is EXTREMELY IMPORTANT!
Lines 10-12 write the header chunk to the stream. We use the .ToCharArray method on the strings to convert them to actual character / byte arrays. If you don’t do this, your header gets messed up with end-of-string characters.
Lines 15-22 write the format chunk.
Lines 25 and 26 write the first two parts of the data array, and the foreach loop writes out every value of the data array.
Now we know exactly how long the file is, so we have to go back and specify the file length as the second value in the file. The first 4 bytes of the file are taken up with “RIFF" so we seek to byte 4 and write out the total length of the stream that we’ve written, minus 8 (as noted by the spec; we don’t count RIFF or WAVE).
Lastly, we close the streams. Our file is written! And it looks like this:
Zoom in a bit to see the awesome sine waviness:
All that’s left are the 5 lines of code that initialize the WaveGenerator object, save the file and play it back to you.
Putting it All Together – Main.cs
Let’s look at Main.cs, the codebehind for our main winform.
On line 3, we reference System.Media. We need this namespace to play back our wave file.
Line 14 is the event handler for the Click event of the only huge button on the form.
On line 16, we define the location of the file to be written. IT IS VERY IMPORTANT THAT YOU CHANGE THIS TO A LOCATION THAT WORKS ON YOUR BOX.
Line 17 initializes the wave generator with a sine wave, and line 18 saves it to the location you defined.
Lines 20 and 21 use System.Media.SoundPlayer to play back the wave that we saved.
Press F5 to run your program and bask in the glory of a very loud 440Hz sine wave.
Next Steps: If you are a math Jedi, you can experiment with the following code from WaveGenerator.cs:
double t = (Math.PI * 2 * freq) / (format.dwSamplesPerSec * format.wChannels);
for (uint i = 0; i < numSamples - 1; i++)
// Fill with a simple sine wave at max amplitude
for (int channel = 0; channel < format.wChannels; channel++)
data.shortArray[i + channel] = Convert.ToInt16(amplitude * Math.Sin(t * i));
Just remember it’s two-channel audio, so you have to write each channel in the frame first before writing the next frame.
In the next article, we’ll look at some algorithms to generate other types of waves.
Currently Playing: Lamb of God – Wrath – Set to Fail