Midterm Exam 1 Review Fall 2003: Increase the volume, stepped #2
(Back to Fall2003 Midterm Review 1)
Answers? Comments? Comments on answers?
grrrrr....i dont know
| A little bird told me that the lecture on Monday, September 15 might cover this kind of material... It just might be worth showing up, in case this kind of question happens to appear on the exam... Stephen Voida |
pages 137-138 are helpful for this problem. my program is not completely correct, but its a start.
def increaseVolumeStepped(sound):
for sampleIndex in range(1,getLength(sound)/3):
value=getSampleValueAt(sound,sampleIndex)
setSampleValueAt(sound,sampleIndex,value * 0.5)
for sampleIndex in range(getLength(sound)/3,getLength(sound)/3 + getLength(sound)/3):
value=getSampleValueAt(sound,sampleIndex)
setSampleValueAt(sound,sampleIndex,value * 1.2)
for sampleIndex in range(getLength(sound)/3 + getLength(sound)/3,getLength(sound)+1):
value=getSampleValueAt(sound,sampleIndex)
largest=0
largest=maximum(largest,value)
multiplier=32767.0/largest
for s in range(getLength(sound)/3+getLength(sound)/3,getLength(sound)+1):
louder=multiplier * getSampleValue(sound,s)
getSampleValueAt(sound,s,louder)
the multiplier is 32767.0 because that is the constant max value
| Indeed, a pretty good start. Look carefully at your third 'for' loop, in particular what's happening with the variable named 'largest'. What is largest supposed to represent? What does your code do? Stephen Voida |
largest=0 should be put outside the loop
| Yes, it should. Also, maximum is not a predefined function in Jython. I think you mean largest=max(largest, value). Lauren Biddle |
I thought that there would not be volume questions (this was mentioned on Monday during the 1:00 lecture).
In the third and forth 'for' loop, why does the last 'getLength(sound)'have a 1 added to it?
| To get every last sample – remember that range() doesn't include the last value. Mark Guzdial |
def increaseVolumeStepped(sound):
for sampleIndex in range(1,getLength(sound)/3):
value=getSampleValueAt(sound,sampleIndex)
setSampleValueAt(sound,sampleIndex,value * 0.5)
for sampleIndex in range(getLength(sound)/3,getLength(sound)/3 + getLength(sound)/3):
value=getSampleValueAt(sound,sampleIndex)
setSampleValueAt(sound,sampleIndex,value * 1.2)
largest=1
for sampleIndex in range(getLength(sound)/3 + getLength(sound)/3,getLength(sound)+1):
value=getSampleValueAt(sound,sampleIndex)
largest=max(largest,value)
multiplier=32767.0/largest
for s in range(getLength(sound)/3+getLength(sound)/3,getLength(sound)+1):
louder=multiplier * getSampleValueAt(sound,s)
setSampleValueAt(sound,s,louder)
return sound
Does this code work? I had to set largest=1 because it gave me an error when I set it to 0 because it tried to divide by a 0 if the first sample was zero.
def increaseVolumeStepped(sound):
for portion1 in range(1,getLength(sound)/3):
value=getSampleValueAt(sound,portion1)
setSampleValueAt(sound,portion1,value*0.5)
for portion2 in range(getLength(sound)/3,(getLength(sound)/3)*2):
value=getSampleValueAt(sound,portion2)
setSampleValueAt(sound,portion2,value*1.2)
largest=0
for portion3 in range((getLength(sound)/3)*2,getLength(sound)):
value=getSampleValueAt(sound,portion3)
largest=max(largest,value)
multiplier=32767.0/largest
for portion3 in range((getLength(sound)/3)*2,getLength(sound)):
value=getSampleValueAt(sound,portion3)
louder=multiplier*value
setSampleValueAt(sound,portion3,louder)
is this working? it doesnt SOUND like its working when i try it in JES ...
I had this code:
def normalizehalf(sound):
largest=0
for sample in range(1,getLength(sound)/2):
largest = max(largest,getSampleValueAt(sound,sample))
greatestmultiplier = 32767.00/largest
for sample in range (1,getLength(sound)/2):
setSampleValueAt(sound,sample,getSampleValueAt(sound,sample)greatestmultiplier)
play(sound)
and every time I executed it, more sound samples went to max. Once this code is executed, nothing is supposed to happen the second time, so what am I doing wrong?
I fixed your indents for you, so I took the liberty of removing your second post. My pleasure.
if you try it in JES, use the "Gettysburg10.wav" sound file. it's long enough to hear the effects.
def increaseVolumeStepped(sound):
for sampleIndex in range(1,getLength(sound)/3):
value=getSampleValueAt(sound,sampleIndex)
setSampleValueAt(sound,sampleIndex,value * 0.5)
for sampleIndex in range(getLength(sound)/3,getLength(sound)/3 + getLength(sound)/3):
value=getSampleValueAt(sound,sampleIndex)
setSampleValueAt(sound,sampleIndex,value * 1.2)
largest=0
for sampleIndex in range(getLength(sound)/3 + getLength(sound)/3,getLength(sound)+1):
value=getSampleValueAt(sound,sampleIndex)
largest=max(largest,value)
multiplier=32767.0/largest
for s in range(getLength(sound)/3+getLength(sound)/3,getLength(sound)+1):
louder= multiplier * getSampleValueAt(sound,s)
setSampleValueAt(sound,s,louder)
This is what I got...the only difference is that "largest= 0" comes out of the for loop. I think this works...
Link to this Page