import csnd6 import time import os import sys import socket import threading class audioengine(csnd6.CppSound): def __init__(self, cbport): csnd6.CppSound.__init__(self) self.flag = 0 self.inputflag = 1 self.cbport = int(cbport) self.cbsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: self.cbsock.connect(('127.0.0.1', self.cbport)) except: print "Unable to connect audio engine to main program" return self.inputloopthread = threading.Thread(target=self.inputloop) self.inputloopthread.start() def inputloop(self): string = '' try: while self.inputflag ==1: print "inputting..." string += self.cbsock.recv(64) while string.count('RATENDMESSAGE'): print "received..." cmd, string = string.split('RATENDMESSAGE', 1) self.cmddelegate(cmd) except: print "Socket problems in audio engine, line 83." print "............................................" print "input loop ending" def scoreloc(self): self.flag = 1 try: while self.flag: if self.getIsPerforming(): # if self.perf.isRunning(): self.cmddelegate('csdstp') self.flag = 0 try: self.cbsock.sendall('ENDCB') except: print "............................................" pass else: time.sleep(.125) loc = self.GetChannel("rattime") ## prevent score cursor from being reset to beginning when stopped: if loc: self.cbsock.sendall('%fCB' % loc) except: print "Socket problems in audio engine, line 110." print "............................................" print "scoreloc thread ending" def cmddelegate(self, cmd): if cmd.startswith('csdopt:'): for word in cmd[14:].split(): self.SetOption(word) print "opt" print cmd # self.setCommand(cmd[7:]) elif cmd.startswith('csdorc:'): self.CompileOrc(cmd[7:]) print "orc" print cmd # self.setOrchestra(cmd[7:]) elif cmd.startswith('csdsco:'): self.ReadScore(cmd[7:]) print "sco" print cmd # self.setScore(cmd[7:]) elif cmd.startswith('csdadd:'): try: self.perf.InputMessage(cmd[7:]) except: print "No Performance Thread" elif cmd.startswith('csdgoz'): self.Start() self.scorelocthread = threading.Thread(target=self.scoreloc) self.scorelocthread.start() while (self.PerformKsmps() == 0): # print 'flag = %d' % self.flag pass self.Perform() # self.exportForPerformance() # self.compile() # if self.getIsGo(): # self.perf = csnd6.CsoundPerformanceThread(self) # self.scorelocthread = threading.Thread(target=self.scoreloc) ## self.scorelocthread.setDaemon(1) # self.scorelocthread.start() # self.perf.Play() # else: # print "Csound Error" # try: # self.cbsock.sendall('ENDCB') # except: # print "No end!" # print "............................................" print "Went" elif cmd.startswith('csdstp'): self.flag = 0 self.inputflag = 0 ## try: # self.cbsock.sendall('ENDCB') # self.scorelocthread.join() try: self.scorelocthread.join() self.Stop() # self.perf.Stop() # self.perf.Join() print "Stopped" # self.cleanup() # print "Cleaned Up" except: print "Stop/Cleanup Failed" elif cmd.startswith('csdcln'): try: print threading.enumerate() print "Csound Cleaned Up" except: print "Unable to Cleanup Csound Instance" def callable(self, dummy): print "::::::::::callable::::::::::" ae = audioengine(sys.argv[1])