# Leila Zilles
# Assignment 6
# music.py
# to demonstrate that I understand 'doTogether'
# and it plays the first few measures of Wind Scene from Chrono Trigger!


# note frequencies from http://www.phy.mtu.edu/~suits/notefreqs.html
# looked up dictionary in Python manual because I was desperate for a 2d array
note = {'A':440.00, 'Bb':466.16, 'B':493.88, 'C':523.25, 'Db':554.37, 'D':587.33, 'Eb':622.25, 'E':659.26, 'F':698.46, 'Gb':739.99, 'G':783.99, 'Ab':830.61}

A = 440.00
B = 493.88
C = 523.25
D = 587.33
E = 659.26
F = 698.46
G = 783.99

# wind scene music from http://www.squaresound.com/arrangements/sibelius/ct/paul_ct_windscene_p.php
# notes match up with times, and this actually keeps them in order
windnotes1 = [note['D'], note['E'], note['F'], note['E'], note['C'], note['A'], note['D'], note['D'], note['C'], note['A'], note['F'] / 2.0, note['G'] / 2.0, note['A'], note['G'] / 2.0, note['E'] / 2.0, note['F'] / 2.0, note['A'], note['C'], note['E'], note['F'], note['C'], note['E'], note['F'], note['E'], note['C'], note['A'], note['D'], note['D'], note['C'], note['A'], note['F'] / 2.0, note['G'] / 2.0, note['A'], note['G'] / 2.0, note['F'] / 2.0, note['A'], note['C'], note['C'], note['E'], note['D'], note['C'], note['D'], note['D']]
windbeats1 = [1, 1, 0.5, 0.5, 0.5, 0.5, 1.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 1, 1, 3, 1, 0.5, 0.5, 0.5, 0.5, 1.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 3]
windnotes2 = [0, note['D'], note['A'], note['D'] * 2, note['Bb'], note['F'], note['Bb'] * 2, note['A'], note['A'] * 2, note['C'], note['C'] * 2, note['F'] / 2, note['F'], note['E'] / 2, note['E'], note['D'] / 2, note['A'], note['D'], note['G'] / 2, note['D'], note['G'], note['C'], note['G'], note['E'], note['F'], note['E'], note['F'], note['Bb'], note['E'], note['F'], note['F'], note['A'], note['A'], note['F'], note['C'], note['E'], note['D'], note['Bb'], note['E'], note['F'], note['F'], note['A'], note['A'], note['G'], note['A'], note['G']]
windbeats2 = [2, 0.5, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 0.5, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2]

frognotes1 = [A, G / 2.0, A, E / 2.0, A, B, C, B, A, B, G / 2.0, D / 2.0, A, G / 2.0, A, E / 2.0, A, B, C ,B, C, D, B, A, G / 2.0, A, E / 2.0, A, B, C, B, A, B, G / 2.0, D / 2.0, A, G / 2.0, A, B, C, B, A, G / 2.0, A, A, G / 2.0, A, A, E, E, E, C, D, E, A, G, E, C, A, 0, A, B,C, B,C,D,C,D,E,D,E,G,E,E,E,C,D,E,A*2,B*2,C,B,A,G,A*2,A*2,B*2,C*2,C*2,C*2,B*2,A*2,G,E,A*2,A*2,A*2,G,F,E,C*2,C*2,C*2,B*2,A*2,G,E,A*2,B*2,C*2,C*2,C*2,B*2,A*2,G,E,A*2,A*2,A*2,G,F,E,C*2,C*2,C*2,B*2,C*2,D*2,B*2,B*2,A*2]
frogbeats1 = [.33, .33, .33, 1, 1, .5, .5, .33, .33, .33, 2, 1, .33, .33, .33, 1, 1, 0.5, 0.5, .33, .33, .33, 3, .33, .33, .33, 1, 1, 0.5, 0.5, .33, .33, .33, 2, 1, .33, .33, .33, 1, 1, 1, .33, .33, .33, .33, .33, .33, 1, 0.5,0.5,.33, .33, .33, 1, 1, 2, 1.5, 0.5, 3, 1, .33, .33, .33, .33, .33, .33, .33, .33, .33, .33, .33, .33, 0.5, 0.5, .33, .33, .33, 1, 1, 1.33, .33, .33, 1, 1, 4, 2, 2, 0.5, 0.5, .33, .33, .33, 1, 1, 0.5, 0.5, .33, .33, .33, 2, 0.5, 0.5, .33, .33, .33, 1, 1, 2, 2, 0.5, 0.5, .33, .33, .33, 1, 1, 0.5, 0.5, .33, .33, .33, 2, 0.5, 0.5, .33, .33, .33, 1, 1, 2, 1]
frognotes2 = [A, C, A, C, G, B, G, B, F, A, F, A, G, B, G, B, A, C, A, C, G, B, G, B, F, A, G, B, A, C, C, 0, C, A, C, B, E, G, E, C, A, F, A, C, G, B, D, B, C, A, C, B, E, G, E, C, F, A, C, F, A, A, E, E, F, A, G, B, F, A, B, C, F, A, G, B, D, note['Gb'], E, E, F, A, G, B, F, A, F, A, A, C, F, A, G, B, A, C, A]

# function to play notes
def playTheNote(instrument, freq, beat, hard):
    instrument.setFrequency(freq)
    instrument.noteOn(hard)
    wait(beat)

# mandolin!
def playMandolin():
    tempo = 85
    m = Mandolin()
    m.connect()
    m.setGain(0.2)
    beat = 1.0 / (tempo / 60.0)
    for freq, time in zip(windnotes1, windbeats1):
        playTheNote(m, freq, beat * time, 0.2)

# moog accompaniment!
def playMoog():
    tempo = 85
    s = MoogSynthesizer()
    s.connect()
    s.setGain(0.1)
    beat = 1.0 / (tempo / 60.0)
    for freq, time in zip(windnotes2, windbeats2):
        playTheNote(s, freq / 2, beat * time, 0.5)
    s.noteOff(0.5)

def playSax():
    tempo = 126
    f = MoogSynthesizer()
    f.connect()
    f.setGain(0.3)
    beat = 1.0 / (tempo / 60.0)
    for freq, time in zip(frognotes1, frogbeats1):
        playTheNote(f, freq, beat * time, 0.2)
        f.noteOff(1.0)

def playMoog2():
    tempo = 126
    s = SineWave()
    s.connect()
    s.setGain(0.05)
    beat = 1.0 / (tempo / 60.0)
    for freq in frognotes2:
        playTheNote(s, freq / 2, beat, 0.5)
    s.noteOff(0.5)


def WindScene():
    doTogether(playMandolin, playMoog)

def FrogTheme():
    doTogether(playSax, playMoog2)
