Processing Sound Ranges part 3 Barb Ericson Georgia Institute of Technology Oct 2009 UsingSoundRanges-part3
UsingSoundRanges-part3 Learning Goals Processing ranges of Sound values Creating echoes Changing the sound frequency Computing concepts Changing a for loop to increment by a value other than 1 Modifying a method to be more general UsingSoundRanges-part3
UsingSoundRanges-part3 Echo By blending a sound with itself we can make an echo Add the sound values to the same sound but after some delay UsingSoundRanges-part3
UsingSoundRanges-part3 Method echo public void echo(int delay) { // make a copy of the original sound Sound s = new Sound(this.getFileName()); int value = 0; // loop from delay to end of sound for (int i = delay; i < this.getLength(); i++) UsingSoundRanges-part3
UsingSoundRanges-part3 Method echo continued /* get the value back by delay samples from the * copy of the sound and make it fainter */ value = (int) (s.getSampleValueAt(i-delay) * 0.6); /* set the value at the current index to the sum * of the current value and the echo */ this.setSampleValueAt(i, this.getSampleValueAt(i) + value); } UsingSoundRanges-part3
UsingSoundRanges-part3 Test the echo method String fileName = FileChooser.getMediaPath( "thisisatest.wav"); Sound sound = new Sound(fileName); sound.explore(); sound.echo(20000); UsingSoundRanges-part3
UsingSoundRanges-part3 How does that work? The echo method takes an integer delay The number of samples to wait before starting the echo It makes a copy of the current sound So that we copy without the echo Loop starting from delay to the end of the sound Get the value at i-delay and multiply it by 0.6 to make it quieter Set the current sound value at i to the current value at i plus the calculated value UsingSoundRanges-part3
Creating Multiple Echoes You can use a nested loop to create multiple echoes Here is an example nested loop in Alice UsingSoundRanges-part3
Creating Multiple Echoes Method public Sound echo(int delay, int numEchoes) { int soundLength = this.getLength(); Sound echoSound = new Sound(numEchoes * delay + soundLength); int value = 0; int echoIndex = 0; int echoValue = 0; double echoAmplitude = 1; // to start // copy the original sound echoSound.splice(this,0,soundLength,0); /* loop starting with 1 to create the first echo at the * right place and end when = the number of echoes */ UsingSoundRanges-part3
Multiple Echoes - continued for (int echoCount = 1; echoCount <= numEchoes; echoCount++) { // decrease the volume (amplitude) of the echo echoAmplitude = echoAmplitude * 0.6; // echo the whole sound for (int i = 0; i < soundLength; i++) echoIndex = i + (delay * echoCount); echoValue = (int) (this.getSampleValueAt(i) * echoAmplitude); echoSound.setSampleValueAt(echoIndex,echoValue + echoSound.getSampleValueAt(echoIndex)); } return echoSound; UsingSoundRanges-part3
UsingSoundRanges-part3 Try Multiple Echoes Sound s = new Sound( FileChooser.getMediaPath("croak.wav")); Sound e = s.echo(8000,5); e.play(); UsingSoundRanges-part3
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 kHz) A above middle C is 440 Hz UsingSoundRanges-part3
UsingSoundRanges-part3 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 | 0 UsingSoundRanges-part3
Double Frequency Method public void doubleFreq() { // make a copy of the original sound Sound s = new Sound(this.getFileName()); /* loop and increment target index * by one but source index by 2, * and set target value * to the copy of the original sound */ UsingSoundRanges-part3
Double Frequency - Continued for (int sourceIndex=0, targetIndex = 0; sourceIndex < this.getLength(); sourceIndex=sourceIndex+2, targetIndex++) this.setSampleValueAt(targetIndex, s.getSampleValueAt(sourceIndex)); // clear out the rest of this sound to silence (0) for (int i = this.getLength() / 2; i < this.getLength(); i++) this.setSampleValueAt(i,0); } UsingSoundRanges-part3
UsingSoundRanges-part3 Test Double Frequency Sound s = new Sound(FileChooser.getMediaPath( "c4.wav")); s.explore(); s.doubleFreq(); The file c4.wav is the note C played on a piano in the fourth octave. UsingSoundRanges-part3
UsingSoundRanges-part3 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? UsingSoundRanges-part3
UsingSoundRanges-part3 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 100 | 200 | 300 | 400 | 500 100 | 100 | 200 | 200 | 300 UsingSoundRanges-part3
Halve Frequency Method public void halveFreq() { // make a copy of the original sound Sound s = new Sound(this.getFileName()); /* 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 sourceIndex=0, targetIndex = 0; targetIndex < this.getLength(); sourceIndex=sourceIndex+0.5, targetIndex++) this.setSampleValueAt((int) targetIndex, s.getSampleValueAt((int) sourceIndex)); } UsingSoundRanges-part3
Testing Halve Frequency Sound s = new Sound(FileChooser.getMediaPath( "c4.wav")); s.explore(); s.halveFreq(); UsingSoundRanges-part3
UsingSoundRanges-part3 Challenge 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 You can add 0.5 and 0.25 and the (int) of that will work fine. But, you can have trouble with things like 1/3 which is .3333333 (and so on). Computers can’t do infinite numbers. So, they have to approximate them. So adding .33 each time will not get over 1 the way it should. UsingSoundRanges-part3
UsingSoundRanges-part3 Summary You can increment or decrement loop variables by numbers other than 1 You can make methods more general By passing in parameters UsingSoundRanges-part3