LAB 30 – Making Sounds by Combining Pieces (part 2 – Changing Frequencies)
Lab Exercises
Topics
n To change the frequency (pitch) of a sound
Exercises
10.3 How Sampling Keyboards Work
i) Type in the code for a main method that declares one Sound objects, asks the user for the sound filename and sends the Sound to the increaseFreq method (where it changes the frequency of the method and then plays (and/or explores) the changed Sound.
Type in the following code in the Program window (changing the comments):
/* Program that changes the frequency of sounds
*
* Sound and SoundSample classes are defined in bookClasses developed
* at Georgia Tech by Mark Guzdial / Barbara Ericson
*
* @author Wayne Summers
* @date Oct. 31, 2007
*/
public class IncreaseFrequency
{
public static void main (String[] args)
{
String fileName = FileChooser.pickAFile();
Sound target = new Sound(fileName);
double value = 0.75;
increaseFreq(target, value);
target.play();
target.explore();
}
ii) Type in the following code in the Program window below the main method and before the last adding your own comments}:
*/
public static void increaseFreq (Sound target, double factor)
{
// make a copy of the original sound
Sound s = new Sound(target.getFileName());
/* loop and increment target index by one but source index by the factor,
* and set target value to the copy of the original sound
*/
for (double sourceIndex=0, targetIndex = 0; targetIndex < target.getLength();
sourceIndex=sourceIndex+factor, targetIndex++)
{
if (sourceIndex >= s.getLength())
sourceIndex = 0;
target.setSampleValueAt((int) targetIndex, s.getSampleValueAt((int) sourceIndex));
}
}
iii) Test your program with different .wav files to confirm that the method works.
iv) Modify your main method to use the Scanner class to ask the user for the frequency factor and assign it to value. Test your program with different .wav files.
QUESTIONS:
Submit the .java file with the increaseFreq and revised main methods through the DropBox in WebCT.