Mini-Lab: Blending Sounds

 


Introduction

In this mini-lab you will gain practice with the techniques of blending sounds and working with python lists.



Blending Sounds Together

Blending sounds together is very simple using a computer - just add the sample values at the same index numbers in each sound wave!
  1. Write a function that blends two sounds together. It has been started for you below:
    # Blend sound1 with sound2
    # return the resulting sound
    def blendSounds(sound1, sound2):
      if getSamplingRate(sound1) != getSamplingRate(sound2):
        print "Error! Sounds must have the same sampling rate."
        return
    
      # 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
    
    
    
  2. Test your function with several different sounds.

Blending Without Clipping

The blendSounds function you wrote above will probably work fine most of the time, but if two loud sounds are added together, the resulting sound could suffer from clipping. One way around this is to store the summed sample values into a python list of numbers rather than directly into the sound. (A python list can store much larger numbers than can be stored in sound samples.) We can then check the summed values to see if any are above the maximum allowed for sounds: 32767. If there are, the list of numbers can be normalized, and the resulting list can be converted back into a sound. In this case, we are using normalization not to create the loudest sound possible, but to ensure that the sound is no louder than it should be.
  1. Complete the blendSoundsNoClipping function below by following the comments that ask you to fill in or complete some lines of code. Two helper functions are provided: listToSound which creates a sound object from a list of numbers, and normalizeList which rescales a list of numbers so that it has the desired maximum value.
    #-----------------------------
    #Takes a python list of numbers, and a sampling rate
    #and returns a sound object. 
    #-----------------------------
    def listToSound(list, samplingRate):
      newSound = makeEmptySound(len(list), int(samplingRate))
      for pos in range(getNumSamples(newSound)):
        setSampleValueAt(newSound, pos, list[pos])
      return newSound
    
    #-----------------------------
    #Normalizes a list of numbers so that the maximum value
    #in the list will be newMax. 
    #THIS FUNCTION DOES NOT RETURN ANYTHING. IT CHANGES THE 
    #LIST THAT IS PASSED IN AS A PARAMETER. 
    #-----------------------------
    def normalizeList(list, newMax):
      oldMax = max(list)
      for pos in range(len(list)):
        list[pos] = list[pos] * newMax/oldMax
    
    
    #-----------------------------
    #Blend two sounds together, rescaling the resulting sound if necessary 
    #to avoid clipping. 
    #-----------------------------
    def blendSoundsNoClipping(sound1, sound2):
      if getSamplingRate(sound1) != getSamplingRate(sound2):
        print "Error! Sounds must have the same sampling rate."
        return
    
      minLength = min(getNumSamples(sound1), getNumSamples(sound2))
      newSoundList = [0.0] * minLength 
    
      #Now add the sample values of the two sounds together, 
      #placing the result in newSoundList
      for pos in range(minLength):
         #ADD CODE BELOW TO SUM UP THE CORRESPONDING SAMPLE VALUES
         newSoundList[pos] = ??????????????????
    
      maxVal = max(newSoundList) #calculate the maximum sample value
    
      #ADD CODE TO COMPLETE THE FOLLOWING THREE STEPS...
      #If maxVal> 32767, there has been clipping, normalize the sound.
      #Use listToSound to create the final sound from newSoundList.
      #Return the resulting sound.
    
    
    
  2. Test your function with several different sounds.

  3. Try blending together two loud sounds using both blendSounds and blendSoundsNoClipping; can you hear a difference in the result?

Submit your results

  1. Submit the file you created in this mini-lab via Kit.
  2. Submit two original sounds and the resulting blended sounds.