CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Spring 2008

Mini-Lab: Echoing and Shifting Sounds

 


Introduction

In this mini-lab you will gain practice with using the techniques of echoing and shifting sounds.



Echoing Sounds

  1. Copy the following modified version of Recipe #64 on p. 184 of your text. This function will create multiple echoes of a sound.
    
    def echoes(sound, delay, num):
      soundLength = getNumSamples(sound)
      newLength = soundLength + (delay*num)  # number of samples
      newSound = makeEmptySoundFromNumSamples(newLength)
    
      echoAmp = 1.0
      for echoCount in range(0, num+1):
        # 60% smaller each time after the original sound
        for soundIndex in range(1, soundLength+1):
          newSoundIndex = soundIndex + (delay*echoCount)
          value1 = getSampleValueAt(sound, soundIndex) * echoAmp
          value2 = getSampleValueAt(newSound, newSoundIndex)
          setSampleValueAt(newSound, newSoundIndex, value1+value2)
        echoAmp = echoAmp * .6
      return newSound
    
    

  2. Test this function by calling it with different sounds, different amounts for the delay, and different numbers of echoes.

  3. Save several echoed sounds as .wav files to be posted on your web page as part of Lab 7.

Shifting Frequencies

We have already seen how to shift the frequency of sounds by halving and doubling. We will now look at an algorithm to allow us to shift by any amount.

  1. Look over Recipe #68 from p.188 in your text. Modify this recipe so that it takes a sound instead of a filename as a parameter. When you do this, you will also need to create an empty sound to hold the result. How long should the new sound be? Rember that when we doubled the frequency of a sound, the duration of the sound was cut in half. When we halved the frequency of a sound, the duration was doubled. What do you think will happen if we increase the frequency by a factor of 3?

  2. Your function should return the shifted sound without playing it.

  3. Test your function with different sounds. Check that if you use a factor of 2 you get the same result as from using the doubleFrequency function, and if you use the factor of 0.5 you get the same result as from halfFrequency.

  4. Save some frequency shifted sounds as .wav files to be posted on your web page as part of Lab 7.