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)

Reverse a Sound

def reverse (source):
  target = makeEmptySound (getLength(source), 44100)
  sourceIndex = 0
  for targetIndex in range (0, getLength(target)/2):
    sourceValue = getSampleValueAt (source, sourceIndex)
    setSampleValueAt (target, targetIndex, sourceValue)
    sourceIndex = sourceIndex + 1
  sourceIndex = getLength(source) -1
  for targetIndex in range (getLength(target)/2, getLength(target)):
    sourceValue = getSampleValueAt (source, sourceIndex)
    setSampleValueAt (target, targetIndex, sourceValue)
    sourceIndex = sourceIndex - 1
  return (target)

Copy a Sound into a Target

# copies the "source"  sound into the "target" sound starting at "start" in "target"
def copy (source, target, start):
  targetIndex = start
  for sourceIndex in range(0, getLength(source)):
    sourceValue = getSampleValueAt (source, sourceIndex)
    setSampleValueAt (target, targetIndex, sourceValue)
    targetIndex = targetIndex + 1

Clip a Sound

# returns a new sound that is the portion of "source" from "start" to "end"
# doesn't alter the original sound.
def clip (source, start, end):
  target = makeEmptySound (end - start, 44100)
  targetIndex = 0
  for sourceIndex in range (start, end):
    sourceValue = getSampleValueAt (source, sourceIndex)
    setSampleValueAt (target, targetIndex, sourceValue)
    targetIndex = targetIndex + 1
  return target

XOR

def XOR():
  # get inputs and echo them to user (don't change these 6 lines)
  A = input ("Please enter 1 or 0 for A: ")
  B = input ("Please enter 1 or 0 for B: ")
  print "A is:  "
  print A
  print "B is:  "
  print B
 
  #set up blank picture (don't change this)
  result = makeEmptyPicture (100, 100)
 
  # conditional (if) statement(s) to check whether sA XOR B is true or false
  # and dispay a red FALSE or green TRUE in a blank picture
  # HINT 1: You should have 4 lines of code here; we want you to use 2 separate
  # statements this time. Use the "addText" function also.
  # HINT 2: "addText(result, 20, 20, "TRUE", green)" is a handy statement!

 
  if A != B:
    addText(result, 25, 50, "True", green)
  if A == B:
    addText(result, 25, 50, "False", red)
 
  # display results (don't change this)
  repaint (result)

Wednesday, December 18, 2013

Finch Orientation

#The following line is required for the Finch to work.
#Please replace the import line with the Finch's location.

import edu.cmu.ri.createlab.terk.robot.finch.Finch as Finch

def happyState():
  # Set things up
  tom=Finch()
  # Continue executing the code below until "tom" is turned upside down.
  while not tom.isFinchUpsideDown():
    if tom.isShaken():
      tom.setLED(red)
      tom.saySomething("Stop shaking me")
      #If Tom is being shaken his beak light should turn red,
      #and he should say #Stop shaking me".

      tom.sleep(3000)
    elif tom.isBeakUp():
      #If his beak is up Tom's light
      #should turn yellow, and he should say "Please put my beak down".

      tom.setLED(yellow)
      tom.saySomething("Please put my beak down")
      tom.sleep(3000)
    else:
      tom.setLED(green)
      tom.saySomething("Now I'm happy")
      #For any other state Tom's light should turn green and he should say
      #"Now I'm happy".

      tom.sleep(3000)
  tom.quit()

Enlarge an Image

def grow (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, int(sourceX), int(sourceY)))
      setColor (getPixel (target, targetX, targetY), color)
      sourceY = sourceY + 0.5
    sourceX = sourceX + 0.5
  return (target)

Shrink 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)

Make the Finch say Something

#The following line is required for the Finch to work.
#Please replace the import line with the Finch's location.

import edu.cmu.ri.createlab.terk.robot.finch.Finch as Finch

def singing(will):
  #Have Will say the same thing twice
  for i in range (0,2):
    will.saySomething("howdy")
    will.sleep(2000)

Mirror an Image Vertically

def mirrorVertical (source):
  width = getWidth (source)
  mirrorPoint = widtht/2
  for y in range (0, getHeight(source)):
    for x in range (0, mirrorPoint):
      leftPixel = getPixel (source, x, y)
      rightPixel = getPixel (source, width - x - 1, y)
      color = getColor (rightPixel)
      setColor (leftPixel, color)
  repaint(source)

