1
2
3
4 """
5 The module for (accurate) timed automation.
6
7 Inherits thread safe queue handling from theTime.py, the relaxed timer.
8 This timer module is coupled to the csound control rate to provide more accurate timing than what is provided within standard python.
9
10 @author: Øyvind Brandtsegg
11 @contact: obrandts@gmail.com
12 @license: GPL
13 """
14
15 import control.theTime
16 import time
17 import bisect
18 import mutex
19 import copy
20
22 """
23 The class containing accurate timer and timed queue methods.
24
25 Thread safe queue handling is inherited from theTime.py.
26 Automation is executed via a queue of events to be executed in time, format for the queue is a list of events,
27 with each event represented as a list with the following format: [type, time, data].
28 The type field is a string, to be parsed in eventCaller.parseEvents(). Time is in beats and may be fractional.
29 The data field may contain several parameters (not as sublist).
30 """
31
32 - def __init__(self, eventCaller, bpm=60):
33 """
34 The class constructor.
35
36 @param self: The object pointer.
37 @param eventCaller: A pointer to the eventCaller object.
38 @param bpm: The beats per minute for the beat counter. Default = 60.
39 """
40 self.queueMutex = mutex.mutex()
41 """The mutex for thread safe handling of the queue list."""
42 self.queueMutex.testandset()
43 self.eventCaller = eventCaller
44
45 self.queue = []
46 """The list of events in the timed queue."""
47 self.beatCounter = 0
48 """Counter for quarter notes at a given bpm."""
49 self.prevBeat = 0
50 self.fractionalBeat = 0.0
51 """Counter for fractions of a beat."""
52 self.bpm = bpm
53 """The tempo in beats per minute for the beat counter."""
54 self.runClock = 0
55 """Flag to run or pause the clock."""
56 self.csoundKr = 441
57 """Csound control rate, value will be set by csound at (csound) init."""
58 self.timePerKperiod = 1.0/self.csoundKr
59 self.beatIncrement = (self.bpm/60.0)*self.timePerKperiod
60
62 """
63 The main clock increment method, called from csound every ksmps period, also polling the event queue.
64
65 @param self: The object pointer.
66 """
67 nextBeatTime = 0
68 if self.runClock == 1:
69
70
71
72 while self.queueMutex.test():
73 self.queueMutex.unlock()
74
75 self.fractionalBeat += self.beatIncrement
76 if self.fractionalBeat >= 1.0:
77 self.beatCounter += 1
78
79 self.fractionalBeat = 0
80
81
82 if self.queue != []:
83 self.checkQueue(self.beatCounter+self.fractionalBeat)
84
86 """
87 Set the clock tempo in beats per minute.
88
89 @param self: The object pointer.
90 """
91 self.bpm = bpm
92 self.beatIncrement = (self.bpm/60.0)*self.timePerKperiod
93
95 """
96 Set the time per control rate tick from csound.
97
98 @param self: The object pointer.
99 @param kr: The csound control rate
100 """
101 print 'setTimePerKperiod', kr
102 self.csoundKr = kr
103 self.timePerKperiod = 1.0/kr
104 self.beatIncrement = (self.bpm/60.0)*self.timePerKperiod
105