+ - 0:00:00
Notes for current slide
Notes for next slide

Introduction to Praat and Praat Scripting

Lab Skills Workshop, Phonetics Laboratory, University of Oxford


Chenzi Xu

DPhil Candidate | 2021/12/30 (updated: 2022-02-03)

1

Our Goal

First

Second

Third

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

3

Q: 1. how many have used Praat? 2. how many have used praat scripts?

  1. how many have learnt other scripting/programming languages?

Why Praat? Praat Icon

5

Why Praat?

  • 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!

    • Recurring tasks become less tedious and more efficient.
    • Consistent and Replicable measurement
    • Minimising human error
  • You can create your own application with an autonomous GUI.

6

commercial software countless hours of soul-crushing repetitions

What is Praat capable of?

7

What is Praat capable of?

Creating and manipulating sounds

Visualizing and annotating sounds

Phonetic measurement and analysis

Creating publication-quality plots

Conducting phonetic experiment

7
8

Are you familiar with these tasks?

  • 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

9

Demonstrate load file,view, textgrid, draw a pic.

Are you familiar with these tasks?

Spectrum > Show spectrogram

Pulses > Show pulses

Pitch > Show pitch

Intensity > Show intensity

Formant > Show formants

10

What does Praat contain?

Praat Objects

  • Sound
  • TextGrid
  • Spectrogram
  • Pitch, PitchTier
  • Intensity, IntensityTier
  • ExperimentMFC
  • ...

Praat Editors

  • SoundEditor
  • TextGridEditor
  • PitchEditor
  • PointEditor
  • SpectrumEditor
  • ...

Praat Commands

  • To Intensity...
  • To TextGrid...
  • Zoom in
  • Get Minimum
  • Convert to mono
  • ...

You must select the correct object/environment to issue the right commands!

11

Praat Script

12

Praat Script

String and numeric variables

For loops, if else statements, while loops

Regular expression matching

Comments

Syntax

12

Praat Script

String and numeric variables

For loops, if else statements, while loops

Regular expression matching

Comments

Syntax

OR ... Fast track?

12

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

Scripting Hacks I

Check GUI for Syntax

Arguments for "To Pitch"

To Pitch: 0.0, 75.0, 600.0
13

We're all linguist. Transitive Verbs take direct object otherwise ungrammatical.

command: arguments/components too! otherwise not running

Scripting Hacks II

Macro Recording Workflow (Command History)

  • Obtain a sequence of instructions
1. New Praat Script
2. Edit > Clear history (script editor)
3. GUI actions
4. Edit > Paste history (script editor)
14

Scripting Hacks III

Parse others' script and adapt

  • 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!

15

Scripting Hacks IV

Boilerplate code

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 strings
for 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
16

formulaic, wrapper code Underscore/lowercase is a good combo.

Scripting Hacks IV

Boilerplate code

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 strings
for 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
17

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!

Scripting Hacks IV

Boilerplate code

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 list
for 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
18

Scripting Hacks IV

Boilerplate code

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 strings
for 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
19

Scripting Hacks V

Errors are stepping stones

How to debug?

  • In script editor: Search > Go to line
  • Use printline statements regularly when testing

Types of errors

  • Syntactic errors: typos, use of quotes, etc.
  • Runtime errors: file doesn't exist, command doesn't exist etc.
  • Semantic errors: output doesn't match your goal...
20

Demo and Practice

Task 1: Get total duration of all .wav files

Task 2*: Measure f0 in specified interval

21

Task 1

Basics

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

Level up

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

22

Step 1: How to get duration of one .wav file?

  1. Open one .wav file in Praat Open > Read from file
  2. Select the Sound object, click Query > Query time domain > Get total duration

Avoid using . in the filename (except in the file extension)

23

Step 2: Print command history

  1. At menu bar, Praat > New Praat script
  2. In the script editor, click Edit > Paste history
  3. 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

24

my first praat script

Step 3: Loop over all .wav files

Update the boilerplate code

Any problems?

dir$ = "data/"
Create Strings as file list: "file_list", dir$ + "*.wav"
nFiles = Get number of strings
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$'"
Get total duration
endfor
25

Step 4: Manage output

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 strings
printline '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
26

Step 5: Calculation in a for loop

Sum

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 strings
total_dur = 0
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'
total_dur = total_dur + dur
endfor
printline 'total_dur:4'
27

Level up (optional)

Clean up

There are different methods.

Sometimes leaving objects helps debugging.

clearinfo
for i from 1 to nFiles
#### MORE CODE HERE ####
select all
minus Strings file_list
Remove
endfor
select all
Remove
printline All finished!
28

Level up (optional)

More info at Praat manual and Phonetics on Speed

User Input form

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 1
endform
#### MORE CODE HERE ####

29

Level up (optional)

Toggle option

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
endif
endfor
if clean_up == 1
select all
Remove
endif
printline All finished!
30

Level up (optional)

Write output

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.

Warning message

nFiles = Get number of strings
outFile$ = "duration.txt"
askBeforeDelete = 1
if askBeforeDelete and fileReadable(outFile$)
pauseScript: "File exists! Overwrite?"
endif
deleteFile: 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$, " ", dur
endfor
31

Task 2

Basics

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

Level up

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?

32

Our Goal

To know a full range of functions Praat can perform

33

Our Goal

To know a full range of functions Praat can perform

To master how to navigate and use Praat's GUI
33

Our Goal

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

33

Our Goal

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


We've found a way around Praat scripts?

33

Would you like a step-up tutorial about Praat scripting?

34

Online resources

Always check: Praat Manual!


References and good resources:

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

35

Thank you!

Q & A

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.

36
Paused

Help

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