#Slide 5
#takes a sound as input and uses range and indexing
#to double the sample values (increase volume by
#a constant)
def increaseVolumeByRange(sound):
   for sampleNumber in range(0,getLength(sound)):
      value = getSampleValueAt(sound, sampleNumber)
      #don't assign the results of set statements
      setSampleValueAt(sound, sampleNumber, value*2)
#takes a sound as input and uses an array of samples
#to double each samples value (increase the volume
#by a constant)
def increaseVolume(sound):
   for sample in getSamples(sound):
      value=getSampleValue(sample)
      #don't assign the results of set statements
      setSampleValue(sample,value*2)
#When to assign function calls to variables
   #When you return something and want to store it
   #Otherwise don't
#Slide 6
#Takes as input a sound
#Doubles the sample value of the first half
#Halves the sample value of the second half
#Does not have a return
def increaseAndDecrease(sound):
   length=getLength(sound)
   #double the values in the first half
   for index in range(0,length/2):
      value = getSampleValueAt(sound,index)
      setSampleValueAt(sound, index, value*2)
   #halve the values in the second half
   for index in range(length/2,length):
      value = getSampleValueAt(sound,index)
      setSampleValueAt(sound,index, value/2)
#Slide 9
#No inputs
#Requires that getMediaPath accesses the mediasources
#(mediasources is set using setMediaPath() )
# Normalizes then combines the sounds guzdial.wav
# and is.wav
# putting a brief pause between them
# returns the combined sound
def merge():
   #initialization
   guzdial = makeSound(getMediaPath("guzdial.wav"))
   normalize(guzdial)   
   isSound = makeSound(getMediaPath("is.wav"))
   normalize(isSound)
   target = makeSound(getMediaPath("sec3silence.wav"))
   targetIndex = 0
   #Copy guzdial onto target
   for sourceIndex in range(0,getLength(guzdial)):
      value=getSampleValueAt(guzdial,sourceIndex)
      setSampleValueAt(target, targetIndex, value)
      targetIndex = targetIndex + 1
   #Insert a 10th of a second Pause into our sound
   for sourceIndex in range(0,int(0.1*getSamplingRate(target))):
      setSampleValueAt(target, targetIndex, 0)
      targetIndex = targetIndex + 1
   #Copy is onto the target
   for sourceIndex in range(0,getLength(isSound)):
      value = getSampleValueAt(isSound, sourceIndex)
      setSampleValueAt(target, targetIndex, value)
      targetIndex = targetIndex + 1
   play(target)
   return target
#helper function for slide 9
# Normalizes the input sound
def normalize(sound):
    maxValue= 0
    for sample in getSamples(sound):
        value = getSampleValue(sample)
        maxValue = max(maxValue,value)
    normalFactor = 32767.0/maxValue
    for sample in getSamples(sound):
        value = getSampleValue(sample)
        setSampleValue(sample,value*normalFactor)
#slide 14
#Inserts the word united between
#"We the" and "people" in 
#the sound preamble10.wav from the mediaPath
#Requires: setMediaPath() is used to get the 
#mediasources
#Returns the constructed sound 
def splicePreamble():
   file = getMediaPath("preamble10.wav")
   source = makeSound(file)
   target = makeSound(file) #This will be the newly spliced sound
   targetIndex=17408 #Starts after "We the"
   for sourceIndex in range(33414,40052): #Where united is
      value = getSampleValueAt(source,sourceIndex)
      setSampleValueAt(target,targetIndex,value)
      targetIndex= targetIndex + 1
   for index in range(0,1000): #Some quiet space
      setSampleValueAt(target,index,0)
      targetIndex= targetIndex + 1
   play(target)
   return target
#Slide 16
#Exercise: Write a specification
def spliceSimpler():
   file = getMediaPath("preamble10.wav")
   source = makeSound(file)
   target = makeSound(file) #The newly spliced sound
   targetIndex = 17408 #targetIndex starts at just after "We the"
   for sourceIndex in range(33414,40052): #Copy United
     setSampleValueAt(target,targetIndex,getSampleValueAt(source,sourceIndex))
     targetIndex = targetIndex + 1
   #hear and return the result
   play(target)
   return target
