Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul...

26
Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09

Transcript of Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul...

Page 1: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

Basic and advanced Praat Scripting

Kyuchul YoonDivision of English

Kyungnam University

Seoul National University, Linguistics Department, 2006. 12.09

Page 2: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

2

Praat Scripting Language

• Where to Get Help

• A Brief Look at a Sample Script

• Basic Topics

• Advanced Topics

• Hands-on Activities

• Scripting Examples

Page 3: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

3

Where to Get Help

• Built-in manual “Help” menu of Praat (“search” button)

• Official Praat homepagewww.praat.org

• Discussion grouphttp://uk.groups.yahoo.com/group/praat-users/

• Scripts from other people“Files” menu from the discussion grouphttp://ling.osu.edu/~kyoon/scripts/praat/http://ling.osu.edu/~welby/praat.html

Page 4: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

4

A Brief Look at a Sample Script

Page 5: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

5

Basic Topics• Looks of Praat• What Praat can do• What is an object? (.TextGrid)• How to run a script (example)• Language elements

– Comments, continuation lines, white space– Variables, arrays, formulas– Jumps, loops

Page 6: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

6

Basic Topics: Looks of Praat

• Windows: Praat objects, Praat picture, Editor• Menus: Praat, New, Read, Write

• Command buttons (context-sensitive)– Activated when an object is selected– Changes depending on the type of an object (e.g. Sound)

…Help, Edit, Play, Draw…, Query-, Modify-, Annotate-Analyse: Periodicity-, Spectrum-, Formants & LPC-, …, To Intensity…Manipulate: To Manipulation…Synthesize: Convert-, Filter-, Combine sounds-

• Command buttons (fixed)– Rename…, Copy…, Info, Remove, Inspect

Page 7: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

7

Basic Topics: What Praat can do

• General functionsSound recordingAnalyses (spectral, pitch, formant, intensity)AnnotationManipulation (pitch, duration, intensity, formant)

• Specialized functionsVoice analysis (jitter, shimmer, noise)Listening experimentsFilteringSynthesis (source-filter, articulatory)Learning (neural networks, OT learning)Statistics (multi-dimensional scaling, etc.)

Page 8: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

8

Basic Topics: What is an object? (.TextGrid)

• A segment of computer memory

• Contains a set of data in the Praat program

• Resides in the Praat object window (e.g. sample.wav)– e.g.) Sound sample

• Is NOT a file! (Must be saved!)

• Needs to be clicked to be selected (Shift, Ctrl, dragging)• Types of objects

Sound, Spectrum, Spectrogram, TextGrid, Pitch, Formant, Intensity, Manipulation

Page 9: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

9

Basic Topics: What is an object? (.TextGrid)

• TextGrid object– Created with a sound object selected

Annotate- To TextGrid…

– Used for annotation (segmentation and labeling)

– Consists of a number of tiers

– Two kinds of tiers: • Interval tier: a connected sequence of labeled intervals with

boundaries in between

• Point tier: a sequence of labeled points

Page 10: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

10

Basic Topics: What is an object? (.TextGrid)

• Examining an object through Query…– Gives you information about the selected object

– Information is displayed by default in the Info window

– Query results can be used to put information into a variable• myStartTime = Get start time• myPointLabel$ = Get label of point… 1 1

• Modifying an object through Modify…– Query results can be used to modify an object, e.g. TextGrid

• Set point text… 1 1 ‘myPointLabel$’

Page 11: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

11

Basic Topics: How to run a script (example)

• What is a script?– An executable text with menu commands and action commands– Praat ==> New/Open Praat script– In the script editor, Run ==> Run

• Edit ==> Paste history in the script editor– Records all the menu/action commands– Can be used to copy/paste recorded commands– Constants could be replaced with variables– Clear history to erase all the earlier commands– A good source for beginners!

Page 12: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

12

Basic Topics: How to run a script (example)Read from file... D:\recording\2syllAP-LHa.wavTo TextGrid... "phonological phonetic" phoneticplus Sound 2syllAP-LHaEditselect TextGrid 2syllAP-LHaInsert boundary... 1 0.5Set interval text... 1 1 test=================================================================fileLocation$ = "D:\recording\2syllAP-LHa.wav"uTonesTierName$ = "phonological"sTonesTierName$ = "phonetic"uTonesTier = 1intervalText$ = "test"Read from file... 'fileLocation$'Rename… soundObjTo TextGrid... "'uTonesTierName$' 'sTonesTierName$'" 'sTonesTierName$'Rename… textgridObjplus Sound soundObjEditpause Insert a sample interval?select TextGrid textgridObjInsert boundary... uTonesTier 0.5Set interval text... uTonesTier 1 'intervalText$'

Paste history

Now a script!

Page 13: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

13

Basic Topics: Language elements

• Comments, continuation lines, white space– All white spaces (and tabs) at line beginnings are ignored

• You can use indenting to make your script readable (tabs preferred)sum = 0for i to 10 for j to 10 sum = sum + i*j endforendforpause The total sum is ‘sum’

– Comments lines start with # or ! or ; (# preferred)

– You can split long command lines into multiple lines using …• myStringVariable$ = left$(myMainSentence$, (lengthOfUtterance-

…lengthOfRightWord))

Page 14: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

14

Basic Topics: Language elements• Variables, arrays, formulas

– Numeric variables, variable = formula• e.g.) lengthOfSentence = 10

– String variables (end in a dollar sign), variable$ = string• e.g.) sentenceToProcess$ = “Praat rules”• Predefined string variables: newline$, tab$, shellDirectory$

– Variable substitution (in between single quotes), ‘variable$’• e.g.) echo The number of characters is ‘lengthOfSentence’

