Section 9.2. Create a Sound Clip

[Page 295 (continued)]

9.2. Create a Sound Clip

Sometimes a sound is too long and you want just part of it. This can happen when you record a sound. There may be silence before and after the actual sound. Or, you may want to pull one word out of a sound. You can "clip" a sound just as we clipped a picture by copying just part of the sound. What if we want to pull the "This" out of the sound thisisatest.wav? How can we tell where it ends? Open an explorer on the sound using sound.explore();. Click at the first flat area after the first non-flat area (silence should have values near 0). Click the PLAY BEFORE button to play the part of the sound before the current index. You can use the arrow buttons to change the current index as well. Using the explorer we see that the word "this" ends at about 8,500 (Figure 9.1).

Figure 9.1. Exploring the "This is a test'' to find the end of the first word.
(This item is displayed on page 296 in the print version)

So to copy just part of a sound into another sound we will need to create a new Sound object. One of the ways to make a new Sound object is to tell it how many samples it will have. To calculate the number of samples we can subtract the ending value from the starting value and add 1.

[Page 296]

We can then create a new sound and loop copying from the start to the end from the source Sound object into the target Sound object starting at the beginning of the target sound. We have to make sure to increment both the index in the source and the index in the target. If we forget to increment the source index, we will copy the same source sample over and over, and if we fail to increment the target index, we will copy to the same place in the target over and over.

We need to return our new sound object in order to be able to refer to it again. To return something from a method, we need to specify the type of the thing that will be returned in the method declaration (replacing the void keyword). We also need to use the keyword return followed by what we want to return.

Program 72. Create a Sound Clip
(This item is displayed on pages 296 - 297 in the print version)

/**  * Method to create a new sound by copying just part of  * the current sound to a new sound  * @param start the index to start the copy at (inclusive)  * @param end the index to stop the copy at (inclusive)  * @return a new sound with just the samples from start to  * end in it  */ public Sound clip(int start, int end) {   //calculate the number of samples in the clip 
[Page 297]
int lengthInSamples = end - start + 1; Sound target = new Sound(lengthInSamples); // hold clip int value = 0; // holds the current sample value int targetIndex = 0; // index in target sound // copy from start to end from source into target for (int i = start; i <= end; i++, targetIndex++) { value = this.getSampleValueAt(i); target.setSampleValueAt(targetIndex,value); } return target; }

Notice that we said that we would return a Sound object from this method by saying that the type of thing returned is from the class Sound. At the end of the method, we use the keyword return followed by the variable that refers to the new Sound object. So in order to refer to this new Sound object again, we will need to declare a variable and set the value of that variable to refer to the returned Sound object.

> Sound test = new Sound(FileChooser.getMediaPath("thisisatest.wav")); > test.explore(); > Sound s1 = test.clip(0,8500); >; > s1.explore();

Use the explorer on the original sound and the clipped sound. Change the number of samples between pixels to be 100 in the clipped sound. Then compare the sample values. Convince yourself that the clipped sound does have the same values as the original (Figure 9.2).

Figure 9.2. Exploring the sound clip.
(This item is displayed on page 298 in the print version)

Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Year: 2007
Pages: 191 © 2008-2017.
If you may any questions please contact us: