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.
copyInto
function
for
pictures available for reference (or see Lab 3).
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.
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:
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 = makeEmptySoundBySeconds(5) >>newSound = copySoundInto(sound, newSound, 0) >>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.
(Note: If you find yourself copying and pasting a lot
of code from your copySoundInto
function, STOP. You should
be calling that function, not copying it. Your new function
should only end up with about 3-5 lines of code, with no loops.)
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
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.
forwardAndReverse
function.
forwardAndReverse
function.