LAB 25 – Modifying All Samples in a Sound (part 3 – Normalizing Sounds)

Lab Exercises

Topics

n      To normalize the volume of sounds

n      To create (and avoid) clipping

n      To use conditionals when manipulating sounds

 

Exercises

 

8.4 Normalizing Sounds

 

Type in the following code in the Program window (changing the comments):

/*  Program that plays sounds

 * using loops

 *

 * Sound and SoundSample classes are defined in bookClasses developed

 * at Georgia Tech by Mark Guzdial / Barbara Ericson

 *

 * @author Wayne Summers

 * @date   Oct. 15, 2007

 */

 

public class PlaySounds2

{

     public static void main (String[] args)

    {

                String fileName1;

                System.out.println(“Find the source file”);

                fileName1 = FileChooser.pickAFile();  // note that this pops up a window to                                                                                      // select a .wav file

                Sound sourceSound;

                sourceSound = new Sound (fileName1);

             

             // calls to methods that manipulate sounds go here

             

               sourceSound.play(); 

    }

}

 

i) Type in the following code in the Program window below the main method and before the last }:

  /* method to normalize the volume of a sound

   * uses the changeVolume method

   * @param source - source sound

   */

public static void normalize(Sound source)

{

    SoundSample[] sampleArray = source.getSamples();

    SoundSample sample = sampleArray[0];

    int value = 0;

    int max = soundSample.getValue();

 

    // loop comparing values to the current largest

    for (int i = 1; i < sampleArray.length; i++)

    {

      sample = sampleArray[i];

      value = sample.getValue();

       if (Math.abs(value) > Math.abs(max))

      {

         max = value;

       } //end if

    } // end for

 

    // calculate the multiplier

    double multiplier = 32767.0 / max;

 

    // change the volume

            changeVolume(source, multiplier);

}

 

ii) Type in the following code in the Program window in the main method and before the sourceSound.play();

 

normalize(sourceSound);

 

iii) Test your program with different .wav files. Use sourceSound.play(); and sourceSound.explore(); to confirm that the volume is normalized.

 

8.4.1 Generating Clipping

 

QUESTIONS:

  1. What happens if you set all positive values to the maximum value (32767) and all negative values to the minimum value (-32768)? Try with different .wav files. Can you understand any of the words?
  2. Does it matter whether you set the values in (1) to maximum and minimum values or some other value (say ˝ maximum and ˝ minimum)?

Submit the .java file and answers to the questions through the DropBox in WebCT.