Skip to content

add OSC send and receive - #293

Open
v10101a wants to merge 2 commits into
processing:mainfrom
v10101a:osc
Open

v10101a wants to merge 2 commits into
processing:mainfrom
v10101a:osc

Conversation

@v10101a

@v10101a v10101a commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

first attempt for #179, adds OSC over UDP for Rust and Python

API

  • osc_listen(port): starts listening for OSC messages on port and returns the port. fails with an error if the port is already in use.
  • osc_send(host, port, address, args...): sends one message, e.g. osc_send("127.0.0.1", 12000, "/mouse", 0.5, 0.25).
  • osc_read(): returns every message received since the last call, oldest first. it can be called every frame like midi_read().
  • osc_stop(): stops listening and closes the port. osc_read() gives an error until osc_listen() is called again.
  • Python: messages are OscMessage objects with address, args, typetag (e.g. "ff" for two floats), host and port. str(msg) gives a readable form like /mouse 0.52 0.25.

Changes

  • added crates/processing_osc/
  • added osc error messages to crates/processing_core/src/error.rs
  • src/lib.rs, src/prelude.rs: add OscPlugin and export the osc_* functions
  • added crates/processing_pyo3/src/osc.rs: converts Python values to OSC types, defines the OscMessage class && crates/processing_pyo3/src/lib.rs: registers the Python functions and OscMessage
  • added OSC IN/OUT examples examples/osc.rs and crates/processing_pyo3/examples/osc.py
  • used rosc instead of nannou_osc, since nannou_osc doesn't have a Bevy plugin. we could probably switch later without changing the API!

some limitations

  • IPv4 only (no IPv6, TCP or multicast yet)
  • only one listening port at a time.
  • foats are sent as 32-bit.

tested on macOS with TouchDesigner

LLM use: wrote the crate and bindings, reviewed the API, and wrote first version of example code with Claude Code

@github-actions

Copy link
Copy Markdown

Visual regression: 1 changed of 43 cases

case status detail
py_flocking_gpu ❌ changed 7.71% of pixels differ

If these changes are intentional, add the deliberate-rendering-change label.

baseline e74f60006a · adapter llvmpipe (LLVM 20.1.2, 256 bits) (Vulkan, Mesa 25.2.8-0ubuntu0.24.04.5 (LLVM 20.1.2))

Open the visual report · workflow run

@tychedelia tychedelia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! A few minor comments. On the design for follow-ups:

  • Right now we are using a bevy resource in order to store the OSC state. We should store these as components so that osc_listen returns a handle (aka Bevy entity) to a port and the read/write APIs accept this handle. I think it would be fine in Python if we want to have a "default" or no-arg version that just uses a default global osc state, that would be cool as it's definitely more ergonomic. But we also want to let users read/write to multiple OSC servers.
  • We should have concenience methods for reading just the latest value. For example, an OSC source may write at 100Hz but we render at 60Hz, a sketch typically just wants the latest value. I'd propose something like osc.value(addr, default) / osc.values(addr, default), a lookup of the latest value for each address.
  • We should consider an osc_event(msg) callback on the sketch. Personally I prefer polling directly in the sketch but this is what the processing serial library does and could be convenient for some use cases.
  • It would be nice to have something like msg.reply("/foo", 1) to reply directly to a message (when dealing with raw messages).

Feel free to fix anything here but I'm also happy to merge and keep going!

.receiver
.as_ref()
.ok_or(ProcessingError::OscNotListening)?;
let mut buf = [0u8; decoder::MTU];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can send more over localhost, might be worth sizing this for that use case when reading from localhost

encoder::encode(&OscPacket::Message(message)).map_err(|e| failed(format!("{e:?}")))?;
if self.sender.is_none() {
let socket =
UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0)).map_err(|e| failed(e.to_string()))?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should try to rebind the listening port if it's available first, some apps will reply by default to where they received messages from. iirc there's some trickyness on windows here though that i can't remember

/// Returns the receiver address, with `host` looked up (e.g. `127.0.0.1:9000`).
#[cfg(not(target_arch = "wasm32"))]
pub fn osc_send(host: &str, port: u16, address: &str, args: Vec<OscType>) -> Result<SocketAddr> {
let message = OscMessage {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should validate the address here

use processing_core::app_mut;
use processing_core::error::{ProcessingError, Result};

pub use rosc::{OscArray, OscMessage, OscType};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these leak into our public rust api. not a big deal for now and can easily be a follow up but we should try to either redefine them or opaquely newtype them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants