+ All Categories
Home > Documents > Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch...

Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch...

Date post: 16-Jul-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
32
Scripting with Praat Day 3: Make Praat make decisions for you! 1
Transcript
Page 1: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Scripting with Praat Day 3: Make Praat makedecisions for you! 1

Page 2: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Housekeeping

• Please sign your name on this sheet and indicate “Audit”, “P/F” or “letter grade”

• Office Hours: moved to Library basement • still Tuesday 5-6 and Thursday 4-5

2

Page 3: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Homework 1 Review

• Record yourself saying “Hello, world!” in any language that you like and save to a .wav file on your computer.

• Write a Praat script to read in this wav file and paint a spectrogram of this sound in the Picture window (please show 0 to 8000Hz). Let Praat “garnish” this spectrogram. Add a title to the figure of the form: ““Hello, world!” in <X language> spoken by <your name>”

• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 = <###> Hz” below the plot (don’t overlap the axis label!)

• Begin the script with a comment that gives your name and a brief description of what the script is meant to do.

• Hand in the script file (YourFullName.praat), your wav file, and a PDF of the spectrogram on Canvas.

3

Page 4: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

sound = Read from file: "/Users/penelope/Desktop/untitled.wav" sgram = To Spectrogram: 0.005, 8000, 0.002, 20, "Gaussian"

Paint: 0, 0, 0, 8000, 100, "yes", 50, 6, 0, "yes" Text top: "no", """Hello, world!"" in Malagasy spoken by Penelope"

selectObject: sound pitch = To Pitch: 0, 75, 600 f0 = Get mean: 0, 0, “Hertz" f0$ = string$(f0) Text bottom: "no", "Mean f0 = " + f0$ + " Hz"

Save as PDF file: "/Users/penelope/Desktop/helloworld.pdf"

Homework 1 solution: this works…

4

Page 5: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

# hw1-key.praat Paint a publishable spectrogram # Reads in a wav file, creates and paints a spectrogram of the sound, adds a meaningful title, # finds mean F0 of the sound and prints this value below the spectrogram, and saves a PDF file. # 07-11-2017 Penelope Howe <[email protected]>

# clean up the Picture window in case previous material was present Erase all

# load sound file to unique Object ID variable and create spectrogram from sound file sound = Read from file: "/Users/penelope/Desktop/untitled.wav" sgram = To Spectrogram: 0.005, 8000, 0.002, 20, "Gaussian"

# select the region of the Picture window to draw to Select outer viewport: 0, 6, 0, 4

# paint spectrogram (note: it was selected implicitly when it was created) and add title Paint: 0, 0, 0, 8000, 100, "yes", 50, 6, 0, "yes" Text top: "no", """Hello, world!"" in Malagasy spoken by Penelope Howe"

# create Pitch object to get mean F0 (note: must select the Sound object first. why??) selectObject: sound pitch = To Pitch: 0, 75, 600 f0 = Get mean: 0, 0, "Hertz"

# display F0 to 0 decimal places (note: fixed$( ) function returns a string value) f0$ = fixed$(f0, 0) Text bottom: "no", "Mean f0 = " + f0$ + " Hz"

Save as PDF file: "/Users/penelope/Desktop/praat.pdf"

# remove the objects we just created (note: avoid Remove all command. why??) removeObject: sound, sgram, pitch

Homework 1 solution: this is greatly improved

5

Page 6: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Syntax Review 1

1. Text top: "no", """Hello, world!"" in Malagasy”

• What does “no” mean? Will it work without quotes?

• What error will we get? Why?

• Boolean args have two alternatives:

no$ = “no”Text top: no$, """Hello, world!"" in Malagasy”

