# a function to apply a 'fade out' to a sound

def fadeOut(sound):

  fadefactor = 0.0001 #set the starting fade factor
  i = getLength(sound) #set the starting sample in the sound
  while fadefactor < 1.0: #loop until fading is finished
    sample = (getSampleValueAt(sound, i))*fadefactor #create the faded sound sample
    setSampleValueAt(sound, i, sample) #write the the faded sample to the sound
    fadefactor = fadefactor + 0.0001 # reduce the fading
    i = i - 1 #move 1 sample back in the sound

  play(sound)

  return sound

