# Sound Functions

def increaseVolume(sound):
  for s in getSamples(sound):
    value = getSampleValue(s)
    setSampleValue(s, 2 * value)

def maximize(sound):
  for s in getSamples(sound):
    value = getSampleValue(s)
    if value >= 0:
      setSampleValue(s,32768)
    if value < 0:
      setSampleValue(s,-32767)

def makeSplice(sound1,sound2):
  newSound = makeEmptySound(getLength(sound1)+getLength(sound2))
  index = 0
  for s in getSamples(sound1):
    setSampleValueAt(newSound,index,getSampleValue(s))
    index = index + 1
  for s in getSamples(sound2):
    setSampleValueAt(newSound,index,getSampleValue(s))
    index = index + 1
  return newSound

def makeMix(sound1,sound2,mixValue1=0.50,mixValue2=0.50):
  newLength = min(getLength(sound1),getLength(sound2))
  newSound = makeEmptySound(newLength)
  index = 0
  for index in range(0,newLength):
    sample1 = getSampleValueAt(sound1,index)
    sample2 = getSampleValueAt(sound2,index)
    newSample = mixValue1*sample1 + mixValue2*sample2
    setSampleValueAt(newSound,index,newSample)
  return newSound

def makeSilence(length):
  return makeEmptySound(length)

def makeEcho(sound,spacing=10000,repeat=5):
  newSound = makeEmptySound(getLength(sound)+spacing*repeat)
  #Put the initial sound in
  index = 0
  for s in getSamples(sound):
    setSampleValueAt(newSound,index,getSampleValue(s))
    index = index + 1
  #Now put the echoes in
  for echoes in range(1,repeat):
    echoSound = makeEmptySound(getLength(sound)+spacing*repeat)
    index = echoes * spacing
    for s in getSamples(sound):
      setSampleValueAt(echoSound,index,getSampleValue(s))
      index = index + 1
    newSound = makeMix(newSound,echoSound,0.9,0.1)
  return newSound

def makePartSound(sound,start,finish):
  newSound = makeEmptySound(finish+start)
  index = 0
  for src in range(start,finish):
    value = getSampleValueAt(sound,src)
    setSampleValueAt(newSound,index,value)
    index = index + 1
  return newSound

def makeScaleSound(sound,factor):
  newLength = int(getLength(sound)*factor)
  newSound = makeEmptySound(newLength)
  src = 0
  for index in range(0,newLength):
    value = getSampleValueAt(sound,int(src))
    setSampleValueAt(newSound,index,value)
    src = src + 1.0/factor
  return(newSound)


# Image Functions
def decreaseRed(picture):
  for p in getPixels(picture):
    value=getRed(p)
    setRed(p,value*0.5)

def decreaseBlue(picture):
  for p in getPixels(picture):
    value=getBlue(p)
    setBlue(p,value*0.7)

def decreaseGreen(picture):
  for p in getPixels(picture):
    value=getGreen(p)
    setGreen(p,value*0.7)

def clearRed(picture):
  for p in getPixels(picture):
    setRed(p,0)

def maxBlue(picture):
  for p in getPixels(picture):
    setBlue(p,255)

def copyPicture(picture):
  returnPicture = makeEmptyPicture(getWidth(picture),getHeight(picture))
  for pixel in getPixels(picture):
    color = getColor(pixel)
    setColor(getPixel(returnPicture,getX(pixel),getY(pixel)),returnPicture)
  return returnPicture

def copyPartPicture(picture,x1,y1,x2,y2):
  returnPicture = makeEmptyPicture(x2-x1,y2-y1)
  targetx = 0
  for srcx in range(x1,x2):
    targety = 0
    for srcy in range(y1,y2):
      srcpixel = getPixel(picture,srcx,srcy)
      targetPixel = getPixel(returnPicture,targetx,targety)
      setColor(targetPixel,getColor(srcpixel))
      targety = targety + 1
    targetx = targetx + 1
  return returnPicture

def pastePicture(canvas,source,startx,starty):
  targetx = startx
  for x in range(0,getWidth(source)):
    targety = starty
    for y in range(0,getHeight(source)):
      srcpixel = getPixel(source,x,y)
      if (targetx < getWidth(canvas)) and (targety < getHeight(canvas)):
        targetPixel = getPixel(canvas,targetx,targety)
        setColor(targetPixel,getColor(srcpixel))
      targety = targety + 1
    targetx = targetx + 1

def resizeWidth(picture,newWidth):
  return scale(picture,float(newWidth)/getWidth(picture))

def resizeHeight(picture,newHeight):
  return scale(picture,float(newHeight)/getHeight(picture))

def scale(picture,factor):
  newHeight = int(factor*getHeight(picture))+1
  newWidth = int(factor*getWidth(picture))+1
  returnPic = makeEmptyPicture(int(newWidth),int(newHeight))
  sx = 0
  for tx in range(0,newWidth):
    sy = 0
    for ty in range(0,newHeight):
      if (int(sx) < getWidth(picture)) and (int(sy) < getHeight(picture)):
        sp = getPixel(picture,int(sx),int(sy))
        tp = getPixel(returnPic,tx,ty)
        setColor(tp,getColor(sp))
      sy = sy + (1/factor)
    sx = sx + (1/factor)
  return returnPic

def negative(picture):
  for p in getPixels(picture):
    r = getRed(p)
    b = getBlue(p)
    g = getGreen(p)
    color = makeColor(255-r, 255-g, 255-b)
    setColor(p,color)

def grayscale(picture):
  for p in getPixels(picture):
    r = getRed(p)
    b = getBlue(p)
    g = getGreen(p)
    brightness = (r+g+b)/3
    color = makeColor(brightness,brightness,brightness)
    setColor(p,color)

def changeAllShirt(picture):
  myGreen = makeColor(132,240,204)
  for pixel in getPixels(picture):
    color = getColor(pixel)
    if distance(color,myGreen) < 100:
      setColor(pixel,pink)

def changeShirt(picture):
  myGreen = makeColor(132,240,204)
  for x in range(220,400):
    for y in range(210,480):
      pixel = getPixel(picture,x,y)
      color = getColor(pixel)
      if distance(color,myGreen) < 50:
        setColor(pixel,pink)

def newBackgroundShirt(picture,background):
  myGreen = makeColor(132,240,204)
  for x in range(220,400):
    for y in range(210,480):
      pixel = getPixel(picture,x,y)
      color = getColor(pixel)
      if distance(color,myGreen) < 100:
        backgroundPixel = getPixel(background,x,y)
        backgroundColor = getColor(backgroundPixel)
        setColor(pixel,backgroundColor)

def chromakey(picture,background,color,threshold=100.0):
   for pixel in getPixels(picture):
     pixelColor = getColor(pixel)
     if distance(color,pixelColor) < threshold:
       newColor = getColor(getPixel(background,getX(pixel),getY(pixel)))
       setColor(pixel,newColor)

def chromakeyBlue(picture,background):
   for pixel in getPixels(picture):
     pixelColor = getColor(pixel)
     if getRed(pixel)<getBlue(pixel) and getGreen(pixel)<getBlue(pixel):
       newColor = getColor(getPixel(background,getX(pixel),getY(pixel)))
       setColor(pixel,newColor)

import os
def chromakeySet(frames,background):
  newdir = "C:/temp/"
  for file in os.listdir(frames):
    if file.endswith(".jpg"):
      printNow(file)
      frame = makePicture(frames+file)
      chromakeyBlue(frame,background)
      writePictureTo(frame,newdir+file)

# Putting it together code goes down here.

def makeACollage():
  betsy = makePicture(getMediaPath("IMG_0802.JPG"))
  hunter = makePicture(getMediaPath("IMG_0808.JPG"))
  guys = makePicture(getMediaPath("IMG_0805.JPG"))
  james = copyPartPicture(guys,352,217,618,475)
  grayscale(betsy)
  clearRed(james)
  pastePicture(betsy,scale(james,0.35),0,0)
  maxBlue(guys)
  pastePicture(betsy,scale(guys,0.35),450,340)
  negative(hunter)
  pastePicture(betsy,scale(hunter,0.35),0,340)
  writePictureTo(betsy,"C:/collage.jpg")

def makeChord():
  notec = makeSound(getMediaPath("brass-c4.wav"))
  notee = makeSound(getMediaPath("brass-e4.wav"))
  noteg = makeSound(getMediaPath("brass-g4.wav"))
  mixce = makeMix(notec,notee)
  play(mixce)
  mixceg = makeMix(mixce,noteg,0.75,0.25)
  play(mixceg)
  return mixceg
