improvements and cleanup

This commit is contained in:
Franz Heinzmann (Frando) 2021-02-19 18:51:09 +01:00
parent 091e6319cd
commit 7558fd3fe9
6 changed files with 107 additions and 127 deletions

View file

@ -1,21 +1,10 @@
use async_std::stream::{StreamExt};
use async_std::stream::StreamExt;
use log::*;
use rosc::{OscMessage, OscPacket, OscType};
use studiox::switcher::{SwitcherError, SwitcherEvent, SwitcherHandle};
// use studiox::udp::UdpStream;
use studiox::osc::OscStream;
use studiox::spawn::FaustHandle;
use studiox::switcher::{SwitcherError, SwitcherEvent, SwitcherHandle};
pub enum Event {
FaustRx(Result<OscPacket, SwitcherError>),
@ -25,34 +14,32 @@ pub enum Event {
#[async_std::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
// let child_task: JoinHandle<io::Result<()>> = task::spawn(async {
// let handle = FaustHandle::spawn().await;
// match handle {
// Ok(handle) => {
// info!("FAUST mixer launched: {:?}", handle)
// }
// Err(e) => error!("Launching FAUST mixer failed: {:?}", e),
// }
// eprintln!("spawn done");
// Ok(())
// // eprintln!("HANDLE: {:?}", handle);
// });
let handle = FaustHandle::spawn().await;
match handle {
let faust_handle = FaustHandle::spawn().await;
match faust_handle.as_ref() {
Ok(handle) => {
info!("FAUST mixer launched: {:?}", handle)
}
Err(e) => error!("Launching FAUST mixer failed: {:?}", e),
Err(e) => {
error!("Launching FAUST mixer failed: {:?}", e);
}
}
let port_faust_tx = 5510;
let port_faust_rx = 5511;
let port_commands = 5590;
let addr_faust_tx = format!("localhost:{}", port_faust_tx);
let addr_faust_rx = format!("localhost:{}", port_faust_rx);
let mut faust_handle = faust_handle?;
let res = main_loop(&faust_handle).await;
match res.as_ref() {
Ok(_) => {}
Err(e) => error!("Error: {:?}", e),
}
faust_handle.child.kill()?;
res
}
async fn main_loop(faust_handle: &FaustHandle) -> anyhow::Result<()> {
// let port_faust_tx = 5510;
// let port_faust_rx = 5511;
let port_commands: u16 = std::env::var("PORT").unwrap_or("5590".into()).parse()?;
let addr_faust_tx = format!("localhost:{}", faust_handle.port_tx);
let addr_faust_rx = format!("localhost:{}", faust_handle.port_rx);
let addr_commands = format!("localhost:{}", port_commands);
let mut switcher = SwitcherHandle::run();
@ -65,7 +52,7 @@ async fn main() -> anyhow::Result<()> {
let mut events = faust_rx.merge(switcher_rx).merge(command_osc_rx);
info!("start main loop");
info!("Listening for OSC commands on localhost:{}", port_commands);
while let Some(event) = events.next().await {
match event {
Event::Switcher(message) => {
@ -74,12 +61,12 @@ async fn main() -> anyhow::Result<()> {
command_osc_tx.send_to(packet, &addr_faust_tx).await?;
}
Event::FaustRx(packet) => {
let packet = packet?;
debug!("faust rx: {:?}", packet);
let _packet = packet?;
}
Event::CommandRx(packet) => {
let packet = packet?;
debug!("command rx: {:?}", packet);
let packet = packet?;
let event = parse_switcher_command(packet);
if let Some(event) = event {
switcher.send(event).await?;