from myro import *
from myro.chuck import *
initChuck()

# Nora Mascioli
# Final Project
# "Istanbul(not Constantinople)," as played by They Might Be Giants
# Dance choreographed and performed by CousinWhat

wait(2)

m=Mandolin()
m.connect()

s=Saxophone()
s.connect()

b = StruckBar()
b.connect()

wait(2)
#Instrumental helper functions

def playInstrument1(freq,volume,beat):
    m.setFrequency(freq)
    m.noteOn(volume)
    wait(beat)
    m.noteOff(0.0)

def playInstrument1Stacc(freq,volume,beat):
    m.setFrequency(freq)
    m.noteOn(volume)
    wait(beat/2.0)
    m.noteOff(0.0)
    wait(beat/2.0)

def playInstrument2(freq,volume,beat):
    s.setFrequency(freq)
    s.noteOn(volume)
    wait(beat)
    s.noteOff(0.0)

def playInstrument3(freq,volume,beat):
    b.setFrequency(freq)
    b.noteOn(volume)
    wait(beat)

#Melodic components
    
def Melody1(volume,beat):
    playInstrument1Stacc(349.23,volume,beat)
    playInstrument1Stacc(415.3,volume,beat)
    playInstrument1(523.25,volume,1.5*beat)
    playInstrument1(261.63,volume,beat/2.0)
    playInstrument1(349.23,volume,beat)
    playInstrument1(415.3,volume,.67*beat)
    playInstrument1(554.37,volume,.33*beat)
    playInstrument1(523.25,volume,.67*beat)
    playInstrument1(415.3,volume,.33*beat)
    playInstrument1(349.23,volume,.67*beat)
    playInstrument1(261.63,volume,.33*beat)

def Melody2(volume,beat):
    playInstrument1(329.63,volume,beat)
    playInstrument1(392,volume,beat)
    playInstrument1(466.16,volume,1.5*beat)
    playInstrument1(261.63,volume,.5*beat)
    playInstrument1(329.63,volume,beat)
    playInstrument1(392,volume,.67*beat)
    playInstrument1(554.37,volume,.33*beat)
    playInstrument1(523.25,volume,.67*beat)
    playInstrument1(466.16,volume,.33*beat)
    playInstrument1(415.3,volume,.67*beat)
    playInstrument1(392,volume,.33*beat)

def Melody3(volume,beat):
    playInstrument1(349.23,volume,.5*beat)
    playInstrument1(349.23,volume,.5*beat)
    playInstrument1(349.23,volume,beat)
    playInstrument1(349.23,volume,beat)
    playInstrument1(349.23,volume,.5*beat)
    playInstrument1(349.23,volume,.5*beat)
    playInstrument1(349.23,volume,beat)
    playInstrument1(349.23,volume,beat)
    playInstrument1(349.23,volume,beat)
    wait(beat)

def Melody4(volume,beat):
    playInstrument1(349.23,volume,.5*beat)
    playInstrument1(349.23,volume,.5*beat)
    playInstrument1(349.23,volume,beat)
    playInstrument1(261.63,volume,beat)
    playInstrument1(329.63,volume,beat)
    playInstrument1(349.23,volume,1.5*beat)
    wait(beat/2.0)

def Melody5(volume,beat):
    playInstrument1(349.23,volume,beat)
    playInstrument1(392,volume,beat)
    playInstrument1(415.3,volume,2*beat)
    playInstrument1(392,volume,2*beat)
    playInstrument1(349.23,volume,3*beat)
    playInstrument1(392,volume,beat)
    playInstrument1(415.3,volume,beat)
    playInstrument1(466.16,volume,beat)
    playInstrument1(392,volume,beat)
    playInstrument1(415.3,volume,beat)
    playInstrument1(349.23,volume,4*beat)
    for i in range(4):
        playInstrument1(349.23,volume,beat)
    playInstrument1(392,volume,beat)
    playInstrument1(349.23,volume,beat)
    playInstrument1(349.23,volume,2*beat)
    playInstrument1(311.13,volume,.5*beat)
    playInstrument1(311.13,volume,.5*beat)
    playInstrument1(349.23,volume,beat)
    playInstrument1(392,volume,beat)
    playInstrument1(415.3,volume,beat)
    playInstrument1(466.16,volume,.5*beat)
    playInstrument1(466.16,volume,.5*beat)
    playInstrument1(466.16,volume,beat)
    playInstrument1(466.16,volume,2*beat)

