weirdnet-streaming 0.1.0

Streaming for Weirdnet: live and recorded audio streams over weirdnet-net with per-listener queues, clocks and remote routing, a follow-transport protocol (start, stop, locate, tempo, beacon), and loop-positioned streams with alignment for record-along.
Documentation
//! A master and a follower on clocks 3 s apart: the master starts the transport, the follower learns the master's clock from the
//! message and tells its engine to start on the right audio block, then keeps in step; a tempo change acts at once. The messages
//! are plain values (here sent as JSON); an app carries them over weirdnet-net or anything else.
//!
//! Run with `cargo run --example transport`.
use weirdnet_streaming::transport::{Action, Follower, Observed, Transport};

const RATE: u32 = 48_000;
const BLOCK: u64 = 256;
const LOOP: u64 = 96_000;
/// The follower's monotonic clock reads this much more than the master's.
const SKEW_NS: u64 = 3_000_000_000;
const BLOCK_NS: u64 = BLOCK * 1_000_000_000 / RATE as u64;

/// A stand-in for an audio engine: commands take effect at the next block, and it reports its last block.
#[derive(Default)]
struct Engine {
    transport: u64,
    playing: bool,
    output_frame: u64,
    pending: Option<Action>,
    last: (u64, u64, bool),
}

impl Engine {
    fn block(&mut self, local_ns: u64) {
        match self.pending.take() {
            Some(Action::Start(f)) => (self.transport, self.playing) = (f, true),
            Some(Action::Locate(f)) => self.transport = f,
            Some(Action::Stop) => self.playing = false,
            Some(Action::Tempo(_)) | None => {}
        }
        self.last = (local_ns, self.transport, self.playing);
        self.output_frame += BLOCK;
        if self.playing {
            self.transport = (self.transport + BLOCK) % LOOP;
        }
    }

    fn observed(&self, now_ns: u64) -> Observed {
        Observed {
            now_ns,
            callback_ns: self.last.0,
            output_frame: self.output_frame - BLOCK,
            transport_frame: self.last.1,
            playing: self.last.2,
            block_frames: BLOCK as u32,
            sample_rate: RATE,
            loop_frames: LOOP,
        }
    }
}

fn main() {
    let mut follower = Follower::new(128);
    let mut engine = Engine::default();

    // The master says: transport frame 0 plays at session time 1.0 s. It sends at session time 0.5 s.
    let wire = serde_json::to_string(&Transport::Start { at_ns: 1_000_000_000, frame: 0, master_ns: 500_000_000 }).expect("JSON");
    println!("master -> {wire}");
    let msg: Transport = serde_json::from_str(&wire).expect("it parses back");
    let mut local = 500_000_000 + SKEW_NS + 2_000_000; // two milliseconds in flight
    follower.on_message(&msg, local);
    println!("follower learned the master's clock: offset {:?} ns", follower.clock().offset());

    let mut started = None;
    for block in 0..200u64 {
        local += BLOCK_NS;
        engine.block(local);
        if let Some(action) = follower.step(&engine.observed(local + 1_000_000)) {
            if started.is_none() {
                println!("block {block}: {action:?}");
                started = Some(block);
            }
            engine.pending = Some(action);
        }
    }
    let session_now = local - SKEW_NS;
    let expected = (session_now - 1_000_000_000) * u64::from(RATE) / 1_000_000_000;
    println!("after {} ms of session time the engine is at frame {} (due: about {expected})", session_now / 1_000_000, engine.transport);

    println!("tempo -> {:?}", follower.on_message(&Transport::Tempo { bpm: 96.0, master_ns: session_now }, local));
}