diff options
Diffstat (limited to 'CsoundXOAgent.py')
-rw-r--r-- | CsoundXOAgent.py | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/CsoundXOAgent.py b/CsoundXOAgent.py new file mode 100644 index 0000000..e5fca9d --- /dev/null +++ b/CsoundXOAgent.py @@ -0,0 +1,140 @@ +import os +import subprocess +import CsoundAPI + +from time import sleep + +class CsoundXOAgent(): + + def __init__(self, host, rand, callback, platform): + self.random = rand + self.host = host + self.port = 7000 + self.callback = callback + self.platform = platform + + self.initialize(host, self.port, callback, platform) + + def initialize(self, host, port, callback, platform): + self.status = 0 + + if not self.launchServer(port): + print "Error in launching server." + self.status = -1 + return + + if not self.loadOrc(): + print "Error in reading orchestra or score file." + self.status = -2 + return + + if not self.connectCsound(port): + print "Error in connecting to csound server." + self.status = -3 + return + + if not self.runCsound(callback): + print "Error in setting up csound server." + self.status = -4 + return + + self.status = 1 + + def recover(self): + print "CsoundXOAgent::recover()" + if self.status != -1: + try: + self.server.kill() + except: + print "cannot kill server" + self.port = self.random.randint(7000, 10000) + self.initialize(self.host, self.port, self.callback, self.platform) + return (self.status == 1) + + def launchServer(self, port): + print self.platform + try: + if self.platform == "sugar-xo" or self.platform == "linux": + self.server = subprocess.Popen(['./csoundxo',str(port)]) + # self.server = subprocess.Popen('strace -o trace'+str(port)+'.txt ./csoundxo ' + str(port), shell=True) + else: # default - windows + self.server = subprocess.Popen("csnd.exe " + str(port)) + except: + return False + return True + + def deactivate(self): + self.api.sendDeactivate() + + def reactivate(self): + self.api.sendReactivate() + + def connectCsound(self, port): + try: + sleep(1) + print "connecting to Server at port " + str(port) + self.connect(self.host, port) + except: + return False + return True + + def runCsound(self, callback): + self.setup(callback) + self.perform() + return True + + def loadOrc(self): + try: + forc = open("mpainter.orc","r") + fsco = open("mpainter.sco","r") + self.orchestra = forc.read() + self.score = fsco.read() + forc.close() + fsco.close() + except: + return False + return True + + def connect(self, address, port): + self.api = CsoundAPI.CsoundAPI(address, port, True, 5, self.recover) + sleep(1) + + def setup(self, callback): + self.api.newInstance() + self.api.sendOrcStr(self.orchestra) + self.api.sendScoStr(self.score) + self.api.setDisplays(0) + self.api.setMsgLevel(3) + self.api.setIOBufFrames(1024) + self.api.setIntAudio() + self.api.sendPrep() + self.api.setP2Timer(0.06, callback) +# self.api.enterBeatInputMode() + + def perform(self): + self.api.sendPlay() + + def sendClearLines(self): + if self.status == 1: + print "Clearing existing csound events..." + self.api.sendClearLines() + + def sendLinevt(self, eventStr): + if self.status == 1: + self.api.sendLinevt(eventStr) + + def current_kperiod(self): + if self.status == 1: + return self.api.current_kperiod() + else: + return -1 + + def close(self): + print "to Close Csoundxo" + if self.status == 1: + self.api.mpClose() + try: + self.server.kill() + except: + print "cannot kill server" + self.status = 0 |