Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Fall 2003: Making sounds to order

Answers? Comments? Questions on Answers? Questions on Comments?

(Back to Fall2003 Midterm Review 2)



 def increasingAmplitude(freq,ampl):
  first=sineWave(freq,ampl)
  second=sineWave(freq*3, ampl*2)
  third=sineWave(freq*5, ampl*3)
  fourth=sineWave(freq*7, ampl*4)
  sound=addSounds(first,second,third,fourth)
  retrun(sound)


not sure about this... just a guess -Ashley Kracke


addSounds only takes two inputs at a time. Mark Guzdial


what about this?

def increasingAmplitude(freq,ampl):
  first=sineWave(freq,ampl)
  second=sineWave(freq*3, ampl*2)
  third=sineWave(freq*5, ampl*3)
  fourth=sineWave(freq*7, ampl*4)
  sound=addSounds(second, first)
  sound2=addSounds(third,first)
  sound3=addSounds(fourth,first)
  return(first)


What does addSounds do again? Does it return a new sound or just alter one of the ones you give it?

This is the code my ta gave us in recitation. I ran it but it gave me an error at "sound1=" line saying a local or global name was used before it was created. Why did it gave me this error with & without the sineWave and addSounds code in the increasingAmplitude code.

def increasingAmplitude(freq,ampl):
  sound1 = sineWave(freq,ampl)
  sound2 = sineWave(3xfreq,2xampl)
  sound3 = sineWave(5xfreq,3xampl)
  sound4 = sineWave(7xfreq,4xampl)
  addSounds(sound1,sound2)
  addSounds(sound2,sound3)
  addSounds(sound3,sound4)
  play(sound4)
  return sound4


Did you have sineWave() and addSounds() in the same file as increasingAmplitude()? Mark Guzdial


def increasingAmplitude(freq,ampl):
  sound1=sineWave(freq,ampl)
  sound2=sineWave(freq*3,ampl*2)
  sound3=sineWave(freq*5,ampl*3)
  sound4=sineWave(freq*7,ampl*4)
  addSound(sound1,sound2)
  addSound(sound2,sound3)
  addSound(sound3,sound4)
  play (sound4)
  return (sound4)


Use the and
 tags. I'll fix it.
_
That is what my code looks like too and i got an error message saying "A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
Please check line 2 of C:\JES\increasingAmplitude.py"...the input should be numbers for the frequency and amplitude, right? (increasingAmplitude(440,2000) for example?)
_
i think you are supposed to have those two functions defined in the same file for it to work. I don't think they are just normal functions in JES, which is why it won't work. 
|You're right. *Mark Guzdial*|
_
def increasingAmplitude(freq, ampl):
  sound1 = sineWave(freq, ampl)
  sound2 = sineWave((freq*3), (ampl*2))
  sound3 = sineWave((freq*5), (ampl*3))
  sound4 = sineWave((freq*7), (ampl*4))
  addSounds(sound1,sound2)
  addSounds(sound2, sound3)
  addSounds(sound3, sound4)
  play(sound4)
  return(sound4)

def addSounds(sound1,sound2):
  for index in range(1,getLength(sound1)+1):
    s1Sample = getSampleValueAt(sound1,index)
    s2Sample = getSampleValueAt(sound2,index)
    setSampleValueAt(sound2,index,s1Sample+s2Sample)

def sineWave(freq,amplitude):
    # Get a blank sound
    mySound =  getMediaPath('sec1silence.wav')
    buildSin = makeSound(mySound)
    # Set sound constant
    sr = getSamplingRate(buildSin)        # sampling rate
    interval = 1.0/freq			     # Make sure it's floating point
    samplesPerCycle =  interval * sr    # samples per cycle
    maxCycle = 2 * pi
    for pos in range (1,getLength(buildSin)+1):
        rawSample = sin( (pos / samplesPerCycle) * maxCycle)
        sampleVal = int( amplitude*rawSample)
        setSampleValueAt(buildSin,pos,sampleVal)         
    return (buildSin)

I feel pretty confident this is correct. I ran it in Jes and didn't get any errors. However, I can't be sure, because just listening to the sound- I have no clue if it worked right.

At some point, read the FAQ so that you know how to put in code without getting multiple multiplies turned into links. Mark Guzdial


I know that for it to run in JES, we have to define sineWave and addSounds. On the test, will we have to also define these, or just give the code for increasing amplitude?

It says in the directions to assume that sineWave and addSounds are available to you, so I think that means that they are already defined, so we don't have to do it. Is this right?
Yes. For this particular problem, that wasn't part of this question. But it's not a bad idea to learn those two functions either. Lauren Biddle

Does that mean that we should memorize the addSound or sineWave functions for the midterm?
It is good to know what the functions that you have studied do. But remember that we will never expect you to write code on a blank sheet of paper (i.e. without any examples). So you never need to memorize anything. You should understand, though. Lauren Biddle

>>> f440=sineWave(440,2000)
>>> f880=sineWave(880,4000)
>>> f1320=sineWave(1320,8000)

Can someone please explain why 440 is 2000; 880, 4000; and 1320, 8000?
sineWave(freq,amp). 2000 has nothing to do with it; it's just that 440 is the frequency and that's how it was chosen to be represented.

That's right – I chose those amplitudes, but they could have been anything (less than 32000). Different amplitudes give you different overall sounds when summing, though. Try it. And like Lauren says – I won't ask you to memorize, but I will ask you to understand and explain. Mark Guzdial



Link to this Page