Combining Pure Data and C++

Making the most of both languages

If you would like to use C++ to do something that is not easily achieved in Pure Data then you’ll want to use a custom render.cpp file as part of your project.

Table of contents

  1. The structure of a Pure Data project on Bela
  2. The code
  3. Using custom render.cpp files
  4. Running the patch with the modified render.cpp file

The structure of a Pure Data project on Bela

In a normal Pure Data project you will have a _main.pd as your top level Pure Data file and any additional .pd that are referenced from _main.pd. In addition to these files it is possible to have a render.cpp file which will replace the default wrapper file that comes with a Pure Data project.

The details of how Bela and Pure Data relate.

This allows us to pass signals and messages back and forth from the Pure Data project to the C++ code.

The code

Find the custom-render sketch in the Pure Data section of the Examples tab of the Bela IDE.

Using custom render.cpp files

It is possible to modify the default libpd wrapper templates in order to combine c++ code with your Pure Data patches.

In this example you should hear a tremolo effect being applied to the output. This is done by taking the output buffer returned from libpd and applying further processing before writing the buffer to the Bela context.

It is also possible to define more input or output channels within the render.cpp file in order to pass auxiliary signals (e.g. envelopes) across the pd patch and the c++ code

In this example we are sending float values to a receiver named ‘tremoloRate’ which is parsed by the modified render.cpp file and used to control the rate of the tremolo effect applied to the output.

See the render.cpp file in the project folder for the libpd implementation. The heavy implementation can be found inside the enclosed /heavy folder. (This is where custom render.cpp files for heavy need to be placed when compiling with heavy). Search for ‘MODIFICATION’ (no quotation marks) inside either of the render files to inspect all the modifications that were made for this example.

Running the patch with the modified render.cpp file

If using libpd, you can simply run this patch from the Bela IDE. The system will automatically detect the modified file and use it instead of the template.

See comments in this example for more information.