General
On-Line Projects
Links
Other Pages Here
Praat-Py: Extended Praat for Python Scripting

Praat-Py: Extended Praat for Python Scripting

By Joshua Tauberer <http://razor.occams.info>

Introduction

Praat-Py (or lovingly also called PyPraat, but not PraatPy without the dash) is an extension to Praat, the computer program used by linguists for doing phonetic analysis on sound files, to allow for scripts to be written in the Python programming language, rather than in Praat's built-in language.

The motivation for this extension is to remove the need for Praat Script writers to learn an entirely new programming language if they already know Python. Additionally, Python is a more complete programming language and so scripts of any complexity should be more easily written in Python. The goal isn't to make all scripts easier or shorter to write, but to make it possible to do some complex tasks more easily.

All of the functionality of Praat's scripting language is (meant to) be retained when using Python. However, forms are not implemented yet. Likewise, because the Python interpreter is used, all Python functionality is possible, such as using all Python modules that you happen to have installed.

This project is released under the same terms as Praat itself, the GPL v2 or later.

Praat-Py works by linking to the Python interpreter. By and large, you will need to compile Praat-Py from sources yourself, which means you are probably on a Linux or Mac OSX computer. In that case, you must have Python installed (and possibly development files as well, i.e. the python-devel package or the like).

Praat Scripting: Do you want to script better than this?

Examples

The following is an example script for Praat written in Python, and on the right is the corresponding script in Praat Script. The script converts all .aif files in a directory into .wav format, resamping them in the process. The Praat script on the right is based on the script here by Mietta Lennes.

Python (with Praat-Py)Praat Script (with Praat-Py or regular Praat)
#lang=python
from glob import glob

aiffdir = "/path/to/my-aif-files/"
delete_AIFF_files_after_conversion = False

for file in glob(aiffdir + "*.aif") :
   go("Read from file...", file)
   soundtype, soundname = selected()
   go("Rename...", "temp")
   print "Converting file", file, "..."
   go("Resample...", 16000, 50)
   go("Rename...", soundname)
   select("Sound temp")
   go("Remove")
   select(("Sound", soundname))
   go("Write to WAV file...", aiffdir + soundname + ".wav")
   if delete_AIFF_files_after_conversion :
      go("filedelete", file)
   go("Remove")
Create Strings as file list... aifffiles 'aiffdir$'*.aif
numberOfFiles = Get number of strings

for file to numberOfFiles
   select Strings aifffiles
   file$ = Get string... file
   Read from file... 'aiffdir$''file$'
   soundname$ = selected$ ("Sound", 1)
   Rename... temp
   printline Converting file 'file$'...
   Resample... 16000 50
   Rename... 'soundname$'
   select Sound temp
   Remove
   select Sound 'soundname$'
   Write to WAV file... 'aiffdir$''soundname$'.wav
   if delete_AIFF_files_after_conversion = 1
      filedelete 'aiffdir$''file$'
   endif
   Remove
endfor

Getting Started

Downloading Praat-Py

Praat-Py is a replacement for the Praat executable file you downloaded from the Praat website. I provide a ready-to-go version of Praat-Py only for certain operating systems (i.e. Linux) because that is what I have available to create it for.

