Some weeks ago I started learning Pure Data. I have known it for a long time but I never used it since I made my music in other tools.
During the years I have seen amazing stuff made using PD. You can create 3D graphics, connect it with Arduino, using OSC protocol and many more thing.
Connecting blocks with PD is very easy and the combination of text and wires makes it very effective. For example if you want to make a simple block that adds a constant to a signal you just need to type “+ 2″… amazing isn’t it?
In my case, PD is great to prototype and test algorithms, and the fact that you can create your own blocks using C language is great.
I was checking the developer documentation and the tutorials to create extensions. This one was very useful to understand how the things work.
HOWTO write an External for puredata
However this document is platform independent and I wasn’t sure what I needed to create and extension using Visual Studio. For that reason I made this small tutorial.
Compiling the hello world example.
The “HOWTO write an External for puredata” contains the following code:
helloworld.c
#include "m_pd.h"
static t_class *helloworld_class;
typedef struct _helloworld {
t_object x_obj;
} t_helloworld;
void helloworld_bang(t_helloworld *x)
{
post("Hello world !!");
}
void *helloworld_new(void)
{
t_helloworld *x = (t_helloworld *)pd_new(helloworld_class);
return (void *)x;
}
void helloworld_setup(void) {
helloworld_class = class_new(gensym("helloworld"),
(t_newmethod)helloworld_new,
0, sizeof(t_helloworld),
CLASS_DEFAULT, 0);
class_addbang(helloworld_class, helloworld_bang);
}
Just put the code in a file named “helloword.c”.
In order to simplify the project creation I used CMake. Download the latest version and install it. Note: I have added the path to the binaries to the PATH variable.
You need to create the following two files.
helloworld.def
LIBRARY helloworld
EXPORTS helloworld_setup
This file is to let know the compiler that we are exporting the symbol “helloworld_setup”. The following file is the cmake project file. This one is going to make most of the magic.
CMakeList.txt
project(helloworld)
add_definitions(-DMSW)
set(SRC helloworld.c)
include_directories("C:/Program Files (x86)/pd/include")
link_directories("C:/Program Files (x86)/pd/bin")
add_library(helloworld SHARED ${SRC} helloworld.def)
target_link_libraries (helloworld pd)
Generating the project files
CMake files are very simple to understand, you define the sources (line 5), you add the includes and libraries directories (lines 7 and 8). The includes and the libraries are located in the folder of your PD-extended installation. In line 10 we define that we are creating a shared library using the sources in variable SRC and we also supply the helloworld.def file.
The new step is to open the command line. Note: you need to able of calling the compiler (cl.exe) from the command line. For that reason I use the shortcut included in the Visual Studio tools menu.
Once in the command line, go to the path where you have the files located and run the following command to generate the project files for Visual Studio 2010: cmake ./ -G “Visual Studio 10”. This will generate all the project files you need.
If you have problems following these steps, you can also refer to the cmake tutorial in HERE
Compiling and debugging your extension
Compiling is straight forward. Open the “ALLBUILD” project in visual studio and build the solution.
In order to debug you new extension you need to make it like any other DLL. You need to make PD use the DLL and attach the debugger. To use the DLL in PD you need to add the path to your debug DLL. You can find where the DLL is by looking at the project properties.
To add the path into PD go to File->Path menu and add a new entry.
After that you need to tell PD to load the DLL when it starts. In the menu File->Startup add the “helloworld” entry.
The next step is to configure the debugger. Since we are compiling a DLL we need to to configure the debugger to run PD instead. In the Properties of the helloworld project, in the debugging tab set the “Command” to the path of pd.exe and preferably the working directory to the same path.
Now just add a breakpoint, run the debugger and see it stop when you’re using your extension.
Hope this small tutorial simplify some steps for you. I’m planning to make a few more explaining other aspects.