add VmcExtMidiNote

This commit is contained in:
Cassandra de la Cruz-Munoz 2023-12-30 10:35:01 -05:00
parent cc84da6563
commit 9c231edec3

View File

@ -549,3 +549,69 @@ impl MessageBehavior for VmcExtKey {
});
}
}
struct VmcExtMidiNote {
active: bool,
channel: i32,
note: i32,
velocity: f32,
}
impl MessageBehavior for VmcExtMidiNote {
fn to_sendable_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>;
if self.active {
args = vec![OscType::from(1)];
} else {
args = vec![OscType::from(0)];
}
args.append(&mut vec![
OscType::from(self.channel),
OscType::from(self.note),
OscType::from(self.velocity),
]);
return OscMessage {
addr: String::from("/VMC/Ext/Midi/Note"),
args,
};
}
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 4 {
return Err(String::from("arg count invalid"));
}
let active: bool;
let channel: i32;
let note: i32;
let velocity: f32;
match msg.args[0] {
OscType::Int(i) => {
if i == 1 {
active = true;
} else if i == 0 {
active = false;
} else {
return Err(String::from("arg value invalid"));
}
}
_ => return Err(String::from("arg type invalid")),
}
match msg.args[1] {
OscType::Int(i) => channel = i,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[2] {
OscType::Int(i) => note = i,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[3] {
OscType::Float(f) => velocity = f,
_ => return Err(String::from("arg type invalid")),
}
return Ok(VmcExtMidiNote {
active,
channel,
note,
velocity,
});
}
}