CSI Lab 09

Tuesday, March 10th


Structure Details

I want to try something different this week.

For this week's lab you are encouraged, although not required, to work with a partner.  The advantage of working with a partner is that two heads are better than one, and you can benefit from discussing the problems with a partner. 

Before you begin, decide between yourselves who will be Partner A and who will be Partner B.  Please notice as the lab goes on that you will change roles several times.  Sometimes you will be the "coder" - the person in charge of the keyboard - and sometimes you will be the "supervisor" - the person in charge of telling the coder what to write.  In order to make this activity work, it is important that you change these roles a couple of times during the class.

You may feel free to turn in ONE signed form for the team, just include the names of BOTH team members on the form.


Introduction

In this week's lab you will be looking at how to play with sound using the materials presented in chapters 6 and 7 in your textbook. 

For this week's lab, you will need to have the following sound file downloaded onto your computer and placed in the mediasources folder where we have been placing your picture files.

As a general guideline, Activities A-D should take you a little more than an hour total, while Activity E should take you the remaining hour.


Activity A : Increase the Volume, and then Decrease the Volume- The Specialized Version

[Partner A - coder :  Partner B - supervisor]

Program 57 on page 169 of your textbook is a method called increaseAndDecrease().  Enter this program into your Sound class.  Test the method on one of the sound files provided, or one of your choice.

sound1 = makeSound( "preamble.wav" )
increaseAndDecrease(sound1)
play(sound1) 

[SIG 1] : Demonstrate the specialized version of  increaseAndDecrease() for a TA.


Activity B : Increase the Volume, and then Decrease the Volume- The Generalized Version

[Partner B - coder :  Partner A - supervisor]

The code you wrote in Activity A is good, but it is very specific.  You can only make half "loud" and then half "soft."   A more generalized method would be one that makes some part loud and then the remaining part soft.  This activity creates that generalized version.

Create a version called  increaseThenDecrease() .  This version should take a sound and a single int as parameters.  It can be assumed that this is a number between 0 and 100 inclusive.  This number should indicate what percentage of the sound clip should be increased in volume.  The remaining part should then have its volume decreased.  

In other words

sound1 = makesSound( "preamble.wav" )
increaseThenDecrease(sound1,50)
play(sound1) 

should sound exactly the same as the sound you produced in the previous activity. 

While

sound1 = makesSound( "preamble.wav" )
increaseThenDecrease(sound1,20)
play(sound1) 

should get quiet much sooner.

[SIG 2] : Demonstrate  increaseThenDecrease() for a TA.


Activity C : Erasing part of a sound - The Specialized Version

[Partner A - coder :  Partner B - supervisor]

Write a method called eraseSecondPart().  This method should erase the 2nd second of a sound sample by setting all the samples in the appropriate interval to 0.

To do this, you need to determine which range of samples represents the 2nd second.  Remember, you can find the sampling rate of a particular Sound by using the getSamplingRate() method.

[SIG 3] : Demonstrate the specialized eraseSecondPart() for a TA


Activity D : Erasing part of a sound - The Generalized Version

[Partner B - coder :  Partner A - supervisor]

Once again, how often will you want to erase the 2nd second of a Sound?  Probably not very often.  However, how often might you want to erase SOME portion of a sound?  Certainly more often than erasing one specific part of a Sound.

In this activity, you should create a second version of erasePart().  This method will take two ints as parameters - the starting point and the ending point.  Both values refer to the number of the sound sample and while the starting point should be inclusive the ending point should be exclusive.  In otherwords, if I wanted to erase the first 100 samples of a Sound I would type

erasePart(sound1,1,101)

which would erase samples 1-100 (again, notice this is 100 samples).

[SIG 4] : Demonstrate the generalized erasePart() for a TA


Activity E : Normalize and Fade

[Change off roles several times]

Create a method called normalizeAndFade().  This method should normalize the first second of a sound, then slowly decrease the sound in steps of 1/5 for each of the following seconds.  Any remaining sound should then be erased

Wow, that's a lot of things to do.  But if we break it down into smaller units, it doesn't become that bad.  I asked you to do 3 things - 1) Normalize the first second, 2) Iteratively decrease the volume of the next 5 seconds, 3) Erase the rest of the sample.  If we break this into smaller steps by writing a series of helper methods, the normalizeAndFade() method becomes only a few lines of code which call the smaller helper methods.

Step 1 - Write the helper method to normalize the first second

The first part of the normalizeAndFade() method asks you to normlize the first second.

You already wrote a normalize() method during lab08.  As we have pointed out several times in this lab, that set of methods was VERY specific.  It would help us if we had a generalized version of the normalize() method.  Using the code from lab08 as a guide, write a normalize() method that takes two ints as parameters - the starting point and the ending point.  As we did in Activity D, these points should refer to sample locations and should be inclusive and exclusive respectively.  To use these values, you will need to revise you for loop allowing you to use specific staring and stopping points. 

Note that to truly be working properly you need to normalize only on this range, which means finding the biggest value only in that range and then using the calculated multiplier only on that range.

Step 2 - Write the helper method to change the volume of a particular section

The next part of the normalizeAndFade() method asks you to decrease the sound in steps of 1/5 for each of the following seconds.  In other words, the 2nd second should have its volume reduced to 80% of its original, the 3rd second should have its volume reduced to 60% of its original, then 40%, then 20%, then 0%.

Since each of these steps is basically the same thing (change a section of the sample to a different volume), it would help if we had a very generalized helper method that would do this for us.

Write a method called changeVolume().  changeVolume() takes two ints and a double as parameters.  These values represent  a starting point (inclusive), an ending point (exclusive), and the factor by which you want to change the volume.

For example, if you think about this method, it actually would have been VERY helpful in Activity A.  Instead of writing all those loops, you could have just said:

//Note, you are not typing this code in your program.
//It is just here to help us make a point.
public void increaseAndDecrease()
{
	int half = this.getLength() / 2;
	
	this.changeVolume(0,half,2.0);
	this.changeVolume(half,this.length(),0.5);
}
(We won't require you to change your methods, but see how much nicer it could have been if we had written this method earlier ?!?)

Step 3 - Write the helper method to erase the remainder of the sound sample

The final  part of the normalizeAndFade() method asks you to erase the remainder of the sound sample.  In other words, you want a method that can take a starting point (inclusive) and an ending point (exclusive), and erase between them.

Write a method called erasePart() that takes two parameters that .... HOLD IT.  We did this already.  That was EXACTLY what you did in Activity D.  [Don't you love it when things are this easy!]

Step 4 - Put it all together inside of normalizeAndFade()

Now that you have your three helper methods ready to go, write the body of normalizeAndFade().  Use your method from step 1 to normalize the first second.  Write a loop that uses the method from step 2 to iteratively decrease the volume of the sample.  Then use the method from step3/Activity D to erase the rest of the sample.

[SIG 5] : Demonstrate normalizeAndFade() for a TA.