LAB 28 – Modifying Samples Using Ranges (part 3 – Reversing and Mirroring Sounds)
Lab Exercises
Topics
n To reverse sounds
n To mirror sounds
n To identify algorithms that cross media boundaries
Exercises
9.4 Reversing a Sound
i) Type in the code for a main method that asks the user for a sound filename and sends the Sound to the reverse method and then plays (and/or explores) the revised sound.
ii) Type in the following code in the Program window below the main method and before the last }:
/* method to reverse a sound
* @param source – source sound
*/
public static void reverse (Sound source)
{
Sound copySound = new Sound(source.getFileName());
int length = source.getLength();
int value = 0;
// loop through the samples
for (int targetIndex = 0, sourceIndex = length - 1; targetIndex < length && sourceIndex >= 0;
targetIndex++, sourceIndex--)
{
value = copySound.getSampleValueAt(sourceIndex);
source.setSampleValueAt(targetIndex, value);
}
}
iii) Test your program with different .wav files. Use sourceSound.play(); and sourceSound.explore(); to confirm that the reverse worked.
iv) Type in the following code in the Program window below the main method and before the last }:
/* method to splice part of a passed sound into the target sound at the given start index
* @param source - source sound to copy
*/
public static void mirrorFrontToBack(Sound source)
{
int length = source.getLength(); // save the length
int mirrorPoint = length / 2; // mirror around this
int value = 0; // hold the current value
// loop from 1 to mirrorPoint
for (int i = 0; i < mirrorPoint; i++)
{
value = source.getSampleValueAt(i);
source.setSampleValueAt(length – i - 1,value);
}
}
v) Modify your main method to call mirrorFrontToBack. Test your program with different .wav files. Use targetSound.play(); and targetSound.explore(); to confirm that the mirror worked.
QUESTIONS:
Submit the .java file with the mirrorFrontToBack and mirrorBackToFront methods through the DropBox in WebCT.