You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7KB

  1. extern crate rosc;
  2. use rosc::encoder;
  3. use rosc::{OscMessage, OscPacket, OscType};
  4. use std::net::{SocketAddrV4, UdpSocket};
  5. use std::str::FromStr;
  6. use std::time::Duration;
  7. use std::{env, f32, thread};
  8. fn get_addr_from_arg(arg: &str) -> SocketAddrV4 {
  9. SocketAddrV4::from_str(arg).unwrap()
  10. }
  11. fn main() {
  12. let args: Vec<String> = env::args().collect();
  13. let usage = format!(
  14. "Usage: {} HOST_IP:HOST_PORT CLIENT_IP:CLIENT_PORT",
  15. &args[0]
  16. );
  17. if args.len() < 3 {
  18. panic!(usage);
  19. }
  20. let host_addr = get_addr_from_arg(&args[1]);
  21. let to_addr = get_addr_from_arg(&args[2]);
  22. let sock = UdpSocket::bind(host_addr).unwrap();
  23. // switch view
  24. let msg_buf = encoder::encode(&OscPacket::Message(OscMessage {
  25. addr: "/3".to_string(),
  26. args: vec![],
  27. }))
  28. .unwrap();
  29. sock.send_to(&msg_buf, to_addr).unwrap();
  30. // send random values to xy fields
  31. let steps = 128;
  32. let step_size: f32 = 2.0 * f32::consts::PI / steps as f32;
  33. for i in 0.. {
  34. let x = 0.5 + (step_size * (i % steps) as f32).sin() / 2.0;
  35. let y = 0.5 + (step_size * (i % steps) as f32).cos() / 2.0;
  36. let mut msg_buf = encoder::encode(&OscPacket::Message(OscMessage {
  37. addr: "/3/xy1".to_string(),
  38. args: vec![OscType::Float(x), OscType::Float(y)],
  39. }))
  40. .unwrap();
  41. sock.send_to(&msg_buf, to_addr).unwrap();
  42. msg_buf = encoder::encode(&OscPacket::Message(OscMessage {
  43. addr: "/3/xy2".to_string(),
  44. args: vec![OscType::Float(y), OscType::Float(x)],
  45. }))
  46. .unwrap();
  47. sock.send_to(&msg_buf, to_addr).unwrap();
  48. thread::sleep(Duration::from_millis(20));
  49. }
  50. }