Praat-Py: Extended Praat for Python Scripting By Joshua Tauberer <http://razor.occams.info> IntroductionPraat-Py is a custom build of 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 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 retained when using Python, except forms. 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. ExampleThe 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.
Getting StartedDownloading Praat-PyPraat-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 platforms because that is what I have available to create it for. You will need to have the indicated version of Python already installed from www.Python.org. The current version is based on Praat v5.1.17 (September 22, 2009) and Praat-Py v0.7 (September 30, 2009) (change history).
On Linux, after downloading the Praat-Py file, you have to make it executable (e.g. If it doesn't work at all, you should install regular Praat from the Praat website to make sure you have everything required to run regular Praat. (On Linux, you'll want to choose the dynamically linked version.) If you use a different platform, you will need to compile Praat-Py from its sources (experienced hackers only): instructions are below. Using Python in Praat-PyStarting a Praat-Py ScriptOnce 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" FunctionsThere are several ways your Python script can make Praat do things. The first way is via the #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 #lang=python
go("To Spectrogram...", .005, maxfreq, .002, 20, "Gaussian")
go("View")The first argument to the
#lang=python
wav = go("Read from file...", "myfile.wav")
spec = go("To Spectrogram...", .005, maxfreq, .002, 20, "Gaussian")
select(wav)
part = go("Extract part...", 0.0, 1.0, True)
remove(wav)
remove(spec)
remove(part)The third way is to have Praat do something but to capture the result of what it does. You can capture numeric results with #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 There is also a function #lang=python
print "The end time is: ", getString("Get end time")Selection FunctionsSome additional commands are provided to make it easier to work with Praat's selected
objects. #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 aboveTo add or remove objects from the selection, use #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
#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 Other ShortcutsBecause removing objects from the Praat objects list is such a common task in scripts that batch-process files, there is a shortcut for selecting and then removing objects using #lang=python
wav = go("Read from file...", "myfile.wav")
...
remove(wav)
or
remove('Sound myfile')Running the Script from the Command LineAs 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 You can also pass command-line parameters to the script: praat myscript.praatpy arg1 arg2 ...and access them with #lang=python print argv # this prints [u'myscript.praatpy', u'arg1', u'arg2'] (they're Unicode strings, assumed UTF-8 on command line) Building Praat-Py from SourcesYou can build Praat-Py on any Unix platform... at least in principle. I build it on Ubuntu. YMMV elsewhere. 1) You will need to have Python 2.5 or 2.6 installed and the "python-dev" package (or equivalent) for your distribution, or an install from sources. 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/praat5117_sources.tar.gz tar -zxf praat5117_sources.tar.gz cd sources_5117 cp makefiles/makefile.defs.linux.dynamic ./makefile.defs make 3) Inside the Praat sources directory (i.e. sources_5117 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: svn co svn://razor.occams.info/praat-py scripting cd scripting 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 The patch also contains a number of fixes to make it compile and run properly on Ubuntu, and to cross-compile with mingw32 for Windows. 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. | |||||||||||||||||||