Scripting with Praat Day 1: Your first scripts! Before we do anything else…

• Make sure you have the latest version of Praat installed (6.0.29)

• (Praat -> About Praat…) to check the version number

• www.praat.org if you need to update

Note: You should include version number and download date when citing Praat in published work, so make a note of these whenever you upgrade. Why script anyway??

• Automate recurring tasks to save yourself countless hours of soul-crushing repetition

• Ensure consistency and avoid random human error

• Improve reproducibility of work and maintain detailed records of methodology Managing expectations…

• Praat can’t distinguish reasonable and unreasonable measurement values

• You will usually still need to manually annotate your data

• Scripts do all and only the same things you can do with the Praat GUI The Power of Praat How do we harness this power?

• 3 domains of necessary knowledge/skill: •Linguistics •Praat •Programming & debugging In this course…

•Linguistics •Praat •Programming & debugging “Programming” vs. “Scripting”? What is “debugging”? Three types of possible errors

• Syntax errors If a line of code doesn’t adhere to Praat’s syntactic rules, it will not be executed, and Praat will issue an error. e.g., 2 + 3 = 5 is grammatically correct; 2 + = 3 5 causes an error

• Runtime errors Praat will always attempt to execute syntactically-correct commands, but a runtime error will occur if some circumstance makes the command impossible. e.g., trying to read a file that doesn’t exist, trying to perform a Textgrid command on a Sound object

• Semantic errors If the syntax is correct and the command can be executed, Praat will produce a result, but it may not be what you expect/want. e.g., you measure formants for a diphthong at its temporal midpoint We can already generate syntax errors…

• Praat -> Goodies -> Calculator

• Keyboard: ⌘U (Mac), ctrl-U (Windows, )

• Try these ( without [ ] ):

• [ 2 + 3 ] ; [ 2 + = 3 ] -> ??; [ 3 + “blind mice” ] -> ?? (Brief) Praat Review: Windows and Objects Objects window

• Central hub of Praat

• All data loaded, recorded, extracted, or generated through commands appears here as an object

• Each object has a type (e.g., Sound, Textgrid, Table, Pitch — see many available types here)

• Each type has specific associated data and possible commands/actions Sound objects

• Objects are listed as: Type name (Sound untitled)

• When object is selected, associated available commands and menus appear to the right = dynamic buttons and menus

• These change according to type of object selected

• Fixed buttons at bottom of screen are always available Exercise: Record a Sound

1. Open the SoundRecorder window ( Objects window → New → Record mono sound, or ⌘R )

2. Record yourself saying something very short (e.g. “help!”)

3. Save directly to a file on your Desktop (not via the Objects window).

4. Now press “Save to list & Close” ScriptEditor (Praat manual)

• Praat has a built-in text editor for writing/editing scripts

• It lacks some helpful functionality of other programming text editors (more on this later), but it does have three extremely useful features:

• History mechanism (indispensable shortcut and learning tool)

• Run (⌘R) and Run Selection (⌘T) commands

• It can add your scripts as commands to Praat’s existing fixed and dynamic menus (more on this much later) Exercise: History Mechanism

1. Open ScriptEditor window ( Praat menu → New Praat script )

2. Press escape [ESC] a few times (delete any text that appears as a result)

3. Paste your command history into the ScriptEditor window ( Edit → Paste history ) or (⌘H)

4. Check out your new Praat script!

Discussion: 1. What do you notice about the text that appears? 2. What commands do not appear? Exercise: Running the output

What will happen if we run this script?? The best way to find out is to try it!!

1. Delete the sound file you previously saved to your Desktop.

2. Run the script ( Run → Run, or ⌘R)

3. What happened?

Don't be afraid to experiment!! Making and breaking our first Praat script!

1. Select the Sound object in the 5. Paste your command history into Objects window the ScriptEditor window ( Edit → Paste history, ⌘H ) 2. Erase any text in the ScriptEditor window ( select all and then Edit → 6. Run your new script ( Run → Run, Erase, or delete ) or ⌘R )

3. Clear your command history in the 7. Create a new TextGrid object ScriptEditor ( Edit → Clear history ) ( Objects window → Annotate → To TextGrid... ) & accept defaults by clicking [OK] 4. Press the dynamic button ‘Play’ in the Objects window 8. Now what happens when you run your script? Error messages are your friends!

• Read them! They often provide detailed indications of the problem in your script.

• If you followed exactly and only the steps outlined above, you should have something like this: Selecting an object

• Before you can use an object and its associated commands, you have to select it!!

• When an object is selected, Praat assumes all further commands apply to that object until you 1) implicitly, or 2) explicitly, tell Praat to select another object: 1. When you create a new object, it is automatically selected (this is why creating a Textgrid caused our script to crash) 2. You can issue an explicit command in your script, such as: selectObject: “Sound untitled” selectObject: “TextGrid untitled” General syntax (see manual): selectObject: “ObjectType objectname” Saving scripts and file types

• You will usually want to save your script files

• Script files are plain text files, but to make them easily identifiable, we often give them the file extension .praat

• You may want to keep all scripts for a particular project in a dedicated ‘scripts/‘ directory, but it really doesn’t matter as long as you know where they are Programming text editors

• You must use a text editor, i.e. NOT a word-processing program like Word, Pages, etc. (these programs add hidden formatting commands and save as binary, not text)

• Despite the various cool features of Praat’s ScriptEditor, other programming-specific text editors provide helpful features you might like to check out (e.g., syntax color-coding, auto-indent, brace matching — you’ll see why these are useful as we progress)

• Suggestions: • All platforms: Sublime Text (free to try) with syntax highlighting • Windows: notepad++ (free download) with possible syntax highlighting • (incl. Mac): vim (comes preinstalled) with syntax highlighting Comments

# lines beginning with a # are comments ; so are lines starting with a semicolon # Praat won’t try to execute them

• You can leave a helpful comment like this: # user needs to hear, and approve, the sound selectObject ( “Sound sound” ) Play

• You can't use # for an inline comment: selectObject ( “Sound sound” ) Play # user needs to hear, and approve, the sound

• But you can do this: selectObject ( “Sound sound” ) Play ; user needs to hear, and approve, the sound Best practices for commenting

• Your code already tells readers how you are doing a thing, your comments should tell them why!

• Don’t wait! Comment while you are coding to explain purpose of each step

• In the header, always include: (1) what the script is for (2) what inputs it expects (and where those come from) and (3) what output it produces (and where that's useful), e.g.:

## makeTextGrid.praat, by Penelope Howe (12 April 2012) ## This script opens all .wav and .TextGrid files in directories (can be same or different) input by the user at runtime. It creates a TextGrid for each of the sound files, then opens the sound file and the TextGrid in the editor so user can add boundaries and labels. It skips over any sound files that already have an associated .TextGrid file. It saves new .TextGrids to the .TextGrid directory. Exercise: Hello, world! Version 1: The Info window

• Please write the following Praat script and save it as ‘hello.praat':

# hello, world! in the Info window writeInfoLine: "hello, world!"

# another good use of comments is to leave # yourself notes while developing or debugging...

# NOTES: # experiment with changing the text and running this # script multiple times. # what EXACTLY does ‘writeInfoLine’ do? Version 1: The Info window

# hello, world! in the Info window writeInfoLine: ”hello, world!"

# experiment with changing the text and running this # script multiple times. # what EXACTLY does 'writeInfoLine' do?

# hint: compare it with the behavior of: appendInfoLine: ”It's nice to meet you."

# see http://www.fon.hum.uva.nl/praat/manual/ Scripting_3_1__Hello_world.html Version 2: The Picture window

# hello, world! in the Picture window

Erase all Text top: "yes", "Hello, world!"

# questions: # - what is the colon in the second line for? # - what does the ‘yes’ in the second line mean? # - how could you learn the scripting syntax for the Text top: # command if you didn’t already know it?

# see http://www.fon.hum.uva.nl/praat/manual/ Scripting_3_1__Hello_world.html Version 3: The Demo window

# hello, world! in the demo window demo Erase all demo Axes: 0, 10, 0, 10 demo Text: 5, “centre”, 5, “half”, “Hello, world!”

# experiment with changing the parameters! # - what happens if you play with the numbers? # - can you identify x and y positions? # - what if you don’t Erase all first? # - can you change the font, size and color?

# see: http://www.fon.hum.uva.nl/praat/manual/Demo_window.html Exercise: The Picture window

# Close the Picture window and rerun the script — what happens? # How can you ensure the text will appear in the same location # each time?

# hello, world! in the Picture window

Erase all # hint: how would you select the drawing area manually? # how can you transfer manually-executed actions to a script? Text top: "yes", "Hello, world!"

# see http://www.fon.hum.uva.nl/praat/manual/ Scripting_3_1__Hello_world.html