def Melody6(volume,beat):
    playInstrument1(349.23,volume,beat)
    playInstrument1(349.23,volume,.67*beat)
    playInstrument1(349.23,volume,.33*beat)
    playInstrument1(349.23,volume,.67*beat)
    playInstrument1(392,volume,.33*beat)
    playInstrument1(415.3,volume,.67*beat)
    playInstrument1(466.16,volume,.33*beat)
    playInstrument1(523.25,volume,2*beat)
    wait(beat)

def MelodyEnd(volume,beat):
    beat=beat*1.25
    playInstrument1(523.25,volume,beat)
    playInstrument1(261.63,volume,beat)
    playInstrument1(261.63,volume,.67*beat)
    playInstrument1(261.63,volume,.33*beat)
    beat=beat*1.25
    playInstrument1(261.63,volume,.67*beat)
    playInstrument1(277.18,volume,.33*beat)
    playInstrument1(293.66,volume,.67*beat)
    playInstrument1(329.63,volume,.33*beat)
    playInstrument1(349.23,volume,3*beat)

def MelodyLoop1(volume,beat):
    Melody1(volume,beat)
    Melody1(volume,beat)
    Melody2(volume,beat)

def playMelody():
    volume=1.0
    beat=0.25
    #verse 1
    MelodyLoop1(volume,beat)
    Melody3(volume,beat)
    #verse 2
    MelodyLoop1(volume,beat)
    Melody4(volume,beat)
    #verse 3
    Melody5(volume,beat)
    #verse 4
    MelodyLoop1(volume,beat)
    Melody6(volume,beat)
    MelodyEnd(volume,beat)

# Song Introduction

def intro1(volume,beat):
    playInstrument2(261.63,volume,.5*beat)
    playInstrument2(349.23,volume,.5*beat)
    playInstrument2(392,volume,.5*beat)
    playInstrument2(415.3,volume,beat)
    wait(beat/2)
    playInstrument2(261.63,volume,.5*beat)
    playInstrument2(349.23,volume,.5*beat)
    playInstrument2(392,volume,.5*beat)
    playInstrument2(415.3,volume,beat)
    wait(beat)
    
def playIntro(volume,beat):
    playInstrument2(233.08,volume,beat)
    playInstrument2(261.63,volume,beat)
    playInstrument2(311.13,volume,beat)
    playInstrument2(392,volume,beat)
    playInstrument2(349.23,volume,beat)
    playInstrument2(392,volume,beat)
    playInstrument2(415.3,volume,beat)
    playInstrument2(466.16,volume,beat)
    playInstrument2(523.2,volume,4*beat)
    playInstrument2(466.16,volume,4*beat)
    playInstrument2(415.3,volume,2*beat)
    playInstrument2(392,volume,2*beat)
    playInstrument2(349.23,volume,2*beat)
    playInstrument2(293.66,volume,2*beat)
    playInstrument2(349.23,volume,.5*beat)
    intro1(volume,beat)
    for i in range(2):
        playInstrument2(261.63,volume,.5*beat)
        playInstrument2(329.63,volume,.5*beat)
        playInstrument2(349.23,volume,.5*beat)
        playInstrument2(392,volume,beat)
        wait(beat/2)
    wait(beat)
    intro1(volume,beat)
    playInstrument2(392,volume,.5*beat)
    playInstrument2(415.3,volume,.5*beat)
    playInstrument2(392,volume,.5*beat)
    playInstrument2(349.23,volume,.5*beat)
    playInstrument2(329.63,volume,.5*beat)
    playInstrument2(349.23,volume,.5*beat)
    playInstrument2(392,volume,.5*beat)
    playInstrument2(349.23,volume,.5*beat)
    playInstrument2(329.63,volume,.5*beat)
    playInstrument2(261.63,volume,.5*beat)
    playInstrument2(246.94,volume,.5*beat)
    playInstrument2(261.63,volume,.5*beat)
    playInstrument2(349.23,volume,2*beat)
    playInstrument2(415.3,volume,2*beat)
    playInstrument2(523.25,volume,2*beat)
    playInstrument2(698.46,volume,2*beat)
    playInstrument2(659.26,volume,5*beat)
    playInstrument2(784,volume,3*beat)
    playInstrument2(698.46,volume,2*beat)
    for i in range(2):
        playInstrument2(1046.5,volume,beat)
    playInstrument2(1046.5,volume,.5*beat)
    playInstrument2(1108.73,volume,.5*beat)
    playInstrument2(1046.5,volume,.5*beat)
    playInstrument2(932.33,volume,.5*beat)
    playInstrument2(1046.5,volume,5*beat)

