-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bob Haddleton
authored and
Bob Haddleton
committed
Aug 3, 2024
1 parent
d9a29a0
commit d89c241
Showing
12 changed files
with
1,220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,6 @@ bin/ | |
|
||
# PyBuilder | ||
target/ | ||
|
||
# PyCharm | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Small example Asynchronous OSC TCP client | ||
This program listens for incoming messages in one task, and | ||
sends 10 random values between 0.0 and 1.0 to the /filter address, | ||
waiting for 1 seconds between each value in a second task. | ||
""" | ||
import argparse | ||
import asyncio | ||
import random | ||
import sys | ||
|
||
from pythonosc import tcp_client | ||
|
||
|
||
async def get_messages(client): | ||
async for msg in client.get_messages(60): | ||
print(msg) | ||
|
||
|
||
async def send_messages(client): | ||
for x in range(10): | ||
r = random.random() | ||
print(f"Sending /filter {r}") | ||
await client.send_message("/filter", r) | ||
await asyncio.sleep(1) | ||
|
||
|
||
async def init_main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", | ||
help="The ip of the OSC server") | ||
parser.add_argument("--port", type=int, default=5005, | ||
help="The port the OSC server is listening on") | ||
parser.add_argument("--mode", default="1.1", | ||
help="The OSC protocol version of the server (default is 1.1)") | ||
args = parser.parse_args() | ||
|
||
async with tcp_client.AsyncSimpleTCPClient(args.ip, args.port, mode=args.mode) as client: | ||
async with asyncio.TaskGroup() as tg: | ||
tg.create_task(get_messages(client)) | ||
tg.create_task(send_messages(client)) | ||
|
||
if sys.version_info >= (3, 7): | ||
asyncio.run(init_main()) | ||
else: | ||
# TODO(python-upgrade): drop this once 3.6 is no longer supported | ||
event_loop = asyncio.get_event_loop() | ||
event_loop.run_until_complete(init_main()) | ||
event_loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import argparse | ||
import asyncio | ||
import sys | ||
|
||
from pythonosc.dispatcher import Dispatcher | ||
from pythonosc.osc_tcp_server import AsyncOSCTCPServer | ||
|
||
|
||
def filter_handler(address, *args): | ||
print(f"{address}: {args}") | ||
|
||
|
||
dispatcher = Dispatcher() | ||
dispatcher.map("/filter", filter_handler) | ||
|
||
|
||
async def loop(): | ||
"""Example main loop that only runs for 10 iterations before finishing""" | ||
for i in range(10): | ||
print(f"Loop {i}") | ||
await asyncio.sleep(10) | ||
|
||
|
||
async def init_main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", | ||
help="The ip of the OSC server") | ||
parser.add_argument("--port", type=int, default=5005, | ||
help="The port the OSC server is listening on") | ||
parser.add_argument("--mode", default="1.1", | ||
help="The OSC protocol version of the server (default is 1.1)") | ||
args = parser.parse_args() | ||
|
||
async with AsyncOSCTCPServer(args.ip, args.port, dispatcher, mode=args.mode) as server: | ||
async with asyncio.TaskGroup() as tg: | ||
tg.create_task(server.start()) | ||
tg.create_task(loop()) | ||
|
||
|
||
if sys.version_info >= (3, 7): | ||
asyncio.run(init_main()) | ||
else: | ||
# TODO(python-upgrade): drop this once 3.6 is no longer supported | ||
event_loop = asyncio.get_event_loop() | ||
event_loop.run_until_complete(init_main()) | ||
event_loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Small example OSC client | ||
This program sends 10 random values between 0.0 and 1.0 to the /filter address, | ||
and listens for incoming messages for 1 second between each value. | ||
""" | ||
import argparse | ||
import random | ||
|
||
from pythonosc import tcp_client | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", | ||
help="The ip of the OSC server") | ||
parser.add_argument("--port", type=int, default=5005, | ||
help="The port the OSC server is listening on") | ||
parser.add_argument("--mode", default="1.1", | ||
help="The OSC protocol version of the server (default is 1.1)") | ||
args = parser.parse_args() | ||
|
||
with tcp_client.SimpleTCPClient(args.ip, args.port, mode=args.mode) as client: | ||
for x in range(10): | ||
n = random.random() | ||
print(f"Sending /filter {n}") | ||
client.send_message("/filter", n) | ||
resp = client.get_messages(1) | ||
for r in resp: | ||
try: | ||
print(r) | ||
except Exception as e: | ||
print(f"oops {str(e)}: {r}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Small example OSC server | ||
This program listens to the specified address and port, and prints some information about | ||
received packets. | ||
""" | ||
import argparse | ||
import math | ||
|
||
from pythonosc import osc_tcp_server | ||
from pythonosc.dispatcher import Dispatcher | ||
|
||
|
||
def print_volume_handler(unused_addr, args, volume): | ||
print("[{0}] ~ {1}".format(args[0], volume)) | ||
|
||
|
||
def print_compute_handler(unused_addr, args, volume): | ||
try: | ||
print("[{0}] ~ {1}".format(args[0], args[1](volume))) | ||
except ValueError: | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", | ||
default="127.0.0.1", help="The ip to listen on") | ||
parser.add_argument("--port", | ||
type=int, default=5005, help="The port to listen on") | ||
parser.add_argument("--mode", default="1.1", | ||
help="The OSC protocol version of the server (default is 1.1)") | ||
|
||
args = parser.parse_args() | ||
|
||
dispatcher = Dispatcher() | ||
dispatcher.map("/filter", print) | ||
dispatcher.map("/volume", print_volume_handler, "Volume") | ||
dispatcher.map("/logvolume", print_compute_handler, "Log volume", math.log) | ||
|
||
server = osc_tcp_server.ThreadingOSCTCPServer( | ||
(args.ip, args.port), dispatcher, mode=args.mode) | ||
print("Serving on {}".format(server.server_address)) | ||
server.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.