The objectives of this mini-lab are to become familiar with using loops, ranges, and sample values to manipulate sounds by changing their pitch.
.wav
files and verify that it does what is expected. Use the MediaTools
Sound Tool to look at the sound waves.
# This function halves the frequency of the original sound # The new sound is twice as long, and sounds slower and deeper def halfFrequency(sound): # make a new sound with twice as many samples and same # sampling rate as original sound numSamples = getNumSamples(sound) samplingRate = int(getSamplingRate(sound)) newSound = makeEmptySound(numSamples * 2, samplingRate) # Use a loop to get values from original sound, setting values in new sound index = 0 for newIndex in range(numSamples * 2): val = getSampleValueAt(sound, int(index)) setSampleValueAt(newSound, newIndex, val) index = index + .5 # Allows us to use each original sample twice # return the new sound return newSound
# This function doubles the frequency of the original sound # The new sound is half as long, and sounds quicker and higher-pitched def doubleFrequency(sound): numSamples = getNumSamples(sound) samplingRate = int(getSamplingRate(sound)) #Uncomment and finish the following line #newSound = makeEmptySound(HOW MANY SAMPLES IN NEW SOUND? WHAT IS ITS SAMPLING RATE?) index = 0 #for newIndex in range(#ADD APPROPRIATE CODE FOR THE RANGE HERE): # 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 index 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.)
halfFrequency
and doubleFrequency
on Kit.