Skip to content

Commit 750831f

Browse files
committed
Adapt examples
1 parent 12edec6 commit 750831f

File tree

14 files changed

+564
-313
lines changed

14 files changed

+564
-313
lines changed

Cargo.toml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ repository = "https://github.com/alisomay/libpd-rs"
1111
documentation = "https://docs.rs/libpd-rs/latest/libpd_rs/#"
1212
keywords = ["puredata", "libpd", "audio", "midi", "bindings"]
1313
categories = ["multimedia"]
14-
exclude = [
15-
"tests/*",
16-
"assets/favicon/*",
17-
"assets/logo_*"
18-
]
14+
exclude = ["tests/*", "assets/favicon/*", "assets/logo_*"]
1915

2016
[lib]
2117
name = "libpd_rs"
@@ -35,11 +31,12 @@ tempfile = "3.3.0"
3531
embed-doc-image = "0.1.4"
3632

3733
[dev-dependencies]
38-
cpal = "0.15.2"
34+
cpal = "0.15.3"
3935
sys-info = "0.9.1"
4036
nannou = "0.19"
4137
nannou_audio = "0.19"
4238
rand = "0.8.5"
39+
serial_test = "3"
4340

4441
# For local development,
4542
# [patch.crates-io]
@@ -48,7 +45,3 @@ rand = "0.8.5"
4845
# For local development,
4946
# [patch."https://github.com/alisomay/libpd-sys"]
5047
# libpd-sys = { path = "../libpd-sys" }
51-
52-
53-
54-

examples/simple.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2020
// Initialize libpd with that configuration,
2121
// with no input channels since we're not going to use them.
2222
let mut pd = Pd::init_and_configure(0, output_channels, sample_rate)?;
23+
let mut ctx = pd.audio_context();
2324

2425
// Let's evaluate a pd patch.
2526
// We could have opened a `.pd` file also.
@@ -49,7 +50,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4950
// Here if we had an input buffer we could have modified it to do pre-processing.
5051

5152
// Process audio, advance internal scheduler.
52-
libpd_rs::functions::process::process_float(ticks, &[], data);
53+
ctx.process_float(ticks, &[], data);
5354

5455
// Here we could have done post processing after pd processed our output buffer in place.
5556
},

examples/with_nannou/bubble.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl Bubble {
140140
}
141141

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

213213
// Collision with the floor!
214214
if distance_to_floor < self.properties.r * 2.0 {
215+
model.pd.set_as_current();
215216
// On collision we tell the right voice to play with the right parameters in pd.
216217
libpd_rs::functions::send::send_list_to("bubble_collision", &self.pack_message())
217218
.unwrap();

examples/with_nannou/main.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
mod bubble;
33

44
use bubble::Bubble;
5+
use libpd_rs::PdAudioContext;
56
use nannou::prelude::*;
67
use nannou_audio as audio;
78
use nannou_audio::Buffer;
@@ -15,7 +16,7 @@ fn main() {
1516
// This data structure will be shared across nannou functions.
1617
pub struct Model {
1718
pd: libpd_rs::Pd,
18-
output_stream: audio::Stream<()>,
19+
output_stream: audio::Stream<PdAudioContext>,
1920
gravity: f32,
2021
bubbles: RefCell<Vec<Bubble>>,
2122
bubble_count: usize,
@@ -50,9 +51,13 @@ fn model(app: &App) -> Model {
5051
// .into_iter()
5152
// .find(|d| d.name().unwrap() == "BlackHole 16ch");
5253

54+
let pd = libpd_rs::Pd::init_and_configure(0, channels as i32, sample_rate as i32).unwrap();
55+
pd.set_as_current();
56+
let audio_ctx = pd.audio_context();
57+
5358
// Start the stream registering our audio callback.
5459
let output_stream = audio_host
55-
.new_output_stream(())
60+
.new_output_stream(audio_ctx)
5661
// Uncomment to pick another audio device.
5762
// .device(device.unwrap())
5863
.channels(channels)
@@ -69,7 +74,7 @@ fn model(app: &App) -> Model {
6974
// This data structure will be shared across nannou functions.
7075
let mut model = Model {
7176
// Initialize pd
72-
pd: libpd_rs::Pd::init_and_configure(0, channels as i32, sample_rate as i32).unwrap(),
77+
pd,
7378
output_stream,
7479
gravity: 0.8,
7580
bubbles: RefCell::new(vec![]),
@@ -138,15 +143,16 @@ impl Model {
138143

139144
// This is where we process audio.
140145
// We hand over all tasks to our pd patch!
141-
fn audio_callback(_: &mut (), buffer: &mut Buffer) {
146+
fn audio_callback(pd_audio_context: &mut PdAudioContext, buffer: &mut Buffer) {
142147
let ticks =
143148
libpd_rs::functions::util::calculate_ticks(buffer.channels() as i32, buffer.len() as i32);
144-
libpd_rs::functions::process::process_float(ticks, &[], buffer);
149+
pd_audio_context.process_float(ticks, &[], buffer);
145150
}
146151

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

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

0 commit comments

Comments
 (0)