Post Lab 11: Audio Editing
Due Monday, December 6, 2010 by 4:00 p.m.
In this assignment you will write a program that will edit audio files to
create an echo effect.
Computers represent sounds as a series of discrete measurements
of a sound's amplitude at a fixed interval. Each amplitude measurement
is called a sample.
The file Sound.java contains a Java class that
has a static method to read an audio file into an array of shorts. Each element
of the array is a sample, a 16-bit number that measures the
amplitude of the sound.
It is possible to modify a sound by simply changing the amplitudes in the array of
samples.
An echo can be added to a sound by "mixing"
the amplitude of the sound in each sample with the amplitudes of
earlier samples; that is, we basically add some fraction of
the amplitude at a previous sample
to the current amplitude.
The distance between copies of amplitude (the current sample and
the previous one) is
the delay of the echo and the amount (fraction)
that the copy diminishes
is the decay of the echo.
Create a program that does the following:
- Prompt the user to enter the name of an audio file (the file must be located
in the same directory as the java file). The filename is just a String.
- Use the static read method of the
Sound class to read the file and convert it to an array of
amplitudes. Note that the signature of the method is as follows:
public static short[] read (String fileName)
The method will return the array of samples if all goes well;
it will return null
if it is unable to read the specified file. If
it is unable to read the file the program should prompt the user again for a file name.
You need to store the array returned in an array of shorts. This is
your array of samples (so name it appropriately!).
- Compute and print the length of the sound in seconds.
The getSampleRate method of the
Sound class will return the number of samples per second of a sound file.
Its signature is as follows:
public static float getSampleRate(String fileName)
Use the samples per second returned by this method
and the length of the sample array (the total
number of samples in the sound) to determine the length of the sound in
seconds.
- TEST your program before going further! Make sure it
compiles and runs and prints reasonable numbers (under 30 seconds).
Two test files are given below.
- Prompt the user to enter the delay of the echo effect to be produced in seconds.
The delay should allow fractional seconds, but not negative values. Prompt the user
again if the number of seconds is not positive.
- Prompt the user to enter the decay of the echo. The decay should be a number
between 0 and 1. Prompt the user again if an invalid decay is entered.
- Calculate the number of samples in the new echo-modified sound as
follows:
- The number
of repetitions of the echo can be computed using the equation:
numEchos = log(0.01) / log(decay)
-
The new sound length in seconds can then be computed as:
new length (in seconds) = original length (in seconds) + numEchos * delay
If the new length is longer than 30 seconds, you should prompt the user to enter new
delay and decay values. (Note - to do this just enclose the code you
already have to read the delay and the decay in a do...while loop.)
- The new length of the sound in number of samples is computed
by using the new length in seconds and the sample rate of the sound.
- TEST this (print your new length - both seconds and number
of samples) before proceeding.
- Create an array to store the new echo-modified sound (using the calculated
length as a number of samples)
and copy the original sound into it.
- Add the echo effect to the new sound by copying and scaling amplitudes. In order
to ensure that the calculated amplitudes do not exceed the allowed level use the following
equation:
samplei = (samplei - delay * decay + samplei) / (1.0 + decay)
- Play the sound using the play method of the Sound class.
Note that the signature of this method is as follows:
public static void play (short[] samples, float sampleRate)
Hence you need to send the new sample array and the sample rate
as parameters.
You can test your program using the audio files hello.aiff
and guitar.wav. You can also test it on your own files, however,
the sound class requires that the audio file be a 16-bit aiff or wav file. You can convert a file
to this format using the open-source sound editing program
Audacity.
NOTE: To hear the sounds on the computers in lab you need
ear phones!
Submission
Tar your postlab11 directory
and copy the tar file containing your code
to the directory:
~ingram/CPSC120/yourname