Processing Sound Ranges part 1 Barb Ericson Georgia
Processing Sound Ranges part 1 Barb Ericson Georgia Institute of Technology Sept 2005 Georgia Institute of Technology
Learning Goals • Processing ranges of Sound values – Creating a sound clip – Splicing sounds together – Mirroring a sound • Computing concepts – Looping through a range – Returning a value from a method – Change more than one variable in a for loop Georgia Institute of Technology
Creating a Sound Clip • To clip the “This” out of “This is a test”. – Determine where it starts and stops – Using the sound explorer: • String file = File. Chooser. get. Media. Path(“thisisatest. wav”); • Sound s = new Sound(file); • s. explore(); Georgia Institute of Technology
Finding the End of the This Play before bar to check Position the bar Get the index Georgia Institute of Technology
To Create a Sound Clip • Create a new Sound object – Of the appropriate size • Ending value – starting value + 1 • Loop from start to end (inclusive) – for (int x = start; x <= end; x++) • Use get. Sample. Value. At(index) – And set. Sample. Value. At(index, value); • Return the new sound object Georgia Institute of Technology
Clip Method public Sound clip(int start, int end) { // calc the num samples int num. Samples = end - start + 1; Sound target = new Sound(num. Samples); int value = 0; int target. Index = 0; // copy from start to end for (int i = start; i <= end; i++, target. Index++) { value = this. get. Sample. Value. At(i); target. set. Sample. Value. At(target. Index, value); } return target; } Georgia Institute of Technology
Testing the Clip Method String file = File. Chooser. get. Media. Path( “thisisatest. wav”); Sound s = new Sound(file); Sound s 2 = s. clip(0, 8500); s 2. write( File. Chooser. get. Media. Path(“this. wav”)); s 2. play(); Georgia Institute of Technology
Challenge • • Create a clip of “is” from thisisatest. wav Determine where to start and end the clip Create the clip Write it to a file Georgia Institute of Technology
Returning a Value from a Method • To return a value from a method – Include a return statement in the body of the method – The type of the thing being returned must match the declared return type – The clip method declared that it returned a Sound object – The return statement returned the target Sound object – If the types don’t match you will get a compile error Georgia Institute of Technology
Splicing Sounds Together • Originally meant cutting the sound tape into segments and then assembling them in the right order • Easy to do digitally • Copy more then one sound into a target sound – Track the source index and target index Georgia Institute of Technology
Splice Method public void splice() { Sound sound 1 = new Sound(File. Chooser. get. Media. Path(“guzdial. wav”)); Sound sound 2 = new Sound(File. Chooser. get. Media. Path("is. wav")); int target. Index = 0; // the starting place on the target int value = 0; // copy all of sound 1 into the current sound (target) for (int i = 0; i < sound 1. get. Length(); i++, target. Index++) { value = sound 1. get. Sample. Value. At(i); this. set. Sample. Value. At(target. Index, value); } Georgia Institute of Technology
Splice Method - Continued // create silence between words by setting values to 0 for (int i = 0; i < (int) (this. get. Sampling. Rate() * 0. 1); i++, target. Index++) { this. set. Sample. Value. At(target. Index, 0); } // copy all of sound 2 into the current sound (target) for (int i = 0; i < sound 2. get. Length(); i++, target. Index++) { value = sound 2. get. Sample. Value. At(i); this. set. Sample. Value. At(target. Index, value); } } Georgia Institute of Technology
Testing the Splice Method • String silence = File. Chooser. get. Media. Path("sec 3 silence. w av"); • Sound target = new Sound(silence); • target. explore(); • target. splice(); • target. explore(); Georgia Institute of Technology
Create an Audio Sentence Exercise • Create a method that splices 3 sounds together to finish the sentence, “Guzdial is”. – Make sure that you don’t copy past the end of the current sound – Be sure to include silence between words – Can you make this method more general? • How about a method to splice a sound into the middle of another sound? – Take starting point in target for splice Georgia Institute of Technology
Reversing a Sound • To reverse a sound – Create a copy of the original sound • Sound orig = new Sound(this. get. File. Name()); – Then loop starting the source. Index at the last index in the source and the target. Index at the first index in the target • Decrement the source. Index each time • Increment the target. Index each time 100 | 200 | 300 | 400 | 500 target. Index 500 | 400 | 300 | 200 | 100 Georgia Institute of Technology source. Index
Reversing Method public void reverse() { Sound orig = new Sound(this. get. File. Name()); int length = this. get. Length(); // loop through the samples for (int target. Index = 0, source. Index = length - 1; target. Index < length && source. Index >= 0; target. Index++, source. Index--) this. set. Sample. Value. At(target. Index, orig. get. Sample. Value. At(source. Index)); } Georgia Institute of Technology
Testing the Reverse Method • String file = File. Chooser. get. Media. Path( “thisisatest. wav”); • Sound s = new Sound(file); • s. explore(); • s. reverse(); • s. explore(); Georgia Institute of Technology
Reverse Part of a Sound Exercise • Reverse just the second half of a sound – Start the target. Index at the length / 2 – Start the source. Index at the length – 1 – Loop while the target. Index < length 100 | 200 | 300 | 400 | 500 100 | 200 | 500 | 400 | 300 target. Index Georgia Institute of Technology source. Index
Mirror a Sound • Copy the first half of the sound to the second half – And reverse the sounds in the second half – This is very similar to mirroring a picture • Calculate the midpoint (length / 2) • Start the source index at 0 and copy from index to length – index -1 • While index < midpoint 100 | 200 | 300 | 400 | 500 100 | 200 | 300 | 200 | 100 Georgia Institute of Technology
Mirror Sound Method public void mirror. Front. To. Back() { int length = this. get. Length(); // save the length int mirror. Point = length / 2; // mirror around this int value = 0; // hold the current value // loop from 1 to mirror. Point for (int i = 0; i < mirror. Point; i++) { value = this. get. Sample. Value. At(i); this. set. Sample. Value. At(length – i - 1, value); } } Georgia Institute of Technology
Testing Mirror Method • Sound s = new Sound(File. Chooser. get. Media. Path( "croak. wav")); • s. explore(); • s. mirror. Front. To. Back(); • s. explore(); Georgia Institute of Technology
Summary • You can work with ranges by changing the start and end value of an index in a loop • You can return a value from a method – Declare the return type – Use a return statement • You are not required to change a variable in a for loop – But you can change more than 1 Georgia Institute of Technology
- Slides: 22