OSCServer¶
OSCServer
¶
A threaded OSC server using python-osc library. With a given message handler function, will call that function on incoming messages.
__init__(in_ip='0.0.0.0', port=5005, osc_msg_handler=None)
¶
Initialize the OSC server with IP, port, and message handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_ip
|
str
|
IP address to bind the server to. If "0.0.0.0", listens on all interfaces. This is the default. Set to a specific IP to only accept messages from that interface. |
'0.0.0.0'
|
port
|
int
|
Port number to listen on. |
5005
|
osc_msg_handler
|
function
|
Function to handle incoming OSC messages. |
None
|
add_target(name, ip, port)
staticmethod
¶
Register a named target for easier sending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Friendly name for the target. |
required |
ip
|
str
|
Target IP address. |
required |
port
|
int
|
Target port number. |
required |
Example
server.add_target("synth", "192.168.1.100", 9000) server.send_to("synth", "/freq", 440.0)
send(key, *args, ip='127.0.0.1', port=5006)
staticmethod
¶
Send an OSC message to a specific destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
OSC address pattern (e.g., "/synth/freq"). |
required |
*args
|
Values to send with the message. |
()
|
|
ip
|
str
|
Target IP address. Defaults to "127.0.0.1". |
'127.0.0.1'
|
port
|
int
|
Target port number. Defaults to 5006. |
5006
|
Example
server.send("/synth/freq", 440.0) server.send("/mixer/volume", 0.8, ip="192.168.1.100", port=9000) server.send("/note", 60, 127, 0.5) # multiple values
send_bundle(messages, ip='127.0.0.1', port=5006, timetag=None)
staticmethod
¶
Send an OSC bundle (multiple messages with optional timetag).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list
|
List of tuples (key, args) where args is a list of values. |
required |
ip
|
str
|
Target IP address. Defaults to "127.0.0.1". |
'127.0.0.1'
|
port
|
int
|
Target port number. Defaults to 5006. |
5006
|
timetag
|
Optional timetag for the bundle (None = immediately). |
None
|
Example
server.send_bundle([ ("/synth/freq", [440.0]), ("/synth/amp", [0.8]), ("/synth/gate", [1]) ])
send_to(target_name, key, *args)
staticmethod
¶
Send an OSC message to a named target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_name
|
str
|
Name of a registered target. |
required |
address
|
str
|
OSC address pattern. |
required |
*args
|
Values to send with the message. |
()
|
Example
server.send_to("synth", "/freq", 440.0)
set_osc_msg_handler(handler)
¶
Set a custom OSC message handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler
|
function
|
Function to handle incoming OSC messages. |
required |
start()
¶
Start the OSC server in a separate thread
stop()
¶
Stop the OSC server gracefully