CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Winter 2009

Mini-Lab: Fade

 


Introduction

The objective of this mini-lab is to practice working with sound in JES.


Changing Volume

  1. The following function allows us to increase or decrease the volume of a sound by any factor. Copy it into JES, add appropriate comments, and try it out.
    def changeVolume(sound, factor):
      newSound = duplicateSound(sound)
      for s in getSamples(newSound):
        val = getSampleValue(s)
        setSampleValue(s, val * factor)
      return newSound
    
  2. In the changeVolume function, every sample value is multiplied by the same factor. There is nothing to stop us from changing the volume of different parts of the sound by different amounts. In particular, we will write a function that leaves the volume at the beginning of the sound unchanged, and then slowly reduces the volume over time. Your function should take two parameters: the sound, and the largest factor that the volume should be reduced by. Your function will return a new sound that fades out by the desired factor. For example, after the command:
    >>> faded = fade(sound, 8.0)
    
    The variable faded should contain a sound that is unchanged at the beginning, and quieter by a factor of 8.0 at the end.

    In order to accomplish this, you can create a variable, curFactor, that represents the amount that the current sample will be reduced by, and increase the value of that variable after each sample is processed. In other words, instead of the line:

     setSampleValue(s, val * factor)
    
    from the function above, your new function will have a line like the following:
     setSampleValue(s, val / curFactor)
    
    Note: When calling this function, the value used for the largest factor should be a floating point number. Otherwise, Python will use integer division, and your sound will not change.
    Design Questions: What should the initial value of curFactor be? (What amount can you divide a sample value by without changing the volume at all?) By how much should curFactor be increased after every sample? Keep in mind that this will depend on the total number of samples in the sound, which can be obtained using the getNumSamples function.
  3. Analysis Question: We've described the fade and changeVolume function as decreasing the "volume" of a sound by some factor. However, what we are really doing is decreasing the amplitude of the sound by some factor. What is the difference? Hint: try using changeVolume to halve the amplitude of a sound, then halve the amplitude of the resulting sound, then halve the amplitude of that, and so on. Does the loudness seem to go down by half each time?
  4. If you have time, create a new function called fadeUp that slowly increases the volume of a sound by the desired amount. In other words, after this expression is executed:
    >>> faded = fadeUp(sound, 8.0)
    
    The variable faded should contain a sound that is unchanged at the beginning, and louder by a factor of 8.0 at the end. Hint: Depending on how you wrote fadeDown , there may be a way to write fadeUp with just a single call to fadeDown.
  5. If you have even more time, write a function called fadeIn that makes the initial portion of a sound quieter by some factor, and then gradually increases the volume until it reaches the original level. Once again, there are two ways to accomplish this. You could use a for loop to modify the individual samples, OR you could make use of two of the functions above.

Print your results

  1. Print the completed functions and hand them in.