CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Spring 2008

Mini-Lab: Appending, Mirroring, Adding, Blending Sounds

 


Introduction

In this mini-lab you will gain practice with using the techniques of mirroring, adding, and blending sounds.



Appending Sounds

  1. It is often useful to take two sounds and combine them into a single longer sound. This can be done easily by making use of JES's built in copySoundInto function. Fill in the following code skeleton to create function that combines two sounds end to end.
    def appendSound(sound1, sound2):
      # Make an empty sound long enough to contain both source sounds.
    
    
      # Copy sound1 into the beginning of the new sound using copySoundInto. 
    
    
      # Copy sound2 into the end of the new sound using copySoundInto.
    
    
      # Return the new sound. 
    

Mirroring Sounds

  1. Recipe #61 in the text is a function that mirrors the first half of a sound over the second half of a sound. We will write a function that will create a new sound that is twice the length of the original sound. The new sound will have the original sound in the first half, and the reverse of the original sound in the second half. This can be accomplished by creating a reversed copy of our original sound, and appending it to the original sound using the appendSound function. You may use the backwards function given below instead of writing your own. Fill in the missing sections of code below. You may use the backwards function given below instead of writing your own.
    # Mirror entire sound (new sound is original sound
    # followed by backwards original sound)
    def mirrorEntireSound(sound):
      # Reverse the original sound
    
      # Append sound and reverseSound
    
      # Return the mirrored sound
    
    
    
  2. You should copy the following version of backwards, which has been modified to take a sound as a parameter instead of a filename, into JES to use.
      def backwards(source):
        numSamples = getNumSamples(source) 
        target = makeEmptySoundFromNumSamples(numSamples)
        # Go backwards through source and write into target
        sourceIndex = getLength(source)
        for targetIndex in range(1, getLength(source)+1):
          sourceValue = getSampleValueAt(source, sourceIndex)
          setSampleValueAt(target, targetIndex, sourceValue)
          sourceIndex = sourceIndex - 1
    
        return target
    

Adding Sounds Together

Adding sounds together is very simple using a computer - just add the sample values at the same index numbers in each wave!
  1. Write a function that adds two sounds together. It has been started for you below:
    # Add sound1 to sound2
    # return the resulting sound
    def addSounds(sound1, sound2):
      # Determine which sound has minimum length
    
      # Create a new sound of that length
    
      for index in range(  ,         ):
        # Get the appropriate sample values from each sound
    
    
    
    
        # Add the sample values together
    
    
    
        # Set the sample value at this index in newSound to be the new value
    
    
    

Blending Two Sounds Together

The process of blending sounds together is very similar to the process of blending two pictures together. Recipe #62 in the text blends two very specific sounds. We will write a more general function to blend two sounds, as we did with pictures.
  1. The following is a skeleton for the function to blend two sounds. It takes two sounds and an overlap amount as parameters. The overlap amount is the number of samples that you want the two sounds to overlap.
    # Blend two sounds 
    def blendSounds(sound1, sound2, overlapAmt):
      # Get the lengths (# of samples) in each sound
      length1 =  
      length2 = 
    
      # Make an empty sound large enough to hold the new sound
      totalNumSamples = 
      newSound = 
    
      # Copy first sound up to the overlap section
      
    
    
    
    
      # Add the overlap section 
      for index in range(  ,                      ):
        value1 =  
        value2 = 
        newValue = 
        setSampleValueAt(                                               )
    
      # Copy the rest of sound2
    
    
    
    
     
      # return the new sound
    
    
    
    

Print your results

  1. Print the functions in this file and hand them in.