# Simone S. Lee
# CS110: Final Project
# Ananya Misra
# 12/9/2008
 

from myro import *
from random import *
from myro.chuck import *
initChuck()


def playSong(instrument, frequency, time, strength):
    instrument.setFrequency(frequency)
    instrument.noteOn(strength)
    wait(time)
    instrument.noteOff(strength)

 
def playBeat(instrument, time, strength):
   instrument.noteOn(strength)
   wait(time)


# The mandolin plays an old French melody.
# The shakers and moog synthesizer beat and melody
# were arranged by me.
 

def playMandolin():
    m = Mandolin()
    m.connect()
    m.setGain(0.5)
    beat = 1.0
    for i in range(3):
        playSong(m, 440, beat/2, .8)
        playSong(m, 440, beat/2, .8)
        playSong(m, 587, beat/2, .8)
        playSong(m, 440, beat/2, .8)
        playSong(m, 440, beat/2, .8)
        playSong(m, 392, beat/2, .8)
        playSong(m, 587, beat/2, .8)
        playSong(m, 392, beat/2, .8)
        playSong(m, 392, beat/4, .8)
        playSong(m, 347, beat/4, .8)
        playSong(m, 587, beat/4, .8)
        playSong(m, 347, beat/4, .8)
        playSong(m, 347, beat, .8)
        playSong(m, 330, beat, .8)
        playSong(m, 262, beat, .8)
        playSong(m, 297, beat, .8)
    m.noteOff(1)

 
def playShakers():
   shakers = Shakers()
   shakers.connect()
   shakers.preset(6)
   beat = 1.0
   for i in range(27):
       playBeat(shakers, beat/4, 1)
       playBeat(shakers, beat/4, 1)
       playBeat(shakers, beat/4, 1)
       playBeat(shakers, beat/4, 1)
   shakers.noteOff(1)
 

def playMoog():
    moog = MoogSynthesizer()
    moog.connect()
    moog.noteOn(1)
    beat = 1.0
    for i in range(3):
        playSong(moog, 440, beat, 1)
        playSong(moog, 587, beat/2, .8)
        playSong(moog, 440, beat, 1)
        playSong(moog, 392, beat/2, .8)
        playSong(moog, 392, beat, 1)
        playSong(moog, 347, beat, 1)
        playSong(moog, 347, beat, .8)
        playSong(moog, 330, beat, .8)
        playSong(moog, 262, beat, .8)
        playSong(moog, 297, beat, .8)
    moog.noteOff(1)
 

def makeCircle(x, y, r):
    # Creates a Circle centered
    # at point (x, y) of radius r
    return Circle(Point(x, y), r)
 

def makeColor():
    # Creates a new color using random values
    red = randrange(0, 256)
    green = randrange(0, 256)
    blue = randrange(0, 256)
    return color_rgb(red, green, blue)

def bounce():
    width = height = 500
    Canvas = GraphWin('Circles', width, height)
    Canvas.setBackground("white")
    # Draws random circles with random colors
    n = 3
    for i in range(n):

        # Picks random center point
        # and radius in the window
        x = randrange(0, width)
        y = randrange(0, height)
        r = randrange(5, 25)
        c = makeCircle(x, y, r)
        # Selects a random color
        c.setFill(makeColor())

        c.draw(Canvas)

        # Animates the circles
        dx = dy = 3
        while timeRemaining(9):
            # Moves the circles
            c.move(dx, dy)

            # Makes sure the circles are within bounds

            center = c.getCenter()
            cx, cy = center.getX(), center.getY()

            if (cx+r >= width) or (cx-r <= 0):
                dx = -dx

            if (cy+r >= height) or (cy-r <= 0):

                dy = -dy

            wait(0.01)

bounce()

doTogether(playMandolin, playShakers, playMoog, bounce)
 
# A ball bounces for 9 seconds, freezes, followed by another ball,
# which freezes after 9 seconds, followed by a third ball.
# Afterwards a new window appears while music plays.  The music
# repeats itself 3 times, each time lasting for 9 seconds.
# This coordinates with how long the balls bounce.


