LAB 27 – Modifying Samples Using Ranges (part 2 – Splicing Sounds)

Lab Exercises

Topics

n      To splice sounds together to make sound compositions

n      To change more than one variable in a loop

n      To identify algorithms that cross media boundaries

 

Exercises

9.3 Splicing Sounds

 

i) Type in the code for a main method that asks the user for a sound filename and sends the Sound to the splice method and then plays (and/or explores) the revised sound.

 

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

 

  /* method to splice two sounds together with some silence between them

   * @param target - target sound

   */

public static void splice (Sound target)

{

    Sound sound1 = new Sound(FileChooser. pickAFile());

    Sound sound2 = new Sound(FileChooser. pickAFile());

    int targetIndex = 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 < sound1.getLength();  i++, targetIndex++)

    {

              value = sound1.getSampleValueAt(i);

              target.setSampleValueAt(targetIndex,value);      

    }

// create silence between words by setting values to 0

    for (int i = 0; i < (int) (target.getSamplingRate() * 0.1); i++, targetIndex++) {

      target.setSampleValueAt(targetIndex,0);

    }

   

    // copy all of sound 2 into the current sound (target)

    for (int i = 0; i < sound2.getLength(); i++, targetIndex++)

    {

      value = sound2.getSampleValueAt(i);

      target.setSampleValueAt(targetIndex,value);

    }

  }

 

iii) Test your program with the file sec3silence.wav. Use targetSound.play(); and targetSound.explore(); to confirm that the splice worked. Try picking different files but make sure that the total time is less than 3 seconds.

 

iv) Type in the following code for the two methods (splice and splicePreamble) in the Program window below the main method and before the last }:

 

  /* method to splice part of a passed sound into the target sound at the given start index

   * @param target - target sound

   * @param source - source sound to copy

   * @param sourceStart - starting index to copy from the source (inclusive)

   * @param sourceStop - ending index (exclusive)

   * @param targetStart - starting index to copy to the target

   */

   public static void splice (Sound target, Sound source, int sourceStart,

                                                int sourceStop, int targetStart)

   {

       int value = 0;

       // copy part of source into the current sound (target)

       for (int sourceIndex = sourceStart, targetIndex = targetStart;

            sourceIndex < sourceStop && targetIndex < target.getLength();

            sourceIndex++, targetIndex++)

       {

           value = source.getSampleValueAt(sourceIndex);

           target.setSampleValueAt(targetIndex,value);      

       }

   }

   

   /**

    * method to splice the preamble into the source so that it says We the United people

    * of the United States

    * @param target - target sound

    */

   public static void splicePreamble (Sound target)

   {

       Sound preamble = new Sound(FileChooser.getMediaPath("preamble10.wav"));

      

       // first splice the "we the" into the target

       splice (target, preamble, 0, 17407, 0);                          

       // next splice the "united" into the target

       splice (target, preamble, 33414, 40052, 17407);                          

       // now splice the "people of the United States" into the target

       splice (target, preamble, 17408, 55510, 24045);

   }

 

v) Modify your main method to call splicePreamble. Test your program with the file sec3silence.wav. Use targetSound.play(); and targetSound.explore(); to confirm that the splice worked.

 

QUESTIONS:

  1. Revise the splice method to accept the source and target sounds with start and end values AND a volume. Use the volume to adjust the volume up or down. Modify splicePreamble so that the volume for UNITED is louder.

 

Submit the .java file with the revised splice and splicePreamble methods through the DropBox in WebCT.