Package braun_ :: Module mod_braun

Source Code for Module braun_.mod_braun

  1  "Braun OSC server" 
  2   
  3  from liblo import * 
  4  import threading 
  5  import Queue 
  6  import sys 
  7   
  8  from config import * 
  9   
 10  inf = 1e300000 
 11  nan = inf/inf 
 12   
 13  namespace = [""] * MAX_LINES 
 14   
 15  if nan != nan: 
16 - def is_nan(x):
17 return isinstance(x, float) and x != x
18 else:
19 - def is_nan(x):
20 return isinstance(x, float) and str(x) == str(nan)
21
22 -def is_finite(x):
23 return x not in (inf, -inf)
24
25 -class OscServer(ServerThread):
26 - def __init__(self, queue):
27 ServerThread.__init__(self, OSC_SERVER_PORT) 28 self.__queue = queue 29 self.current_method = 0 # The index of the last added method 30 self.add_method(None, None, self.message_handler)
31
32 - def init_handler(self, path, args, types, src):
33 if path not in namespace: 34 namespace[self.current_method] = path 35 print "Adding method for %s" % path 36 self.add_method(path, None, self.message_handler) 37 self.current_method = (self.current_method + 1) % MAX_LINES
38
39 - def message_handler(self, path, args, types, src):
40 if path not in namespace: 41 namespace[self.current_method] = path 42 self.current_method = (self.current_method + 1) % MAX_LINES 43 try: 44 id = namespace.index(path) 45 except ValueError: 46 # This should never happen 47 return 0 48 try: 49 val = float(args[0]) 50 except ValueError: 51 return 0 52 53 if not is_nan(val) and is_finite(val): 54 self.__queue.put({id: val}) 55 elif is_nan(val): 56 print "NaN on channel %d, value dropped" % id 57 elif not is_finite(val): 58 print "Infinite value on channel %d, value dropped" % id
59
60 -class Receiver(threading.Thread):
61 - def __init__(self, queue):
62 self.__queue = queue 63 threading.Thread.__init__(self) 64 self.maxima = [None] * MAX_LINES 65 self.minima = [None] * MAX_LINES 66 self.ranges = [0.0] * MAX_LINES 67 self.maximum = None 68 self.minimum = None 69 self.range = None 70 self._GLOBAL = 0 71 self._INDIVIDUAL = 1 72 self.scaling_mode = self._GLOBAL # The default
73
74 - def set_scaling_mode_global(self):
75 self.scaling_mode = self._GLOBAL
76
78 self.scaling_mode = self._INDIVIDUAL
79
80 - def stop(self):
81 self.__queue.put(None)
82
83 - def run(self):
84 while 1: 85 item = self.__queue.get() 86 if item is None: 87 break 88 data = item.popitem() 89 self.id = data[0] 90 self.value = data[1] 91 self.realvalue = data[1] 92 self.adapt() 93 self.scale() 94 self.dispatch()
95
96 - def dispatch(self):
97 '''Dispatch data to GUI or whatever (implemented by subclass)''' 98 pass
99
100 - def adapt(self):
101 id = self.id 102 if self.minima[id] is None: 103 self.minima[id] = self.value 104 elif self.value < self.minima[id]: 105 self.minima[id] = self.value 106 if self.maxima[id] is None: 107 self.maxima[id] = self.value 108 elif self.value > self.maxima[id]: 109 self.maxima[id] = self.value 110 if self.minimum is None: 111 self.minimum = self.value 112 elif self.value < self.minimum: 113 self.minimum = self.value 114 if self.maximum is None: 115 self.maximum = self.value 116 elif self.value > self.maximum: 117 self.maximum = self.value 118 119 self.ranges[id] = self.maxima[id] - self.minima[id] 120 self.range = self.maximum - self.minimum 121 122 GLOBAL_MINIMUM = self.minimum 123 GLOBAL_MAXIMUM = self.maximum
124
125 - def scale(self):
126 id = self.id 127 if self.scaling_mode == self._INDIVIDUAL: 128 if self.ranges[id] == 0.0: 129 self.value = 0 130 else: 131 self.value = ((self.value - self.minima[id]) /\ 132 self.ranges[id]) * SCALE_RANGE 133 elif self.scaling_mode == self._GLOBAL: 134 if self.range == 0.0: 135 self.value = 0 136 else: 137 self.value = ((self.value - self.minimum) /\ 138 self.range) * SCALE_RANGE 139 self.value = self.value + SCALE_BOTTOM
140
141 -def osc_server(queue):
142 global server #Not sure why this is the case 143 try: 144 server = OscServer(queue) 145 except ServerError, err: 146 print str(err) 147 sys.exit() 148 server.start()
149
150 -def main():
151 queue = Queue.Queue(BUFFERSIZE) 152 try: 153 server = OscServer(queue) 154 except ServerError, err: 155 print str(err) 156 sys.exit() 157 server.start() 158 r = Receiver(queue) 159 r.start() 160 a = raw_input("press enter to quit...\n") 161 if a == '': 162 Receiver(queue).stop()
163 164 if __name__ == "__main__": main() 165