In this mini-lab you will gain practice with the techniques of mirroring and adding sounds and working with python lists.
copySoundInto function. Fill in the following code
skeleton to create function that combines two sounds end to end.
def appendSound(sound1, sound2): # Make an empty sound long enough to contain both source sounds. # Copy sound1 into the beginning of the new sound using copySoundInto. # Copy sound2 into the end of the new sound using copySoundInto. # Return the new sound.
appendSound function. You may use the backwards
function given below instead of writing your own.
Fill in the missing sections of code below. You may use the backwards
function given below instead of writing your own.
# Mirror entire sound (new sound is original sound # followed by backwards original sound) def mirrorEntireSound(sound): # Reverse the original sound # Append sound and reverseSound # Return the mirrored sound
backwards, which has been modified to take a sound as a
parameter instead of a filename, into JES to use.
def backwards(source):
numSamples = getNumSamples(source)
target = makeEmptySound(numSamples, int(getSamplingRate(source)))
# Go backwards through source and write into target
sourceIndex = getLength(source)
for targetIndex in range1(1,getLength(source)):
sourceValue = getSampleValueAt(source, sourceIndex)
setSampleValueAt(target, targetIndex, sourceValue)
sourceIndex = sourceIndex - 1
return target
# Add sound1 to sound2
# return the resulting sound
def addSounds(sound1, sound2):
if getSamplingRate(sound1) != getSamplingRate(sound2):
print "Error! Sounds must have the same sampling rate."
return
# Determine which sound has minimum length
# Create a new sound of that length
for index in range( , ):
# Get the appropriate sample values from each sound
# Add the sample values together
# Set the sample value at this index in newSound to be the new value
addSounds function you wrote above will probably work
fine most of the time, but if two loud sounds are added together, the
resulting sound could suffer from clipping. One way around this is to
store the summed sample values into a python list of numbers rather
than directly into the sound. (A python list can store much larger
numbers than can be stored in sound samples.) We can then check the
summed values to see if any are above the maximum allowed for sounds:
32767. If there are, the list of numbers can be normalized, and the
resulting list can be converted back into a sound. In this case, we
are using normalization not to create the loudest sound possible, but
to ensure that the sound is no louder than it should be.
listToSound which creates a sound
object from a list of numbers, and normalizeList which
rescales a list of numbers so that it has the desired maximum value.
#-----------------------------
#Takes a python list of numbers, and a sampling rate
#and returns a sound object.
#-----------------------------
def listToSound(list, samplingRate):
newSound = makeEmptySound(len(list), int(samplingRate))
for pos in range1(getNumSamples(newSound)):
setSampleValueAt(newSound, pos, list[pos-1])
return newSound
#-----------------------------
#Normalizes a list of numbers so that the maximum value
#in the list will be newMax.
#THIS FUNCTION DOES NOT RETURN ANYTHING. IT CHANGES THE
#LIST THAT IS PASSED IN AS A PARAMETER.
#-----------------------------
def normalizeList(list, newMax):
oldMax = max(list)
for pos in range(len(list)):
list[pos] = list[pos] * newMax/oldMax
#-----------------------------
#Add two sounds together, rescaling the resulting sound if necessary
#to avoid clipping.
#-----------------------------
def addSoundsNoClipping(sound1, sound2):
if getSamplingRate(sound1) != getSamplingRate(sound2):
print "Error! Sounds must have the same sampling rate."
return
minLength = min(getNumSamples(sound1), getNumSamples(sound2))
newSoundList = [0.0] * minLength
#Now add the two sounds together, placing the result in newSoundList
for pos in range1(1,minLength):
#ADD CODE HERE TO SUM UP THE SOUNDS
maxVal = max(newSoundList) #calculate the maximum sample value
#ADD CODE TO COMPLETE THE FOLLOWING THREE STEPS...
#If maxVal> 32767, there has been clipping, normalize the sound.
#Use listToSound to create the final sound from newSoundList.
#Return the resulting sound.