Expand description
Work with pd arrays
This module provides all tools to work with pd named arrays which are exposed by libpd with some extra safety such as bounds checking.
Corresponding libpd functions in libpd repository could be explored here.
Examples
use libpd_rs::{
array::{array_size, read_float_array_from, resize_array, write_float_array_to},
close_patch,
init, initialize_audio, open_patch,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
init()?;
initialize_audio(1, 2, 44100)?;
// Place your own patch here.
let handle = open_patch("tests/patches/array_sketch_pad.pd")?;
let size = array_size("sketch_pad")?;
// Arrays are sized to 100 by default.
assert_eq!(size, 100);
// We can resize this array to 8.
resize_array("sketch_pad", 8)?;
let size = array_size("sketch_pad")?;
assert_eq!(size, 8);
// Let's write some stuff to our array.
write_float_array_to("sketch_pad", 0, &[1.0, 2.0, 3.0, 4.0], 4)?;
// Let's overwrite the second part of the array with the first part of our slice.
write_float_array_to("sketch_pad", 2, &[500.0, 600.0, 3.0, 4.0], 2)?;
// Now we can read the array to the second half of our slice.
let mut read_to: Vec<f32> = vec![0.0; 4];
// Read it all back.
read_float_array_from("sketch_pad", 0, 4, &mut read_to)?;
assert_eq!(read_to, [1.0, 2.0, 500.0, 600.0]);
// Now we can read the second half of our array to our first half of our slice.
read_float_array_from("sketch_pad", 2, 2, &mut read_to)?;
assert_eq!(read_to, [500.0, 600.0, 500.0, 600.0]);
close_patch(handle)?;
Ok(())
}
Functions
Gets the size of an array by its name from the pd patch which is loaded.
Reads a named array from pd to a mutable slice of f64
.
Reads a named array from pd to a mutable slice of f32
.
Resizes an array found by its name from the pd patch which is loaded.
Writes a slice of f64
to a pd named array.
Writes a slice of f32
to a pd named array.