#!/usr/bin/env python2.4
import pygst
pygst.require("0.9")
import gst
import time
import sys

tempo = 180
scale = { "A3" : 220, "B3" : 246.94, 
	"C3" : 261.62, "D3" : 293.66, "E3" : 329.62, 
	"F3" : 349.23, "G3" : 391.99, "A4" : 440, 
	"B4" : 493.88, "C4" : 523.25, "D4" : 587.33,
	"E4" : 659.25, "F4" : 698.46, "G4" : 783.99, 
	"A5" : 880, "B5" : 987.77, "C5" : 1046.5,
	"D5" : 1174.66, "E5" : 1318.51, "F5" : 1396.91, 
	"G5" : 1567.98 }

# CCGGAAG FFEEDDC GGFFEED GGFFEED CCGGAAG FFEEDDC
tune = ( ( 2, "C3", 1, 0.25 ), ( 2, "G3", 1, 0.25 ), 
	 ( 2, "A4", 1, 0.25 ), ( 1, "G3", 1, 0.25 ), 
	 ( 1, "", 1.25 ), 
	 ( 2, "F3", 1, 0.25 ), ( 2, "E3", 1, 0.25 ), 
	 ( 2, "D3", 1, 0.25 ), ( 1, "C3", 1, 0.25 ),
	 ( 1, "", 1.25 ), 
	 ( 2, "G3", 1, 0.25 ), ( 2, "F3", 1, 0.25 ), 
	 ( 2, "E3", 1, 0.25 ), ( 1, "D3", 1, 0.25 ),
	 ( 1, "", 1.25 ), 
	 ( 2, "G3", 1, 0.25 ), ( 2, "F3", 1, 0.25 ), 
	 ( 2, "E3", 1, 0.25 ), ( 1, "D3", 1, 0.25 ),
	 ( 1, "", 1.25 ), 
	 ( 2, "C3", 1, 0.25 ), ( 2, "G3", 1, 0.25 ), 
	 ( 2, "A4", 1, 0.25 ), ( 1, "G3", 1, 0.25 ),
	 ( 1, "", 1.25 ), 
	 ( 2, "F3", 1, 0.25 ), ( 2, "E3", 1, 0.25 ), 
	 ( 2, "D3", 1, 0.25 ), ( 1, "C3", 1, 0.25 )
    )

p = gst.parse_launch(""" sinesrc name=s ! audio/x-raw-int,rate=44100 ! adder name=add ! alsasink 
    sinesrc name=s2 ! add.  sinesrc name=s3 ! add. """)
s = ( p.get_by_name ("s"), p.get_by_name ("s2"), p.get_by_name ("s3") )

tick = 60.0 / tempo
for i in s:
    i.set_property ("freq", 0)

p.set_state (gst.STATE_PLAYING)
time.sleep (0.5)
for note in tune:
    for rep in range (note[0]):
	if note[1] != "":
	    s[0].set_property ("freq", scale [note[1]])
	    s[1].set_property ("freq", scale [note[1]] + 5)
	    s[2].set_property ("freq", 2 * scale [note[1]])
	    sys.stdout.write ("%s " % (note[1]))
	    sys.stdout.flush()
	    time.sleep (note[2] * tick)

	    if note[3] != 0.0:
		s[0].set_property ("freq", 0)
		s[1].set_property ("freq", 0)
		s[2].set_property ("freq", 0)
		time.sleep (note[3] * tick)
	else:
	    sys.stdout.write ("REST ")
	    sys.stdout.flush()
	    s[0].set_property ("freq", 0)
	    s[1].set_property ("freq", 0)
	    s[2].set_property ("freq", 0)
	    time.sleep (note[2] * tick)

print "DONE"
time.sleep (1)
p.set_state (gst.STATE_READY)