no = 0Text top: no, """Hello, world!"" in Malagasy”

• What’s going on with all the quotes in the 2nd argument?

6

Page 7: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Syntax Review 2

1. f0$ = “150” Text bottom: “no”, "Mean f0 = " + f0$ + " Hz”

• What are the pluses for?

• Could we replace them with commas and why?

• Can we use a numeric variable ( f0 ) instead of f0$?

2. f0 = 150 writeInfoLine: “Mean f0 = “, f0, “ Hz

• So how come commas are legal in this example?

• How come we can use a numeric variable in this example?

• What else do we have to change if we want to replace these commas with pluses?

7

Page 8: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Using Variables to Improve Readability/FlexibilityRemember this:

no = 0Text top: no, """Hello, world!"" in Malagasy”

Why might we want to do this?1) Variables help you remember what arguments refer to:

# sgram = To Spectrogram: 0.005, 8000, 0.002, 20, “Gaussian”

window_len = 0.005 ; window length (broadband vs. narrowband)max_freq = 8000 ; maximum frequency analyzedtime_step = 0.002 ; time stepfreq_step = 20 ; frequency stepwin_shape$ = “Gaussian”

sgram = To Spectrogram: window_len, max_freq, time_step, ... freq_step, win_shape$ 8

Page 9: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Using Variables to Improve Readability/FlexibilityRemember this:

