Goal: Before you attempt to write your own Praat script, you will probably try to use one of the many existing scripts. This guide is not going to teach you how to write your own scripts, or even how to modify existing scripts in minor ways for your own needs. All it covers is how to use scripts that someone else wrote. It is intended for students in an introductory phonetics course, who usually have little to no background in working with scripts or programming languages.
The point of Praat scripting is to automate tasks, so you, the human, can do all the smart stuff, and leave the computer to do repetitive boring stuff. For example, suppose you are working on a study where your interest is in measuring durations of certain consonants. Praat cannot accurately label the edges of those consonants for you–that task falls under “smart stuff” that requires human eyes and ears. But Praat can rapidly collect the durations of labeled intervals from a TextGrid for you, if you prepare the right sort of TextGrid and tell Praat exactly what you want.
The most basic thing you will do with a Praat script is make it run and tell it to do something to a file you provide, and then save the result to your computer. The confidence builder exercise below talks you through that.
But first…
Praat vs. the internet and your phone
Praat cannot read your mind.
If you are like most students taking an introductory phonetics course right now (I’m writing in 2021), the internet has always existed for you, and it isn’t your mom’s internet of 2002. Most of the apps on your phone are fronts for web apps–they allow you to interact with a website or a distributed network. Thus, your familiarity with computers is intimately tied with how the internet has evolved. Websites and web apps are designed to try to read the user’s mind, and to fail gracefully in the face of typos and missing pages. They keep your stuff organized for you, so that you can search for pictures or music or videos and somehow always find it. All the magic is hidden. One side effect of this is that kids these days can’t find things on their computers.
By contrast, Praat was originally written in the late 1990s. It has been continually maintained, and features have been added to it, but its basic nature is very 90’s. It is meant for people who know their way around their computer and who understand how to get the computer to do things the old-fashioned way. The challenge before you, then, is to learn how to talk to a 90’s computer. You need to tell the computer exactly where stuff is. (You need to tell the computer the order in which to do things, too. If you’re working with existing scripts, as in this tutorial, you generally don’t have to worry about that.)
Another side effect of Praat’s not knowing how to read your mind is that you have to be very careful with typos and whitespace. (I’ll explain what “whitespace” means below.) Approximate is not good enough; you have to be precise. You need to be extremely attentive to detail.
You are eventually going to encounter some situations where things go wrong, but Praat doesn’t tell you why. See, Praat cannot read your mind, but you have to read its mind. This is what we call debugging. You need to decompose the situation that leads to failure into steps that you can change, one at a time, to isolate the step that is causing problems. This requires an analytic approach that can be a lot of fun, as long as you frame the problem correctly.
A confidence builder
Let’s try a simple script, which is known to run on a pre-chosen audio file. The script we’ll use is Mietta Lennes’s mark pauses, and the audio file is Newgatenovelist’s reading of Her Hair, a poem from the free Librivox project. You can hear the original mp3 at the linked page; here is the .wav file I made (I used ffmpeg). As a rule, you should be working with .wav files in Praat, not .mp3 files, because the time stamps in mp3 files are not accurate.
- First, go to your
Desktop
and make a folder there namedpauses.
[Mac instructions][Windows instructions] - Save the .wav file to that folder. Once the file is on your computer, you can view its properties by right-clicking on it (Mac users, hit Control+click). The properties will give you the location of the file, as well as its size and other details.
- Download the script file to the same folder.
- Open Praat, and load the audio file into it as a
LongSound
object. To do this, from Praat, go toOpen>Open long sound fIle
. The newest sound object opened should be selected by default. If it isn’t, click on it to select it. This will become important in a sec. - Now open the script: click on
Praat>Open Praat script....
You’ll see a boring-looking text file, like this: - Hit “Run”. You now see a dialog window called
"Run script: Give the parameters for pause analysis."
- Okay, now it’s time to edit something. Pay attention! You need to change the path in the last field, the one called
Save TextGrid file to folder:
, to the location of your audio file. If you put it inpauses
on your Desktop, then the path will end inDesktop/pauses/
if you’re on a Mac or Linux machine (or Chromebook?), andDesktop\pauses\
if you’re on Windows. The part before that will be your home directory; look at the path to the file as explained in step 2 if you don’t know how to find your home directory. The slash at the end of that is important–and if you make any typos, things will break. - When you’re ready, hit
OK.
If you did everything correctly, you should see a white Praat Info window that saysReady! The TextGrid file was saved as /wherever/you/told/Praat/to/save/it.TextGrid
. If not, well, it’s time to debug things, so read the rest of this page and come back to step 7. - You should also be able to see a new TextGrid object in your Praat object window. Open it together with the sound file, and you’ll see that there is a TextGrid with a “sentence” level that has intervals marked. The intervals are silences longer than 0.6 seconds. Look at the
Run script
window again and you’ll see that this is a default value defined in the third field of that dialog. You can also figure out how the script identified pauses by looking at that window: there is an intensity threshold (59 decibels) that defines a pause.
Okay, so what was the point of that? If you look at Mietta Lennes’s other scripts, you begin to see how this might be useful. There is a script on that page that will label a TextGrid with labels from a text file. For example, your poem. You will need to edit the TextGrid to make sure that the pauses coincide with line breaks, but it’s a lot quicker to do than to doing it from scratch. In the case of this recording, you just need to remove some interval boundaries.
There is also a script that will save labeled intervals to separate audio files, and create matching TextGrids to boot. I hope you can see how that would be useful for the final project I’ve assigned you.
A random list of problems and solutions
- Many problems can be fixed by starting with a clean Praat session. Remove all objects from the Object window, and start afresh. Praat scripts often operate by putting objects in the window and doing stuff to them; if there are already objects there, and they are of the wrong type, it can break the script.
- “Unable to save file“… If you get errors about files not being found, you messed up your path somehow. Maybe you left out a slash at the end of path, or added a slash when you shouldn’t have. Maybe you skipped a crucial directory in your path. Check everything–and use the information about file locations that you get from looking at their properties, as described above.
- Script works on some files but breaks on others. For scripts that are supposed to work on a bunch of files: if the script is working for some of your files and failing for others, you should examine closely those text grids where the script is having trouble. That will give you an idea of what could be going wrong (for example, one of the interval boundaries might be missing in that file, but not in the others–that can break a script.)
- The script is asking you to enter a non-zero value, and you don’t know what to enter. (The default value in the “save labeled intervals to wav files” script is 0.000, which I normally change to 0.0001.) Try a few different numbers and see what the output looks like. The script doesn’t modify your original data, so there’s no harm in experimenting. That’s how you learn what the script does.
- The script giving you undefined values for some of your intervals. This could be because you mis-segmented your text grids–if you placed the boundary on something that isn’t a vowel and has no formants to measure, then of course the script will not be able to measure your formants. (Ditto for intensity, possibly.) Solution: make sure your segmentation identifies vowels.
- Are you running the script on the wrong file? You’re having trouble running the formant/intensity/duration collecting script on your large data file with a single text grid. That’s because the script is intended for a bunch of small individual files paired with text grids. Make sure you read the homework instructions carefully, don’t skip steps.
- Empty results.txt file. This one has many causes, but probably the chief one is that you’re telling the script to look for files in the wrong location. Did you change the paths to ones for you computer, or did you leave them as the defaults that were already there? On a Mac, your path probably should start with “/Users”; on a Windows machine, with “C:”. The paths below are from my Linux machine; your paths will need to be different.
- The script break on files that have spaces in their names. Your labeled intervals have spaces in them. Your sound files and your text grid files do not have matching names, so the script chokes. What’s the solution? Respect whitespace! See below.
- You’re running your data collection script on empty text grids. This is another source of the “empty results.txt file” problem. Did you save the TextGrid files after you changed their contents? Remember, Praat doesn’t save files automatically for you, like other programs you’re used to. There is no autosave of changed objects in the Object Window. You have to overwrite the files in the directory where Praat is looking for annotated text grids.
Some other gotchas in Praat scripting
- Praat itself will warn you if you try to overwrite an existing file, but a Praat script will not. Always test a script in such a way that there is no danger of it overwriting important work.
- Solution: create a copy of the directory with your text grids and run the script on the copy, so there is no danger of screwing up your originals. If all goes well, you can delete the originals.
- A Praat script might be configured to have a default file name for a text grid or an audio file it created, so if you want to save multiple audio files cut out of a single big audio file, it may overwrite the same file with new content without telling you.
- Solution: If you end up with one file but you expected several, and the one file you see is the last file you expected to be created, then the answer to your problem lies in how files are named.
- A typo in a key location will break things. There’s no solution here but to pay attention; see also below.
- Praat will not give you interpretable error messages unless the script you’re running provided for them. Again, there’s no solution other than to be careful.
- There are no helpful mouseover (or finger press) hints that explain what buttons do. Once you hit that “Run” button, you might be running a risk of losing stuff. So check everything many times before you run a script.
- There might be technical problems with a particular measurement: for some acoustic analyses, the time window has to be of a certain length, so if your vowel, say, is too short, a Praat script measuring it will fail for inexplicable reasons.
- Solution: attempt to do the measurement by hand at the point where the script is failing.
Respecting spelling and whitespace
What is whitespace, you ask? It’s all the stuff that looks white on a screen (or black, if you like Dark Mode) but contains invisible characters that the computer does not ignore.
Examples of whitespace:
- An actual space, as in between words.
- A tab, like what you use in a word processor. It’s usually 4-5 spaces wide, but it’s distinct from a sequence of spaces. Just ask a Python programmer to explain this to you.
- An end-of-line character. There are actually several of these, and they differ depending on the operating system. This is one of the reasons why simple .txt files sometimes look insane when they travel from a Windows computer to a Linux or a Mac computer.
- A truly diabolical example of a whitespace character is a letter that is rendered in a font color that matches the background. Select the line below with your mouse, and you’ll see.
Hahaha! I am whitespace in white color! I got you, sucka!
Why are we talking about whitespace so much? Because Praat does not like spaces. In the olden days, many computers could not handle spaces in file names at all. This is the reason why oldsters such as myself use underscores for readability, as in, sound_and_language_syllabus.pdf. Behind the scenes, spaces in file names have to be modified; in URLs, for example, spaces are replaced with other character sequences. Look at the URL of any website and you will not see spaces in it. This is because computers treat spaces as meaningful breaks between objects.
So, if you open a file in Praat with spaces in the filename, Praat will automatically convert them to underscores in the name of an object. Try it! If you ignore the underscores in your handling of files, which I think many non-programmers tend to do, you will be punished for your carelessness. You’ll wonder why you have what looks like duplicates, or wonder where your file went, or, if you have spaces in the names of your folders, things might not work at all.
Solution: Do not use spaces in file names. Use a concatenation of lower and uppercase letters, as in SoundLangSyllabus.pdf, or, better, use underscores, as above.
Capital vs. Lowercase
Speaking of capitals: capital and lowercase letters are not the same characters to a computer. Whenever a program seems to treat upper- and lowercase as the same, it was explicitly told to do this. So, make sure to respect that difference.
Differences between symbols that look the same to you
Now we get to some really old-school stuff, like the ASCII vs. non-ASCII distinction. The ASCII character set (pronounced “ass key”) is the oldest, designed for English. Even commonly used Western European characters such as <ñ> and <ü> are not part of the basic ASCII set of 127 (see it here, if you need to). The oldest programs do not know how to deal with anything other than ASCII.
And this includes Praat. If you want to annotate a TextGrid, Praat will not let you enter IPA characters in Unicode; you can enter them using deadkey combinations, as explained in the manual, but you cannot paste them in directly from your word processor that uses a proper, modern IPA font.
Where this becomes a problem in working with Praat scripts is if your script depends on labels matching something. For example, if you want to extract all the intervals labeled “g”, but some of your “g” labels are IPA [ɡ], and others are regular font [g]. (Look carefully and you’ll see a difference in those symbols.) For me, some problems I’ve run into involved Russian script ” а” vs ASCII “a”; they look identical but are different to a computer. (There were scam URLs a while back that capitalized on Latin and Cyrillic using some similar looking letters… You think you’re visiting Amazon.com, but really you’re somewhere in the middle of the Azov sea. But I digress.)
Ready to go beyond these basics?
In the confidence builder example, you didn’t change the script text itself, and we did not look at the contents of the scripts. But all the Praat scripts linked above were created by people who weren’t Praat programmers (Boersma and Weenink)–and most can be modified by users such as yourself. You can take these scripts, look at their contents, and change them to suit your needs.
For another confidence builder, then, open the mark_pauses.Praat
script as in the instructions above, but before you hit “Run”, do the following: go to “search”, enter “lennes”. That will take you to a line that says
text folder /home/lennes/
Change this line to /home/yourname/
, and save the script. Now run it–what do you see in the field at the bottom of the dialog?
What you did is modify the form that the script generates. That window is part of the script, and the field is pre-populated by a default value. All of the form values can be defined either in the form when you run the script or by changing the script itself.
Once you understand that the contents of that file is not magic, you can take on the Praat scripting tutorial in Praat itself, or check out the various resources written by other people.
I like Eleanor Chodroff’s Praat scripting tutorial here. She has some other nice resources on her page.
And now, the obligatory metaphor
If you’ve taken classes with me, you know how I like tortured metaphors. And similes.
The way young people use computers is sort of like Hermione’s magic bag from the Harry Potter and the Deathly Hallows book. It’s a tiny bag, magically crammed with stuff, and she often uses the Accio charm to get things out of it. This is a summoning charm that just find the right thing in the mess–sort of like that search function you rely on. Hermione doesn’t bother to organize books, Muggle clothing, essence of Dittany, and all the rest of the junk in the bag because it’s bound to fall over anyway. And why bother, when you have a magic summoning charm? When you rely on search to find stuff on your computer, or on the internet, it’s pretty much like using the Accio charm.
[Another tortured metaphor: relying on the internet to keep your stuff for you is pretty much Leprechaun gold territory. Internet stuff disappears. You can practice a Zen-like non-attachment to your stuff, or you can keep it locally, and organized, if you want it to be around for years to come.]
To an older person, hearing about this Search approach is akin to learning that you keep your socks and your forks and your toothpaste and your books just randomly piled all over your room, jumbled together. We keep things in separate cabinets and drawers. We know where to find forks not because we have a magic summoning charm, but because we put the forks into the place where forks go.
The way computers actually work in reality is that you have to tell them were the fork drawer is, in explicit detail, without typos. Even when you have an operating system that can search through your files, this becomes useless if you don’t know what to search for. And believe me, a time will come when you won’t remember what you called that one file, or where on the internet you saw that one thing.
Because you see, the search-for-it approach is only good if you know what to search for.
Back to Praat. In setting up an experimental project, there are a few common-sense, fork-drawer-organizing things you should consider doing:
- Put files into directories. For example, have a “raw_audio” directory where you keep the unsegmented data, and a “segmented_audio” and maybe “textgrids” for the eponymous things.
- Give files sensible names without spaces. Just ask yourself–would I be able to figure out what all these files are 10 years from now?
- Keep a small local “readme.txt” file that explains what order you did things in, and what each folder is for, in case you forget (because believe me, you will… it’s not just a matter of how well your brain works but also how much stuff is cluttering your memory; the more you know, the harder it is to recall things, paradoxically).
Okay, I’m done hectoring you for now! Did this help? Do you have any tips to include here, or anything you ran into that I didn’t cover above? Let me know.