weirdnet-audio-engine 0.5.0

Audio engine pieces for Weirdnet: the AudioSink trait, a manual sink for tests, a cpal device sink (feature `cpal`) and a waveform player.
Documentation
//! The router as a local executor of a `weirdnet_audio_route` graph (feature `route`).

use crate::router::{Route, RouterControl, RouterError};
use crate::talkback::db_to_gain;
use weirdnet_audio_route::{Executor, Graph, PortId, RouteError};

impl Executor for RouterControl {
    /// Routes every enabled connection whose two ports are endpoints of this router (the port id is the endpoint id), with the
    /// connection's gain. The graph owns the routes between its ports: those it no longer has go, and routes with an end the graph
    /// does not know (the router's own wiring, such as a bus to the device) stay. Connections with an end elsewhere are left for a
    /// remote executor and listed by [`RouterControl::remote_connections`]. A refused graph leaves the router as it was.
    fn apply(&mut self, graph: &Graph) -> Result<(), RouteError> {
        let known = |id: &str| graph.port(&PortId::new(id)).is_some();
        let mut routes: Vec<Route> = self.routes().iter().filter(|r| !(known(&r.from) && known(&r.to))).cloned().collect();
        let mut skipped = Vec::new();
        for c in graph.connections() {
            if !c.enabled {
                continue;
            }
            if self.kind(c.from.as_str()).is_some() && self.kind(c.to.as_str()).is_some() {
                routes.push(Route { from: c.from.as_str().to_string(), to: c.to.as_str().to_string(), gain: db_to_gain(c.gain_db) });
            } else {
                skipped.push(c.id.0.clone());
            }
        }
        self.set_routes(routes).map_err(|e| match e {
            RouterError::Cycle(_) => RouteError::Cycle,
            RouterError::Unknown(id) => RouteError::UnknownPort(PortId::new(&id)),
            RouterError::NotAnOutput(_) | RouterError::NotAnInput(_) => RouteError::WrongDirection,
            RouterError::Duplicate(..) => RouteError::Duplicate,
            other => RouteError::Denied(other.to_string()),
        })?;
        self.skipped = skipped;
        Ok(())
    }
}

impl RouterControl {
    /// The ids of the connections the last [`Executor::apply`] left alone because an end is not here.
    pub fn remote_connections(&self) -> &[String] {
        &self.skipped
    }
}