14 Pure Data
Phil Schatzmann edited this page 2024-02-28 13:38:47 +01:00

Pure Data (or just "Pd") is an open source visual programming language for multimedia. Pure Data is developed by Miller Puckette since 1996 and you can find it on his official website along with the official documentation and other related resources.

I wanted to have the possibility to run PD sketches on microcontrollers. In order to achieve this I am using the hvcc hcompiler that can translate a pd file to c and c++ code.

Here are the steps to produce a working sketch using the AudioTools library. I am assuming that you have the latest version of the AudioTools library already installed.

  1. Install PureData on your desktop
  2. Install Python3 and hvcc with pip3 install hvcc
  3. Start PureData and define your patch. I was using a minimum example which outputs a sine tone

image

  1. Save your patch as a file: The file content of test.pd was:
#N canvas 349 94 450 300 12;
#X obj 134 65 osc~ 220;
#X obj 132 178 dac~;
#X connect 0 0 1 0;
  1. Now you can generate the c and c++ code with hvcc -n <name> <pd-file> e.g. hvcc -n test test.pd. The -n parameter gives your generated code a unique name: The generated Heavy_test class will be used in our sketch.
  2. Create a new Arduino Sketch:
#include "AudioTools.h"
#include "Heavy_test.hpp"  // import before PureDataStream!
#include "AudioLibs/PureDataStream.h"

Heavy_test pd_test(44100);
PureDataStream pd(pd_test);
I2SStream out; // or use any other output stream
StreamCopy copier(out, pd);  // copy kit to kit

void setup() {
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Warning);

  // setup pd
  pd.begin();

  // setup output
  auto cfg = out.defaultConfig(TX_MODE);
  cfg.sd_active = false;
  cfg.copyFrom(pd.audioInfo());
  out.begin(cfg);
}

void loop() { copier.copy(); }

Here you can find further information on how to use the Heavy API

  1. Save the sketch and then close it!
  2. Copy the generated files from the c subdirectory to the sketch directory.
  3. Open the project in Arduino, compile and deploy it on your microcontroller.

You need to use a microcontroller with quite some big Progmem: On an ESP32 you get

Sketch uses 612817 bytes (46%) of program storage space. Maximum is 1310720 bytes.
Global variables use 32392 bytes (9%) of dynamic memory, leaving 295288 bytes for local variables. Maximum is 327680 bytes.