no = 0Text top: no, """Hello, world!"" in Malagasy”

Why might we want to do this?2) Variables also help you conveniently modify script function:

# Select outer viewport: 0, 6, 0, 4

plot_x1 = 0plot_x2 = 6plot_y1 = 0plot_y2 = 4

Select outer viewport: plot_x1, plot_x2, plot_y1, plot_y2

9

Page 10: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Exercise: fun with objects, selections, variablesUse an existing short sound file or record a new one and save it to your Desktop. Remove it from the Objects window. Do the following in a script: 1. Store the directory path and file name of your sound file in a variable and

read the sound file into Praat2. Save the total duration of the sound file to a variable3. Use ( Convert -> Lengthen… ) to create a longer version of your original

sound (how much longer is up to you)4. Get the total duration of your new sound, and print the durations of each

sound and the ratio between them to the Info window, to verify that the Lengthen… function worked as expected

5. Create a TextGrid of your original sound (default settings fine)6. Store the directory path and file name for the TextGrid to another variable by

modifying the sound filename variable7. Save the TextGrid to the same location as the sound file8. Remove both the original Sound and the TextGrid from the Objects window

10

Page 11: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

A solution: fun with objects, selections, variables# load sound file and store total duration sfilename$ = "/Users/penelope/Desktop/tone.wav" Read from file: sfilename$ sound = selected("Sound") dur_orig = Get total duration

# create new file with double the length, get new duration and ratio Lengthen (overlap-add): 75, 600, 2 dur_long = Get total duration dur_ratio = dur_long / dur_orig

# print duration info to Info window writeInfoLine: "The original duration is ", dur_orig, "s." appendInfoLine: "The new duration is ", dur_long, "s." appendInfoLine: "Duration increased by a factor of “, dur_ratio, “.”

#create TextGrid from original sound, make filename and save selectObject: sound To TextGrid: "Mary John bell", "bell" tgfilename$ = (sfilename$ - “.wav”) + ".TextGrid" Save as text file: tgfilename$

# TextGrid is already selected, add original sound to selection, remove both plusObject: sound Remove

11

Page 12: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Conditional Statements

https://imgs.xkcd.com/comics/conditionals.png 12

Page 13: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Making decisions

• Sometimes we need a program to perform different actions depending on input or outcomes of calculations

• As in natural language, in programming we call the expressions that introduce multiple possible worlds conditionals

• The simplest form of a conditional has the following structure:

# note, this is not in any particular programming # language. This is an example of pseudocode. if <test> commands to execute if the test returns “true” endif

13

Page 14: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Making decisions

Here's an example with real Praat code:

# what does this code do when executed? if 7 > 9 writeInfoLine: "hello, world!" endif

# what about this? if hello > goodbye writeInfoLine: "hello, world!" endif

14

Page 15: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

if, elsif, else, endif

Praat has four “reserved words” (i.e., don’t use them for anything else, like variable names) set aside for conditional expressions:

• if – introduces a conditional, must include a test condition.

• elsif / elif – allows for the inclusion of another possible true outcome, in case the if condition was false ( Note the spelling, this is not a typo! )

• else – if none of the preceding tests returned true, execute this block of code

• endif – ends the conditional statement

15

Page 16: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Layout best practices (manual)• Notice the indentation:

if 7 >= 9 # we have dropped through a wormhole into # a parallel (and inconsistent) universe writeInfoLine: "hello, not our world!" else # the physical laws of our universe apply writeInfoLine: "hello, world!" endif

• People typically use either a tab or 2 to 4 spaces (or sometimes more) to indent lines inside a block (programmers love to debate about using tabs vs. spaces)

• Blocks within blocks (“nested” blocks) are further indented

• Most importantly, BE CONSISTENT!16

Page 17: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Layout: where does white space matter?• Praat IGNORES white space in the following places:

• At the beginning of any line of code (you can press space or tab as many times as you want)

• Completely blank lines (use blank space to separate chunks of code to make your script more readable)

• Between comma-separated arguments, within mathematical expressions, between and function and the parentheses containing its arguments (again, use spaces to make it readable)

• White space tells Praat where word boundaries are, so it is critical:• To separate words within and at the end of a command name

• Text top: NOT Texttop: • Text top: “no” NOT Text top:”no”

• As a literal character within strings17

Page 18: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Conditional example# get F0 at a fixed time point, and if it is defined,# give its value relative to 150 Hz.

selectObject: “Pitch tone”f0 = Get value at time: 0.01, “Hertz”, “Linear”writeInfoLine: “f0 = “, f0

if f0 = undefined ; Notice NOT --undefined-- appendInfoLine: “F0 is undefined. Check measurement.”elsif f0 = 150 appendInfoLine: "F0 is exactly 150 Hz."elsif f0 < 150 appendInfoLine: "F0 is less than 150 Hz."else appendInfoLine: "F0 is greater than 150 Hz."endif

appendInfoLine: "wasn't that fun?"18

Page 19: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Unknown Variable

• Praat will give you an "Unknown variable" error if you try to use a variable before you declare it.

• You will see this if you try either of the following:

y = m * x + b

text$ = "Please say what this word is " + bit$

19

Page 20: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Declaring Variables

• The way around this problem is to always declare each variable prior to using it. We could fix our line formula with something like:

deltaX = 2

deltaY = 4

b = 1

m = deltaY / deltaX

x = 8

y = m * x + b

writeInfoLine: "y equals ", y, " when x is ", x

20

Page 21: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Unknown Variable Example

You'll also get this Unknown Variable error if you try to use a variable in a test (conditional) prior to assigning it any value, e.g.

# generates an error

if variable$ = ""

writeInfoLine( "Variable has not been set yet" )

else

writeInfoLine( "It exists and it is """, variable$,"""" )

endif

21

Page 22: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Pseudocode script outlining

• Use comments and/or pseudocode to write an outline before you attempt to implement

• With more complicated scripts, it will be important to outline the flow of the script first

• flow control = conditionals, loops (next week), procedures (later), etc., which tell Praat which chunks of code to execute in what order

22

Page 23: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Pseudocode script outlining (NOT a real script!)# measure phonetic characteristics according to segment type

for all TextGrids in given directory

select the TextGridtiername$ = Get name of first tier

if tiername$ = “fricative”get start and end times of the labeled intervalselect the associated Soundmeasure things related to fricatives in that interval

... use more elsif statements for other cons. types

else ; it must be a vowelmeasure things related to vowels in that interval

endif

... repeat these for the 2nd tier in the TextGridendfor 23

Page 24: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Exercise: Random Test

• Praat has a function randomInteger(min, max) which is essentially telling Praat to pick a number between min and max (inclusive). e.g. randomInteger(1, 10) could be read "pick a number from 1 to 10".

• Please write a script to:

1. Generate a random number between 1 and 10 and store it in a numeric variable.

2. Test whether this number is greater than or equal to 6. If so, write "The number was 6 or higher." to the Info window.

3. If the number is not equal to or greater than 6, write "Your number was 5 or lower." to the Info window.

24

Page 25: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Using functions

• Functions perform some action on their input, report some fact about that input, or give some value calculated from that input (some functions also take multiple inputs)

• Like commands, each function expects inputs of a certain type (numeric or string) in a designated order, separated by commas and contained within parentheses

• Each function returns either a string or a numeric value, indicated by presence or absence of $ in the function name

• Already seen some examples: string$(), number(), fixed$()

25

Page 26: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Using functions: example

• Praat has a length() function that simply determines and returns the length of a single string argument

string$ = "what has it got in its pocketses?"length = length( string$ )writeInfoLine: "The string '", string$, "' is ", length, ..." characters long."

26

Page 27: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Test expressions

• The test expression in a conditional ( the expression following if or elsif ) evaluates to a boolean true or false: 0 = false, other numbers = true

• You can verify this yourself with something like:

n = -4if n writeInfoLine( "true!" )endif

• We can also take advantage of this fact to use functions in conditionals...

27

Page 28: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Testing functions

• For example, to test if a file is a wav file or a TextGrid, you could write:

if endsWith( filename$, ".wav" ) # file is a wav, do sound file things to it # ...

elsif endsWith( filename$, ".TextGrid" ) # file is a TextGrid, do TextGrid file things to it # ...else # file is neither (or the case didn't match or typo, etc.) # ...endif

# endsWith(): compares second string to end of first string, if they match, returns true (1), if they don’t match, returns false (0)

28

Page 29: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Available functions

• The Praat documentation lists the available functions:

• Mathematical functions ( manual: Formulas 4 )

• String functions ( manual: Formulas 5 )

29

Page 30: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

Predefined variables

Praat comes with a number of pre-defined variables and constants, some of them are:

• Numeric: • praatVersion stores the current version of Praat you are running.• macintosh, windows, and unix are true (1) if you are on that

platform.• pi, e, and undefined ( manual: constants )

• String: • praatVersion$ version of Praat as a string.• newline$, tab$ store your platform's take on newline and tab.• defaultDirectory$ is the path containing the script file.• homeDirectory$, preferencesDirectory$, and temporaryDirectory$ provide useful directory paths (manual).

30

Page 31: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

How many digits of π does Praat's constant give you?

• Input: Praat's pi constant

• Output: Info window should read:

Praat stores pi to N decimal places.

• where 'N' is replaced with the correct number of decimal places.

• Hint: Praat will give you a number, but you probably want to work with it as a string.

Exercise: digits of π

31

Page 32: Scripting with Praat Day 3: Make Praat make• Calculate mean F0 of the sound file (hint: Pitch object) and print the text “Mean F0 =  Hz” below the plot (don’t overlap

1. Without hard-coding any numbers into your script, use string functions to do the following:

• From input string “Welcome to the linguistics institute”

• Produce the output string “Welcome to the lingstitute”

2. Use numeric and string functions to find for which vowel quality this speaker has maximum and minimum f0 values and the values themselves, given the following inputs:

f0_a = 104 ; f0 for /a/ vowel onset f0_e = 110 ; ... /e/ f0_o = 109 ; ... /o/ f0_i = 117 ; ... /i/ f0_u = 115 ; ... /u/ order$ = “aeoiu”

# Output: The speaker's minimum f0 is ## Hz on vowel /V1/. # The speaker’s maximum f0 is ## Hz on vowel /V2/.

Exercise: behavior of functions

32


Recommended