Browse Source

Experimental OSC array type encoding

pull/6/head
Marcus Bitzl 1 year ago
parent
commit
d1d1e7c418
2 changed files with 46 additions and 19 deletions
  1. +44
    -19
      src/encoder.rs
  2. +2
    -0
      src/types.rs

+ 44
- 19
src/encoder.rs View File

@ -1,7 +1,7 @@
use types::{Result, OscType, OscPacket, OscBundle, OscMessage};
use errors::OscError;
use types::{OscBundle, OscMessage, OscPacket, OscType, Result};
use byteorder::{ByteOrder, BigEndian};
use byteorder::{BigEndian, ByteOrder};
/// Takes a reference to an OSC packet and returns
/// a byte vector on success. If the packet was invalid
@ -36,9 +36,9 @@ fn encode_message(msg: &OscMessage) -> Result> {
if let Some(ref args) = msg.args {
for arg in args {
let (bytes, tag): (Option<Vec<u8>>, char) = encode_arg(arg)?;
let (bytes, tags): (Option<Vec<u8>>, String) = encode_arg(arg)?;
type_tags.push(tag);
type_tags.extend(tags.chars());
if bytes.is_some() {
arg_bytes.extend(bytes.unwrap());
}
@ -91,34 +91,34 @@ fn encode_bundle(bundle: &OscBundle) -> Result> {
Ok(bundle_bytes)
}
fn encode_arg(arg: &OscType) -> Result<(Option<Vec<u8>>, char)> {
fn encode_arg(arg: &OscType) -> Result<(Option<Vec<u8>>, String)> {
match *arg {
OscType::Int(ref x) => {
let mut bytes = vec![0u8; 4];
BigEndian::write_i32(&mut bytes, *x);
Ok((Some(bytes), 'i'))
Ok((Some(bytes), 'i'.to_string()))
}
OscType::Long(ref x) => {
let mut bytes = vec![0u8; 8];
BigEndian::write_i64(&mut bytes, *x);
Ok((Some(bytes), 'h'))
Ok((Some(bytes), 'h'.to_string()))
}
OscType::Float(ref x) => {
let mut bytes = vec![0u8; 4];
BigEndian::write_f32(&mut bytes, *x);
Ok((Some(bytes), 'f'))
Ok((Some(bytes), 'f'.to_string()))
}
OscType::Double(ref x) => {
let mut bytes = vec![0u8; 8];
BigEndian::write_f64(&mut bytes, *x);
Ok((Some(bytes), 'd'))
Ok((Some(bytes), 'd'.to_string()))
}
OscType::Char(ref x) => {
let mut bytes = vec![0u8; 4];
BigEndian::write_u32(&mut bytes, *x as u32);
Ok((Some(bytes), 'c'))
Ok((Some(bytes), 'c'.to_string()))
}
OscType::String(ref x) => Ok((Some(encode_string(x.clone())), 's')),
OscType::String(ref x) => Ok((Some(encode_string(x.clone())), 's'.to_string())),
OscType::Blob(ref x) => {
let padded_blob_length: usize = pad(x.len() as u64) as usize;
let mut bytes = vec![0u8; 4 + padded_blob_length];
@ -127,14 +127,40 @@ fn encode_arg(arg: &OscType) -> Result<(Option>, char)> {
for (i, v) in x.iter().enumerate() {
bytes[i + 4] = *v;
}
Ok((Some(bytes), 'b'))
Ok((Some(bytes), 'b'.to_string()))
}
OscType::Time(ref x, ref y) => Ok((Some(encode_time_tag(*x, *y)), 't')),
OscType::Midi(ref x) => Ok((Some(vec![x.port, x.status, x.data1, x.data2]), 'm')),
OscType::Color(ref x) => Ok((Some(vec![x.red, x.green, x.blue, x.alpha]), 'r')),
OscType::Bool(ref x) => if *x { Ok((None, 'T')) } else { Ok((None, 'F')) },
OscType::Nil => Ok((None, 'N')),
OscType::Inf => Ok((None, 'I')),
OscType::Array(ref x) => {
let mut bytes = vec![0u8; x.len()];
let mut type_tags = String::from("[");
for v in x.iter() {
match encode_arg(v).unwrap() {
(Some(other_bytes), other_type_tags) => {
bytes.extend(other_bytes);
type_tags.extend(other_type_tags.chars());
}
(None, other_type_tags) => {
type_tags.extend(other_type_tags.chars());
}
}
}
type_tags = type_tags + "]";
Ok((Some(bytes), type_tags))
}
OscType::Time(ref x, ref y) => Ok((Some(encode_time_tag(*x, *y)), 't'.to_string())),
OscType::Midi(ref x) => Ok((
Some(vec![x.port, x.status, x.data1, x.data2]),
'm'.to_string(),
)),
OscType::Color(ref x) => Ok((Some(vec![x.red, x.green, x.blue, x.alpha]), 'r'.to_string())),
OscType::Bool(ref x) => {
if *x {
Ok((None, 'T'.to_string()))
} else {
Ok((None, 'F'.to_string()))
}
}
OscType::Nil => Ok((None, 'N'.to_string())),
OscType::Inf => Ok((None, 'I'.to_string())),
}
}
@ -172,7 +198,6 @@ pub fn pad(pos: u64) -> u64 {
}
}
fn encode_time_tag(sec: u32, frac: u32) -> Vec<u8> {
let mut bytes = vec![0u8; 8];
BigEndian::write_u32(&mut bytes[..4], sec);

+ 2
- 0
src/types.rs View File

@ -17,6 +17,7 @@ pub enum OscType {
Color(OscColor),
Midi(OscMidiMessage),
Bool(bool),
Array(Vec<OscType>),
Nil,
Inf,
}
@ -45,6 +46,7 @@ value_impl! {
(float, Float, f32),
(string, String, String),
(blob, Blob, Vec<u8>),
(array, Array, Vec<OscType>),
(long, Long, i64),
(double, Double, f64),
(char, Char, char),

Loading…
Cancel
Save