Processing Sound Ranges part 3 Barb Ericson Georgia
Processing Sound Ranges part 3 Barb Ericson Georgia Institute of Technology Sept 2005 Georgia Institute of Technology
Learning Goals • Processing ranges of Sound values – Blending pictures – Changing the sound frequency • Computing concepts – Method overloading – Changing a for loop to increment by a value other than 1 – Modifying a method to be more general Georgia Institute of Technology
Modify Blend Sounds Exercise • Create another blend. Sounds method – That takes the file name of the sounds to blend – And a value to start the blend at and another to stop the blend at – Modify the original blend. Sounds method to call this one Georgia Institute of Technology
Overloading Methods • You can have several methods with the same name – As long as the parameter list is different • In number of parameters • And/or types of parameters – blend. Sounds() – blend. Sound(String name 1, String name 2, int start. Blend, int end. Blend) Georgia Institute of Technology
Changing the Sound Frequency • The frequency of a wave is the number of cycles per second (cps), or Hertz (Hz) – (Complex sounds have more than one frequency in them. ) • Our perception of pitch is related (logarithmically) to changes in frequency – Higher frequencies are perceived as higher pitches – We can hear between 5 Hz and 20, 000 Hz (20 k. Hz) – A above middle C is 440 Hz Georgia Institute of Technology
Double the Frequency • If we take every other sample we double the frequency of the sound – Completes two cycles instead of one in the same time – It will sound higher 100 | 200 | 300 | 400 | 500 100 | 300 | 500 | 0 Georgia Institute of Technology
Double Frequency Method public void double. Freq() { // make a copy of the original sound Sound s = new Sound(this. get. File. Name()); /* loop and increment target index * by one but source index by 2, * and set target value * to the copy of the original sound */ Georgia Institute of Technology
Double Frequency - Continued for (int source. Index=0, target. Index = 0; source. Index < this. get. Length(); source. Index=source. Index+2, target. Index++) this. set. Sample. Value. At(target. Index, s. get. Sample. Value. At(source. Index)); // clear out the rest of this sound to silence (0) for (int i = this. get. Length() / 2; i < this. get. Length(); i++) this. set. Sample. Value. At(i, 0); } Georgia Institute of Technology
Test Double Frequency Sound s = new Sound(File. Chooser. get. Media. Path( "c 4. wav")); s. explore(); s. double. Freq(); s. explore(); Georgia Institute of Technology
Challenge • Create a method that will take every third sample – Will this sound higher or lower? • Can you make this method more general? – By passing in the amount to add to the source index each time? Georgia Institute of Technology
Halving the Frequency • We can copy each source value twice to half the frequency – Only get through half a cycle in the same time we used to get through a full cycle – It will sound lower • This is the same algorithm that we used to scale up a picture 100 | 200 | 300 | 400 | 500 100 | 200 | 300 Georgia Institute of Technology
Halve Frequency Method public void halve. Freq() { // make a copy of the original sound Sound s = new Sound(this. get. File. Name()); /* loop through the sound and increment target index * by one but source index by 0. 5 and set target value * to the copy of the original sound */ for (double source. Index=0, target. Index = 0; target. Index < this. get. Length(); source. Index=source. Index+0. 5, target. Index++) this. set. Sample. Value. At((int) target. Index, s. get. Sample. Value. At((int) source. Index)); } Georgia Institute of Technology
Testing Halve Frequency Sound s = new Sound(File. Chooser. get. Media. Path( "c 4. wav")); s. explore(); s. halve. Freq(); s. explore(); Georgia Institute of Technology
Change Frequency Exercise • Write a method that will copy each sound value 4 times to the target – Will the new sound be higher or lower? • Can you make this more general? – By passing in the number of times to copy the source value – Try it with 3 times and check the index values to make sure that you are doing it right Georgia Institute of Technology
Summary • You can have more than one method with the same name: method overloading – As long as the parameter list is different • Different number of parameters • Different types of parameters • You can increment or decrement loop variables by numbers other than 1 • You can make methods more general – By passing in parameters • Some algorithms are the same for pictures and sounds – Scaling a picture down and doubling the frequency of a sound Georgia Institute of Technology
- Slides: 15