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:
EXTRA CHALLENGE:
Submit the .java file and answers to the questions through the DropBox in WebCT.