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:

  1. 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.

  2. 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!).

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Calculate the number of samples in the new echo-modified sound as follows:

  8. TEST this (print your new length - both seconds and number of samples) before proceeding.

  9. 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.

  10. 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)

  11. 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