CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Winter 2009

Lab 6: Simple Sound Manipulation

 


Introduction

The objectives of this lab are to become familiar with using loops, ranges, and sample values to manipulate sounds, and to learn how to splice sounds together.



Lab Entrance Assignment

    Type in Recipes 53 (changeVolume), 54 (normalize), 55 (onlymaximize), 57 (increaseAndDecrease), and 60 (backwards). You will need to use the getSampleValue function in place of the getSample function. For instance, any line of code that looks like value = getSample(sample) in the book should be rewritten as value = getSampleValue(sample). Save these recipes in a file called Lab6.py. Remember to add appropriate comments at the beginning of the file with your name, today's date, and program description.

Part 1: Experiment with sound manipulations

  1. Run the recipes you saved from the lab entrance assignment with several different .wav files. In addition to the .wav files in the MediaSources folder, there are also words in .wav files in the Speech folder in MediaSources.
    Analysis Questions: Do the functions do what you expect them to? How do you know? What can you do to check?

  2. Copy the following function to halve the frequency of a sound. (This was discussed in class.) Run it with several .wav files and verify that it does what is expected. Use the MediaTools Sound Tool to look at the sound waves.
    
    def halfFrequency(sound):
      numSamples = getNumSamples(sound)
      newSound = makeEmptySound(numSamples * 2)
      srcIndex = 1
      for targetIndex in range1(1, numSamples * 2):
        val = getSampleValueAt(sound, int(srcIndex))
        setSampleValueAt(newSound, targetIndex, val)
        srcIndex = srcIndex + .5
      return newSound
    
    

  3. Copy the following code skeleton to double the frequency of a sound. Follow the comments in the code to write the sections that are missing.
    
    
    def doubleFrequency(sound):
      numSamples = getNumSamples(sound)
      #Uncomment and finish the following line
      #newSound = makeEmptySound(HOW MANY SAMPLES IN NEW SOUND?)  
    
      destIndex = 1
      #for sourceIndex in range1(#ADD CODE HERE TO USE EVERY OTHER SAMPLE IN SOUND):
        # Get the sample value from the original sound... 
        # ADD A LINE OF CODE HERE
        # Set the sample value for the new sound...
        # ADD A LINE OF CODE HERE
        # Increment destIndex appropriately...
        # ADD A LINE OF CODE HERE
      # ADD CODE HERE TO RETURN THE NEW SOUND
    
    
    

  4. Edit the comments in the doubleFrequency function:

  5. Save some of the new sounds you created, using the writeSoundTo function. (It works similarly to the writePictureTo function.)

Part 2: Splicing

Splicing sounds is similar to copying and cropping pictures. You may want to have a copy of your functions for copying and cropping pictures available for reference.
  1. When we copied a picture into a new canvas, we passed the picture, the canvas, and the starting x- and y- coordinates of where to start copying on the canvas as parameters to the copyInto function. Next we looked at how much space was available to copy into, and checked whether or not that was enough room for the entire picture. We then copied as much of the original picture as we could onto the canvas. We can do something similar to copy a sound onto a new "tape", by passing in the sound, the tape, and the index of where to start copying onto the tape as parameters. We should determine how much space is left on the tape, and copy only as much of the sound as will fit onto the tape.

    Write a function called copySoundInto which takes a sound, a tape, and a starting index as parameters. It should compute the space left on the tape beginning from the starting index, and then determine the minimum of the length of the sound and the space on the tape. It should then copy as much of the original sound as it can onto the tape. As with our copyInto function, we will not modify either of the sounds that are passed in as parameters. Instead we will make a duplicate of the "tape", copy our sound into the duplicate, and return modified duplicate at the end of the function.

  2. You can make empty sounds by using the makeEmptySoundBySeconds function, which takes a time in seconds as a parameter. So, to execute your copySoundInto function, you could execute the following sequence of commands in the command area:
    >>sound = makeSound(pickAFile())
    >>newSound = makeEmptySoundBySeconds(5)
    >>newSound = copySoundInto(sound, newSound, 1)
    >>play(sound)
    >>play(newSound)
    
    Try copying different sounds onto different length empty sounds at different positions, and see what happens. Try copying multiple sounds onto the same tape at different positions.

  3. (If Time Permits...) Write a new version of your copySoundInto function, copySoundIntoAtSec, that takes as a parameter the number of seconds into the tape to start copying, instead of the number of samples. You can accomplish this by converting the desired number of seconds to the corresponding number of samples, and then making a call to copySoundInto. The function getSamplingRate will be helpful for making the conversion. Keep in mind that getSamplingRate returns a real number, not an integer. That means that any value derived from the sampling rate will need to be converted to an integer using the int function before it can be used as an index value.

Print your results

  1. Print the functions you created in this lab and hand them in.

Post your results

  1. Create a new Lab 6 web page to include links for your new sound files created by the changeVolume, increaseAndDecrease, backwards, halfFrequency, and doubleFrequency functions. You should always include the original sound, and then the modified sound. Since we do not have pictures to look at, it is even more important now that you add some text to describe what the viewer will be listening to. You might consider using a table to organize your sounds.

  2. Copy your web page and all the sound (.wav) files that should be on it to your personal web space on kzoo.edu. (You may want to review the instructions in Lab 1 if you've forgotten how to do this.)
  3. Include a link to your new Lab 6 web page from your course page. Bring up your course page in a web browser and test your link.