Programming with Praat

You can extend the functionality of the Praat program by adding modules written in C or C++ to it. All of Praat's source code is available under the General Public Licence.

1. Warning

Before trying the task of learning how to write Praat extensions in C or C++, you should be well aware of the possibilities of scripting. Many built-in commands in Praat have started their lives as Praat scripts, and scripts are easier to write than extensions in C or C++. If you have a set of scripts, you can distribute them as a plug-in.

2. Getting the existing source code

You obtain the Praat source code via www.praat.org, in a file with a name like praat5218_sources.tar.gz (depending on the Praat version), and unpack this by double-clicking (on old computers, use gunzip and tar xvf if Unix, or StuffIt® Expander if Macintosh, or Aladdin® Expander if Windows). The result will be a set of directories called kar, GSL, num, audio (with FLAC and mp3 in it), sys, dwsys, stat, fon, dwtools, LPC, FFNet, gram, artsynth, contrib, main, makefiles, and test, plus a makefile, a Codewarrior project for Windows, and an Xcode project for Macintosh.

3. Building Praat on Macintosh

Open praat.xcodeproj in Xcode and choose Build and Run.

4. Building Praat on Windows

Open praat.mcp in CodeWarrior (version 9.0 or higher), choose the target praat_win, and choose Make or Run.

Praat may compile under MinGW as well.

5. Building Praat on Linux

To compile and link Praat on Linux, you go to the directory that contains the source directories and the makefile, and copy a makefile.defs file from the makefiles directory:

> cp makefiles/makefile.defs.linux ./makefile.defs

You have to have installed libgtk2.0-dev (and its dependencies) and libasound2-dev.

On other Unixes, you do the same, but the file makefile.defs may require some editing after this, because Silicon Graphics Irix, Sparc Solaris and HPUX may use different libraries or have them in different locations than Linux.

6. Extending Praat

You can edit main/main_Praat.cpp. This example shows you how to create a very simple program with all the functionality of the Praat program, and a single bit more:

#include "praat.h"

DIRECT (HelloFromJane)
    Melder_information (L"Hello, I am Jane.");
END

int main (int argc, char **argv) {
    praat_init ("Praat_Jane", argc, argv);
    INCLUDE_LIBRARY (praat_uvafon_init)
    praat_addMenuCommand (L"Objects", L"New", L"Hello from Jane...", NULL, 0, DO_HelloFromJane);
    praat_run ();
    return 0;
}

7. Learning how to program

To see how objects are defined, take a look at sys/Thing.h, sys/Data.h, sys/oo.h, the XXX_def.h files in the fon directory, and the corresponding XXX.c and XXX.cpp files in the fon directory. To see how commands show up on the buttons in the fixed and dynamic menus, take a look at the large interface description file fon/praat_Fon.cpp.

8. Using the Praat shell only

For building the Praat shell (the Objects and Picture windows) only, you need only the code in the six directories kar, GSL, num, audio, sys, and dwsys. You delete the inclusion of praat_uvafon_init from main. You will be able to build a Praat shell, i.e. an Objects and a Picture window, which has no knowledge of the world, i.e., which does not know any objects that can be included in the list of objects. You could use this Praat shell for modelling your own world and defining your own classes of objects. For advanced programmers only.

Links to this page


© ppgb, August 31, 2011