Package control :: Module eventCaller
[hide private]
[frames] | no frames]

Source Code for Module control.eventCaller

  1  #!/usr/bin/python 
  2  # -*- coding: latin-1 -*- 
  3   
  4  """  
  5  Handles all communication between different parts of the barebones system. 
  6   
  7  @author: Øyvind Brandtsegg 
  8  @contact: obrandts@gmail.com 
  9  @license: GPL 
 10  @requires: Pyro 
 11  """ 
 12   
 13  import time 
 14  import sys 
 15  import threading 
 16  import control.theTime 
 17  import comp.randMelody  
 18  import comp.serialMelody  
 19  from constants import * 
 20   
21 -class EventCaller:
22 """ 23 The central control and communitation module 24 25 All events from the user interface(s) are sent here. 26 All events from the timed queue are processed here. EventCaller also communicates with the compositional logic, 27 and with Csound through cs.messages. 28 """ 29
30 - def __init__(self):
31 """ 32 ## Class constructor. 33 # 34 # @param self: The object pointer. 35 """ 36 37 self.csMessages = '' 38 """Pointer to csMessages instance.""" 39 40 self.rMelody1 = comp.randMelody.RandMelody(self) 41 """Instance of the RandMelody composition class.""" 42 self.rMelody2 = comp.randMelody.RandMelody(self) 43 """Instance of the RandMelody composition class.""" 44 45 self.sMelody1 = comp.serialMelody.SerialMelody(self) 46 """Instance of the SerialMelody composition class.""" 47 48 self.theTimeSeconds = control.theTime.TheTime(self, 60) 49 """Instance of a relaxed timed queue used for slow automation (seconds, minutes, hours).""" 50 self.theTime = self.theTimeSeconds#'' 51 """Pointer to a precise timed queue, clock slaved to Csound control rate."""
52 53 #################################################################################################################### 54 ### Initialization methods 55 #################################################################################################################### 56
57 - def initValues(self):
58 """ 59 Initialize the system. 60 61 This includes setting various initial values and instantiating csound instruments as needed for normal operation. 62 63 @param self: The object pointer. 64 """ 65 # start always on instruments in Csound 66 scoreLength = 600000 # 67 self.csMessages.csoundInputMessage('i 98 0 %i'%scoreLength) # reverb instr 68 self.csMessages.csoundInputMessage('i 99 0 %i'%scoreLength) # master audio out instr
69 70 #################################################################################################################### 71 ### Composition process methods 72 #################################################################################################################### 73
74 - def perform(self, module, state):
75 """ 76 Wrapper for (any) composition module's perform method. 77 78 @param self: The object pointer. 79 @param module: The composition module to perform. 80 """ 81 func = 'self.%s.perform(%i)'%(module, state) 82 c = compile(func, 'string', 'exec') 83 exec(c)
84
85 - def setParameter(self, module, parameter, value):
86 """ 87 Set a parameter for a composition module. 88 89 @param self: The object pointer. 90 @param module: The composition module to set a parameter value for. 91 @param parameter: The parameter name. 92 @param value: The value to set the parameter to. 93 """ 94 print 'setParameter', module, parameter, value 95 func = 'self.%s.setParameter(%s, %s)'%(module, parameter, value) 96 c = compile(func, 'string', 'exec') 97 exec(c)
98 99 #################################################################################################################### 100 ### Timed Queue related methods 101 #################################################################################################################### 102
103 - def parseEvent(self, event):
104 """ 105 Parsing of events output from queue, called from theTime. 106 107 @param self: The object pointer. 108 @param event: The event to be parsed. 109 """ 110 #print 'parseEvent', event 111 beat = event[0] 112 try: 113 event[1][0]() 114 except: 115 print '*** WARNING ***, timed event not recognized by eventCaller.parseEvent' 116 print '...or, maybe your composition module contains a bug preventing it from being run correctly: see below...' 117 print event 118 print 'exception', sys.exc_info()
119 #note: may have parsing of other event types here (non-code-objects) 120 121
122 - def startStopClock(self, state, offset=1):
123 """ 124 Start or stop the timed queue clock 125 126 @param self: The object pointer. 127 @param state: The state (1 or 0) for the clock. Clock runs while state is 1, pause when state is 0. 128 """ 129 self.theTime.startStopClock(state, offset)
130 131 #################################################################################################################### 132 ### Various 'System' methods 133 #################################################################################################################### 134
135 - def setTimeBpm(self, bpm):
136 """ 137 Set tempo in bpm for the variable-tempo timed queue. 138 139 @param self: The object pointer. 140 @param bpm: The tempo in bpm. 141 """ 142 print 'theTime tempo set to ', bpm 143 self.theTime.setBpm(bpm)
144
145 - def startThreads(self):
146 """ 147 Start timed queue (sequencer) threads. 148 149 @param self: The object pointer. 150 """ 151 self.theTimeSeconds.start() 152 self.theTimeSeconds.startStopClock(1)
153
154 - def stopThreads(self):
155 """ 156 Stop timed queue (sequencer) threads. 157 158 @param self: The object pointer. 159 """ 160 self.theTimeSeconds.stop()
161
162 - def setPointers(self, csMessages, theTime):
163 """ 164 Set pointers to other modules in the system. 165 166 @param self: The object pointer. 167 @param csMessages: Pointer to the csMessages object. 168 @param theTime: Pointer to the theTime object 169 """ 170 self.csMessages = csMessages 171 self.theTime = theTime
172
173 - def recordAudio(self, state, name="demofile.wav"):
174 """ 175 Make an audio recording of a realtime session. 176 177 @param self: The object pointer. 178 @param state: START to start recording, STOP to stop. 179 @param name: The file name to record audio to. 180 """ 181 if name.find('.wav') < 0: 182 print 'filename must end with .wav, record aborted' 183 return 184 if state == START: 185 self.csMessages.csoundInputMessage('i 100 0 -1 "%s"'%name) 186 else: 187 self.csMessages.csoundInputMessage('i -100 0 1') 188 self.csMessages.csoundInputMessage('i 101 0 1')
189