-
Notifications
You must be signed in to change notification settings - Fork 0
/
ios_stream_viewer.py
360 lines (288 loc) · 11.2 KB
/
ios_stream_viewer.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from scene import *
from random import randint
from threading import Thread, Lock
from collections import deque
from pythonosc import dispatcher, osc_server
import ui
import time, socket
import random, math, itertools
PACKET_SAMPLE_SIZE = 200
DEVICE_IDS = ('e4f7','cec8','6b37','3fa5')
class Series(ShapeNode):
def __init__(self, name, bsize, line_color, color, *args, **kwargs):
self.bsize = bsize
border = ui.Path.rect(0,0, self.bsize.w, self.bsize.h)
ShapeNode.__init__(self, border, *args, **kwargs)
self.lines = deque()
self.buffer = [0]
self.line_color = line_color
self.color = color
self.bufferLength = 201
# label channel
self.label = LabelNode(name, position=(self.bsize.w - 2 , self.bsize.h),
font = ('Helvetica', 12), color = 'black', parent = self, anchor_point = (1,1))
# grid
self.grids = [
ShapeNode(ui.Path.rect(0, 0, self.bsize.w, self.bsize.h/2),
stroke_color = 'black', fill_color='clear',
parent = self, anchor_point = (0,0)),
ShapeNode(ui.Path.rect(0, 0, self.bsize.w, self.bsize.h/2),
stroke_color = 'black', fill_color='clear',
position=(0, self.bsize.h/2),
parent = self, anchor_point = (0,0))
]
def trim(self, y):
if y <= -self.bsize.h / 2 + 2:
return -self.bsize.h / 2 + 2
if y >= self.bsize.h / 2 :
return self.bsize.h / 2
return y
def update(self):
while len(self.buffer) > self.bufferLength:
data = self.buffer[:self.bufferLength]
del self.buffer[:self.bufferLength - 1]
if len(self.lines) == 0:
last_line_x_end = self.bsize.w + self.bufferLength * self.scene.timeScale
else:
last_line_x_end = self.lines[-1].position.x + self.bufferLength * self.scene.timeScale
# drop the oldest line & update valScale
if self.lines[0].position.x < 0:
first_line = self.lines.popleft()
first_line.remove_from_parent()
### append a new line
path = ui.Path()
# draw upper bound
path.move_to(0, -self.bsize.h / 2 - 4)
path.line_to(self.bufferLength * self.scene.timeScale, -self.bsize.h / 2 - 4)
# draw waveform
if data[0]:
y0 = self.trim(data[0] * self.scene.valueScale * self.bsize.h)
path.move_to(0, y0)
isLastNone = False
else:
isLastNone = True
for x,y in enumerate(data):
if y:
y = self.trim(y * self.scene.valueScale * self.bsize.h)
if isLastNone:
path.move_to(x * self.scene.timeScale, y)
else:
path.line_to(x * self.scene.timeScale, y)
isLastNone = False
else:
isLastNone = True
path.line_width = 1
new_line = ShapeNode(path,
parent=self,
stroke_color= self.line_color,
fill_color='clear',
position=(last_line_x_end - 1 * self.scene.timeScale, self.bsize.h + 6),
anchor_point=(1,1)
)
self.lines.append(new_line)
if len(self.lines) > 0:
return(self.bsize.w - self.lines[-1].position.x)
else:
return(0)
class Viewer(Scene):
def __init__(self, device_ids, nChannel, server, lock, *args, **kwargs):
Scene.__init__(self, *args, **kwargs)
self.server = server
self.device_ids = device_ids
self.nChannel = nChannel
self.lock = lock
self.sampleCounters = dict(zip(device_ids, [0]*len(device_ids)))
self.prevSampleIndex = dict(zip(device_ids, [0]*len(device_ids)))
self.devices = dict(zip(device_ids, [None]*len(device_ids)))
self.masks = dict(zip(device_ids, [None]*len(device_ids)))
self.deviceLabels = dict(zip(device_ids, [None]*len(device_ids)))
self.deviceStatusLabels = dict(zip(device_ids, [None]*len(device_ids)))
self.prevTouch = None
self.isRunning = True
self.prevSampleSecond = self.t
self.timeScale = .5
self.valueScale = -300
self.runningSamples = dict(zip(device_ids, [deque([],10) for i in device_ids]))
def touch_began(self, touch):
self.isRunning = False
def touch_ended(self, touch):
self.isRunning = True
def did_change_size(self):
screen_size = get_screen_size()
for i, id in enumerate(self.devices):
if screen_size.w > screen_size.h:
mask_size = Size(screen_size.w / len(self.devices) + 1, screen_size.h)
mask_position = Point(i * screen_size.w / len(self.devices), 0)
self.deviceLabels[id].position = (mask_position.x + 4, mask_size.h - 1)
self.deviceStatusLabels[id].position = (mask_position.x + 2, 2)
else:
mask_size = Size(screen_size.w , screen_size.h / len(self.devices) + 1)
mask_position = Point(0, i * screen_size.h / len(self.devices))
self.deviceLabels[id].position = (mask_position.x + 4, (i+1)*mask_size.h - 1)
self.deviceStatusLabels[id].position =(mask_position.x + 2, i*mask_size.h + 2)
# update mask
self.masks[id].size = mask_size
self.masks[id].crop_rect = Rect(mask_position.x, mask_position.y,
mask_size.w+1, mask_size.h)
# update series
for j, series in enumerate(self.devices[id]) :
if screen_size.w > screen_size.h:
series_size = Size(screen_size.w / len(self.devices) - 1,
screen_size.h / self.nChannel - 1)
series_pos = Point(i * screen_size.w / len(self.devices),
j * screen_size.h / self.nChannel)
else:
series_size = Size(screen_size.w ,
screen_size.h / ( self.nChannel * len(self.devices)) - 1)
series_pos = Point(0,
i * screen_size.h / len(self.devices) + j * screen_size.h / (self.nChannel * len(self.devices))
)
series.bsize = series_size
series.maxLines = series.bsize.w / series.bufferLength / series.scene.timeScale
series.position = series_pos
series.path = ui.Path.rect(0,0, series_size.w, series_size.h)
series.grids[0].path = ui.Path.rect(0, 0, series_size.w, series_size.h/2)
series.grids[1].path = ui.Path.rect(0, 0, series_size.w, series_size.h/2)
series.grids[1].position = (0, series_size.h/2)
series.label.position = (series.bsize.w - 2 , series.bsize.h)
for line in series.lines:
line.remove_from_parent()
series.lines.clear()
def setup(self):
fill_colors = ('grey','darkgray','grey','darkgray')
line_colors = ('lightgreen','lightblue','lightpink','lightyellow')
colors = ('darkgreen', 'darkblue', 'darkred', 'darkorange')
screen_size = get_screen_size()
for i, id in enumerate(self.device_ids):
# Mask Node (Device Window)
mask = EffectNode(parent = self)
label = LabelNode('Device {}'.format(id),
font = ('Helvetica', 12), color = 'black',
anchor_point=(0,1), z_position = 2, parent= mask
)
status_label = LabelNode('', font = ('Helvetica', 12),
anchor_point=(0,0), z_position = 2, parent=mask
)
if screen_size.w > screen_size.h:
mask_size = Size(screen_size.w / len(self.devices) + 1, screen_size.h)
mask_position = Point(i * screen_size.w / len(self.devices) , 0)
label.position= (mask_position.x + 4, mask_size.h - 1)
status_label.position=(mask_position.x + 2, 2)
else:
mask_size = Size(screen_size.w , screen_size.h / len(self.devices) + 1)
mask_position = Point(0, i * screen_size.h / len(self.devices) )
label.position= (mask_position.x + 4, (i+1)*mask_size.h - 1)
status_label.position=(mask_position.x + 2, i*mask_size.h + 2)
mask.crop_rect = Rect(mask_position.x, mask_position.y,
mask_size.w+1, mask_size.h)
self.masks[id] = mask
self.deviceLabels[id] = label
self.devices[id] = list()
self.deviceStatusLabels[id] = status_label
for j in range(self.nChannel):
if screen_size.w > screen_size.h:
series_size = Size(screen_size.w / len(self.devices) - 1,
screen_size.h / self.nChannel - 1)
series_pos = Point(i * screen_size.w / len(self.devices) ,
j * screen_size.h / self.nChannel)
else:
series_size = Size(screen_size.w ,
screen_size.h / ( self.nChannel * len(self.devices) ) - 1)
series_pos = Point(0, i * screen_size.h / len(self.devices) +
j * screen_size.h / (self.nChannel * len(self.devices) ))
series = Series(
name = 'Channel {}'.format(j+1),
bsize=series_size,
position=series_pos,
anchor_point=(0,0),
line_color = 'white', #line_colors[j],
color = 'white', #colors[j],
stroke_color = 'clear',
fill_color = fill_colors[i],
z_position=0,
parent=mask)
self.devices[id].append(series)
def update(self):
duration = self.t - self.prevSampleSecond
if duration >= 1:
for i, id in enumerate(self.devices):
self.lock.acquire()
sampleCount = self.sampleCounters[id]
for series in self.devices[id]:
deltaX = series.update()
move_by = Action.move_by(deltaX, 0, duration)
for line in series.lines:
line.run_action(move_by)
self.lock.release()
self.runningSamples[id].append(sampleCount)
averageSampleRate = sum(self.runningSamples[id])/len(self.runningSamples[id])
self.deviceStatusLabels[id].text = '{:.0f}Hz {:.0f}Hz(10s)'.format(sampleCount, averageSampleRate)
self.lock.acquire()
self.sampleCounters[id] = 0
self.lock.release()
self.prevSampleSecond = self.t
def stop(self):
self.server.shutdown()
self.server.server_close()
print('server shuntdown.')
def raw_osc_handler(unused_addr, *args):
global viewer
global lock
id = args[0][0] # device index
msg = args[1:]
# print(msg[0])
try:
device = viewer.devices[id]
assert len(msg) == 5, 'wrong message length: {}'.format(msg)
dropCount = 0
sampleIndex = msg[0]
sample = msg[1:]
if sampleIndex - viewer.prevSampleIndex[id] != 1:
if sampleIndex != 0:
if sampleIndex < viewer.prevSampleIndex[id]:
dropCount = sampleIndex + 200 - viewer.prevSampleIndex[id]
else:
dropCount = sampleIndex - viewer.prevSampleIndex[id]
viewer.prevSampleIndex[id] = sampleIndex
lock.acquire()
viewer.sampleCounters[id] += 1 + dropCount
for j, series in enumerate(device):
series.buffer.extend([None] * dropCount)
series.buffer.append(sample[j])
lock.release()
except Exception as e:
print('error in parsing osc message: {!s}. {}'.format(e, msg))
def switched(sender):
global used_device_ids
if sender.value:
used_device_ids.add(sender.device_id)
else:
used_device_ids.discard(sender.device_id)
def start_viewer(sender):
global viewer
### OSC Server ###
local_address = socket.gethostbyname(socket.gethostname())
dispatch = dispatcher.Dispatcher()
for i, id in enumerate(used_device_ids):
dispatch.map('/{}'.format(id), raw_osc_handler, id)
server = osc_server.ThreadingOSCUDPServer((local_address, 5005), dispatch)
server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 512000)
server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVLOWAT, 1)
viewer = Viewer(device_ids=used_device_ids, nChannel=4, server=server, lock=lock)
### START SERVER ###
server_thread = Thread(target=server.serve_forever)
server_thread.setDaemon(True)
server_thread.start()
print('serving at {}:{}'.format(local_address, 5005))
sceneView = SceneView()
sceneView.scene = viewer
sceneView.flex = 'LRHWT'
sceneView.present('full_screen')
if __name__ == '__main__':
used_device_ids = set(('e4f7','cec8'))
lock = Lock()
### UI Viewer ###
v = ui.load_view('stream_viewer.pyui')
v.present(style='full_screen')
### Visualization Starts ####
#run(viewer, show_fps = True, frame_interval = 1, anti_alias = True)