- The added line is THIS COLOR.
- The deleted line is THIS COLOR.
#freeze
[[FrontPage]]
#logparanoia()
* A Quick Look at Praat Scripting [#ncd89e32]
- This page is intended as a brief but somewhat "unorthodox" introduction to Praat scripting.
-- The primary target readers are ones who have some (partial) knowledge about programming/script language (e.g., C, perl, awk, python, VBA), and thus know, say, what ''array/if (conditional)/for (iteration, loop)'' mean. If you new to programming, see other sources((Highly recommended: Ingmar Steiner (2006-2007) '''Automatic Speech Data Processing with Praat'''. Lecture Notes. (PDF file is available at: http://www.coli.uni-saarland.de/~steiner/praat/lecturenotes.pdf .) )).
-- The contents are fragmentary. For unknown commands, consult ''Help'' in Praat first. It provides you valuable information.
The contents are fragmentary. Consult other sources if you want to learn Praat Scripting from a scratch((Highly recommanded: Ingmar Steiner (2006-2007) '''Automatic Speech Data Processing with Praat'''. Lecture Notes. (PDF file is available at www.coli.uni-saarland.de/~steiner/praat/lecturenotes.pdf.) )).
//PDF file is [[here>www.coli.uni-saarland.de/~steiner/praat/lecturenotes.pdf]].
''Table of Contents''
#contents
** Hello world [#xbb42d70]
+ Choose ''New Praat script'' from the ''Praat menu'' to open the ScriptEditor window.
+ Type:
echo Hello world
+ Choose ''Run'' from the ''Run menu'' (or type C-r) to execute.
- '''Cf.''' [[Scripting 3.1. Hello world>http://www.fon.hum.uva.nl/praat/manual/Scripting_3_1__Hello_world.html]] (official site)
** Language Specification Worth Mentioning [#b3a53f90]
- comment
-- If you want to write a comment at the end of a statement, for example, you can use ";", instead of "#"
str$ = "abcdefg" ; string variable
num = 3 ; numeric variable
echo 'str$''tab$''num'
# An error occurs if ";" is replaced with "#"
*** General [#k6026e40]
//- No declaration is required to use a variable.
- variable types: numerics and strings
- array
- Output to the Info Window: ''echo'', ''printline'', and ''print''
--The three scripts below work in the same way (''clearinfo'' clears the Info window):
---
echo Hello world
---
clearinfo
printline Hello world
---
clearinfo
print Hello world'newline$'
-- The two scripts below, however, do not work in the same way.
---
echo This is the first sentence.
echo This is the second sentence.
---
clearinfo
printline This is the first sentence.
printline This is the second sentence.
- Comments
-- "''#''" and "'';''" are comment characters. Praat ignores the comment characters and after them, to the end of the line.
-- Use ";" instead of "#", if you want to write a comment after a statement in the same line.
n = 0
if n = 0 ;conditional
clearinfo
printline Hello world
endif
# An error occurs if ";" is replaced with "#".
--- Some commands, however, cannot be followed by a comment in the same line. For example,
n = 0
if n = 0 ;conditional
clearinfo
printline Hello world ; This sentence is also displayed on the Info window.
endif
// # Hello world script in Praat
// echo Hello world ; This sentence is also displayed on the Info window.
- index numbers start at 1 (not 0).
- Variable Types
-- numerics and strings
--- Variables (either numeric or string) must begin with lower cases.
--- String variables must end with ''$''.
str$ = "foo"
num = 3
### statements below causes an error. ###
#Str$ = "foo" ; it begins with Upper case.
#Num = 3
#str = "foo" ; it lacks $, though it has a string.
-- Arrays are also available.
--- The indices are enclosed with ''"["'' and ''"]"''
--- Like scalars, arrays must begin with lower cases.
--- Similarly, arrays which store strings must have ''$'' before ''"["''
str$[1] = "foo"
str$[2] = "bar"
str$[3] = "hoge"
str$[4] = "piyo"
num[1] = 3
num[2] = 34
num[3] = 22
num[4] = 98
### statements below causes an error. ###
#str[1] = "foo" ; it lacks $, though it has a string.
#Num[4] = 98 ; it begins with Upper case.
-- Unfortunately, hashes (or associative arrays) are not available.
- Comparison Operators
-- You can use "=", in addition to "=="
- Variable Substitution
-- Variables are substituted when put between single quotes.
fruit$ = "apple(s)"
count = 3
echo I bought 'count' 'fruit$'.
--- ''NOTE1:'' No double quotations are required to literals in ''print'' (and ''printline'' and ''echo''). Compare
fruit$ = "apple(s)"
count = 3
echo "I bought" 'count' 'fruit$' "."
--- ''NOTE2:'' If the index in an array is also a variable, a dummy variable is required.
str$[1]= "apple"
str$[2]= "orange"
str$[3]= "melon"
clearinfo
for i from 1 to 3
hop$ = str$[i] ;; a dummy variable
printline 'i''tab$''hop$'
endfor
- Operators
-- Comparison Operators
--- The "equal" operator is "="((In fact, "==" can also be used as an equal operator.)), identical with the assignment operator.
num = 3
str$ = "hoge"
clearinfo
if num = 3
printline 'num'
endif
if str$ = "hoge"
printline 'str$'
endif
- ''elsif'' (neither ''elseif'' nor ''else if'')
-- Strings
--- Basically, operators used for numerics are also applicable to strings.
--- Interestingly, not only string "addition" (i.e., concatenation), but also string "subtraction" (i.e., truncation) is possible, using "+" and "-".
str1$ = "abc"
str2$ = "def"
str3$ = "c"
str4$ = str1$ + str2$
str5$ = str1$ - str3$
clearinfo
printline 'str4$'
printline 'str5$'
--- result:
abcdef
ab
-- autoincrement/autodecrement operators (e.g., i++, i--) are not available.
- Regular expressions are available in many functions.
- Conditional statements
-- ''if(-elsif)(-else)-endif''
--- ''NOTE:'' ''elsif'' (or ''elif''), rather than ''elseif'' and ''else if''.
- ''form'' and ''endform''
- Loops
-- ''for i (from m) to n-endfor'', ''while-endwhile'', ''repeat-until''
--- ''from m'' in ''for'' can be omitted when m = 1.
--- It is not possible to decrement the loop counter in ''for''-loop. Use ''while-endwhile'', instead.
n = 10
clearinfo
while n >= 1
printline 'n'
n = n-1
endwhile
--- There seems to be no statement which escapes from the loop (''break'' in C, or ''last'' in perl). Instead, you can substitute the loop counter (or something) with a value which does not satisfy the loop condition, for example.
clearinfo
max = 10
for i to max
if i = 5
i = max+1 ; escape from the loop
else
printline 'i'
endif
endfor
- No double quotations are required in ''print'' (and ''printline'' and ''echo'')
- ''goto'' is %%not%% available. &color(red){(modified July 17, 2012)};
# the "goto" command in Praat scripts
clearinfo
for i to 10
if i mod 2 = 0
goto next
endif
printline 'i'
label next
endfor
- ''system''
- Subroutines: ''procedure-endproc''
- procedures (or subroutines/functions)
- Regular expressions are available in some string functions:
-- ''index_regex(str$,regex$)''
-- ''rindex_regex(str$,regex$)''
-- ''replace_regex$(str$,regex$,substr$,n)''
--- ''NOTE:'' Functions which return strings (not numerics) require ''$''.
str$ = "abcdefg"
regex$ = "b[^e]*"
clearinfo
if index_regex(str$,regex$) <> 0
printline match
else
printline not match
endif
- decrement in ''for''-loop
- Generally, index numbers start at 1 (not 0).
-- Check, for example, the value of the following string functions:
--- ''mid$(str$,index,len)''
str$ = "abcdefg"
len = length(str$)
clearinfo
for i from 1 to len
hop$ = mid$(str$,i,1)
printline 'i''tab$''hop$'
endfor
--- ''index(str$,substr$)''
str$ = "abcdefg"
substr$ = "bcd"
clearinfo
n = index(str$,substr$)
printline 'n'
- Consider the use of ''Table Object'' when you refer to the contents in text files.
- File IO
-- ''text$ <'' '''filename''' (read the contents of '''filename''')
file$ = "d:\foo.txt"
text$ < 'file$'
clearinfo
print 'text$'
-- ''text$ >'' '''filename''' (make a new file and write the contents of text$)
-- ''text$ >>'' '''filename''' (add the contents of text$ to '''filename''')
file$ = "d:\bar.txt"
text$ = "praat scripting is fun." + "'newline$'"
text$ > 'file$'
#text$ >> 'file$'
-- ''NOTE:'' Consider the use of ''Object'', rather than "''<''" or "''>''". Very often, Object manipulation is easier and more straightforward. For Object, see below.
//, especially when it contains multiple lines .
- [[Scripting 3.1. Hello world>http://www.fon.hum.uva.nl/praat/manual/Scripting_3_1__Hello_world.html]] (official site)
*** Particular to Praat (1): Object Manipulation [#e34387b5]
RIGHT:&color(red){Those who conquer object manipulation techniques conquer Praat scripting. (The present author)};
*** Particular to Praat (2): form-endform [#z692053a]
- ''form'' and ''endform''
//- ''system''
// # Windows
// system dir > hoge.txt && start notepad.exe hoge.txt
//
//- ''nocheck'' '''command'''
//-- equivalent to ''on error resume next'' in some scripts
//
//- ''exit'' ('''message''') and ''pause'' ('''message''')
** Functions of ScriptEditor [#m4b8a142]
- History
- Run a script
** Other ways to execute a script [#c753a9f4]
- praatcon.exe (in Windows)
- sendpraat.exe
- praatcon.exe (in Windows)
-- scripts can be executed by using sendpraat.exe or praatcon.exe.
-- You may use option -a in praatcon.exe if you handle Unicode. (cf. [[Re: [praat-users] Bug in latest praatcon version (by Paul Boersma)>http://uk.groups.yahoo.com/group/praat-users/message/4950]]