Docsity

CS1315 Spring 2005 Pre-quiz 2: Reversing and Normalizing Sound Files, Quizzes of Computer Science

Toggle sidebar
bg1
CS1315 Spring 2005
Pre-quiz 2
Write a function called reverseAndNormal that takes a filename as input and then normalizes the whole
sound and turns backwards just the last half of the sound and returns the result.
To assist you here is a function that reverses a sound:
def reverse(filename):
orig = makeSound(filename)
targ = makeSound(filename)
targdex = getLength(targ)
for index in range(1, getLength(orig)+1):
sv = getSampleValueAt(orig, index)
setSampleValueAt(targ, targdex, sv)
targdex = targdex - 1
Here is a function that silences the second half of the sound:
def silenceBackHalf(sound):
for index in range(getLength(sound)/2, getLength(sound)+1):
setSampleValueAt(sound, index, 0)
And a function that normalizes a sound:
def normalize(sound):
largest = 0
for s in getSamples(sound):
largest = max(largest,getSample(s))
multiplier = 32767.0/largest
print "Largest value was",largest
print "Multipiler is" , multiplier
for s in getSamples(sound):
louder = multiplier*getSample(s)
setSample (s,louder)
Page 1 of 1

Partial preview of the text

Download CS1315 Spring 2005 Pre-quiz 2: Reversing and Normalizing Sound Files and more Quizzes Computer Science in PDF only on Docsity!

CS1315 Spring 2005

Pre-quiz 2

Write a function called reverseAndNormal that takes a filename as input and then normalizes the whole sound and turns backwards just the last half of the sound and returns the result.

To assist you here is a function that reverses a sound:

def reverse(filename): orig = makeSound(filename) targ = makeSound(filename) targdex = getLength(targ) for index in range(1, getLength(orig)+1): sv = getSampleValueAt(orig, index) setSampleValueAt(targ, targdex, sv) targdex = targdex - 1

Here is a function that silences the second half of the sound:

def silenceBackHalf(sound): for index in range(getLength(sound)/2, getLength(sound)+1): setSampleValueAt(sound, index, 0)

And a function that normalizes a sound:

def normalize(sound): largest = 0 for s in getSamples(sound): largest = max(largest,getSample(s)) multiplier = 32767.0/largest print "Largest value was",largest print "Multipiler is" , multiplier for s in getSamples(sound): louder = multiplier*getSample(s) setSample (s,louder) Page 1 of 1