Skip to content

alisomay/libpd-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

libpd-rs

Build and Test Status Code Coverage

Safe rust abstractions over libpd-sys.

Pure Data (Pd) is a visual programming language developed by Miller Puckette in the 1990s for creating interactive computer music and multimedia works. While Puckette is the main author of the program, Pd is an open-source project with a large developer base working on new extensions. It is released under BSD-3-Clause.

Though pd is designed as a desktop application, libpd is an open source project which exposes it as a C library opening the possibility to embed the functionality of pd to any platform which C can compile to.

libpd-rs aims to bring libpd to the Rust ecosystem. It aims to expose the full functionality of libpd with some extra additions such as bundling commonly used externals and addition of extra functionality for increased ease of use.

It is thoroughly documented, well tested and enriched with various examples to get you started right away.

Now let's make some sound! πŸ””

Build dependencies

To be able to buid libpd install cmake and llvm if you don't have them already. See building libpd for more details and options.

Linux

sudo apt install cmake

Follow the llvm installation instructions

windows

With chocolately

choco install cmake
choco install llvm

With winget

winget install -e --id Kitware.CMake
winget install -e --id LLVM.LLVM

macOS

brew install cmake
brew install llvm

Cargo dependencies

Add the following dependencies to your Cargo.toml:

[dependencies]
libpd-rs = "0.1"
cpal = "0.15"

Example code

Paste the code into your main.rs:

⚠️ Warning ⚠️: This example will produce audio, so please keep your volume at a reasonable level for safety.

use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use libpd_rs::convenience::PdGlobal;

fn main() -> Result<(), Box<dyn std::error::Error>> {

    // Initialize cpal
    // This could have been another cross platform audio library
    // basically anything which gets you the audio callback of the os.
    let host = cpal::default_host();

    // Currently we're only going to output to the default device
    let device = host.default_output_device().unwrap();

    // Using the default config
    let config = device.default_output_config()?;

    // Let's get the default configuration from the audio driver.
    let sample_rate = config.sample_rate().0 as i32;
    let output_channels = config.channels() as i32;

    // 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's evaluate a pd patch.
    // We could have opened a `.pd` file also.
    // This patch would play a sine wave at 440hz.
    pd.eval_patch(
        r#"
    #N canvas 577 549 158 168 12;
    #X obj 23 116 dac~;
    #X obj 23 17 osc~ 440;
    #X obj 23 66 *~ 0.1;
    #X obj 81 67 *~ 0.1;
    #X connect 1 0 2 0;
    #X connect 1 0 3 0;
    #X connect 2 0 0 0;
    #X connect 3 0 0 1;
        "#,
    )?;

    // Build the audio stream.
    let output_stream = device.build_output_stream(
        &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);

            // 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);

            // Here we could have done post processing after pd processed our output buffer in place.
        },
        |err| eprintln!("an error occurred on stream: {}", err),
        None,
    )?;

    // Turn audio processing on
    pd.activate_audio(true)?;

    // Run the stream
    output_stream.play()?;

    // Wait a bit for listening..
    std::thread::sleep(std::time::Duration::from_secs(5));

    // Turn audio processing off
    pd.activate_audio(false)?;

    // Pause the stream
    output_stream.pause()?;

    // Close the patch
    pd.close_patch()?;

    // Leave
    Ok(())
}

This is just the tip of the iceberg about what you can do with libpd.

The patch we had just evaluated would look like this in pd desktop application:

Running the examples and tests

After cloning the repository, in the repository root run:

cargo run --example <name of the example>

e.g.

cargo run --example with_nannou

Please check the README on examples for more information.

For the tests, you may run cargo test directly.

Next steps

Please check the examples and tests directories if you learn better when reading code.

Or if you would like to dive in to documentation please go ahead.

Resources

Road map

Support

  • Desktop

    • macOS:
      • x86_64 βœ…
      • aarch64 βœ…
    • linux:
      • x86_64 βœ…
      • aarch64 βœ…
    • windows:
      • msvc
        • x86_64 βœ…
        • aarch64 (not tested but should work)
      • gnu
        • x86_64 (not tested but should work)
        • aarch64 (not tested but should work)
  • Mobile

    • iOS (not yet but will be addressed)
    • Android (not yet but will be addressed)
  • Web (not yet but will be addressed)

List of bundled externals

The way to add externals to libpd is to compile and statically link them.

libpd-rs will be bundling some of the essential and commonly used externals in pure data. This list will be growing as we add more externals.

If you have ideas please consider writing an answer to this post.

  • moog~
  • freeverb~

Contributing

  • Be friendly and productive
  • Follow common practice open source contribution culture
  • Rust code of conduct applies

Thank you πŸ™

Similar projects

Last words

Generative or algorithmic music is a powerful tool for exploration, pumps up creativity and goes very well together with traditional music making approaches also.

Making apps which produce meaningful sound is difficult, I wish that this crate would ease your way on doing that and make complicated audio ideas in apps accessible to more people.

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages