from Csound import *
from CsoundServer import *

orchestra = """
sr      =        44100
kr      =        441
ksmps   =        100
nchnls  =        2
;=========================================================================
gar     init     0
;=========================================================================
; TURN ON GLOBAL INSTRUMENTS ---------------------------------------------
;=========================================================================
turnon  129             ; REVERB
;=========================================================================
; LOAD INSTRUMENT AIFFS --------------------------------------------------
;=========================================================================
giclarinet   ftgen 1, 0, 0, 1,   "clarinetC5.aif", 0, 0, 0
giguitar     ftgen 2, 0, 0, 1,   "guitarB3.aif",   0, 0, 0
giorgan      ftgen 3, 0, 0, 1,   "organE3.aif",    0, 0, 0
gipiano      ftgen 4, 0, 0, 1,   "pianoF3.aif",    0, 0, 0
girhodes     ftgen 5, 0, 0, 1,   "rhodesC4.aif",   0, 0, 0
givibe       ftgen 6, 0, 0, 1,   "vibeA4.aif",     0, 0, 0
giviolin     ftgen 7, 0, 0, 1,   "violinC5.aif",   0, 0, 0
givoice      ftgen 8, 0, 0, 1,   "voiceC4.aif",    0, 0, 0
;=========================================================================
; INSTRUMENTS ------------------------------------------------------------
;=========================================================================
instr 1
	ifrq	=	cpspch(p5)
        kenv    linen   4000, .02, p3, .08
        asig    loscil	kenv, ifrq, giclarinet, 523.251 
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 2
	ifrq	=	cpspch(p5)
        kenv    linen   7000, .02, p3, .08
        asig    loscil  kenv, ifrq, giguitar, 246.942        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 3
	ifrq	=	cpspch(p5)
        kenv    linen   4000, .02, p3, .08
        asig    loscil  kenv, ifrq, giorgan, 164.814        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 4
	ifrq	=	cpspch(p5)
        kenv    linen   5000, .02, p3, .08
        asig    loscil  kenv, ifrq, gipiano, 174.614        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 5
	ifrq	=	cpspch(p5)
        kenv    linen   6000, .02, p3, .08
        asig    loscil  kenv, ifrq, girhodes, 261.626        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 6
	ifrq	=	cpspch(p5)
        kenv    linen   7000, .02, p3, .08
        asig    loscil  kenv, ifrq, givibe, 440.00        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 7
	ifrq	=	cpspch(p5)
        kenv    linen   3000, .02, p3, .08
        asig    loscil  kenv, ifrq, giviolin, 523.251       
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 8
	ifrq	=	cpspch(p5)
        kenv    linen   5000, .02, p3, .08
        asig    loscil  kenv, ifrq, givoice, 261.626      
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 51
	ifrq	=	cpspch(p5)
        kenv    linenr  4000, 0.01, 2, 0.01
        asig    loscil 	kenv, ifrq, giclarinet, 523.251 
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 52
	ifrq	=	cpspch(p5)
        kenv    linenr  7000, 0.01, 2, 0.01
        asig    loscil  kenv, ifrq, giguitar, 246.942        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 53
	ifrq	=	cpspch(p5)
        kenv    linenr  4000, 0.01, 2, 0.01
        asig    loscil  kenv, ifrq, giorgan, 164.814        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 54
	ifrq	=	cpspch(p5)
        kenv    linenr  5000, 0.01, 2, 0.01
        asig    loscil  kenv, ifrq, gipiano, 174.614        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 55
	ifrq	=	cpspch(p5)
        kenv    linenr  6000, 0.01, 2, 0.01
        asig    loscil  kenv, ifrq, girhodes, 261.626        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 56
	ifrq	=	cpspch(p5)
        kenv    linenr  7000, 0.01, 2, 0.01
        asig    loscil  kenv, ifrq, givibe, 440.00        
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 57
	ifrq	=	cpspch(p5)
        kenv    linenr  3000, 0.01, 2, 0.01
        asig    loscil  kenv, ifrq, giviolin, 523.251       
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin
instr 58
	ifrq	=	cpspch(p5)
        kenv    linenr  5000, 0.01, 2, 0.01
        asig    loscil  kenv, ifrq, givoice, 261.626      
                outs 	asig, asig
        gar	=	gar + (asig * 0.125)
endin;=========================================================================
; GLOBAL REVERB INSTRUMENT -----------------------------------------------
;=========================================================================
        instr    129
asig    reverb2  gar, 3.6, 0.25
        outs   asig, asig
gar   =        0
        endin
"""

score = """
;i129 0 1200
f0 1200
"""

class CsoundAgent:
#    def __init__(self):
#        pass

#    def notificationTime(self, message):
#        print "Custom Time Notification: "+ str(message[2])
    def csoundMessages(self, message):
        message.display("Csound:")

    def serverMessages(self, line, userData = None):
        print "Server: ", line,
    
    def setup(self):
#        global orchestra, simplescore
        
        self.server = CsoundServer()
        self.server.setMessageCallback(self.serverMessages)
        self.server.launch()

        # ask the server to create an instance of Csound
        self.csound = Csound("OrcScoString")
        self.csound.setMessageCallback(self.csoundMessages)
        self.csound.setupOrchestraAsString(orchestra)
        self.csound.setupScoreAsString(score)

        # this will compile the orchestra and sort the score
        self.csound.initialize()

    def perform(self):
	# begin the performance!
	self.csound.perform()

	# wait for it to finish performing
	#self.csound.waitForPerformanceToFinish()

	#csound.shutdownServer()
        ##self.play()