#A note on writing general functions:
#Want exactly the variables you'll need and nothing
#more.

#General Clip (slide 17)
#Clips a portion from a source input sound 
#from the integer start to the integer end
#Returns a target sound containing the clipped
#samples with no empty pauses.
def clip(source, start, end):
   target=makeEmptySound(end-start)
   tIndex=0
   #copy piece of source from start to end to target
   for sIndex in range(start,end):
      value = getSampleValueAt(source, sIndex)
      setSampleValueAt(target, tIndex, value)
      tIndex = tIndex + 1
   return target
#General Copy (Slide 18)
#Note that there is no end!!!
#Need: Sound to copy from (source)
#Need: Sound to copy to (target)
#Can copy whole source, and if we want only a piece
#use clip first.
#Where do we end on the target?
#A: We end when we are done copying the source
#Requirement length of target is at least
#start+getLength(source)

def copy(source, target, start):
     tIndex = start #don't erase previous work
     for sIndex in range(0, getLength(source)):
        value = getSampleValueAt(source, sIndex)
        setSampleValueAt(target, tIndex, value)
        tIndex = tIndex + 1
#Simpler Version of this Function (Slide 19)
# inputs: none
# requirement:MediaPath is set to the mediasources
# Uses clip and copy to build the new preamble.
# return: the constructed sound.
def createNewPreamble():
    file = getMediaPath("preamble10.wav")
    preamble = makeSound(file) #old preamble
    #Created the source sounds
    united = clip(preamble, 33414, 40052) #United
    start = clip(preamble,0,17404) #We The
    end = clip(preamble, 17408, 55510) #the rest
    #Create the target sound, first calculate its 
    #length
    len = getLength(start) + getLength(united) + getLength(end)
    newPre = makeEmptySound(len)
    #Copy sources to target
    copy(start, newPre, 0) #copy the beginning
    copy(united, newPre,17404) #copy the middle
    copy(end, newPre, 17404+(40052-33414)) #copy the end
    #normalize, play and return
    normalize(newPre)
    play(newPre)
    return newPre
#Slide 21
# input: A source sound to be reversed
# DOES NOT MODIFY INPUT
# Reverses a sound
# returns: the reversed sound
def reverse(source):
   target = makeEmptySound(getLength(source))
   sourceIndex = getLength(source)-1
   for targetIndex in range(0,getLength(source)): 
   #move forwards on target
     value=getSampleValueAt(source,sourceIndex)
     setSampleValueAt(target,targetIndex,value)
     sourceIndex = sourceIndex - 1
     #moves backwards on source
   return target
#Slide 22
def mirrorSound(sound):
   len = getLength(sound)
   mirrorPoint = len/2
   for index in range(0,mirrorPoint):
      left = getSampleObjectAt(sound,index)
      right = getSampleObjectAt(sound,len-index-1)
      value = getSampleValue(left)
      setSampleValue(right,value)
#How would we reverse it word by word?
#Don't have enough information to do generally
#def wordbywordReverse(sound):
    #won't actually work
#input:none
#requirement: mediaPath is set
#preamble10.wav is in the media path and is the
#preamble10.wav from the mediasources
def wordByWordReverseOfPreamble():
    #get input file
    file=getMediaPath("preamble10.wav")
    preamble=makeSound(file)
    #get individual words clipped
    we = clip(preamble,0,15730)
    the1 = clip(preamble,15730,17407)
    people = clip(preamble,17407,26726)
    of = clip(preamble, 26726, 32131)
    the2 = clip (preamble,32131,33413)
    united = clip (preamble,33413, 40052)
    states = clip (preamble,40052,55510)
    #build the target
    target = makeEmptySound(55510)
    #copying word by word onto the target
    copy(states,target,0) #start at index 0
    copy(united,target,55510-40052) #already used
    copy(the2, target, 55510-33413)
    copy(of, target, 55510-32131)
    copy(people, target, 55510-26726)
    copy(the1, target, 55510-17407)
    copy(we, target, 55510-15730)
    play(target)
    return(target)

