Introduction to Praat and Praat Scripting
Lab Skills Workshop, Phonetics Laboratory, University of Oxford
DPhil Candidate | 2021/12/30 (updated: 2022-02-03)
To know a full range of functions Praat can perform
To master how to navigate and use Praat's GUI
To learn scripting basics, parse praat script, and start praat scripting
Q: 1. how many have used Praat? 2. how many have used praat scripts?
Open source, free of charge
Available for major computer platforms (MacOS, Windows, Linux) and systems
Graphical User Interface
A macro recording mechanism (Command History)
You can create your own Praat scripts to automate things!
You can create your own application with an autonomous GUI.
commercial software countless hours of soul-crushing repetitions
Creating and manipulating sounds
Visualizing and annotating sounds
Phonetic measurement and analysis
Creating publication-quality plots
Conducting phonetic experiment
Loading files
Managing objects (create, copy, rename, save, remove)
View objects and analyses
Setting analysis parameters
Querying objects (using buttons or menu commands)
TextGrid annotation
Demonstrate load file,view, textgrid, draw a pic.
Spectrum > Show spectrogram
Pulses > Show pulses
Pitch > Show pitch
Intensity > Show intensity
Formant > Show formants
You must select the correct object/environment to issue the right commands!
String and numeric variables
For loops, if else statements, while loops
Regular expression matching
Comments
Syntax
String and numeric variables
For loops, if else statements, while loops
Regular expression matching
Comments
Syntax
teach fishing
Sequentially ordered set of instructions that are given to a program that can interpret & execute them
editor type in instructions/commands
In programming, a variable is a place in the computer’s memory where something is stored, containers.name = value
To Pitch: 0.0, 75.0, 600.0
We're all linguist. Transitive Verbs take direct object otherwise ungrammatical.
command: arguments/components too! otherwise not running
1. New Praat Script2. Edit > Clear history (script editor)3. GUI actions4. Edit > Paste history (script editor)
Parse the script in chunks and see if they suit your needs. Sometimes no need to reinvent the wheels.
Learn from others' script.
Be cautious when using others'script. The syntax of Praat was updated since 2014.
Test it out first and remember to cite it!
Save frequently!
Place all paths, input/output files, and global variables at the top
Paths must end with a backslash /
Paths can be relative or absolute
Windows: "C:\Users\Chenzi\mydir\data\"
Avoid "special" characters in file/path names.
dir$ = "/Users/Chenzi/mydir/data/"Create Strings as file list: "file_list", dir$ + "*.wav"nFiles = Get number of stringsfor i from 1 to nFiles selectObject: "Strings file_list" filename$ = Get string: i basename$ = filename$ - ".wav" Read from file: dir$ + filename$ ######################## #### MORE CODE HERE #### ########################endfor
formulaic, wrapper code Underscore/lowercase is a good combo.
Create list of all .wav files in the directory and save the number to a variable
String variable ends in $
; strings wrapped in ""
Assignment operator =
Variable names in Praat must begin with a lowercase ASCII letter
dir$ = "/Users/Chenzi/mydir/data/" Create Strings as file list: "file_list", dir$ + "*.wav"nFiles = Get number of stringsfor i from 1 to nFiles selectObject: "Strings file_list" filename$ = Get string: i basename$ = filename$ - ".wav" Read from file: dir$ + filename$ ######################## #### MORE CODE HERE #### ########################endfor
In Praat, Names must start with lowercase letters, and have only letters, digits, and underscores. Informative names help your future self to understand your script!
Set up a for loop to iterate over all files on the list
1
is the starting point. It +1
each time through the loop
Comments (Praat will ignore) must start with "#
" or ";
"
Comments are good for reproducibility (and your future self)!
dir$ = "/Users/Chenzi/mydir/data/" Create Strings as file list: "file_list", dir$ + "*.wav"nFiles = Get number of strings# for-loop to iterate over all files on the listfor i from 1 to nFiles selectObject: "Strings file_list" filename$ = Get string: i basename$ = filename$ - ".wav" Read from file: dir$ + filename$ ######################## #### MORE CODE HERE #### ########################endfor
Read in each .wav file.
Get string
: get the nth row of the list of filenames
There are different ways to get the basename.
Read from file: dir$ + filename$basename$ = selected$ ("Sound")
dir$ = "/Users/Chenzi/mydir/data/" Create Strings as file list: "file_list", dir$ + "*.wav"nFiles = Get number of stringsfor i from 1 to nFiles selectObject: "Strings file_list" filename$ = Get string: i basename$ = filename$ - ".wav" Read from file: dir$ + filename$ ######################## #### MORE CODE HERE #### ########################endfor
Search > Go to line
printline
statements regularly when testingTask 1: Get total duration of all .wav files
Task 2*: Measure f0 in specified interval
1.1 Get the number of .wav files in a directory
1.2 Get duration of each .wav file
1.3 Get total duration of all .wav files
1.4 Print results in Praat Info window
1.5 Toggle option to "clean up" as you go
1.6 Add a user input form
1.7 Write results to a Text file
Adapted from Thea Knowles's Praat Tutorial
Open > Read from file
Query > Query time domain > Get total duration
Avoid using
.
in the filename (except in the file extension)
Praat > New Praat script
Edit > Paste history
File > Save
, type "test.praat"
, and Run
Limitations:
Query commands may behave differently in scripts than in the GUI (e.g. Praat scripting assumes that you want to handle the query result inside the script)
Changing scripting environments is not recorded
my first praat script
dir$ = "data/" Create Strings as file list: "file_list", dir$ + "*.wav"nFiles = Get number of stringsfor i from 1 to nFiles selectObject: "Strings file_list" filename$ = Get string: i basename$ = filename$ - ".wav" Read from file: dir$ + filename$ selectObject: "Sound 'basename$'" Get total durationendfor
printline
prints the argument to the Praat Info window
Contents of a variable enclosed in ''
in built-in Praat functions
(force Praat to substitute a string value for its variable name)
Numeric variable doesn't end in $
dir$ = "data/" Create Strings as file list: "file_list", dir$ + "*.wav"nFiles = Get number of stringsprintline 'nFiles'for i from 1 to nFiles selectObject: "Strings file_list" filename$ = Get string: i basename$ = filename$ - ".wav" Read from file: dir$ + filename$ printline 'basename$' selectObject: "Sound 'basename$'" dur = Get total duration printline 'dur'endfor
Set up a dummy numeric variable total_dur
and update its value through each loop
Numeric variables must start with a lower case letter, and be only letters, digits, and underscores
:4
: print with 4 decimal places
dir$ = "data/" Create Strings as file list: "file_list", dir$ + "*.wav"nFiles = Get number of stringstotal_dur = 0for i from 1 to nFiles selectObject: "Strings file_list" filename$ = Get string: i basename$ = filename$ - ".wav" Read from file: dir$ + filename$ printline 'basename$' selectObject: "Sound 'basename$'" dur = Get total duration printline 'dur' total_dur = total_dur + durendforprintline 'total_dur:4'
There are different methods.
Sometimes leaving objects helps debugging.
clearinfofor i from 1 to nFiles #### MORE CODE HERE #### select all minus Strings file_list Removeendforselect allRemoveprintline All finished!
More info at Praat manual and Phonetics on Speed
Input forms are composed of fields, one per line. User provides arguments to the script.
Input form code blocks are always processed first.
You can't have two forms in one script.
form Make selection comment Enter directory of files sentence Directory data/ boolean Clean_up 1endform#### MORE CODE HERE ####
Create Boolean variable (TURE/FALSE or 1/0)
Nest it in a if statement.
for i from 1 to nFiles #### MORE CODE HERE #### if clean_up == 1 select all minus Strings file_list Remove endifendforif clean_up == 1 select all Removeendifprintline All finished!
If you write a file to a path that already exists, it will be overwritten, and with NO UNDO.
Check if the file exists and ask permission before overwriting it.
nFiles = Get number of stringsoutFile$ = "duration.txt"askBeforeDelete = 1if askBeforeDelete and fileReadable(outFile$) pauseScript: "File exists! Overwrite?"endifdeleteFile: outFile$for i from 1 to nFiles selectObject: "Strings file_list" filename$ = Get string: i basename$ = filename$ - ".wav" Read from file: dir$ + filename$ selectObject: "Sound 'basename$'" dur = Get total duration appendInfoLine: basename$, " ", dur appendFileLine: outFile$, basename$, " ", durendfor
1.1 Get the number of .wav files in a directory
1.2 Get pitch/f0 track (and corresponding times) of each .wav file
1.3 Print results in Praat Info window
1.4 Add a user input form to specify source and destination directory
1.5 Toggle option to "clean up" as you go
1.6 Save f0 results of each .wav file to a text file
Advanced If we manually corrected some pitch points in the PitchTier of a sound file, or we're only interested in a part of a pitch track, how do we extract and save the desired pitch track?
Could you do it yourself now?
To know a full range of functions Praat can perform
To know a full range of functions Praat can perform
To know a full range of functions Praat can perform
To learn scripting basics, parse praat script, and start praat scripting
To know a full range of functions Praat can perform
To learn scripting basics, parse praat script, and start praat scripting
We've found a way around Praat scripts?
Would you like a step-up tutorial about Praat scripting?
Always check: Praat Manual!
If you want to have more solid understanding and more practices, check out:
Phonetics on Speed: Praat Scripting Tutorial
If you would like to learn more about Praat script basics, check out: Praat Scripting Tutorial by Eleanor Chodroff
Recorded tutorial about Praat script by CASA Lab: Praat Scripting Tutorial by CASA Lab, University at Buffalo
Slides created via the R packages:
xaringan
gadenbuie/xaringanthemer
The chakra comes from remark.js, knitr, and R Markdown.
Theme design ideas from Katie Jolly.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |