Thursday, December 26, 2013

SlideShow Code

#Purpose: This program will play a slide show of 4 slides
# The slides will appear on one large canvas one at a time,
# from left to right, with previous slides remaining in the
# picture after new ones are added.
# An appropriate narration for each slide will play after the
# slide appears
# until the time when the next slide appears.
# The narration will be blended with selected music.

import time

#This function mixes two sounds.
def makeMix(sound1, sound2, mixValue1, mixValue2):
  newLength = min(getLength(sound1),getLength(sound2))
  newSound = makeEmptySound(newLength)
  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

#This function shrinks an image. 
def shrink (source):
  # set up target picture
  target = makeEmptyPicture( getWidth(source)/2, getHeight(source)/2)
  # do the actual copying
  sourceX = 0
  for targetX in range (0, getWidth(source)/2):
    sourceY = 0
    for targetY in range (0, getHeight(source)/2):
      color = getColor (getPixel (source, sourceX, sourceY))
      setColor (getPixel (target, targetX, targetY), color)
      sourceY = sourceY + 2
    sourceX = sourceX + 2
  return (target)   
 
#The main function
def slideShow():
  # Create empty canvas
  canvas = makeEmptyPicture(1929, 240)
 
  #The four images
  greece1 = makePicture(getMediaPath("greece.jpg"))
  pic1 = shrink(greece1)
  pic2 = makePicture(getMediaPath ("waikiki.jpg"))
  m3 = makePicture(getMediaPath ("moon-surface.jpg"))
  pic3 = shrink(m3)
  pic4 = makePicture(getMediaPath ("caterpillar.jpg"))

  #The four narrations
  Narration1 = makeSound(getMediaPath("Clip1.wav"))
  Narration2 = makeSound(getMediaPath("Clip2.wav"))
  Narration3 = makeSound(getMediaPath("Clip3.wav"))
  Narration4 = makeSound(getMediaPath("Clip4.wav"))
 
  #The four background instrumentals
  GreeceSound = makeSound(getMediaPath("Compatibility 30.wav"))
  WaikikiSound = makeSound(getMediaPath("One Clear Moment 30.wav"))
  SpaceSound = makeSound(getMediaPath("Disappearing_Acts_30.wav"))
  EvilSound = makeSound(getMediaPath("Darkness Falls 30.wav"))

  #The blended sounds
  GreeceAudio = makeMix(GreeceSound, Narration1, .25, .75)
  WaikikiAudio = makeMix(WaikikiSound, Narration2, .25, 1)
  SpaceAudio = makeMix(SpaceSound, Narration3, .25, 1)
  EvilAudio = makeMix(EvilSound, Narration4, .25, 1)
 
  checkTime = false
 
  #copying the images into the canvas
 
  t0 = time.clock()
  copyInto (pic1, canvas, 320, 20)
  if (checkTime):
    print time.clock() - t0, "seconds process time for copy 1"
  repaint (canvas)
  blockingPlay (GreeceAudio)
 
  t0 = time.clock()
  copyInto (pic2, canvas, 640, 0)
  if (checkTime):
    print time.clock() - t0, "seconds process time for copy 1"
  repaint (canvas)
  blockingPlay (WaikikiAudio) 
 
  t0 = time.clock()
  copyInto (pic3, canvas, 960, 0)
  if (checkTime):
    print time.clock() - t0, "seconds process time for copy 1"
  repaint (canvas)
  blockingPlay (SpaceAudio) 
 
  t0 = time.clock()
  copyInto (pic4, canvas, 1280, 45)
  if (checkTime):
    print time.clock() - t0, "seconds process time for copy 1" 
  repaint (canvas)
  blockingPlay (EvilAudio) 

Echo a Sound

#This function will echo the sample in the sample's duration.
#I chose the delay to be 15800, because it sounded good at 15800.
def echo(delay):
  e1 = makeSound("yoursound.wav")
  e2 = makeSound("yoursound.wav")
  for index in range (delay, getLength(e1)):
    echoSample = 0.6*getSampleValueAt(e2, index-delay)
    comboSample = getSampleValueAt(e1, index) + echoSample
    setSampleValueAt(e1, index, comboSample)
  return e1

Produce a Negative

#This function prooduces a negative.
def negative(picture):
  for pixel in getPixels(picture):
    r = getRed(pixel)
    g = getGreen (pixel)
    b = getBlue(pixel)
    nucolor = makeColor(255-r, 255-g, 255-b)
    setColor (pixel, nucolor)
   

Friday, December 20, 2013

Mix Three Sounds

# This function blends 3 sounds.
def makeMix(sound1, sound2, sound3, mixValue1, mixValue2, mixValue3):
  newLength = min(getLength(sound1) ,getLength(sound2), getLength(sound3))
  newSound = makeEmptySound(newLength)
  for index in range(0,newLength):
    sample1 = getSampleValueAt (sound1, index)
    sample2 = getSampleValueAt (sound2, index)
    sample3 = getSampleValueAt (sound3, index)
    newSample = mixValue1*sample1 + mixValue2*sample2 + mixValue*sample3
    setSampleValueAt (newSound, index, newSample)
  return newSound

Interleave Two Sounds

#This function requires the 'clip' and 'copy' functions to work.

# Interleaves 2 sounds until the shortest of
# the 2 sounds is done. Takes an integer to designate how long intervals
# should occur.

def interleave(sound, sound2):
  samplelength = input ("Please enter an integer to designate how long intervals should occur: ")
  minLength = min(getLength(sound1) ,getLength(sound2))
  newSound = makeEmptySound(minLength*2)
  numSegments = minLength / sampleLength
  for segment in range (0, numSegments):
    segment1 = clip (sound1, segment*sampleLength, (segment + 1)*samplelength)
    segment2 = clip (sound2, segment*samplelength, (segment + 1)*samplelength)
    copy (segment1, newSound, segment*s(amplelength*2))
    copy (segment2, newSound, segment*(samplelength*2) + samplelength)
  return newSound

Mix Two Sounds

# This function blends 2 sounds.
def makeMix(sound1, sound2, mixValue1, mixValue2):
  newLength = min(getLength(sound1) ,getLength(sound2))
  newSound = makeEmptySound(newLength)
  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

Thursday, December 19, 2013

Double the Frequency

# Doubles the frequency of the sound
def doubleFrequency(source):
  len = getLength(source)/2 + 1
  target = makeEmptySound (len)
  targetIndex = 0
  for sourceIndex in range (0, getLength(source), 2):
    sourceValue = getSampleValueAt (source, sourceIndex)
    setSampleValueAt (target, targetIndex, sourceValue)
    targetIndex = targetIndex + 1
  return (target)