You will need to have Python 2.5 or later installed. (Maybe it works with earlier Pythons. I haven't tried.)

The ready-to-go Praat-Py downloads here are currently based on:

Praat version 5.0.27 (June, 28 2008)
and
Praat-Py version 0.502 (December 22, 2007)
Pre-made (Binary) Downloads:
Linux Unbuntu 8.10/i386/32bit Download: praat-py_linux_i386
That's it. Sorry!

If none of these downloads are useful to you, you will need to build Praat-Py from its source files. (Experienced users only.) Instructions are below.

You can also...

Using Python in Praat-Py

Starting a Praat-Py Script

Once you have Praat-Py, you can write scripts in either Praat's built-in scripting language or in Python. You should be roughly familiar with Praat scripting before reading on (see this page), as well as Python. To script with Python, all you have to do is start your scripts with this:

#lang=python

That is, the very first line of your script must be a hash (#) followed immediately by "lang=python" in lowercase letters. This instructs Praat to interpret the remainder of your script as Python code. Without this special line, the script is run as a normal Praat script.

"Print" and "Go" Functions

There are several ways your Python script can make Praat do things. The first way is via the print Python command, which will output a string to the Praat Info window:

#lang=python
print 11500 * 20

The next way is to have Praat do one of its commands on a menu or the main window buttons. You do this with the go(command, arguments...) function.

#lang=python
go("To Spectrogram...", .005, maxfreq, .002, 20, "Gaussian")
go("View")

The first argument to the go function is the name of a command on a Praat menu to run. The remaning arguments are any options that that command takes, in the order that they show up in Praat. You'll notice that as compared to Praat Script, they way you put the arguments together is significantly more straight-forward. To use variables, for instance, you just use them (no strange quotes). In Praat Script, you never quote the final argument. Here, you quote all arguments uniformly: strings are quoted, and nothing else, as in Python normally.

The third way is to have Praat do something but to capture the result of what it does. You can capture numeric results with getNum(command, arguments...):

#lang=python
print "The end time is: ", getNum("Get end time")*1000, " (ms)"

As in regular Praat scripts, this works by capturing the first number the command would normally print to Praat's Info window. As with go, you can provide any number of arguments to getNum. getNum returns a Python float value.

There is also a function getString which works like getNum but it returns the entire output line sent to the Info window by the command as a Python string value.

#lang=python
print "The end time is: ", getString("Get end time")

Selection Functions

Some additional commands are provided to make it easier to work with Praat's selected objects. select(object name1, [object name2, ...]) selects one or more objects by their names. It's just a convenience; you can also use go("select", objectname). The argument(s) to select can be either Python strings that have the name of the object, or a tuple of the object type and name separately: select( (type1, name1) [, ...]) . Here are some examples:

#lang=python
select("Sound mysound") # selects just a sound
select("Sound mysound", "TextGrid mysound") # selects these two together (and unselects everything else)
select( ("Sound", "mysound") ) # same as above, might be useful in some cases, note the double parens!
select( ("Sound", "mysound"), ("TextGrid", "mysound") ) # same as above

To add or remove objects from the selection, use plus and minus as in Praat scripting. They work just the same as select:

#lang=python
select("Sound mysound") # selects just a sound
plus("TextGrid mysound") # selects additionally the TextGrid
minus("Sound mysound", "TextGrid mysound") # deselects them
minus( ("Sound", "mysound"), ("TextGrid", "mysound") ) # deselects them using the tuple form

selected() is a function that returns the selected object. It returns a tuple containing the type and name of the object separately.

#lang=python
select("Sound mysound")
(type, name) = selected() # returns ('Sound', 'MySound')

s = selected()
print s # prints "('Sound', 'MySound')"
print s[0] # prints the type of the object, "Sound"
print s[1] # prints the name of the object, "MySound"
minus(s) # deselects the currently selected object

(These new commands are defined in the praat module and are automatically imported.)

Running the Script from the Command Line

As with Praat Scripts normally, you can run a script from the command-line directly (and never see Praat itself), besides running it from a script window. Say "myscript.praatpy" is a Praat-Py script, i.e. a Python program beginning with "#lang=python". You can then run it as:

praat myscript.praatpy

Limitations

Praat forms are not implemented

I don't know how to build Praat on Windows, so there's no Windows version

It would be nice to be able to manipulate Praat from an externally running script, too

Can we get some or all of this integrated into the main Praat distribution?

Building Praat with Praat-Py from Sources

Building Praat-Py only works on Linux at the moment.

1) You will need to have Python installed, and perhaps the "python-devel" package (or equivalent) for your distribution.

2) Get the Praat sources and follow the instructions to build Praat on your platform. (You don't really need to build Praat now, but it would be a really good idea to make sure it builds before going forward.)

For instance, on Linux, with the latest version of Praat at the time of writing:

wget http://www.fon.hum.uva.nl/praat/praat4631_sources.tar.gz
tar -zxf praat4631_sources.tar.gz
cd sources_4631
cp makefiles/makefile.defs.linux.dynamic ./makefile.defs
make

3) Inside the Praat sources directory (i.e. sources_4430 or something similar), make a new directory called scripting and unzip the Praat-Py ZIP package into that directory.

mkdir scripting
cd scripting
wget http://razor.occams.info/code/praat-py/dist/praat-py.zip
unzip praat-py.zip

Alternatively, you can check out the SVN module svn://razor.occams.info/praat-py as the scripting directory.

4) The Praat sources must be patched to build Praat-Py and to invoke the Python interpreter instead of the Praat script interpreter when the script begins with the magic string "#lang=python". Do this to patch your Praat source files:

make patch-praat

5) Then exit out of the scripting directory and build Praat.

cd ..
make

The praat executable in the current directory will now support Python scripts.