Thursday, December 19, 2013

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)