Lab 1: Wav Files
Resources Needed
- If you need to install Java, please follow these instructions.
- Accept the GitHub Classroom assignment invitation in Canvas and then, in IntelliJ, create a new project from Version Control using the repository URL. If you haven't used this before, see the pre-lab activity that was part of CS1011 Lab 6.
WavFile
Documentation
Overview
In this assignment you will make use of the WavFile
class to read and write .wav
audio files.
Details
ArrayList Requirement
This assignment is meant as a review of material covered in CS1011. You are required to use at least one ArrayList
object.
Using a Supplied Class
For this assignment, you will need to make use of the WavFile
class (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
.wav
extension) and then read the file in and write a separate.wav
file with all of the audio samples placed in reverse order. For example, if the user enterscymbal
, the output file should be calledcymbalRev.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
.wav
file 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
.wav
file 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.
Hints
- To generate one second of audio, make sure that the
numFrames
and thesampleRate
contain the same value. - You may use
cymbal.wav
andominous.wav
as sample.wav
files. You may wish to use theWavFile.toString()
method to determine appropriate values to pass the multi-argumentWavFile
constructor. There are some additional .wav files in thesounds
folder for those interested. - To access the sound files you will need to add the
sounds
folder to the filename, e.g., "sounds/cymbal.wav". - 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
i
is the counter in the loop generating samples. Since thesampleRate
is the number of samples per second, if you desire one second worth of sound, you'll need to generatesampleRate
samples. - For one second of single-channel audio (option 2), these numbers should work when creating the
WavFile
object:numChannels==1
,numFrames==8000
,validBits==8
, andsampleRate==8000
. - 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
ArrayList
of 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
WavFile
object, be sure to call.close()
to be sure the file has been written completely.
Acknowledgment
This laboratory assignment was developed by Dr. Chris Taylor.