not finished commit
This commit is contained in:
commit
a5d76ba126
18 changed files with 4517 additions and 0 deletions
11
esp/uosc/__init__.py
Executable file
11
esp/uosc/__init__.py
Executable file
|
|
@ -0,0 +1,11 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""A minimal OSC client and server library for MicroPython.
|
||||
|
||||
To use it with the unix port of MicroPython, install the required modules from
|
||||
the micropython-lib:
|
||||
|
||||
$ for mod in argparse ffilib logging socket struct; do
|
||||
micropython -m upip install micropython-$mod
|
||||
done
|
||||
|
||||
"""
|
||||
34
esp/uosc/__main__.py
Executable file
34
esp/uosc/__main__.py
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/env micropython
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from uosc.server import run_server
|
||||
|
||||
|
||||
DEFAULT_ADDRESS = '0.0.0.0'
|
||||
DEFAULT_PORT = 9001
|
||||
|
||||
|
||||
def main(args=None):
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument('-v', '--verbose', action="store_true",
|
||||
help="Enable debug logging")
|
||||
ap.add_argument('-a', '--address', default=DEFAULT_ADDRESS,
|
||||
help="OSC server address (default: %s)" % DEFAULT_ADDRESS)
|
||||
ap.add_argument('-p', '--port', type=int, default=DEFAULT_PORT,
|
||||
help="OSC server port (default: %s)" % DEFAULT_PORT)
|
||||
|
||||
args = ap.parse_args(args if args is not None else sys.argv[1:])
|
||||
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
|
||||
|
||||
try:
|
||||
run_server(args.address, int(args.port))
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv[1:]) or 0)
|
||||
204
esp/uosc/client.py
Executable file
204
esp/uosc/client.py
Executable file
|
|
@ -0,0 +1,204 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# uosc/client.py
|
||||
#
|
||||
"""Simple OSC client."""
|
||||
|
||||
import socket
|
||||
|
||||
try:
|
||||
from ustruct import pack
|
||||
except ImportError:
|
||||
from struct import pack
|
||||
|
||||
from uosc.common import Bundle, to_frac
|
||||
|
||||
|
||||
if isinstance('', bytes):
|
||||
have_bytes = False
|
||||
unicodetype = unicode # noqa
|
||||
else:
|
||||
have_bytes = True
|
||||
unicodetype = str
|
||||
|
||||
TYPE_MAP = {
|
||||
int: 'i',
|
||||
float: 'f',
|
||||
bytes: 'b',
|
||||
bytearray: 'b',
|
||||
unicodetype: 's',
|
||||
True: 'T',
|
||||
False: 'F',
|
||||
None: 'N',
|
||||
}
|
||||
|
||||
|
||||
def pack_addr(addr):
|
||||
"""Pack a (host, port) tuple into the format expected by socket methods."""
|
||||
if isinstance(addr, (bytes, bytearray)):
|
||||
return addr
|
||||
|
||||
if len(addr) != 2:
|
||||
raise NotImplementedError("Only IPv4/v6 supported")
|
||||
|
||||
addrinfo = socket.getaddrinfo(addr[0], addr[1])
|
||||
return addrinfo[0][4]
|
||||
|
||||
|
||||
def pack_timetag(t):
|
||||
"""Pack an OSC timetag into 64-bit binary blob."""
|
||||
return pack('>II', *to_frac(t))
|
||||
|
||||
|
||||
def pack_string(s, encoding='utf-8'):
|
||||
"""Pack a string into a binary OSC string."""
|
||||
if isinstance(s, unicodetype):
|
||||
s = s.encode(encoding)
|
||||
assert all((i if have_bytes else ord(i)) < 128 for i in s), (
|
||||
"OSC strings may only contain ASCII chars.")
|
||||
|
||||
slen = len(s)
|
||||
return s + b'\0' * (((slen + 4) & ~0x03) - slen)
|
||||
|
||||
|
||||
def pack_blob(b, encoding='utf-8'):
|
||||
"""Pack a bytes, bytearray or tuple/list of ints into a binary OSC blob."""
|
||||
if isinstance(b, (tuple, list)):
|
||||
b = bytearray(b)
|
||||
elif isinstance(b, unicodetype):
|
||||
b = b.encode(encoding)
|
||||
|
||||
blen = len(b)
|
||||
b = pack('>I', blen) + bytes(b)
|
||||
return b + b'\0' * (((blen + 3) & ~0x03) - blen)
|
||||
|
||||
|
||||
def pack_bundle(bundle):
|
||||
"""Return bundle data packed into a binary string."""
|
||||
data = []
|
||||
for msg in bundle:
|
||||
if isinstance(msg, Bundle):
|
||||
msg = pack_bundle(msg)
|
||||
elif isinstance(msg, tuple):
|
||||
msg = create_message(*msg)
|
||||
|
||||
data.append(pack('>I', len(msg)) + msg)
|
||||
|
||||
return b'#bundle\0' + pack_timetag(bundle.timetag) + b''.join(data)
|
||||
|
||||
|
||||
def pack_midi(val):
|
||||
assert not isinstance(val, unicodetype), (
|
||||
"Value with tag 'm' or 'r' must be bytes, bytearray or a sequence of "
|
||||
"ints, not %s" % unicodetype)
|
||||
if not have_bytes and isinstance(val, str):
|
||||
val = (ord(c) for c in val)
|
||||
|
||||
return pack('BBBB', *tuple(val))
|
||||
|
||||
|
||||
def create_message(address, *args):
|
||||
"""Create an OSC message with given address pattern and arguments.
|
||||
|
||||
The OSC types are either inferred from the Python types of the arguments,
|
||||
or you can pass arguments as 2-item tuples with the OSC typetag as the
|
||||
first item and the argument value as the second. Python objects are mapped
|
||||
to OSC typetags as follows:
|
||||
|
||||
* ``int``: i
|
||||
* ``float``: f
|
||||
* ``str``: s
|
||||
* ``bytes`` / ``bytearray``: b
|
||||
* ``None``: N
|
||||
* ``True``: T
|
||||
* ``False``: F
|
||||
|
||||
If you want to encode a Python object to another OSC type, you have to pass
|
||||
a ``(typetag, data)`` tuple, where ``data`` must be of the appropriate type
|
||||
according to the following table:
|
||||
|
||||
* c: ``str`` of length 1
|
||||
* h: ``int``
|
||||
* d: ``float``
|
||||
* I: ``None`` (unused)
|
||||
* m: ``tuple / list`` of 4 ``int``s or ``bytes / bytearray`` of length 4
|
||||
* r: same as 'm'
|
||||
* t: OSC timetag as as ``int / float`` seconds since the NTP epoch
|
||||
* S: ``str``
|
||||
|
||||
"""
|
||||
assert address.startswith('/'), "Address pattern must start with a slash."
|
||||
|
||||
data = []
|
||||
types = [',']
|
||||
|
||||
for arg in args:
|
||||
type_ = type(arg)
|
||||
|
||||
if isinstance(arg, tuple):
|
||||
typetag, arg = arg
|
||||
else:
|
||||
typetag = TYPE_MAP.get(type_) or TYPE_MAP.get(arg)
|
||||
|
||||
if typetag in 'ifd':
|
||||
data.append(pack('>' + typetag, arg))
|
||||
elif typetag in 'sS':
|
||||
data.append(pack_string(arg))
|
||||
elif typetag == 'b':
|
||||
data.append(pack_blob(arg))
|
||||
elif typetag in 'rm':
|
||||
data.append(pack_midi(arg))
|
||||
elif typetag == 'c':
|
||||
data.append(pack('>I', ord(arg)))
|
||||
elif typetag == 'h':
|
||||
data.append(pack('>q', arg))
|
||||
elif typetag == 't':
|
||||
data.append(pack_timetag(arg))
|
||||
elif typetag not in 'IFNT':
|
||||
raise TypeError("Argument of type '%s' not supported." % type_)
|
||||
|
||||
types.append(typetag)
|
||||
|
||||
return pack_string(address) + pack_string(''.join(types)) + b''.join(data)
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, host, port=None):
|
||||
if port is None:
|
||||
if isinstance(host, (list, tuple)):
|
||||
host, port = host
|
||||
else:
|
||||
port = host
|
||||
host = '127.0.0.1'
|
||||
|
||||
self.dest = pack_addr((host, port))
|
||||
self.sock = None
|
||||
|
||||
def send(self, msg, *args, **kw):
|
||||
dest = pack_addr(kw.get('dest', self.dest))
|
||||
|
||||
if not self.sock:
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
if isinstance(msg, Bundle):
|
||||
msg = pack_bundle(msg)
|
||||
elif args or isinstance(msg, unicodetype):
|
||||
msg = create_message(msg, *args)
|
||||
|
||||
self.sock.sendto(msg, dest)
|
||||
|
||||
def close(self):
|
||||
if self.sock:
|
||||
self.sock.close()
|
||||
self.sock = None
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.close()
|
||||
|
||||
|
||||
def send(dest, address, *args):
|
||||
with Client(dest) as client:
|
||||
client.send(address, *args)
|
||||
62
esp/uosc/common.py
Executable file
62
esp/uosc/common.py
Executable file
|
|
@ -0,0 +1,62 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# uosc/common.py
|
||||
#
|
||||
"""OSC message parsing and building functions."""
|
||||
|
||||
try:
|
||||
from time import time
|
||||
except ImportError:
|
||||
from utime import time
|
||||
|
||||
|
||||
# UNIX_EPOCH = datetime.date(*time.gmtime(0)[0:3])
|
||||
# NTP_EPOCH = datetime.date(1900, 1, 1)
|
||||
# NTP_DELTA = (UNIX_EPOCH - NTP_EPOCH).days * 24 * 3600
|
||||
NTP_DELTA = 2208988800
|
||||
ISIZE = 4294967296 # 2**32
|
||||
|
||||
|
||||
class Impulse:
|
||||
pass
|
||||
|
||||
|
||||
class Bundle:
|
||||
"""Container for an OSC bundle."""
|
||||
|
||||
def __init__(self, *items):
|
||||
"""Create bundle from given OSC timetag and messages/sub-bundles.
|
||||
|
||||
An OSC timetag can be given as the first positional argument, and must
|
||||
be an int or float of seconds since the NTP epoch (1990-01-01 00:00).
|
||||
It defaults to the current time.
|
||||
|
||||
Pass in messages or bundles via positional arguments as binary data
|
||||
(bytes as returned by ``create_message`` resp. ``Bundle.pack``) or as
|
||||
``Bundle`` instances or (address, *args) tuples.
|
||||
|
||||
"""
|
||||
if items and isinstance(items[0], (int, float)):
|
||||
self.timetag = items[0]
|
||||
items = items[1:]
|
||||
else:
|
||||
self.timetag = time() + NTP_DELTA
|
||||
|
||||
self._items = list(items)
|
||||
|
||||
def add(self, *items):
|
||||
self._items.extend(list(items))
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._items)
|
||||
|
||||
|
||||
def to_frac(t):
|
||||
"""Return seconds and fractional part of NTP timestamp as 2-item tuple."""
|
||||
sec = int(t)
|
||||
return sec, int(abs(t - sec) * ISIZE)
|
||||
|
||||
|
||||
def to_time(sec, frac):
|
||||
"""Return NTP timestamp from integer seconds and fractional part."""
|
||||
return sec + float(frac) / ISIZE
|
||||
163
esp/uosc/server.py
Executable file
163
esp/uosc/server.py
Executable file
|
|
@ -0,0 +1,163 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# uosc/server.py
|
||||
#
|
||||
"""A minimal OSC UDP server."""
|
||||
|
||||
|
||||
import socket
|
||||
|
||||
try:
|
||||
from ustruct import unpack
|
||||
except ImportError:
|
||||
from struct import unpack
|
||||
|
||||
from uosc.common import Impulse, to_time
|
||||
|
||||
#if __debug__:
|
||||
# from uosc.socketutil import get_hostport
|
||||
|
||||
|
||||
|
||||
MAX_DGRAM_SIZE = 1472
|
||||
|
||||
|
||||
def split_oscstr(msg, offset):
|
||||
end = msg.find(b'\0', offset)
|
||||
return msg[offset:end].decode('utf-8'), (end + 4) & ~0x03
|
||||
|
||||
|
||||
def split_oscblob(msg, offset):
|
||||
start = offset + 4
|
||||
size = unpack('>I', msg[offset:start])[0]
|
||||
return msg[start:start + size], (start + size + 4) & ~0x03
|
||||
|
||||
|
||||
def parse_timetag(msg, offset):
|
||||
"""Parse an OSC timetag from msg at offset."""
|
||||
return to_time(unpack('>II', msg[offset:offset + 4]))
|
||||
|
||||
|
||||
def parse_message(msg, strict=False):
|
||||
args = []
|
||||
addr, ofs = split_oscstr(msg, 0)
|
||||
|
||||
if not addr.startswith('/'):
|
||||
raise ValueError("OSC address pattern must start with a slash.")
|
||||
|
||||
# type tag string must start with comma (ASCII 44)
|
||||
if ofs < len(msg) and msg[ofs:ofs + 1] == b',':
|
||||
tags, ofs = split_oscstr(msg, ofs)
|
||||
tags = tags[1:]
|
||||
else:
|
||||
errmsg = "Missing/invalid OSC type tag string."
|
||||
if strict:
|
||||
raise ValueError(errmsg)
|
||||
else:
|
||||
print(errmsg + ' Ignoring arguments.')
|
||||
tags = ''
|
||||
|
||||
for typetag in tags:
|
||||
size = 0
|
||||
|
||||
if typetag in 'ifd':
|
||||
size = 8 if typetag == 'd' else 4
|
||||
args.append(unpack('>' + typetag, msg[ofs:ofs + size])[0])
|
||||
elif typetag in 'sS':
|
||||
s, ofs = split_oscstr(msg, ofs)
|
||||
args.append(s)
|
||||
elif typetag == 'b':
|
||||
s, ofs = split_oscblob(msg, ofs)
|
||||
args.append(s)
|
||||
elif typetag in 'rm':
|
||||
size = 4
|
||||
args.append(unpack('BBBB', msg[ofs:ofs + size]))
|
||||
elif typetag == 'c':
|
||||
size = 4
|
||||
args.append(chr(unpack('>I', msg[ofs:ofs + size])[0]))
|
||||
elif typetag == 'h':
|
||||
size = 8
|
||||
args.append(unpack('>q', msg[ofs:ofs + size])[0])
|
||||
elif typetag == 't':
|
||||
size = 8
|
||||
args.append(parse_timetag(msg, ofs))
|
||||
elif typetag in 'TFNI':
|
||||
args.append({'T': True, 'F': False, 'I': Impulse}.get(typetag))
|
||||
else:
|
||||
raise ValueError("Type tag '%s' not supported." % typetag)
|
||||
|
||||
ofs += size
|
||||
|
||||
return (addr, tags, tuple(args))
|
||||
|
||||
|
||||
def parse_bundle(bundle, strict=False):
|
||||
"""Parse a binary OSC bundle.
|
||||
|
||||
Returns a generator which walks over all contained messages and bundles
|
||||
recursively, depth-first. Each item yielded is a (timetag, message) tuple.
|
||||
|
||||
"""
|
||||
if not bundle.startswith(b'#bundle\0'):
|
||||
raise TypeError("Bundle must start with b'#bundle\\0'.")
|
||||
|
||||
ofs = 16
|
||||
timetag = to_time(*unpack('>II', bundle[8:ofs]))
|
||||
|
||||
while True:
|
||||
if ofs >= len(bundle):
|
||||
break
|
||||
|
||||
size = unpack('>I', bundle[ofs:ofs + 4])[0]
|
||||
element = bundle[ofs + 4:ofs + 4 + size]
|
||||
ofs += size + 4
|
||||
|
||||
if element.startswith(b'#bundle'):
|
||||
for el in parse_bundle(element):
|
||||
yield el
|
||||
else:
|
||||
yield timetag, parse_message(element, strict)
|
||||
|
||||
|
||||
def handle_osc(data, src, dispatch=None, strict=False):
|
||||
try:
|
||||
head, _ = split_oscstr(data, 0)
|
||||
|
||||
if head.startswith('/'):
|
||||
messages = [(-1, parse_message(data, strict))]
|
||||
elif head == '#bundle':
|
||||
messages = parse_bundle(data, strict)
|
||||
except Exception as exc:
|
||||
pass
|
||||
#if __debug__:
|
||||
# print(data)
|
||||
return
|
||||
|
||||
try:
|
||||
for timetag, (oscaddr, tags, args) in messages:
|
||||
if __debug__:
|
||||
print("OSC address: %s" % oscaddr)
|
||||
print("OSC type tags: %r" % tags)
|
||||
print("OSC arguments: %r" % (args,))
|
||||
|
||||
if dispatch:
|
||||
dispatch(timetag, (oscaddr, tags, args, src))
|
||||
except Exception as exc:
|
||||
print("Exception in OSC handler: %s", exc)
|
||||
|
||||
|
||||
def run_server(saddr, port, handler=handle_osc):
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
if __debug__: print("Created OSC UDP server socket.")
|
||||
|
||||
sock.bind((saddr, port))
|
||||
print("Listening for OSC messages on %s:%i.", saddr, port)
|
||||
|
||||
try:
|
||||
while True:
|
||||
data, caddr = sock.recvfrom(MAX_DGRAM_SIZE)
|
||||
handler(data, caddr)
|
||||
finally:
|
||||
sock.close()
|
||||
print("Bye!")
|
||||
35
esp/uosc/socketutil.py
Executable file
35
esp/uosc/socketutil.py
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# uosc/socketutil.py
|
||||
#
|
||||
|
||||
import socket
|
||||
|
||||
|
||||
INET_ADDRSTRLEN = 16
|
||||
INET6_ADDRSTRLEN = 46
|
||||
|
||||
inet_ntoa = getattr(socket, 'inet_ntoa', None)
|
||||
if not inet_ntoa:
|
||||
import ffilib
|
||||
inet_ntoa = ffilib.libc().func("s", "inet_ntoa", "p")
|
||||
|
||||
|
||||
inet_ntop = getattr(socket, 'inet_ntop', None)
|
||||
if not inet_ntop:
|
||||
import ffilib
|
||||
_inet_ntop = ffilib.libc().func("s", "inet_ntop", "iPpi")
|
||||
|
||||
def inet_ntop(af, addr):
|
||||
buf = bytearray(INET_ADDRSTRLEN if af == socket.AF_INET else
|
||||
INET6_ADDRSTRLEN)
|
||||
res = _inet_ntop(af, addr, buf, INET_ADDRSTRLEN)
|
||||
return res
|
||||
|
||||
|
||||
def get_hostport(addr):
|
||||
if isinstance(addr, tuple):
|
||||
return addr
|
||||
|
||||
af, addr, port = socket.sockaddr(addr)
|
||||
return inet_ntop(af, addr), port
|
||||
86
esp/uosc/threadedclient.py
Executable file
86
esp/uosc/threadedclient.py
Executable file
|
|
@ -0,0 +1,86 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""OSC client running in a separate thread.
|
||||
|
||||
Communicates with the main thread via a queue. Provides the same API as the
|
||||
non-threaded client, with a few threading-related extensions:
|
||||
|
||||
from uosc.threadedclient import ThreadedClient
|
||||
|
||||
# start=True starts the thread immediately
|
||||
osc = ThreadedClient('192.168.0.42', 9001, start=True)
|
||||
|
||||
# if the OSC message can not placed in the queue within timeout
|
||||
# raises a queue.Full error
|
||||
osc.send('/pi', 3.14159, timeout=1.0)
|
||||
# Stops and joins the thread and closes the client socket
|
||||
osc.close()
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import threading
|
||||
|
||||
try:
|
||||
import queue
|
||||
except ImportError:
|
||||
import Queue as queue
|
||||
|
||||
from uosc.client import Client
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ThreadedClient(threading.Thread):
|
||||
def __init__(self, host, port=None, start=False, timeout=3.0):
|
||||
super(ThreadedClient, self).__init__()
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.timeout = timeout
|
||||
self._q = queue.Queue()
|
||||
|
||||
if start:
|
||||
self.start()
|
||||
|
||||
def run(self, *args, **kw):
|
||||
self.client = Client((self.host, self.port))
|
||||
|
||||
while True:
|
||||
msg = self._q.get()
|
||||
if msg is None:
|
||||
break
|
||||
|
||||
addr, msg = msg
|
||||
log.debug("Sending OSC msg %s, %r", addr, msg)
|
||||
self.client.send(addr, *msg)
|
||||
|
||||
self.client.close()
|
||||
|
||||
def send(self, addr, *args, **kw):
|
||||
self._q.put((addr, args), timeout=kw.get('timeout', self.timeout))
|
||||
|
||||
def close(self, **kw):
|
||||
timeout = kw.get('timeout', self.timeout)
|
||||
log.debug("Emptying send queue...")
|
||||
|
||||
while True:
|
||||
try:
|
||||
self._q.get_nowait()
|
||||
except queue.Empty:
|
||||
break
|
||||
|
||||
if self.is_alive():
|
||||
log.debug("Signalling OSC client thread to exit...")
|
||||
self._q.put(None, timeout=timeout)
|
||||
|
||||
log.debug("Joining OSC client thread...")
|
||||
self.join(timeout)
|
||||
|
||||
if self.is_alive():
|
||||
log.warning("OSC client thread still alive after join().")
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.close()
|
||||
Loading…
Add table
Add a link
Reference in a new issue