CS 140 - Lab 19 Overlaying Sounds with a sound method

Objectives

  1. Learn basics of functions (methods that return a value)
  2. Review previous lab on adding two sounds.
  3. Write a Sound method that adds two sounds together.

Part I - Potpourri

  1. Add a function named min to your Sound class. The min function takes two integer parameters and returns the smaller of the two. min is a class method, so be sure to declare it static. Here's an example of how the function might be called in your Lab19 class.
      System.out.println(Sound.min(5,10));
    
  2. Use Audacity to record two .wav files. One wave file should be of your voice and the other wave file should be of your partner's or someone else in the classroom. Make the wave files short, speak for a few seconds. Store these on your P: drive using your name as the name of the file before the .wav part. For example if the lab partners are Fred and Sally then the files should be named fred.wav and sally.wav.
  3. E-mail me the two wave files as an attachment so I can put them on the T: drive (with your permission).
  4. Create two sound objects in your Lab19 class for these new sound files and also play them.
  5. Write a Java expression in your Lab19 class that uses your min method to print the number of samples that is the smallest between the two sounds. Use the getNumSamples() method on a sound object and pass it to min.

Part II - An addSound method

In the previous lab you wrote a program that adds two sounds together and puts the resulting sound into a new sound object. In this part of the lab we're going to do this again but by adding a new method named addSound to the Sound class in the file Sound.java. This is similar to the way you added a decreaseVolume method to the Sound class.

  1. Add a method addSound() to the Sound class. This method takes one parameter, a sound, and adds it to the current sound (this). This method should use your min function to calculate the the number of samples to use (recall the last lab). Here is how your method might be called in your Lab19 class.
       // assuming s1 and s2 are two sound objects
       // to your wave files from part 1
       s1.addSound(s2);     // add s2 to s1
       s1.explore();        // should play the combined sound
    

© Ed Harcourt