Basic and advanced Praat Scripting Kyuchul Yoon Division

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

Praat Scripting Language • • • Where to Get Help A Brief Look at a Sample Script Basic Topics Advanced Topics Hands-on Activities Scripting Examples 2

Where to Get Help • Built-in manual “Help” menu of Praat (“search” button) • Official Praat homepage www. praat. org • Discussion group http: //uk. groups. yahoo. com/group/praat-users/ • Scripts from other people “Files” menu from the discussion group http: //ling. osu. edu/~kyoon/scripts/praat/ http: //ling. osu. edu/~welby/praat. html 3

A Brief Look at a Sample Script 4

Basic Topics • • • Looks of Praat What Praat can do What is an object? (. Text. Grid) How to run a script (example) Language elements – Comments, continuation lines, white space – Variables, arrays, formulas – Jumps, loops 5

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 6

Basic Topics: What Praat can do • General functions Sound recording Analyses (spectral, pitch, formant, intensity) Annotation Manipulation (pitch, duration, intensity, formant) • Specialized functions Voice analysis (jitter, shimmer, noise) Listening experiments Filtering Synthesis (source-filter, articulatory) Learning (neural networks, OT learning) Statistics (multi-dimensional scaling, etc. ) 7

Basic Topics: What is an object? (. Text. Grid) • 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, Text. Grid, Pitch, Formant, Intensity, Manipulation 8

Basic Topics: What is an object? (. Text. Grid) • Text. Grid object – Created with a sound object selected Annotate. To Text. Grid… – 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 9

Basic Topics: What is an object? (. Text. Grid) • 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 • my. Start. Time = Get start time • my. Point. Label$ = Get label of point… 1 1 • Modifying an object through Modify… – Query results can be used to modify an object, e. g. Text. Grid • Set point text… 1 1 ‘my. Point. Label$’ 10

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

Basic Topics: How to run a script (example) Paste history Now a script! Read from file. . . D: recording2 syll. AP-LHa. wav To Text. Grid. . . "phonological phonetic" phonetic plus Sound 2 syll. AP-LHa Edit select Text. Grid 2 syll. AP-LHa Insert boundary. . . 1 0. 5 Set interval text. . . 1 1 test ================================= file. Location$ = "D: recording2 syll. AP-LHa. wav" u. Tones. Tier. Name$ = "phonological" s. Tones. Tier. Name$ = "phonetic" u. Tones. Tier = 1 interval. Text$ = "test" Read from file. . . 'file. Location$' Rename… sound. Obj To Text. Grid. . . "'u. Tones. Tier. Name$' 's. Tones. Tier. Name$'" 's. Tones. Tier. Name$' Rename… textgrid. Obj plus Sound sound. Obj Edit pause Insert a sample interval? select Text. Grid textgrid. Obj Insert boundary. . . u. Tones. Tier 0. 5 Set interval text. . . u. Tones. Tier 1 'interval. Text$' 12

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 = 0 for i to 10 for j to 10 sum = sum + i*j endfor pause The total sum is ‘sum’ – Comments lines start with # or ! or ; (# preferred) – You can split long command lines into multiple lines using … • my. String. Variable$ = left$(my. Main. Sentence$, (length. Of. Utterance…length. Of. Right. Word)) 13

Basic Topics: Language elements • Variables, arrays, formulas – Numeric variables, variable = formula • e. g. ) length. Of. Sentence = 10 – String variables (end in a dollar sign), variable$ = string • e. g. ) sentence. To. Process$ = “Praat rules” • Predefined string variables: newline$, tab$, shell. Directory$ – Variable substitution (in between single quotes), ‘variable$’ • e. g. ) echo The number of characters is ‘length. Of. Sentence’ • e. g. ) Try ‘length. Of. Sentence: 2’ – Array variables: simulated with quote substitution • e. g. ) for j from 1 to 5 square‘j’ = j*j endfor square 1 = 1, square 2 = 4, square 3 = 9, square 4 = 16, square 5 = 25 14

