Expand description
Send messages to pd
Collection of functions where messages may be sent to pd
Examples
use libpd_rs::{
close_patch,
init, initialize_audio, open_patch,
process::process_float,
convenience::{dsp_on, calculate_ticks},
receive::{receive_messages_from_pd, on_print, on_float, start_listening_from},
send::{send_float_to}
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
init()?;
initialize_audio(1, 2, 44100)?;
// Place your own patch here.
let patch_handle = open_patch("tests/patches/echo.pd")?;
// Turn the dsp on
dsp_on()?;
// Register some listeners
// Print is a special one which is always listened from.
on_print(|value| {
println!("{value}");
});
on_float(|source, value| {
assert_eq!(source, "float_from_pd");
assert_eq!(value, 42.0);
println!("{value} received from {source}");
});
// For others we need to register them.
start_listening_from("float_from_pd")?;
// We're going to treat this separate thread as the
// high priority audio callback from the OS for this example.
// Open some channels to communicate with it.
let (tx, rx) = std::sync::mpsc::channel::<()>();
let handle = std::thread::spawn(move || {
// Mimic audio callback buffers.
let input_buffer = [0.0f32; 512];
let mut output_buffer = [0.0f32; 1024];
let output_channels = 2;
let sample_rate = 44100;
// Run pd
loop {
// Mimic the call frequency of the audio callback.
let approximate_buffer_duration =
(output_buffer.len() as f32 / sample_rate as f32) * 1000.0;
std::thread::sleep(std::time::Duration::from_millis(
approximate_buffer_duration as u64,
));
// Receive messages from pd.
receive_messages_from_pd();
// Calculate ticks for the internal scheduler
let ticks = calculate_ticks(output_buffer.len() as i32, output_channels);
// Process the audio and advance the internal scheduler by the number of ticks.
process_float(ticks, &input_buffer, &mut output_buffer);
// This is just meaningful for this example,
// so we can break from this loop.
match rx.try_recv() {
Ok(_) => break,
_ => continue,
}
}
});
// When processing starts pd becomes alive because the scheduler is running.
// Let's send a float to pd, it will be caught by the float listener,
// because our patch which we've loaded, echoes it back.
send_float_to("float_from_rust", 42.0)?;
// After some time
std::thread::sleep(std::time::Duration::from_millis(1000));
// We may join the thread.
tx.send(()).unwrap();
handle.join().unwrap();
// And close the patch
close_patch(patch_handle)?;
Ok(())
}Functions
Adds an f64 to the current message in the progress of composition
Adds an f32 to the current message in the progress of composition
Adds a symbol to the current message in the progress of composition
Finishes the current message and send as a list to a receiver in the loaded pd patch
Finishes the current message and send as a typed message to a receiver in the loaded pd patch
Sends a MIDI after touch message to |touchin| objects in pd.
Sends a bang to the pd receiver object specified in receiver the argument
Sends a MIDI control change message to ctlin objects in pd.
Sends an f64 value to the pd receiver object specified in the receiver argument
Sends an f32 value to the pd receiver object specified in the receiver argument
Sends a list to a receiver in the loaded pd patch
Sends a typed message to a receiver in the loaded pd patch
Sends a raw MIDI byte to |midiin| objects in pd.
Sends a MIDI note on message to |notein| objects in pd.
Sends a MIDI pitch bend message to |bendin| objects in pd.
Sends a MIDI poly after touch message to |polytouchin| objects in pd.
Sends a MIDI program change message to pgmin objects in pd.
Sends a symbol to the pd receiver object specified in the receiver argument
Sends a raw MIDI byte to |midirealtimein| objects in pd.
Sends a raw MIDI byte to |sysexin| objects in pd.
Start composition of a new list or typed message of up to max element length
