CPSC 120 Principles of Computer Science
Using Multimedia Design
Fall 2008

Lab 9: Modifying Samples in a Range

Introduction

In this lab, we will write more computer programs for manipulating sounds. As with the previous lab, you will need ear phones in order to hear the sounds that you are transforming or have transformed. IMPORTANT: USE CAUTION WHEN TESTING OUT A MODIFIED SOUND. PLAY THE SOUND WITH THE EAR PHONES A FEW INCHES AWAY FROM YOUR EARS TO MAKE SURE THAT IT IS NOT TOO LOUD. If the sound is at a reasonable volume then you can put the ear phones in your ear and play the sound again.

In this lab, we will use a new type of for loop that will allow us to modify samples within a particular region of the sound. It is analogous to the for x in range(1, getLength(sound)) loop we used with pictures. Rather than looping over a range of x values (or y values), we will loop over a range of indices in the sound. For example, the following loop will double the amplitude for all sampled values in a sound:

for index in range(1, getLength(sound)):
  v = getSampleValueAt(sound, index)
  setSampleValueAt(sound, index, 2*v)
The loop defines a variable index, which takes on the values 1, 2, 3, ..., length-1 where length is the number of samples in the sound. For each value of index the commands within the loop are repeated. In this case, the first command gets the next sampled value using getSampleValueAt. The second command sets the sampled value to twice its original value using setSampleValueAt (see the help in kJES for more details on how to use these built-in functions).

Of course, we can modify the range values to focus on one particular region of the sound. For example, the following loop doubles the amplitude for only the first half of the sound:

for index in range(1, getLength(sound)/2):
  v = getSampleValueAt(sound, index)
  setSampleValueAt(sound, index, 2*v)

As you can see this loop gives us a lot more flexibility than the loop (for samp in getSamples(sound)) from the last lab. In the exercises, you will need to use this loop in various ways.

Exercises

Here are the exercises for this week's lab. There are 4 required exercises. They are due in lab next Friday (at the start of lab). You should write the functions for the exercises below in a single Python file called exercises.py within the lab09 folder within your cs120 folder on your desktop.

Make sure you comment all of your code as described in lab 3. This will be an important part of your grade. Important: make sure you provide a comment describing how the function should be called so that I can execute the function as you do. Also, make sure you put all sound files that you use in this lab in your lab09 folder so that I have access to them (you will also need to copy some of them to your wwww folder so that they can be put on your web page).

  1. Write a function called splice for splicing one sound into another. Use the function to splice three short sounds into one larger sound. In exercise 4, you will put this sound on your web page.

    For example, I took someone reading part of Lincoln's Gettysburg address and spliced in three other sounds: someone saying rabbit, pig, and horse. Here is my sound:

    Gettysburg address spliced
  2. In class we wrote a function for doubling the frequency of a sound. In this exercise, you will write a function called changeFreq that changes the frequency by a user-specified factor. The function will have two parameters: the sound and the factor to change the frequency by. Rather than just doubling the frequency, your function must use this second parameter when changing the frequency. If the parameter is set to 2 then your function should double the frequency, if the parameter is set to .5 then your function should halve the frequency, and so forth. In addition, your function should not modify the original sound parameter, but instead should duplicate it, and modify the duplicated sound. The function should return the modified sound.

    Note: the modified sound should have the same content as the original, it will just be longer or shorter based on the amount of change in the frequency. In other words, if the sound is someone speaking "this is a test" (which was played in class) and the frequency change amount is 2, then the new sound should be half as long but at a higher pitch. If we play the new sound, we should still hear "this is a test", just at a higher pitch. Likewise, if the frequency change amount is .5, then the new sound should be twice as long but at a lower pitch.

    To create the new sound use the built-in function makeEmptySoundFromNumSamples, which was shown in class. This function takes as input the new number of samples. So for example, you could do the following to create a new sound with 1000 samples:

    newLength = 1000
    newSound = makeEmptySoundFromNumSamples(newLength)
    

    However, in our case, we cannot simply set newLength to 1000. Its value must be based on the number of samples in the original sound and the frequency change. There is a simple formula you can use here. Hint: the number of samples in the new sound is equal to the number of samples in the original sound times 1.0 over the frequency change (note: use 1.0 not 1 to ensure the result is a real number not an integer number). Hint 2: the final value passed to makeEmptySoundFromNumSamples must be an integer so you will need to use the int function to convert a potentially real value to an integer value.

    Use your function to double the frequency of some sound and to halve the frequency of that sound. In exercise 4, you will put both of these transformations on your web page.

  3. Write a function for creating an echo in a sound of spoken text. Unlike the function shown in class, your function should include a parameter (in addition to the sound parameter) for specifying the offset between the original sampled values and the echoed sampled values (in class we used a fixed value of 10,000).

    Use your function to create an echo in a sound with some spoken text. Use a different sound then the "this is a test" sound, which was used in class. In exercise 4, you will put this sound on your web page.

  4. Put the sounds that you created in the earlier exercises on your cs120 web page (cs120.html). These should include the spliced sound from exercise 1, the double frequency and half frequency modified sounds from exercise 2, and the echoed sound from exercise 3. To save each of them, use the function writeSoundTo (see the kJES help or lab 8 for information on how to use writeSoundTo). You will add them to the table in cs120.html within your www folder (not your lab02 folder!).

    To add them to your web page you will create a link within a table cell that refers to the corresponding sound file. Most browsers will know how to play the wav file if the user clicks the link (if not the user can download the file and then play it using an application that does understand wav files -- like kJES). For example, here is how a link tag might look in your web page:

    <a HREF="increase-freq.wav">Increased Frequency</a>
    

    This would allow the viewer to play a wav file called increase-freq.wav (that wav file would need to be copied to www) by clicking on the link.

    Here is my version of cs120.html. Your page should look similar.


Good luck and have fun!


Valid HTML 4.01!