Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/City/CsSched.py
blob: d3288dccf91ac60d65ed9df29ad8f81554d2af0b (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
#This python module is part of the Jam2Jam XO Activity, March, 2010
#
#Copyright (C) 2010 Thorin Kerr & Andrew Brown
#    
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or any
#later version.
#   
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import csnd, heapq
from CsHelpers import *

class Csound:
    "precompiles a csound object"
    def __init__(self):
        self.csound = csnd.CppSound()
        self.csound.setPythonMessageCallback()
        self.csound.PreCompile()
    def __repr__(self):
        return "Precompiled Csound object"


class Sched:
    def __init__(self):
        self.queue = []
    def schedEvent(self, time, func, *args):
        heapq.heappush(self.queue, (time, func, args))
    def getTime(self):
        if self:
            return self.queue[0][0]
        else:
            return False
    def getFunc(self):
        if len(self) == 0:
            return False
        else:
            return self.queue[0][1]
    def getArgs(self):
        if len(self) == 0:
            return False
        else:
            return self.queue[0][2]
    def __len__(self):
        return len(self.queue)
    def __repr__(self):
        return str(self.queue)

class CsoundChan:
    "a container for Csound channel data"
        #type is either Audio, Control, or String
        #Dirction = INput or Output
        #subType = interger, linear or exponential
        #default = default value
        #minval = suggested minimum
        #maxval == suggested maximum
    pass
    


def channels(csound):
    "returns a list of Input and output software bus channels"
    Chanlist = csnd.CsoundChannelList(csound)
    result = []
    for ndx in range(Chanlist.Count()):
        ch = CsoundChan()
        ch.name = Chanlist.Name(ndx)
        if Chanlist.IsAudioChannel(ndx):
            ch.type = "Audio"
        elif Chanlist.IsControlChannel(ndx):
            ch.type = "Control"
        elif Chanlist.IsStringChannel(ndx):
            ch.type = "String"
        else: pass
        if Chanlist.IsInputChannel(ndx) and Chanlist.IsOutputChannel(ndx):
        	ch.direction = "bidirectional"
        elif Chanlist.IsInputChannel(ndx):
        	ch.direction = "input"
        elif Chanlist.IsOutputChannel(ndx):
        	ch.direction = "output"
        else: pass 
        if Chanlist.SubType(ndx) > 0:
            tmp = ['integer', 'linear', 'exponential']
            ch.subtype = (tmp[Chanlist.SubType(ndx) - 1])
            ch.default = (Chanlist.DefaultValue(ndx))
            ch.minval = (Chanlist.MinValue(ndx))
            ch.maxval = (Chanlist.MaxValue(ndx))
        result.append(ch)
    Chanlist.Clear()
    return result



class CsoundPerformer:
    def pollScheduler(self, schedObj):
        st =  schedObj.getTime()
        if st:
            t = self.perfTime()
            if t >= st:
                obj = heapq.heappop(schedObj.queue)
                (obj[1] (*obj[2]))
    def __init__(self, schedObj, orcObj, *cs):
        "SchedObj is a Csound timer instance, orcObJ is a CsOrcConstructor Object"
        self.Timer = csnd.CsoundTimer()
        self.schedObj = schedObj
        self.orcObj = orcObj
        if len(cs) == 0:
            cs = Csound()
            self.csound = Csound.csound
        else: self.csound = cs[0] 
        self.csound.setOrchestra(orcObj.exportOrc())
        self.csound.setScore(orcObj.sco)
        self.time = 0
        if platform == "Sugar":
            self.csound.setCommand("csound -b256 -B2048 -odac --expression-opt --sched=1 -d -m0 /tmp/tmp.orc /tmp/tmp.sco")
        else:
            self.csound.setCommand("csound -b256 -B2048 -odac --expression-opt -d -m0 /tmp/tmp.orc /tmp/tmp.sco")
        self.csound.exportForPerformance()
        self.csound.compile()
        self.Channels = channels(self.csound)
        self.perf = csnd.CsoundPerformanceThread(self.csound) 
        self.perf.Play()
        self.perf.SetProcessCallback(self.pollScheduler, schedObj)
    def perfTime(self):
        return self.Timer.GetRealTime()
    def Stop(self):
        self.perf.Stop()
        #self.perf.Join()
        self.csound.Cleanup()
    def setChannelValue(self, channame, value):
        self.csound.SetChannel(channame, value)
    def getChannelValue(self, channame):
        return self.csound.GetChannel(channame)
    def getChannelList(self):
        return csnd.CsoundChannelList(self.csound)
    def getChannelNames(self):
        chlst = self.getChannelList()
        return [chlst.Name(ch) for ch in range(chlst.Count())]
    def playParams(self, ins, start, dur, *params):
        "send score message to Csound with parameter values"
        if start < 0: start = 0
        s = ' '.join(map(str, (['i', ins, start, dur] + [str(n) for n in params])))
        self.perf.InputMessage(s)
    def playNote(self, ins, note):
        "send score message to Csound using a note object"
        start = note[0]
        dur = note[1]
        params = note[2:]
        self.perf.InputMessage(' '.join(['i', str(ins),str(start),str(dur)]+[str(n) for n in params]))