Skip to content

Commit

Permalink
Re-design the library with multi instance support and pass all tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alisomay committed Nov 27, 2024
1 parent 0e485ab commit cd6f364
Show file tree
Hide file tree
Showing 42 changed files with 2,257 additions and 1,667 deletions.
32 changes: 16 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libpd-rs"
version = "0.2.0"
version = "0.3.0"
authors = ["alisomay <[email protected]>"]
edition = "2021"
license = "BSD-3-Clause"
Expand All @@ -17,19 +17,28 @@ exclude = [
"assets/logo_*"
]

[lib]
name = "libpd_rs"
path = "src/lib.rs"
test = true
doctest = true
bench = false
doc = true
edition = "2021"
crate-type = ["lib"]

[dependencies]
# libpd-sys = "0.2"
libpd-sys = { path = "../libpd-sys" }
thiserror = "1.0.30"
libpd-sys = "0.3"
thiserror = "2"
libffi = "3.0.0"
tempfile = "3.3.0"
embed-doc-image = "0.1.4"

[dev-dependencies]
cpal = "0.15.2"
sys-info = "0.9.1"
nannou = "0.18"
nannou_audio = "0.18"
nannou = "0.19"
nannou_audio = "0.19"
rand = "0.8.5"

# For local development,
Expand All @@ -40,15 +49,6 @@ rand = "0.8.5"
# [patch."https://github.com/alisomay/libpd-sys"]
# libpd-sys = { path = "../libpd-sys" }

[lib]
name = "libpd_rs" # The name of the target.
path = "src/lib.rs" # The source file of the target.
test = true # Is tested by default.
doctest = true # Documentation examples are tested by default.
bench = false # Is benchmarked by default.
doc = true # Is documented by default.
proc-macro = false # Set to `true` for a proc-macro library.
edition = "2021" # The edition of the target.
crate-type = ["lib"] # The crate types to generate.



2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Here if we had an input buffer we could have modified it to do pre-processing.

// Process audio, advance internal scheduler.
libpd_rs::process::process_float(ticks, &[], data);
libpd_rs::functions::process::process_float(ticks, &[], data);

// Here we could have done post processing after pd processed our output buffer in place.
},
Expand Down
9 changes: 5 additions & 4 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use libpd_rs::convenience::PdGlobal;
use libpd_rs::Pd;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize cpal
Expand All @@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Initialize libpd with that configuration,
// with no input channels since we're not going to use them.
let mut pd = PdGlobal::init_and_configure(0, output_channels, sample_rate)?;
let mut pd = Pd::init_and_configure(0, output_channels, sample_rate)?;

// Let's evaluate a pd patch.
// We could have opened a `.pd` file also.
Expand All @@ -43,12 +43,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
&config.into(),
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
// Provide the ticks to advance per iteration for the internal scheduler.
let ticks = libpd_rs::convenience::calculate_ticks(output_channels, data.len() as i32);
let ticks =
libpd_rs::functions::util::calculate_ticks(output_channels, data.len() as i32);

// Here if we had an input buffer we could have modified it to do pre-processing.

// Process audio, advance internal scheduler.
libpd_rs::process::process_float(ticks, &[], data);
libpd_rs::functions::process::process_float(ticks, &[], data);

// Here we could have done post processing after pd processed our output buffer in place.
},
Expand Down
3 changes: 2 additions & 1 deletion examples/with_nannou/bubble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ impl Bubble {
// Collision with the floor!
if distance_to_floor < self.properties.r * 2.0 {
// On collision we tell the right voice to play with the right parameters in pd.
libpd_rs::send::send_list_to("bubble_collision", &self.pack_message()).unwrap();
libpd_rs::functions::send::send_list_to("bubble_collision", &self.pack_message())
.unwrap();

// Physics
self.properties.dy = -self.properties.dy;
Expand Down
19 changes: 7 additions & 12 deletions examples/with_nannou/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {

// This data structure will be shared across nannou functions.
pub struct Model {
pd: libpd_rs::convenience::PdGlobal,
pd: libpd_rs::Pd,
output_stream: audio::Stream<()>,
gravity: f32,
bubbles: RefCell<Vec<Bubble>>,
Expand Down Expand Up @@ -62,19 +62,14 @@ fn model(app: &App) -> Model {
.unwrap();

// Listen for console messages from pd
libpd_rs::receive::on_print(|val| {
libpd_rs::functions::receive::on_print(|val| {
println!("{}", val);
});

// This data structure will be shared across nannou functions.
let mut model = Model {
// Initialize pd
pd: libpd_rs::convenience::PdGlobal::init_and_configure(
0,
channels as i32,
sample_rate as i32,
)
.unwrap(),
pd: libpd_rs::Pd::init_and_configure(0, channels as i32, sample_rate as i32).unwrap(),
output_stream,
gravity: 0.8,
bubbles: RefCell::new(vec![]),
Expand All @@ -95,7 +90,7 @@ fn model(app: &App) -> Model {

// Initially pd needs to know how many bubbles we have.
// Because it will create adequate amount of voices for them.
libpd_rs::send::send_float_to("bubble_count", model.bubble_count as f32).unwrap();
libpd_rs::functions::send::send_float_to("bubble_count", model.bubble_count as f32).unwrap();

// Run pd!
model.pd.activate_audio(true).unwrap();
Expand Down Expand Up @@ -145,14 +140,14 @@ impl Model {
// We hand over all tasks to our pd patch!
fn audio_callback(_: &mut (), buffer: &mut Buffer) {
let ticks =
libpd_rs::convenience::calculate_ticks(buffer.channels() as i32, buffer.len() as i32);
libpd_rs::process::process_float(ticks, &[], buffer);
libpd_rs::functions::util::calculate_ticks(buffer.channels() as i32, buffer.len() as i32);
libpd_rs::functions::process::process_float(ticks, &[], buffer);
}

// This is where we draw repeatedly!
fn view(app: &App, model: &Model, frame: Frame) {
// Let's poll pd messages here, for every frame.
libpd_rs::receive::receive_messages_from_pd();
libpd_rs::functions::receive::receive_messages_from_pd();

let background_color = nannou::color::srgb8(238, 108, 77);

Expand Down
Loading

0 comments on commit cd6f364

Please sign in to comment.