Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/CsoundXOAgent.py
blob: e5fca9dea4fec5bd30dcf7c84f723ac7728b1185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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