Lab 6: Splicing Sounds



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.


Splicing sounds is similar to copying and cropping pictures. You may want to have a copy of your copyInto function for pictures available for reference (or see Lab 3).
  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, 0)
    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. 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.

Reversing and Splicing

  1. In the Sound Manipulations reading, there is a function to reverse a sound. That function is repeated here. Copy this code and test it with several different sounds. Does it work like you think it should?
        def reverse(sound):
            # create a new empty sound with same # of samples and
            # sampling rate as the original sound
            newSound = makeEmptySound(getNumSamples(sound),int(getSamplingRate(sound)))
            # set up index to start at end of new sound
            newIndex = getNumSamples(newSound) - 1
            # loop through original sound, setting
            # values in new sound
            for index in range(getNumSamples(sound)):
                value = getSampleValueAt(sound, index)
                setSampleValueAt(newSound, newIndex, value)
                newIndex = newIndex - 1
            # return the new sound
            return newSound

  2. Write a new function, forwardAndReverse, that creates a new sound. The first half of this sound is an original sound. The second half of this sound is the original sound in reverse. The function should take a sound as a parameter, then create a new, empty sound, with the appropriate length and sampling rate. It will need to call on the reverse function to get the reversed version of the original sound, and then copy the two sounds into the new, empty sound at the appropriate locations.

  3. Test your function with several different sounds to be sure it is working correctly.

  4. Save one of your sounds created by the forwardAndReverse function.

Submit your results

  1. Submit the file you created in this lab via .

Post your results

  1. Create a new Lab 6 web page to include links for the new sounds that you created. You should include links to all original sounds you used as well as the new sounds. (You should have a new sound created from the splicing section of the lab as well as from the reversing and splicing section.)

  2. Copy your web page and all the sound (.wav) files that should be on it to your personal web space on
  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.