Prelab 8: Sounds & Debugging

1. Review background reading

2. Practice tracing through functions

  1. From Georgia Tech's CS 1315: Take a look at the program below, then answer the questions:
    def spliceWeird(): file = "thisisatest.wav" # Assume media path is set properly source = makeSound(file) target = makeSound(file) # This will be the newly spliced sound targetIndex=1 # targetIndex starts at the beginning # Loop A for sourceIndex in range( 40327, 55770): # Where the word "Test" is in the sound setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex)) targetIndex = targetIndex + 1 # Loop B for sourceIndex in range( 40327, 55770,2): # Where the word "Test" is in the sound setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex)) targetIndex = targetIndex + 1 # Loop C for sourceIndex in range( 55770,40327,-2): # Where the word "Test" is in the sound setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex)) targetIndex = targetIndex + 1 # Loop D for sourceIndex in range( 40327, 55770,2): # Where the word "Test" is in the sound setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex)) targetIndex = targetIndex + 1 # Loop E for sourceIndex in range( 55770,40327,-2): # Where the word "Test" is in the sound setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex)) targetIndex = targetIndex + 1 # Loop F for sourceIndex in range( 55770,40327,-1): # Where the word "Test" is in the sound setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex)) targetIndex = targetIndex + 1 play(target) #Let's hear and return the result return target

    1. What do each of the loops in the above program do? What does the resulting sound sound like?
      
      
      
      
      
      
      
      
      
      
    2. Is there anything left in the sound when Loop F finishes? What is the value of targetIndex when the function \emph{spliceWeird} ends?
      
      
      
      
      
      
      
      
      
      
  2. Consider the following function: def bar(sound): s = "" for index in range (1, 100): val = getSampleValueAt(sound, index) if val > 0: s = s + "+" elif val == 0: s = s + "0" else: s = s + "-" print s
    1. The function bar prints out a string, based on samples in the sound. What does the string represent?
      
      
      
      
      
      
    2. How many characters will this function output?
      
      
  3. The following function, mirrorSound, mirrors a sound from front to back. That is, given a sound like "Jeffrey", mirrorSound will change it to "Jeffej."

    def mirrorSound(sound): mid = getLength(sound)/2 for index in range(1, mid): value = getSampleValueAt(sound, index) setSampleValueAt(sound, getLength(sound) - index + 1, value)

    Describe how to change mirrorSound to mirror the sound from back to front.


Comments?
Last modified: Sat Mar 8 19:08:34 EST 2008