CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Spring 2008

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. 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.

Print your results

  1. Print the completed functions and hand them in.