Crate serde_osc [] [src]

OSC protocol

Open Sound Control (OSC) is an open, transport-independent, message-based protocol developed for communication among computers, sound synthesizers, and other multimedia devices.

It mixes human-readable API endpoints (termed "addresses") with binary-encoded arguments and headers. An example OSC message is


In this example, the first 4 bytes signify the length of the message; the null-terminated string "/audio/play" signifies the endpoint (which component the message is intended for); the ",ifb" string indicates that there are 3 arguments: one i32, one f32 and one binary "blob" (u8 array). The rest of the message is the payload, i.e., the values corresponding to each of these arguments. The full specification can be found at Serde_osc implements version 1.0 of the spec (i.e. 'i', 'f', 's' and 'b' argument types).

Serde_osc usage

Generic encoding of OSC packets is intended to be done via serde_osc::to_write with decoding done by serde_osc::from_read. These work with any data sink/source that implements std::io::Write or std::io::Read, respectively.

Convenience functions are also provided for some common formats; see serde_osc::to_vec and serde_osc::from_vec.


The following example serializes a struct into a Vec<u8>, formatted as an OSC packet, and then deserializes the OSC packet back into the struct.

extern crate serde_derive;
extern crate serde;
extern crate serde_bytes;
extern crate serde_osc;

use serde_bytes::ByteBuf;
use serde_osc::{de, ser};

/// Struct we'll serialize.
/// This represents a single OSC message with three arguments:
///   one of type 'i', 'f' and 'b', encoded in the order they appear in the struct.
#[derive(Debug, Deserialize, Serialize)]
struct Message {
    address: String,
    // ByteBuf is the object we use for OSC "blobs".
    // It's a thin wrapper over Vec<u8> provided by Serde that allows
    // for more computationally-efficient serialization/deserialization.
    args: (i32, f32, ByteBuf),

fn main() {
    let message = Message {
        address: "/audio/play".to_owned(),
        args: (
            ByteBuf::from(vec![0xde, 0xad, 0xbe, 0xef]),
    println!("Serializing {:?}", message);

    // Serialize the message to an OSC packet stored in a Vec<u8>
    let as_vec = ser::to_vec(&message).unwrap();
    println!("Serialied to: {:?}", as_vec);

    // Deserialize an OSC packet contained in a Vec<u8> into the Message struct
    let received: Message = de::from_slice(&as_vec).unwrap();
    println!("Received: {:?}", received);


pub use de::from_read;
pub use de::from_slice;
pub use ser::to_write;
pub use ser::to_vec;



OSC packet deserialization framework.


Errors returned upon serialization/deserialization failure.


OSC packet serialization framework.