Praat Scripting for fun and (minimal) profit

Will Styler - Fall 2016

### What you cannot do with Praat scripting
* Label your data/speech recognition * You can label known data with “Forced Alignment”
* Work with programs that aren’t Praat (*)
* Run statistics on your data
* … but you can dump your data into R!
* Anything that you can’t, eventually, do through the Praat UI
* Generate measurements as consistent as hand measurements
* … but you can definitely do some things more easily!

What is a Praat script?


When scripting, you will need to be a bit more specific than you would with a human…


# This is a comment, Praat ignores lines that start with #
# Get the selected sound
soundname$ = selected$ ("Sound")
select Sound 'soundname$'
# Rename it!
Rename... 'soundname$'_resampled
# Resample it, then write to file!
Resample... 10000 50
Write to WAV file... ‘soundname$‘_10000.wav

Praat has changed some of the scripting commands to become “more modern”


Your very first Praat script


Open Praat, then go to “New Praat Script”


Capturing the commands using “History”

Now, open a sound, and narrow the spectrogram

Then, go to the script window and “Paste History”


History saves everything you do in Praat


Almost done!



Praat scripting commands resemble the commands in the GUI


Spectrogram settings… 0 5000 0.005 50


You can make the spectrogram broad-band again!


To go fancier, we need more scripting knowledge!


Variables are like pronouns for data.

“Remember whats-his-name, that guy who brought the great pecan pie last year?”

“Yeah…?”

“Invite whats-his-name to Thanksgiving this year!”

whatshisname$ = Find guy who brought pecan pie

party-invite whatshisname$


Assigning Variables

variable = 1000

variable = Get number of intervals... '1'

start = Get starting point... 1 2

end = Get end point... 1 2

midpoint = start + ((end - start) / 2)

Get vocal tract length from F3 at cursor

f3h = Get third formant
length = (1715/(4 * f3h))
lcm = length * 100
print Your vocal tract length is 'lcm:1' cm

Getting a bit fancier

Sometimes, you want to automate more than one command

You can put multiple lines into a file, and Praat runs through them at a single command!

# Get the selected sound
soundname$ = selected$ ("Sound")
select Sound 'soundname$'
# Rename it!
Rename... 'soundname$'_resampled
# Resample it, then write to file!
Resample... 10000 50
Write to WAV file... ‘soundname$‘_10000.wav

The Button/Skynet continuum


You’ll always trade control for speed of measurement


Working across files


To work on many files, you need more than just the file(s) you have open


Giving Praat files and information



Textgrids are your way of giving Praat the information it can’t figure out on its own


‘for loops’


Let’s say you’re building a lot of toy cars…


‘for’ loops

For loops iterate through large amounts of data, doing the same thing each time.

They always have the format for [var] from 1 to [other var], followed by an indented block, ended with an endfor

select TextGrid Sound1
number_intervals = Get number of intervals... 2
for k from 1 to number_intervals
    int_start = Get starting point... 1 'k'
    int_end = Get end point... 1 'k'
    int_duration = (int_end - int_start)
    print int_duration
endfor
# This code gets the duration of every interval and prints it

Taking control with ‘if’ statements

‘if’ statements

Used when a given action happens only if another condition is met

Usually take the form if [var] = [value], indented block, endif

### If you want an example…
select TextGrid Sound1 number_intervals = Get number of intervals… 2 for k from 1 to number_intervals int_label$ = Get label of interval… 1 ‘k’ if int_label$ = “v” int_start = Get starting point… 1 ‘k’ int_end = Get end point… 1 ‘k’ int_duration = (int_end - int_start) print int_duration endif endfor

Other Residents of Script-land


Praat is weird


Sanity Checking


Debugging

Think before you automate


… but don’t be too reluctant



Praat’s Scripting Help

It’s actually really good


In Defense of Cannibalism


Steal code.


The majority of my scripts are open source

https://github.com/stylerw

Please steal them.


You will hate your life if you Praat script


… but suddenly, it will work



And you will realize that the world is a wonderful place


Get more scripting information

Download useful examples (as well as a fun guide on using Praat) from:

http://savethevowels.org/praat/


You can also see a more fleshed out version of this slide show here


… and please feel free to email me when you’re having trouble!


Let’s look at an example!

Example script is here

# This part presents a form to the user
form Measure Formants and Duration
    comment Sound file extension:
        optionmenu file_type: 2
        option .aiff
        option .wav
endform

directory$ = chooseDirectory$ ("Choose the directory containing sound files and textgrids")
# This will need to be changed to \ below for PC users
directory$ = "'directory$'" + "/" 
resultfile$ = "'directory$'"+"formantlog.txt"

header_row$ = "filename" + tab$ + "vowel" + tab$ + "Duration" + tab$ + "F1" +
tab$ + "F2" + tab$ + "F3" + newline$

fileappend "'resultfile$'" 'header_row$'

# List of all the sound files in the specified directory:
Create Strings as file list... list 'directory$'*'file_type$'
number_files = Get number of strings

# This opens all the files one by one
for j from 1 to number_files
    select Strings list
    filename$ = Get string... 'j'
    Read from file... 'directory$''filename$'
    soundname$ = selected$ ("Sound")
    filedur = Get total duration
    # identify associated TextGrid
    gridfile$ = "'directory$''soundname$'.TextGrid"
    if fileReadable (gridfile$)
        Read from file... 'gridfile$'
        select TextGrid 'soundname$'
        number_intervals = Get number of intervals... 1

# Go through each item
for k from 1 to number_intervals
    select TextGrid 'soundname$'
    int_label$ = Get label of interval... 1 'k'
    #checks if interval has a label
    if int_label$ <> ""
        # Calc start, end, and duration of interval
        intstart = Get starting point... 1 'k'
        intend = Get end point... 1 'k'
        intdur = intend - intstart
        intmid = intstart + (intdur / 2)

    # Get all the formants!
    select Sound 'soundname$'
    To Formant (burg)... 0 5 5500 0.025 50
    intf1 = Get value at time... 1 'intmid' Hertz Linear
    intf2 = Get value at time... 2 'intmid' Hertz Linear
    intf3 = Get value at time... 3 'intmid' Hertz Linear

    # Dump results into a file.
    result_row$ = "'filename$'" + tab$ + "'int_label$'" + tab$ + "'intdur'" + tab$ +
    "'intf1'" + tab$ + "'intf2'" + tab$ + "'intf3'" + newline$
    fileappend "'resultfile$'" 'result_row$'
endif
endfor
endif
endfor
# That's all!