Basic Topics: Language elements • Jumps, loops for/endfor ######################### throws = 0 repeat sum = random. Integer(1, 6) + random. Integer(1, 6) throws = throws + 1 until sum = 12 echo Took me ‘throws’ trials ######################### throws = 1 sum = random. Integer(1, 6) + random. Integer(1, 6) while sum <> 12 throws = throws + 1 sum = random. Integer(1, 6) + random. Integer(1, 6) endwhile echo Took me ‘throws’ trials 15

Advanced Topics • • • Arguments to commands/scripts Object selection Calling system commands Info window, user interaction, file handling Scripting in an editor window 16

Advanced Topics Arguments to commands/scripts • form/endform keywords form Types of script arguments natural num. Of. Sentences_(positive_whole_number ) 337 integer amplitude. Steps_(whole_number ) -2000 real factor. Of. Zoom. Out_(real_number ) -3. 5 positive factor. Of. Zoom. In_(positive_real_number ) 4. 7 comment A line with any text word target. Word_(string_without_spaces ) school sentence target. Sentence_(any_short_string ) my school text target. Text_(variable. Name_not_shown ) my school bus boolean true. Or. False_(with_a_checkbox ) 1 choice my. Choice_(with_a_radiobox ) 1 button choice 2 button choice 3 comment Choose any color optionmenu Color: 2 option Red option Green endform 17

Advanced Topics Object selection • How to select objects from your script select Sound hello 1 plus Sound hello 2 minus Sound hello 2 select all minus Sound hello 2 Rename… greeting 1 Remove ######### full. File. Name$ = “male 02 -Seoul-32 yrs. wav” Read from file… ‘full. File. Name$’ file. Prefix$ = full. File. Name$ - “. wav” new. Text. Grid. Name$ = file. Prefix$ + “. Text. Grid” 18

Advanced Topics Calling system commands • Call system (DOS/Unix/Linux) commands from a script directory. Name$ = “result. Folder” system_nocheck mkdir ‘directory. Name$’ will create a new subfolder in the current folder 19

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 script clearinfo printline Minimum min. Value = 1 max. Value = 10 print ‘min. Value’ printtab print ‘max. Value’ Maximum 20

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 editor start. Of. Selection = Get start of selection end. Of. Selection = Get end of selection 21

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… file. List. Obj C: /sound/*. wav – The object file. List. Obj has a list of filename strings in one column – With for/endfor, repeated actions can be taken 22

Advanced Topics Scripting in an editor window • In a Praat script – editor/endeditor required • In an editor script – Only endeditor is required editor Sound sound. Obj #do your stuff here cursor. Position = Get cursor Select. . . cursor. Position-0. 02 cursor. Position+0. 02 Extract sound selection (time from 0) endeditor To Spectrum (fft) Edit 23

Hands-on Activities • Write a script that will help measure the duration of a userselected segment for all of the. wav files in a folder Keywords to use: form/endform Create Strings as file list… for/endfor Edit pause fileappend 24

Hands-on Activities # Put variables here form Parameter settings word in. Folder_(with_sounds) demo-folder word out. File_(to_be_created) measurements. txt endform # Make a list of files in the folder Create Strings as file list. . . file. List. Obj ‘in. Folder$’/*. wav Sort num. Of. Files = Get number of strings pause ‘num. Of. Files’ files identified. Continue? # Loop through each file for i. File to num. Of. Files select Strings file. List. Obj file. Name$ = Get string. . . i. File pause Opening the file ‘ file. Name$’ Read from file. . . ‘in. Folder$’/’file. Name$’ Rename. . . sound. Obj Edit editor Sound sound. Obj pause Select the target segment target. Duration = Get selection length endeditor Remove fileappend ‘out. File$’ ‘file. Name$’‘tab$’‘target. Duration: 2’‘newline$’ endfor Remove all # End of script 25

Scripting Examples • Scripts for (1) Prosody swapping (2) Speech corpus searching (3) K-To. BI labeling 26
- Slides: 26