Skip to content

Commit 10e2511

Browse files
committed
Add an OSC sender method to send OSC data from STDIN
1 parent d84b8e9 commit 10e2511

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed

src/main.rs

+80-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ mod midi_io;
22
mod osc_io;
33

44
use clap::{Arg, Command};
5+
use osc_io::OscSender;
6+
use std::io::{self, BufRead};
57
use std::str::FromStr;
68
use std::net::{SocketAddrV4};
79

@@ -153,17 +155,14 @@ impl OscToMidi{
153155
if OscToMidi::verbose() {
154156
println!("Ignored message on OSC address: {:?}", msg.addr.as_str());
155157
}
156-
157-
158158
}
159159
}
160160

161161
OscPacket::Bundle(bundle) => {
162162
println!("OSC Bundle: {:?}", bundle);
163163
}
164164
}
165-
166-
0
165+
return 0;
167166
}
168167
}
169168

@@ -209,6 +208,66 @@ fn is_host_with_port(v: &str) -> Result<String,String>{
209208
}
210209
}
211210

211+
fn osc_send(osc_target_host_address: &str, verbose: bool) {
212+
213+
let stdin = io::stdin();
214+
215+
let osc_sender = OscSender::new(osc_target_host_address.to_string());
216+
for line in stdin.lock().lines() {
217+
let line = line.unwrap(); // Handle potential error
218+
let mut tokens: Vec<&str> = line.split_whitespace().collect();
219+
220+
221+
// do not send empty messages
222+
if tokens.len() == 0 {
223+
if verbose {
224+
print!("Empty message; nothing send\n");
225+
}
226+
continue;
227+
}
228+
229+
// else
230+
231+
// check if the first token is a valid osc method (starts with '/')
232+
let osc_method = match tokens.get(0) {
233+
Some(v) => {
234+
if v.starts_with('/') {
235+
v.to_string()
236+
} else {
237+
format!("/{}", v)
238+
}
239+
},
240+
None => {
241+
println!("No OSC method provided");
242+
continue;
243+
}
244+
};
245+
246+
// remove the first token
247+
tokens.remove(0);
248+
249+
let mut osc_args: Vec<OscType> = Vec::new();
250+
251+
for token in tokens {
252+
if let Ok(int) = token.parse::<i32>() {
253+
osc_args.push(OscType::Int(int));
254+
} else if let Ok(float) = token.parse::<f32>() {
255+
osc_args.push(OscType::Float(float));
256+
} else {
257+
osc_args.push(OscType::String(token.to_string()));
258+
}
259+
}
260+
261+
osc_sender.send(osc_method.to_string(), osc_args.clone());
262+
263+
if verbose {
264+
print!("Sent OSC message to {} with args {:?}\n", osc_target_host_address, osc_args);
265+
}
266+
}
267+
}
268+
269+
270+
212271
fn main() {
213272
let matches = Command::new("mot")
214273
.bin_name("mot")
@@ -288,6 +347,16 @@ fn main() {
288347
.help("the host:port to receive OSC data")
289348
.value_parser(is_host_with_port))
290349
)
350+
.subcommand(Command::new("osc_send")
351+
.about("Send OSC messages from STDIN. The first token of each line is the OSC method, the rest are the arguments. Only floats, ints and strings are converted to OSC types.")
352+
.arg(Arg::new("verbose")
353+
.short('v')
354+
.help("print verbose information"))
355+
.arg(Arg::new("host:port")
356+
.default_value("127.0.0.1:1234")
357+
.help("the host:port to send OSC data to")
358+
.value_parser(is_host_with_port))
359+
)
291360
.subcommand(Command::new("midi_roundtrip_latency")
292361
.about("Test MIDI roundtrip latency")
293362
.arg(Arg::new("list")
@@ -316,10 +385,16 @@ fn main() {
316385
//let midi_input_index = usize::from_str(sub_matches.value_of("midi_input_index").unwrap()).unwrap();
317386
if midi_io::MidiIn::check_midi_input_port_index(midi_input_index) {
318387
MidiEcho::new(midi_input_index).echo_midi();
319-
}
388+
}
320389
}
321390
}
322391

392+
if let Some(sub_matches) = matches.subcommand_matches("osc_send") {
393+
let osc_target_host_address = sub_matches.get_one::<String>("host:port").unwrap();
394+
let verbose = sub_matches.value_source("verbose") == Some(clap::parser::ValueSource::CommandLine);
395+
osc_send(osc_target_host_address, verbose);
396+
}
397+
323398
if let Some(sub_matches) = matches.subcommand_matches("midi_to_osc") {
324399
if sub_matches.value_source("list") == Some(clap::parser::ValueSource::CommandLine) {
325400
midi_io::MidiIn::list_midi_input_ports();

0 commit comments

Comments
 (0)