-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosc_handler3.py
More file actions
281 lines (217 loc) · 8.62 KB
/
osc_handler3.py
File metadata and controls
281 lines (217 loc) · 8.62 KB
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
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 22 12:33:01 2018
@author: jj
"""
import numpy as np
import time
import jack
from pythonosc import udp_client
from pythonosc import osc_message_builder as omb
from pythonosc import dispatcher
from pythonosc import osc_server
import os
from os import listdir
from os.path import isfile, join
import math
from multiprocessing import Process
class osc_handler(object):
def __init__(self, ip=None, sendport=None, receiveport=None, outpath=None,
inpath=None):
if ip is None:
self.ip = "127.0.0.1"
else:
self.ip = ip
if sendport is None:
self.sendport = 50001
else:
self.sendport = sendport
if receiveport is None:
self.receiveport = 50002
else:
self.receiveport = receiveport
if outpath is None:
self.outpath = "oscwrite/"
else:
self.outpath = outpath
if not os.path.exists(self.outpath):
os.makedirs(self.outpath)
if inpath is None:
self.inpath = "oscread/"
else:
self.inpath = inpath
if not os.path.exists(self.inpath):
os.makedirs(self.inpath)
self.max_sourceID = 0;
def set_ip(self, ip):
self.ip =ip
self.osc_client = udp_client.SimpleUDPClient(self.ip, self.sendport)
def set_sendport(self, sendport):
self.sendport =sendport
#self.osc_client = udp_client.SimpleUDPClient(self.ip, self.sendport)
def set_receiveport(self, receiveport):
self.receiveport =receiveport
def set_outpath(self, outpath):
self.outpath = outpath
def set_inpath(self, inpath):
self.inpath = inpath
def volume_handler(self, unused_addr, ch1, ch2, gain, timestamp): #make this function external?
f = open('gains', 'a')
f.write("gain")
f.write("\t")
#f.write(str(timestamp))
f.write(str(self.jclientr.transport_frame / self.jclientr.samplerate))
f.write("\t")
f.write(str(ch1))
f.write("\t")
f.write(str(ch2))
f.write("\t")
f.write(str(gain))
f.write("\n")
def position_handler(self, unused_addr, ID, x, y, timestamp): #make this function external?
print("positionhandler working")
tmpName = self.outpath + "pos" + str(ID) + ".osc";
f = open(tmpName, 'a')
f.write("position")
f.write("\t")
#f.write(str(timestamp))
f.write(str(self.jclientr.transport_frame))
f.write("\t")
f.write(str(ID))
f.write("\t")
f.write(str(x))
f.write("\t")
f.write(str(y))
f.write("\n")
def act_jack_play(self):
#jack client for playing
self.jclientp = jack.Client('osc-player' + str(id(self))) #new client necessary for every class member?
self.jclientp.activate();
def act_jack_record(self):
# jack client for recording
self.jclientr = jack.Client('osc-recorder' + str(id(self))) #new client necessary for every class member?
self.jclientr.activate();
def act_send(self):
# osc-client for sending osc
self.osc_client = udp_client.SimpleUDPClient(self.ip, self.sendport)
self.osc_client._sock.bind(('',0))
address, self.clientport = self.osc_client._sock.getsockname() #get UDP source port
print("clientport is " + str(self.clientport))
def act_dispatcher(self):
# server for recording osc
self.dispatcher = dispatcher.Dispatcher()
self.dispatcher.map("/gain/", self.volume_handler )
self.dispatcher.map("/source/position", self.position_handler )
#print(osc_server.ThreadingOSCUDPServer.allow_reuse_address)
osc_server.ThreadingOSCUDPServer.allow_reuse_address = True
#print(osc_server.ThreadingOSCUDPServer.allow_reuse_address)
self.server = osc_server.ThreadingOSCUDPServer((self.ip, self.clientport), self.dispatcher)
print(self.server.allow_reuse_address)
#self.server = osc_server.ThreadingOSCUDPServer(
#(self.ip, self.sendport), dispatcher)
print("Serving on {}".format(self.server.server_address))
def poll(self):
self.osc_client.send_message("/poll",' ')
def activate(self):
self.act_jack()
self.act_send()
self.act_dispatcher()
self.poll()
def subscribe(self):
msg = omb.OscMessageBuilder(address="/subscribe")
msg.add_arg(True,"T")
#msg.add_arg(self.ip)
#msg.add_arg(str(self.receiveport))
#msg.add_arg(1) #message level
msg=msg.build()
self.osc_client.send(msg)
def alive(self):
while 1:
self.osc_client.send_message("/alive",' ')
time.sleep(0.5)
print("sent /alive")
def message_level(self, i):
msg = omb.OscMessageBuilder(address="/message_level")
msg.add_arg(int(i),"i")
msg=msg.build()
self.osc_client.send(msg)
def create_sources(self, N, r):
for i in range(0,N):
msg = omb.OscMessageBuilder(address="/source/new")
msg.add_arg("source"+ str(i+1) , "s")
msg.add_arg("point")
msg.add_arg("1", "s")
#arrange sources in a circle
msg.add_arg(r*math.cos(i/N*3.14159*2+3.14159/2), "f")
msg.add_arg(r*math.sin(i/N*3.14159*2+3.14159/2), "f")
msg.add_arg(1.0, "f") #orientation
msg.add_arg(1.0, "f")
#msg.add_arg(1, "i")
#msg.add_arg("1", "s")
msg.add_arg(False,"F")
msg.add_arg(False,"F")
msg.add_arg(False,"F")
msg=msg.build()
self.osc_client.send(msg)
self.max_sourceID = self.max_sourceID + N
print("executed create_sources with N=" + str(N) + " and r=" +str(r))
def delete_source(self, i):
msg = omb.OscMessageBuilder(address="/source/delete")
msg.add_arg(i)
msg=msg.build()
self.osc_client.send(msg)
if i == self.max_sourceID:
self.max_sourceID = self.max_sourceID - 1 # will cause problems if a source with non-maximal ID will be deleted
def read_send(self):
oscFiles = [f for f in listdir(self.inpath) if isfile(join(self.inpath, f))]
N = len(oscFiles)
t=[]
ID = []
x=[]
y=[]
#self.create_sources(N,1.0)
for i in oscFiles:
print(["loading".__add__(i)])
positions = np.loadtxt(self.inpath.__add__(i), delimiter='\t', usecols=(1,2,3,4))
t.append(positions[:,0])
ID.append(positions[:,1])
x.append(positions[:,2])
y.append(positions[:,3])
print('All files read - ready for playback!')
jackPos = self.jclientp.transport_frame
last_jackPos= jackPos;
while 1:
#print(jackPos)
#print(last_jackPos)
jackPos = self.jclientp.transport_frame
if jackPos != last_jackPos:
for i in range(0,N):
#print(str(i) + "position")
tmpIdx = np.argmin(np.abs(t[i] -jackPos))
msg = omb.OscMessageBuilder(address="/source/position")
msg.add_arg(ID[i][tmpIdx])
msg.add_arg(x[i][tmpIdx]*5)
msg.add_arg(y[i][tmpIdx]*- 5)
msg=msg.build()
self.osc_client.send(msg)
last_jackPos = jackPos;
time.sleep(0.020)
def receive_write(self):
self.server.serve_forever()
print("serveforever")
# before record(), do:
# osc1 = osc_handler()
# osc1.act_send()
# osc1.alive() to check for portnumber, then terminate this process
# osc1.set_receiveport(RIGHTPORTNUMBER)
#def receive(self):
# while True:
# data, addr = self.osc_client._sock.recvfrom(1024)
# print( "received message:" + data )
# def record(self):
#self.subscribe()
#self.act_dispatcher()
#p1 = Process(target=self.alive())
#p2 = Process(target=self.receive())
# p1.start()
# p2.start()