# A function to normalise a sound and reverse it

def normaliseBackward(filename):
  backwardsound = playBackward(filename) #reverse the sound
  normalise(backwardsound) #normalise the sound

  return backwardsound  #return the sound

# Recipe 54: Normalise the sound to a maximum amplitude

def normalise(sound):
  largest = 0
  for s in getSamples(sound):        
    largest = max(largest,getSample(s))
  amplification = 32767.0 / largest

  print "Largest sample in original sound was", largest
  print "Aplification factor is", amplification

  for s in getSamples(sound):      
    louder =  amplification * getSample(s)  
    setSample(s, louder)  

  return sound  

# Recipe 60: Return the given sound backwards

def playBackward(filename):
  source = makeSound(filename)
  dest = makeSound(filename)

  srcSample = getLength(source)
  for destSample in range(1, getLength(dest)+1):
    srcVolume = getSampleValueAt(source, srcSample)
    setSampleValueAt(dest, destSample, srcVolume)
    srcSample = srcSample - 1

  return dest
