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 from GitHub (https://github.com/praat), in a file with a name like praat6499_sources.zip or praat6499_sources.tar.gz (depending on the Praat version), and unpack this by double-clicking. The result will be a set of directories called kar, melder, external (with clapack, gsl, glpk, flac, mp3, portaudio, espeak, vorbis and opusfile in it), sys, dwsys, stat, fon, sensors, dwtools, LPC, FFNet, gram, artsynth, EEG, main, makefiles, test, dwtest, and generate, plus a makefile and Xcode project for macOS and a README.md file.

3. Building Praat

Consult the README file on GitHub for directions to compile and link Praat for your platform.

4. Extending Praat

To start extending Praat’s functionality, 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 (namely an additional command in the New menu):

#include "praatM.h" // for macros such as DIRECT and INCLUDE_LIBRARY

DIRECT (INFO_HelloFromJane) { // a macro to include commands without parameters
Melder_information (U"Hello, I am Jane.");
}

int main (int argc, char **argv) {
praat_init (
U"Praat_Jane", // the name of your app
U"1.2.01", // the version of your app, as a string
1201, // the version of your app, as a number
2025, // the year of your app's version
9, // the month of your app's version (here: September)
18, // the day of your app's version (between 1 and 31)
U"jane.doe", // the first part of your contact email address
U"univ.edu", // the second part of your contact email address
argc, // the number of command line arguments (0 if started from GUI)
argv // the command line arguments (empty if started from GUI)
);
INCLUDE_LIBRARY (praat_uvafon_init) // include all Praat functionality
praat_addMenuCommand (
U"Objects", // the window in which this command will appear
U"New", // the menu under which this command will appear
U"Hello from Jane...", // the title of the menu command
nullptr, // to insert after another command (unused here)
0, // the depth at which this command will appear (0 for top level)
INFO_HelloFromJane // the command defined above
);
praat_run ();
return 0; // obligatory
}

5. Learning how to program

To see how objects are defined, take a look at sys/Thing.h, sys/Daata.h, sys/oo.h, the XXX_def.h files in the fon folder, and the corresponding XXX.cpp files in the fon folder. 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.

6. Using the Praat shell only

For building the Praat shell (the Objects and Picture windows) only, you need only the code in the nine directories kar, melder, external/{clapack,gsl,flac,mp3,portaudio,lame,opusfile,vorbis}, 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


© Paul Boersma 2023-04-09, 2024, 2025