LAB 24 – Modifying All Samples in a Sound (part 2 – Manipulating Sounds)

Lab Exercises

Topics

n      To manipulate the volume of sounds

n      To debug sound programs (methods)

n      To use iteration for manipulating sounds

 

Exercises

 

8.3 Changing the Volume of Sounds

8.3.1 Increasing Volume

 

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 PlaySounds

{

     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 increase the volume of a sound

   * @param source - source sound

   */

public static void increaseVolume(Sound source)

  {

    SoundSample[] sampleArray = source.getSamples();

    int value = 0;                           // value at sample

 

    // loop through SoundSample objects

    for (SoundSample sample : sampleArray)

    {

      value = sample.getValue();     // get the value

      sample.setValue(value * 2);    // set the value

     }

  }

 

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

 

increaseVolume(sourceSound);

 

iii) Test your program with different .wav files. Replace sourceSound.play(); with sourceSound.explore(); to confirm that the volume increased.

 

iv) Change increaseVolume to changeVolume by adding a second parameter called factor of type double. [You can use either a for-each, while, or for loop]

 

v) Change the call in the main method so that it calls changeVolume and sends it a value for factor. [try both numbers greater than and less than 1.0]

 

QUESTIONS:

  1. What happens if you increase a volume too far? Explore this by increasing the volume (using changeVolume) again and again. Does the sound always keep getting louder? Explain.
  2. Modify changeVolume so that instead of multiplying by a factor (like 2 or 0.5), you add a value (like 100 or 1000) to every sample. Explain what happened to the sound.

EXTRA CHALLENGE:

  1. Set a few hundred samples in the middle of the sound to a constant value. What if you set the samples to 32,767? -32,768?

 

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