LAB 29 – Making Sounds by Combining Pieces (part 1 – Blending and Echoing Sounds)

Lab Exercises

Topics

n      To blend sounds so that one fades into another

n      To create echoes

n      To identify algorithms that cross media boundaries

 

Exercises

10.2 Blending Sound

 

i) Type in the code for a main method that declares three Sound objects, asks the user for two sound filenames and sends the two Sounds to the blend method, returning a combined Sound and then plays (and/or explores) the combined Sound.

 

Type in the following code in the Program window (changing the comments):

/*  Program that combines and plays sounds

  *

 * Sound and SoundSample classes are defined in bookClasses developed

 * at Georgia Tech by Mark Guzdial / Barbara Ericson

 *

 * @author Wayne Summers

 * @date   Oct. 29, 2007

 */

public class PlaySounds

{

     public static void main (String[] args)

    {

         System.out.println(“Find the first source file”);

         Sound sourceSound = new Sound (FileChooser.pickAFile()); // pops window for wav file

         System.out.println(“Find the second source file”);

         Sound sourceSound2 = new Sound (FileChooser.pickAFile()); // pops window for wav file

         Sound blendedSound;

 

             // calls to methods that manipulate sounds go here

             

         blendedSound.play(); 

    }

}

 

ii) Type in the following code in the Program window below the main method and before the last }:

 

   /* method to blend two sounds

   * @param sound1 – source sound

   * @param sound2 – source sound

   * @return blend – new sound blending sound1 and sound2

   */

public static Sound blendSounds(Sound sound1, Sound sound2)

{

   int length1 = sound1.getLength();

   int length2 = sound2.getLength();

  

   Sound newSound = new Sound(length1+length2);

    int value = 0;

    int newIndex = 0;

    // copy the first half of sound1 into target

    for (int index = 0; index < length1/2-1; index++, newIndex++)

      newSound.setSampleValueAt(newIndex,  sound1.getSampleValueAt(index));

// copy the 2nd half of sound1 and blend with the first half of sound2

    for (int index2 = 0, index = length1/2; (index < length1 && index2 < length2-1); index++, index2++, newIndex++)

    {

      value = (int) ((sound1.getSampleValueAt(index) *  0.5) + (sound2.getSampleValueAt(index2) * 0.5));

      newSound.setSampleValueAt(newIndex,value);

    }

 

    // copy the last half of sound2 into the target

    for (int index2=length2/2; index2 < length2; index2++, newIndex++)

      newSound.setSampleValueAt(newIndex, sound2.getSampleValueAt(index2));

   

    return newSound;

  }

iii) Test your program with different .wav files. Use blendedSound.play(); and blendedSound.explore(); to confirm that the reverse worked.

 

iv) Modify your blendSounds method to accept another parameter for the percentage blend (for example if percentage is 0.25, then ¼ of the first sound will used, followed by ¾ of the first and second sounds, followed by the last ¼ of the second sound. Test your program with different .wav files.

 

QUESTIONS:

 

Submit the .java file with revised blendSounds method through the DropBox in WebCT.