Changing sound frequencies
# Takes a file containing a sound and returns a sound with half the frequency (i.e. one octave lower).
# This works by copying each source sample twice onto the destination sound.
# We stop when we get to the end of the target sound, which is the same length as the source sound. (That means that really we are producing
# a lower frequency version of the first half of the source sound. If we wanted to reproduce all of the samples twice, our target sound would have
# to be twice as long.
def halveFrequency(filename):
source = makeSound(filename)
dest = makeSound(filename) # This is our canvas. We will overwrite it, so we could have also made an empty sound that was the correct length.
srcIndex = 1
for destIndex in range(1, getLength(dest)):
sample = getSampleValueAt(source, int(srcIndex) ) # We have to use int for reasons that become clearer below.
setSampleValueAt(dest, destIndex, sample)
srcIndex = srcIndex + 0.5 # Each time through the loop we only increment the source index by half. So it goes up half the speed of the destination index.
return dest
# Takes a file containing a sound and returns a sound with double the frequency (i.e. one octave higher).
# We do this by copying only every other sample from the source onto the target sound. This means that we run out of source samples when we are still only
# halfway through the target sound.
def doubleFrequency(filename):
source = makeSound(filename)
target = makeSound(filename)
targetIndex = 1
for sourceIndex in range(1, getLength(source), 2):
value = getSampleValueAt(source, sourceIndex)
setSampleValueAt( target, targetIndex, value)
targetIndex = targetIndex + 1
# Because our target sound was originally a copy of the source, the second half of the target is still the same as the second half of the source.
# So we need to make it silent. Try commenting out the following loop to hear what happens.
for index in range(targetIndex, getLength(target)-1):
setSampleValueAt(target, index, 0)
return target
Link to this Page