Lab 1: Wav Files
In this assignment you will make use of the
WavFile class to read and write
.wav audio files.
This assignment is meant as a review of material covered in CS1011. You are required to use at least one
Using a Supplied Class
For this assignment, you will need to make use of the WavFile.java file (javadoc). You may not change the package for the
WavFile class. You must place it in the appropriate folder within your project.
Basic Program Flow
You'll need to create one class called
WavGenerator which should be in a package named the same as your MSOE username (e.g.,
taylor). Note: Do not place all of the functionality for your program in
main. Prior to coding, decide on how you plan to encapsulate functionality into multiple methods. This class must contain your program that makes use of the
WavFile class to do the following:
- Ask the user to enter 0, 1, 2, or 3. If the user enters anything other than one of those four options, the program should reprompt the user to enter one of the four options (it should continue to do this forever).
- If the user enters 0, the program should exit.
- If the user enters 1, the program should prompt the user to enter a filename (without the
.wavextension) and then read the file in and write a separate
.wavfile with all of the audio samples placed in reverse order. For example, if the user enters
cymbal, the output file should be called
cymbalRev.wav. Once this has been completed, the program should return to the prompt which asks the user to enter 0, 1, 2, or 3.
- If the user enters 2, then the program should ask the user for a filename (without an extension) and a frequency. The program should then create a
.wavfile containing one second worth of audio that represents a tone at the specified frequency. Once this has been completed, the program should return to the prompt which asks the user to enter 0, 1, 2, or 3.
- Optional: If the user enters 3, then the program should ask the user for a filename (without an extension) and two frequencies. The program should then create a
.wavfile containing one second worth of audio in stereo. One frequency should be on one channel and the other frequency on the other channel. Once this has been completed, the program should return to the prompt which asks the user to enter 0, 1, 2, or 3.
Note: Your program must not crash on menu selection regardless of what the user enters for a menu option.
- To generate one second of audio, make sure that the
sampleRatecontain the same value.
- You may use cymbal.wav and ominous.wav as sample
.wavfiles. You may wish to use the
WavFile.toString()method to determine appropriate values to pass the multi-argument
WavFileconstructor. Here are some additional .wav files for those interested.
- Place the
.wavfile in the project folder (in the same folder/directory that contains the
srcfolder (not in the
- To generate a tone at a given frequency you should generate a sine wave with values between -1.0 and 1.0 at the specified frequency. The formula to use for this is sin(2pi x i x (freq/sampleRate)). Where
iis the counter in the loop generating samples. Since the
sampleRateis the number of samples per second, if you desire one second worth of sound, you'll need to generate
- For one second of single-channel audio (option 2), these numbers should work when creating the
- To generate stereo audio you will need to set the number of channels to two and interleave the audio samples for each channel in the
ArrayListof samples. Said another way, the samples for the first channel should be placed only at even indices and the samples for the second channel should be placed only at odd indices.
- Once you have placed the samples into the
WavFileobject, be sure to call
.close()to be sure the file has been written completely.
This laboratory assignment was developed by Dr. Chris Taylor.