#Rhythm Section
def rhythm1(volume,beat):
    #verse 1
    for i in range(4):
        for j in range(4):
            playInstrument3(349.23,volume,beat)
    for i in range(2):
        for j in range(4):
            playInstrument3(261.63,volume,beat)
    for i in range(2):
        playInstrument3(349.23,volume,beat)
    for i in range(4):
        playInstrument3(233.08,volume,beat)
    #verse 2
    for i in range(18): 
        playInstrument3(349.23,volume,beat)
    for i in range(8):
        playInstrument3(261.63,volume,beat)
    for i in range(2):
        playInstrument3(349.23,volume,beat)
    playInstrument3(261.63,volume,beat)
    playInstrument3(392,volume,beat)
    for i in range(2):
        playInstrument3(349.23,volume,beat)
    wait(2*beat)
    for i in range(16):
        playInstrument3(349.23,volume,beat)
    wait(4*beat)
    for i in range(4):
         playInstrument3(261.63,volume,beat)
    for i in range(4):
        playInstrument3(349.23,volume,beat)
    for i in range(4):
        playInstrument3(261.63,volume,beat)   
    for i in range(16):
        playInstrument3(349.23,volume,beat)
    for i in range(8):
        playInstrument3(261.63,volume,beat)
    for i in range(4):
        playInstrument3(349.63,volume,beat)
    for i in range(2):
        playInstrument3(261.63,volume,beat)

        
def playRhythm():
    rhythm1(1.0,.25)

#Dance:

#wiggle should take two beats
def wiggle(speed,beat):
    rotate(speed)
    wait(beat)
    stop()
    rotate(-speed)
    wait(beat)
    stop()

#yoyo should 2 beats
def yoyo(speed,beat):
    forward(speed,beat)
    backward(speed,beat)

def danceVerse2(speed,beat):
    wiggle(speed/2,beat*.4)
    wait(beat*1.5)
    wiggle(-speed/2,beat*.4)
    wait(3*beat)
    yoyo(speed/2,beat*.4)
    wait(beat)
    wiggle(speed/2,beat*.4)
    wait(4*beat)
    yoyo(-speed/2,beat*.4)
    wait(beat)
    wiggle(-speed/2,beat*.4)
    wait(3.5*beat)
    
def dance():
    beat = 0.25
    speed = 5.0
    #verse 1
    wait(3*beat)
    wiggle(speed,beat*.4)
    wait(6*beat)
    wiggle(-speed,beat*.4)
    wait(5*beat)
    yoyo(speed,beat*.4)
    wait(8*beat)
    yoyo(-speed,beat*.4)
    wait(2*beat)
    #verse 2
    danceVerse2(speed,beat)
    #verse 3
    wait(2*beat)
    move(speed/3,speed/4)
    wait(2*beat)
    stop()
    wait(beat)
    move(-speed/3,-speed/4)
    wait(2*beat)
    stop()
    wait(2*beat)
    move(speed/3,0)
    wait(2*beat)
    stop()
    wait(beat)
    move(speed/6,-speed/6)
    wait(2*beat)
    stop()
    move(0,speed/3)
    wait(2*beat)
    stop()
    wait(beat)
    move(-speed/6,-speed/6)
    wait(2*beat)
    stop()
    wait(beat)
    move(-speed/6,speed/6)
    wait(beat)
    stop()
    wait(2*beat)
    wiggle(speed,beat)
    wait(beat)
    wiggle(-speed,beat)
    wait(2*beat)
    #verse 4
    danceVerse2(speed,beat)

#And finally, the Main Performance:   
def istanbul():
    playIntro(.5,.25)
    wait(.1)
    doTogether(playRhythm,playMelody,dance)
    
istanbul()



