/
osc.py
75 lines (61 loc) · 2.03 KB
/
osc.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
# -*- coding: utf-8 -*-
#
# Copyright 2016 Holger Ballweg
#
# This file is part of chison.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# osc
import inspect
import os
import sys
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"pyosc")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
from OSC import OSCClient, OSCMessage
def set_port(port=57120):
global client
client.connect( ("localhost", port) )
def send_osc(addr, *args):
msg = OSCMessage(addr)
for d in args:
msg.append(d)
return client.send(msg)
client = OSCClient()
set_port(57120)
from OSC import OSCServer
import sys
from time import sleep
import threading
server = OSCServer( ("localhost", 7110) )
server.timeout = 0
run = True
# this method of reporting timeouts only works by convention
# that before calling handle_request() field .timed_out is
# set to False
def handle_timeout(self):
self.timed_out = True
# funny python's way to add a method to an instance of a class
import types
server.handle_timeout = types.MethodType(handle_timeout, server)
def server_loop():
while run:
server.timed_out = False
# handle all pending requests then return
while not server.timed_out:
server.handle_request()
sleep(1)
server.close()
server_thread = threading.Thread(target=server_loop)
server_thread.start()