Skip to content

Commit

Permalink
Adapt examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alisomay committed Nov 28, 2024
1 parent 12edec6 commit 750831f
Show file tree
Hide file tree
Showing 14 changed files with 564 additions and 313 deletions.
13 changes: 3 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ repository = "https://github.com/alisomay/libpd-rs"
documentation = "https://docs.rs/libpd-rs/latest/libpd_rs/#"
keywords = ["puredata", "libpd", "audio", "midi", "bindings"]
categories = ["multimedia"]
exclude = [
"tests/*",
"assets/favicon/*",
"assets/logo_*"
]
exclude = ["tests/*", "assets/favicon/*", "assets/logo_*"]

[lib]
name = "libpd_rs"
Expand All @@ -35,11 +31,12 @@ tempfile = "3.3.0"
embed-doc-image = "0.1.4"

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

# For local development,
# [patch.crates-io]
Expand All @@ -48,7 +45,3 @@ rand = "0.8.5"
# For local development,
# [patch."https://github.com/alisomay/libpd-sys"]
# libpd-sys = { path = "../libpd-sys" }




3 changes: 2 additions & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 = Pd::init_and_configure(0, output_channels, sample_rate)?;
let mut ctx = pd.audio_context();

// Let's evaluate a pd patch.
// We could have opened a `.pd` file also.
Expand Down Expand Up @@ -49,7 +50,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::functions::process::process_float(ticks, &[], data);
ctx.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 @@ -140,7 +140,7 @@ impl Bubble {
}

/// Transforms the voice message of the bubble to send to pure data.
pub fn pack_message(&self) -> Vec<libpd_rs::types::Atom> {
pub fn pack_message(&self) -> Vec<libpd_rs::atom::Atom> {
self.state
.message
.into_iter()
Expand Down Expand Up @@ -212,6 +212,7 @@ impl Bubble {

// Collision with the floor!
if distance_to_floor < self.properties.r * 2.0 {
model.pd.set_as_current();
// On collision we tell the right voice to play with the right parameters in pd.
libpd_rs::functions::send::send_list_to("bubble_collision", &self.pack_message())
.unwrap();
Expand Down
16 changes: 11 additions & 5 deletions examples/with_nannou/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod bubble;

use bubble::Bubble;
use libpd_rs::PdAudioContext;
use nannou::prelude::*;
use nannou_audio as audio;
use nannou_audio::Buffer;
Expand All @@ -15,7 +16,7 @@ fn main() {
// This data structure will be shared across nannou functions.
pub struct Model {
pd: libpd_rs::Pd,
output_stream: audio::Stream<()>,
output_stream: audio::Stream<PdAudioContext>,
gravity: f32,
bubbles: RefCell<Vec<Bubble>>,
bubble_count: usize,
Expand Down Expand Up @@ -50,9 +51,13 @@ fn model(app: &App) -> Model {
// .into_iter()
// .find(|d| d.name().unwrap() == "BlackHole 16ch");

let pd = libpd_rs::Pd::init_and_configure(0, channels as i32, sample_rate as i32).unwrap();
pd.set_as_current();
let audio_ctx = pd.audio_context();

// Start the stream registering our audio callback.
let output_stream = audio_host
.new_output_stream(())
.new_output_stream(audio_ctx)
// Uncomment to pick another audio device.
// .device(device.unwrap())
.channels(channels)
Expand All @@ -69,7 +74,7 @@ fn model(app: &App) -> Model {
// This data structure will be shared across nannou functions.
let mut model = Model {
// Initialize pd
pd: libpd_rs::Pd::init_and_configure(0, channels as i32, sample_rate as i32).unwrap(),
pd,
output_stream,
gravity: 0.8,
bubbles: RefCell::new(vec![]),
Expand Down Expand Up @@ -138,15 +143,16 @@ impl Model {

// This is where we process audio.
// We hand over all tasks to our pd patch!
fn audio_callback(_: &mut (), buffer: &mut Buffer) {
fn audio_callback(pd_audio_context: &mut PdAudioContext, buffer: &mut Buffer) {
let ticks =
libpd_rs::functions::util::calculate_ticks(buffer.channels() as i32, buffer.len() as i32);
libpd_rs::functions::process::process_float(ticks, &[], buffer);
pd_audio_context.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.
model.pd.set_as_current();
libpd_rs::functions::receive::receive_messages_from_pd();

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

0 comments on commit 750831f

Please sign in to comment.