CS 111 - Program Design I

Lab 13

For this lab we will repeat some of the actions from Lab 11 where we added a background soundtrack to a foreground sound. However, we will add some of the fade-in and fade-out techniques to control the volume of the background sound. The code in Lect0424a.java shows how to fade-in a sound from no volume to full volume.

The final sound is to be 12 seconds longer than the original foreground sound. For the discussion below, the length of the original foreground sound is X seconds. So the overall length of the final sound will be X + 12 seconds. Only the background sound will be played during the first 6 seconds and the last 6 seconds.

  • During the first 3 seconds of the final sound, the background sound is to be played at full volume.
  • During the next 3 seconds of the final sound, the background sound is to fade-out from full volume down to 1/3 of its volume. (Note: don't fade the volume down to 0!)
  • During the next X seconds, the final sound should contain the foreground sound at its full volume and the background sound at 1/3 of its original volume.
  • During the next 3 seconds of the final sound, the background sound is to fade-in from 1/3 of its volume up to full volume.
  • During the last 3 second of the final sound, the background sound is to be played at full volume.
Remember, the background file should be repeated it if is not at least 12 seconds longer than the foreground file. To do this, if the current index from the foreground sound is a value bigger than the length of the background sound, you need to subtract the length of the background sound from the current index until the result produces a valid array index for background sound. The use of the modulus operator % may help perform this calculation.

Here are some good files for the background sound and foreground sound:

Combining Two Sounds Together

To combine two sound files, you simply just add the sample values together.

Remember, the only problem occurs if the resulting value goes above 32,767 or below -32,768. A simple solution is to use an if statement to check for this. Two simple if statements can check for this and correct the problem if it occurs. To correct this, just set the value to either 32,767 if the value was greater than 32,767 or set the value to -32768. if the value was less than -32,768.

So, What is my Volume multiplier at Index i?

The next big question is: "How do I calculate the volume multiplier for the background sound for the indexes during the 3 seconds before and the 3 seconds after the foreground sound plays?"

Well this is a simple geometry problem using a sloped line.

Assume we have to X,Y coordinate grid. We will have the X corrdinate be the index from the sound sample and the Y coordinate be the volume multiplier.

First assume we are using Sounds with 22050 samples per second (which all of the given sounds have). We need to determine the X coordinate for the fade-out from the 3 second point to the 6 second point. The X coordinates for the 3 second point is 66150 (3 * 22050). The X coordinate for the 6 second point is 132300 (6 * 22050).

Now we need to determine the Y coordinate for these to points. Remember the Y corrdinate is the volume multiplier. At the 3 second point, our volume multiplier is 1. At the 6 second point, our volume multiplier is 1/3 (0.33333333).

This gives us the X,Y coordinate of (66150, 1) for the 3 second point and the X,Y coordinate of (132300, 1/3) for the 6 second point.

Using point-slope form of a line

Now to calculate the volume multiplier for any index between 66150 and 132300, we can use the point-slope form of a line to find the answer.

  • y - y1 = m (x - x1)
  • where
    • m is the slope of the line
    • x1 and y1 are known points
So to solve for y (the volume multiplier) for an index X between 66150 and 132300, where x1 and y1 use the X,Y coordinate of (66150, 1), we use the Java statement of:

  • y = ( m * (x - x1 ) ) + y1
  • double y = ( m * (x - 66150.0) ) + 1;
The slope m should be a double value in Java to make sure we don't do integer division. This calculation would be calculated as follows using the two points of (66150, 1) and (132300, 1/3):
  • m = (change in y) / (change in x)
  • m = ( 1 - 1/3 ) / ( 66150 - 132300)
  • double m = ( 1.0 - 1./3.0 ) / (66150.0 - 132300.0 );
From the above, you should be able to figure out the code for the fade-out during seconds 3 to seconds 6.

For the fade-in code at toward the end of the sound, repeat the above calculations but the starting index of this part depends on the length of the foreground sound.

So, if we have a variable len1 that contains the number of samples in foreground sound, the begining index of the fade-in would be at index:

  • len1 + (6 * 22050)
  • len1 + 132300
The ending index of the fade-in would be at index:
  • len1 + (6 * 22050) + (3 * 22050)
  • len1 + (9 * 22050)
  • len1 + 198450
The volume multiplier at the beginning of the fade-in is 1/3, so the X,Y coordinate for this point is:
  • (len1 + 132300, 1/3)
The volume multiplier at the end of the fade-in is 1, so the X,Y coordinate for this point is:

  • (len1 + 198450, 1)

Lab Assignment 13

Due: Wednesday 4/30/2014 by 11:59 pm

Create a Java program that will:

  1. Be writen using good programming style which includes:
    • Good variable names
    • In-line commenting
    • Proper indentation of program statements
    • Use of blank lines to separate blocks of code.
    • Header block commenting for the program and each method written

      Your header block comment for the program must include the following:

      • Your Name,
      • Net-ID,
      • Course Name,
      • Assignment Name and
      • Day and time of your CS 101 lab section (i.e. Wednesday at 9:00)
      • A short description of the assignment.
Header block comments for each method must include the following:
      • A description of the purpose of the method
      • A listing of the name, type and purpose of every parameter
      • A description of the return value and its type

  1. Contain the main() method that will
    • Prompt the user for the "Foreground" sound file and make a sound object from this file.
    • Prompt the user for the "Background" sound file and make a sound object from this file.
    • Call a method combineSounds()
    • play (or explore) the resulting sound and
    • Prompt the user to save the resulting sound in a file

  1. Contain the method combineSounds() that will
    • Take two sound objects as parameters: the foreground sound and the background sound.
    • Determine the length of the resulting sound that will be 12 second longer that the length of the foreground sound.
    • Place the background sound into the resulting sound so that:
      • during the first 3 seconds, the background sound plays at full volume
      • during the next 3 seconds, the background sound fades-out from full volume down to 1/3 volume
      • during the next X seconds (where X is the length of the forground sound), the background sound plays at 1/3 volume
      • during the next 3 seconds, the background sound fades-in from 1/3 volume up to full volume.
      • during the last 3 seconds, the background sound plays at full volume.
    • Note: if the background sound file is shorter than needed, repeat the background sound as often as needed. Use the 3 second version of the Star Wars Theme in StarWars3.wav or the 3 second version of CantinaBand3.wav to test this out.
    • Add the foreground sound to the resulting sound starting at 6 seconds into the resulting sound. Be sure to check for and correct any "clipping" that may result from the addition being greater than 32,767 or less than -32,768.

  1. You are also to submit the Java file electronically via the assignment link for Lab 13 in BLackboard.
Here are some example sound files you can use with this assignment. -- Main.troy - 2014-04-28
Topic revision: r1 - 2014-04-28 - 16:25:33 - Main.troy
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF