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.
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.
.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?
.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 = makeEmptySoundFromNumSamples(numSamples * 2)
srcIndex = 1
for targetIndex in range(1, numSamples * 2):
val = getSampleValueAt(sound, int(srcIndex))
setSampleValueAt(newSound, targetIndex, val)
srcIndex = srcIndex + .5
return newSound
def doubleFrequency(sound):
numSamples = getNumSamples(sound)
#Uncomment and finish the following line
#newSound = makeEmptySoundFromNumSamples(HOW MANY SAMPLES IN NEW SOUND?)
destIndex = 1
#for sourceIndex in range(#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
doubleFrequency function:
writeSoundTo function. (It works similarly to the
writePictureTo function.)
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.
makeEmptySound 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:
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.>>sound = makeSound(pickAFile()) >>newSound = makeEmptySound(5) >>copySoundInto(sound, newSound, 1) >>play(sound) >>play(newSound)
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.
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.
.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.)