LAB 26 – Modifying Samples Using Ranges (part 1 – Manipulating Sections of Sounds)

Lab Exercises

Topics

n      To manipulate parts of a sound differently

n      To create a sound clip

n      To return a value from a method

n      To use ranges in iteration

 

Exercises

 

9.1 Manipulating Different Sections of a Sound Differently

 

i) Type in the code for a main method that asks the user for a sound filename and sends the Sound to the increaseAndDecrease 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 increase the volume of the first half of a sound and decrease the second half

   * @param source - source sound

   */

public static void increaseAndDecrease(Sound source)

{

    int value = 0;

    int half = source.getLength() / 2;

 

    // loop through the first half of the sound

    for (int i = 1; i < half; i++)

    {

      value = source.getSampleValueAt(i);               //get current value

       source.setSampleValueAt(i, value * 2);                       // double the value

    }

 

    // loop through the second half of the sound

    for (int i = half; i < source.getLength(); i++)

    {

      value = source.getSampleValueAt(i);                           //get current value

       source.setSampleValueAt(i, (int) (value * 0.5));                     // double the value

    }

}

iii) Test your program with different .wav files. Use sourceSound.play(); and sourceSound.explore(); to confirm that the volume is changed.

 

9.2 Create a Sound Clip

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

 

  /* method to create a new sound by copying part of the current sound to a new sound

   *

   * @param source - source sound

   * @param start – index to start the copy at (inclusive)

   *  @param end – index to stop the copy at (inclusive)

   *  @return a new sound with just the samples from start to end

   */

public static Sound clip(Sound source, int start, int end)

{

    // calculate the number of samples in the clip

    int numSamples = end - start + 1; 

    Sound target = new Sound(numSamples);

    int value = 0;

    int targetIndex = 0; 

 

// copy from start to end

    for (int i = start; i <= end; i++, targetIndex++)

   {

      value = source.getSampleValueAt(i);

      target.setSampleValueAt(targetIndex, value);

    }

   

    return target;

}

 

v) Modify your main method to call clip. Test your program with different .wav files and different start and end values. Use sourceSound.play(); and sourceSound.explore(); to confirm that the volume is changed.

 

 

QUESTIONS:

  1. Revise the increaseAndDecrease method to accept the sound AND a percentage. Use the percentage to adjust how far into the sound to go before dropping the volume

 

Submit the .java file with the revised increaseAndDecrease method and the clip method to the questions through the DropBox in WebCT.