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. The following function will create multiple echoes of a sound. Copy and paste this function into JES. The sound is the sound you want to be echoed, the delay is the number of samples from the beginning of the sound at which the echo should begin, and the num is the number of echoes you want to hear.
    
    def echoes(sound, delay, num):
      soundLength = getNumSamples(sound)
      newLength = soundLength + (delay*num)  # number of samples
      newSound = makeEmptySound(newLength, int(getSamplingRate(sound)))
    
      echoAmp = 1.0
      for echoCount in range(num+1):
        # 60% smaller each time after the original sound
        for soundIndex in range(soundLength):
          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 a function to allow us to shift by any amount.

# This function shifts the frequency of a sound by the specified factor
# A factor > 1 shortens the frequency, a factor < 1 lengthens frequency
def shift(sound, factor):
  # make the new sound
  newSound = makeEmptySound(getNumSamples(sound), int(getSamplingRate(sound)))
  
  # copy the sample values from original sound into new sound
  index = 0
  for newIndex in range(getNumSamples(newSound)):
    value = getSampleValueAt(sound, int(index))
    setSampleValueAt(newSound, newIndex, value)
    index = index + factor
    if (index >= getNumSamples(sound)):
      index = 0
      
  return newSound
    

  1. Copy and paste this function into JES. Test this function with several different factors. Make sure to use factors > 1 as well as factors < 1. When you use factors of 2 and 0.5, do you get the same results as you did with the doublFrequency and halfFrequency functions?

  2. Modify this function so that the new sound will be long enough to contain exactly one copy of the modified sound. (Hint - think about how you change the number of samples needed when the new sound is created.)