10.3. Creating an Echo
Creating an echo effect is similar to the splicing method (Program 74 (page 300)) that we saw in the last chapter, but involves actually creating sounds that didn't exist before. We do that by actually adding wave forms. What we're doing here is adding samples from a delay number of samples away into the sound, but multiplied by 0.6 so that they're fainter.
Program 81. Make a Sound and a Single Echo of It
/** * Method to add an echo to a sound * @param delay the number of samples before the echo starts */
The echo method takes a delay: the number of samples before the echo starts. Try this with different amounts of delay. With low values of delay, the echo will sound more like vibrato. Higher values (try 10,000 or 20,000) will give you a real echo.
This method creates a copy of the current sound s. This is where we'll get the original, unadulterated samples for creating the echo. (You could try this without creating a copy to get some interesting layered echoes.)
Next we declare a variable value to hold a value of a sample.
Our loop starts with the index i being set to the passed delay and continues through the rest of the sound.
The echoed sound is delay samples back, so i-delay is the sample we need. We multiply it by 0.6 to make it softer in volume.
We then add the echoed sample to the current sample at i and set it in the current Sound object.
Try this method on sounds with words in them (Figure 10.3).
> String fileName = FileChooser.getMediaPath("thisisatest.wav"); > Sound sound = new Sound(fileName); > sound.explore(); > sound.echo(20000); > sound.explore();
This method actually lets you set the number of echoes that you get. You can generate some amazing effects in this way.
/** * Method to create multiple echoes of the current sound * @param delay the number of samples before the echo starts
To try out this method, create a Sound object, and then invoke this method on the Sound object. Be sure to save the resulting Sound.
> Sound sound = new Sound(FileChooser.getMediaPath("croak.wav")); > Sound echo = sound.echo(8000,5); > echo.play();