Mirror an Image Horizontally

def mirrorHorizonatal (source):
  height = getHeight (source)
  mirrorPoint = height/2
  for x in range (0, getWidth(source)):
    for y in range (0, mirrorPoint):
      topPixel = getPixel (source, x, y)
      bottomPixel = getPixel (source, x, height - y - 1)
      color = getColor (bottomPixel)
      setColor (topPixel, color)
  repaint(source)

Grayscale a Picture

def grayScale(picture):
  for p in getPixels(picture):
    intensity = (getRed(p)+getGreen(p)+getBlue(p))/3
    setColor(p, makeColor(255-intensity, 255-intensity, 255-intensity))
  repaint(picture)

Tuesday, December 17, 2013

Randomizes the RGB Values in each Pixel

def randomizePic (picture):
  for p in getPixels(picture):
    red = getRed(p)
    green = getGreen(p)
    blue = getBlue(p)
    setRed(p, red+int(random.random()*100))
    setGreen(p, green+int(random.random()*100))
    setBlue(p, blue+int(random.random()*100))

Change the Color of the Finch's Beak (+ Moving the Finch)

#The following line is required for the Finch to work. 
#Please replace the import line with the Finch's location.
import edu.cmu.ri.createlab.terk.robot.finch.Finch as Finch
import random

def beakChanger():
  #This initializes the Finch
  fred = Finch()
 
  #Moves forward, green beak
  fred.setLED(0, 255, 0)
  fred.setWheelVelocities(100,100,1000)
 
  #Moves backward, gold beak
  fred.setLED(255,255,0)
  fred.setWheelVelocities(-100,-100,1000)
 
  #Spin left 1 second, random beak color
  fred.setLED(int(random.random()*255), int(random.random()*255), int(random.random()*255))
  fred.setWheelVelocities(-100,100,1000)
 
  #Quit the Finch
  fred.quit()
 

Moving the Finch

#The following line is required for the Finch to work. 
#Please replace the import line with the Finch's location.
import edu.cmu.ri.createlab.terk.robot.finch.Finch as Finch

def moveFinch():
  #The following line creates a Finch, called myFinch, for Python to use
  myFinch = Finch()
 
  #The following lines will make myFinch move
  myFinch.setWheelVelocities(255, 255, 1000)
  myFinch.setWheelVelocities(-255, -255, 2000)
  myFinch.setWheelVelocities(255, -200, 1000)
  myFinch.setWheelVelocities(255, 255, 1000)
  myFinch.setWheelVelocities(-255, 200, 2000)
 
  #Always quit the Finch before the program ends
  myFinch.quit()

Halve the sound's frequency

#Create a new sound that cuts the frequency of a sound in half 
#(doubles its length so it sounds "lower")
def halveFrequency(sound):
  target = makeEmptySound(getLength(sound)*2)
  targetIndex = 0
  for s in getSamples(sound):
    value = getSampleValue(s)
    setSampleValueAt(target, targetIndex, value)
    setSampleValueAt(targetIndex, value)
    targetIndex = targetIndex +2
  return(target)

Shift all samples up by 32767

#Shift all samples in a sound "up" by 32767
def upShift (sound):
  for sample in getSamples(sound):
    value = getSampleValue(sample)
    setSampleValue(sample, value + 32767)

Make the bottom-right corner white

#Turns bottom-right corner of a picture (100x100 pixels) white
def whiteCorner (pic):
  for x in range (getWidth(pic)-100, getWidth(pic)):
    for y in range (getHeight(pic)-100, getHeight(pic)):
      px = getPixel(pic, x, y)
      setColor(px, white)

Set all Red values to Blue

#Turns all mostly-red pixels blue ("mostly-red" means within a distance of 140 of red)
def redtoBlue (pic):
  color = getColor(p)
  if distance(color, red) < 140:
    setColor(p, blue)

Increase Volume in a Sound by 2

def increaseVolume(sound):
  for sample in getSamples(sound):
    value = getSample(sample)
    setSample(sample, value * 2)

Increase the Blue in a Picture by 2

def increaseBlue(pict):
  for p in getPixels(pict):
    value = getBlue(p)
    setBlue(p, value * 2)
  repaint(pict)