• e.g.) Try ‘lengthOfSentence:2’

– Array variables: simulated with quote substitution• e.g.) for j from 1 to 5

square‘j’ = j*jendfor

square1 = 1, square2 = 4, square3 = 9, square4 = 16, square5 = 25

Page 15: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

15

Basic Topics: Language elements

• Jumps, loopsfor/endfor

################################################# throws = 0repeat

sum = randomInteger(1,6) + randomInteger(1,6) throws = throws + 1

until sum = 12echo Took me ‘throws’ trials

#################################################throws = 1sum = randomInteger(1,6) + randomInteger(1,6)while sum <> 12 throws = throws + 1

sum = randomInteger(1,6) + randomInteger(1,6)endwhileecho Took me ‘throws’ trials

Page 16: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

16

Advanced Topics

• Arguments to commands/scripts

• Object selection

• Calling system commands

• Info window, user interaction, file handling

• Scripting in an editor window

Page 17: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

17

Advanced Topics Arguments to commands/scripts• form/endform keywords

form Types of script arguments

natural numOfSentences_(positive_whole_number) 337

integer amplitudeSteps_(whole_number) -2000

real factorOfZoomOut_(real_number) -3.5

positive factorOfZoomIn_(positive_real_number) 4.7

comment A line with any text

word targetWord_(string_without_spaces) school

sentence targetSentence_(any_short_string) my school

text targetText_(variableName_not_shown) my school bus

boolean trueOrFalse_(with_a_checkbox) 1

choice myChoice_(with_a_radiobox) 1

button choice 1

button choice 2

button choice 3

comment Choose any color

optionmenu Color: 2

option Red

option Green

endform

Page 18: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

18

Advanced Topics Object selection

• How to select objects from your script

select Sound hello1plus Sound hello2minus Sound hello2select allminus Sound hello2Rename… greeting1Remove

##################fullFileName$ = “male02-Seoul-32yrs.wav”Read from file… ‘fullFileName$’filePrefix$ = fullFileName$ - “.wav”newTextGridName$ = filePrefix$ + “.TextGrid”

Page 19: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

19

Advanced Topics Calling system commands

• Call system (DOS/Unix/Linux) commands from a script

directoryName$ = “resultFolder”system_nocheck mkdir ‘directoryName$’

will create a new subfolder in the current folder

Page 20: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

20

Advanced Topics Info window, user interaction, files

• Writing to the Info window from a script…– echo text, clearinfo, print text, printtab, printline text

• A sample scriptclearinfoprintline Minimum MaximumminValue = 1maxValue = 10print ‘minValue’printtabprint ‘maxValue’

Page 21: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

21

Advanced Topics Info window, user interaction, files

• Messages to the user– exit text : to stop the execution of the script with the text message– nowarn Write to WAV file… hello.wav : prevents warning

messages, e.g. about clipped sound samples– noprogress To Pitch… 0 75 500 : prevents from showing a

progress window– nocheck Remove : causes the script to continue even if there’s

nothing to remove– pause text : temporarily halts a Praat script. Continue & Stop buttons.

• Can be used to accept user actions which will be stored in variables• e.g.) User selecting a particular section of a sound object in

an editorstartOfSelection = Get start of selectionendOfSelection = Get end of selection

Page 22: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

22

Advanced Topics Info window, user interaction, files

• File handling– Reading an existing file, e.g. hello.wav, sentences.txt

• Read from file… C:/sound/hello.wav• Read Strings from raw text file… C:/text/sentences.txt

– Creating a new text file in the current folder, e.g. result.txt• fileappend result.txt Hello world!’newline$’• fileappend result.txt I am back!

– Deleting an existing file• filedelete C:/sound/hello.wav

– Getting a list of file names of a particular folder• Create Strings as file list… fileListObj C:/sound/*.wav

– The object fileListObj has a list of filename strings in one column

– With for/endfor, repeated actions can be taken

Page 23: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

23

Advanced Topics Scripting in an editor window

• In a Praat script– editor/endeditor required

• In an editor script– Only endeditor is required

editor Sound soundObj#do your stuff herecursorPosition = Get cursorSelect... cursorPosition-0.02 cursorPosition+0.02Extract sound selection (time from 0)

endeditor

To Spectrum (fft)

Edit

Page 24: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

24

Hands-on Activities

• Write a script that will help measure the duration of a user-selected segment for all of the .wav files in a folder

Keywords to use:

form/endformCreate Strings as file list…for/endforEditpausefileappend

Page 25: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

25

Hands-on Activities# Put variables hereform Parameter settings

word inFolder_(with_sounds) demo-folderword outFile_(to_be_created) measurements.txt

endform# Make a list of files in the folderCreate Strings as file list... fileListObj ‘inFolder$’/*.wavSortnumOfFiles = Get number of stringspause ‘numOfFiles’ files identified. Continue? # Loop through each filefor iFile to numOfFiles

select Strings fileListObjfileName$ = Get string... iFilepause Opening the file ‘fileName$’Read from file... ‘inFolder$’/’fileName$’Rename... soundObjEditeditor Sound soundObj

pause Select the target segmenttargetDuration = Get selection length

endeditorRemovefileappend ‘outFile$’ ‘fileName$’‘tab$’‘targetDuration:2’‘newline$’

endforRemove all# End of script

Page 26: Basic and advanced Praat Scripting Kyuchul Yoon Division of English Kyungnam University Seoul National University, Linguistics Department, 2006. 12.09.

26

Scripting Examples

• Scripts for

(1) Prosody swapping(2) Speech corpus searching(3) K-ToBI labeling