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:
17 return isinstance(x, float) and x != x
18 else:
20 return isinstance(x, float) and str(x) == str(nan)
21
23 return x not in (inf, -inf)
24
31
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
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
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
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
73
75 self.scaling_mode = self._GLOBAL
76
78 self.scaling_mode = self._INDIVIDUAL
79
81 self.__queue.put(None)
82
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
97 '''Dispatch data to GUI or whatever (implemented by subclass)'''
98 pass
99
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
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
149
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