RustAudio / rust-jack

Decent jack bindings for rust
MIT License
206 stars 34 forks source link

Passing user data to a non closure process handler #196

Open phstrauss opened 1 month ago

phstrauss commented 1 month ago

In the following code, a non-closure process handler, how do I pass user data to the jack process handler, like the ports and float buffers for interfacing with the non-RT (GUI) part of my app? Many thanks for sharing rust-jack!

struct Process;

impl jack::ProcessHandler for Process {
    fn process(&mut self, client: &jack::Client, _: &jack::ProcessScope) -> jack::Control {
        /* let mut in_l = client
            .register_port("in_l", jack::AudioIn::default())
            .unwrap();
        let mut in_r = client
            .register_port("in_r", jack::AudioIn::default())
            .unwrap(); */
        jack::Control::Continue
    }
}

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct SinePlot {

    #[serde(skip)]
    line_plot: LinePlot,
    #[serde(skip)]
    jclient: jack::AsyncClient<Notifications, Process>,
}

impl Default for SinePlot {
    fn default() -> Self {
        let (client, _status) = jack::Client::new("birdsong_rs", jack::ClientOptions::NO_START_SERVER).unwrap();

        Self {
            line_plot: LinePlot::default(),
            jclient: client.activate_async(Notifications, Process).unwrap(),
        }
    }
}
wmedrano commented 1 month ago

Data should be initialized in the "constructor" of your Process object. Example:

pub struct Process {
  left: jack::Port<AudioInPort>,
  right: jack::Port<AudioInPort>,
}

impl Process {
    fn new(client: &Client) -> Result<Process, jack::Error> {
        Ok(Process{
            left: client.register_port("in_l", jack::AudioIn::default())?,
            right: client.register_port("in_r", jack::AudioIn::default())?,
        })
    }
}
phstrauss commented 1 month ago

Thanks for being my Rust teacher for 5 minutes :-)