| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Scripting 5.5. Procedures</title> |
| 3 | <style> |
| 4 | td { padding-left: 5pt; padding-right: 5pt; } |
| 5 | th { padding-left: 5pt; padding-right: 5pt; } |
| 6 | code { white-space: pre-wrap; } |
| 7 | dd { white-space: pre-wrap; } |
| 8 | </style> |
| 9 | </head><body bgcolor="#FFFFFF"> |
| 10 | |
| 11 | <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 | Scripting 5.5. Procedures |
| 13 | </b></font></table></table> |
| 14 | <p>Sometimes in a Praat script, you will want to perform the same thing more than once. In <a href="Scripting_5_4__Loops.html">§5.4</a> we saw how <i>loops</i> can help there. In this section we will see how <i>procedures</i> (also called <i>subroutines</i>) can help us.</p> |
| 15 | <p>Imagine that you want to play a musical note with a frequency of 440 Hz (an “A”) followed by a note that is one octave higher, i.e. has a frequency of 880 Hz (an “a”). You could achieve this with the following script:</p> |
| 16 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 440, 0.2, 0.01, 0.01<br></code> |
| 17 | <code> Play<br></code> |
| 18 | <code> Remove<br></code> |
| 19 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 880, 0.2, 0.01, 0.01<br></code> |
| 20 | <code> Play<br></code> |
| 21 | <code> Remove<br></code> |
| 22 | <p>This script creates a sound with a sine wave with an amplitude of 0.4 and a frequency of 440 Hz, then plays this sound, then changes the sound into a sine wave with a frequency of 880 Hz, then plays this changed sound, and then removes the Sound object from the object list.</p> |
| 23 | <p>This script is perfect if all you want to do is to play those two notes and nothing more. But now imagine that you want to play such an octave jump not only for a note of 440 Hz, but also for a note of 400 Hz and for a note of 500 Hz. You could use the following script:</p> |
| 24 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 440, 0.2, 0.01, 0.01<br></code> |
| 25 | <code> Play<br></code> |
| 26 | <code> Remove<br></code> |
| 27 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 880, 0.2, 0.01, 0.01<br></code> |
| 28 | <code> Play<br></code> |
| 29 | <code> Remove<br></code> |
| 30 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 400, 0.2, 0.01, 0.01<br></code> |
| 31 | <code> Play<br></code> |
| 32 | <code> Remove<br></code> |
| 33 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 800, 0.2, 0.01, 0.01<br></code> |
| 34 | <code> Play<br></code> |
| 35 | <code> Remove<br></code> |
| 36 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 500, 0.2, 0.01, 0.01<br></code> |
| 37 | <code> Play<br></code> |
| 38 | <code> Remove<br></code> |
| 39 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 1000, 0.2, 0.01, 0.01<br></code> |
| 40 | <code> Play<br></code> |
| 41 | <code> Remove<br></code> |
| 42 | <p>This script works but is no longer perfect. It contains many similar lines, and is difficult to read.</p> |
| 43 | <p>Here is where <i>procedures</i> come in handy. With procedures, you can re-use similar pieces of code. To make the three parts of the above script more similar, I’ll rewrite it using two variables (<i><code><font size=+1>frequency</font></code></i> and <i><code><font size=+1>octaveHigher</font></code></i>):</p> |
| 44 | <code> frequency = 440<br></code> |
| 45 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01<br></code> |
| 46 | <code> Play<br></code> |
| 47 | <code> Remove<br></code> |
| 48 | <code> octaveHigher = 2 * frequency<br></code> |
| 49 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 50 | <code> Play<br></code> |
| 51 | <code> Remove<br></code> |
| 52 | <code> frequency = 400<br></code> |
| 53 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01<br></code> |
| 54 | <code> Play<br></code> |
| 55 | <code> Remove<br></code> |
| 56 | <code> octaveHigher = 2 * frequency<br></code> |
| 57 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 58 | <code> Play<br></code> |
| 59 | <code> Remove<br></code> |
| 60 | <code> frequency = 500<br></code> |
| 61 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01<br></code> |
| 62 | <code> Play<br></code> |
| 63 | <code> Remove<br></code> |
| 64 | <code> octaveHigher = 2 * frequency<br></code> |
| 65 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 66 | <code> Play<br></code> |
| 67 | <code> Remove<br></code> |
| 68 | <p>You can now see that seven lines of the script appear identically three times. I'll put those seven lines into a <i>procedure</i> that I name <b><code><font size=+1>playOctave</font></code></b>:</p> |
| 69 | <code> <b>procedure</b> playOctave<br></code> |
| 70 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01<br></code> |
| 71 | <code> Play<br></code> |
| 72 | <code> Remove<br></code> |
| 73 | <code> octaveHigher = 2 * frequency<br></code> |
| 74 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 75 | <code> Play<br></code> |
| 76 | <code> Remove<br></code> |
| 77 | <code> <b>endproc</b><br></code> |
| 78 | <p>As you see, a <i>procedure definition</i> in Praat consists of three parts:</p> |
| 79 | <dl> |
| 80 | <dd style="position:relative;padding-left:1em;text-indent:-2em">1. a line with the word <b><code><font size=+1>procedure</font></code></b>, followed by the name of the procedure, followed by a pair of parentheses;</dd> |
| 81 | <dd style="position:relative;padding-left:1em;text-indent:-2em">2. the <i>body</i> of the procedure (here: seven lines);</dd> |
| 82 | <dd style="position:relative;padding-left:1em;text-indent:-2em">3. a line with the word <b><code><font size=+1>endproc</font></code></b>.</dd> |
| 83 | </dl> |
| 84 | <p>You can put a procedure definition anywhere in your script; the beginning or end of the script are common places.</p> |
| 85 | <p>The bodies of procedures are executed only if you <i>call</i> the procedure explicitly, which you can do anywhere in the rest of your script:</p> |
| 86 | <code> frequency = 440<br></code> |
| 87 | <code> @playOctave<br></code> |
| 88 | <code> frequency = 400<br></code> |
| 89 | <code> @playOctave<br></code> |
| 90 | <code> frequency = 500<br></code> |
| 91 | <code> @playOctave<br></code> |
| 92 | <code> <b>procedure</b> playOctave<br></code> |
| 93 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01<br></code> |
| 94 | <code> Play<br></code> |
| 95 | <code> Remove<br></code> |
| 96 | <code> octaveHigher = 2 * frequency<br></code> |
| 97 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 98 | <code> Play<br></code> |
| 99 | <code> Remove<br></code> |
| 100 | <code> <b>endproc</b><br></code> |
| 101 | <p>This script works as follows. First, the number 440 is assigned to the variable <i><code><font size=+1>frequency</font></code></i> in line 1. Then, execution of the script arrives at the <b><code><font size=+1>@</font></code></b> (“call”) statement of line 2. Praat then knows that it has to jump to the procedure called <i><code><font size=+1>playOctave</font></code></i>, which is found on line 7. The execution of the script then proceeds with the first line of the procedure body, where a Sound is created. Then, the other lines of the procedure body are also executed, ending with the removal of the Sound. Then, the execution of the script arrives at the <b><code><font size=+1>endproc</font></code></b> statement. Here, Praat knows that it has to jump back to the line after the line that the procedure was called from; since the procedure was called from line 2, the execution proceeds at line 3 of the script. There, the number 400 is assigned to the variable <i><code><font size=+1>frequency</font></code></i>. In line 4, execution will jump to the procedure again, and with the next <b><code><font size=+1>endproc</font></code></b> the execution will jump back to line 5. There, 500 is assigned to <i><code><font size=+1>frequency</font></code></i>, followed by the third jump to the procedure. the third <b><code><font size=+1>endproc</font></code></b> jumps back to the line after the third <b><code><font size=+1>@</font></code></b>, i.e. to line 7. Here the execution of the script will stop, because there are no more executable commands (the procedure definition at the end is not executed again).</p> |
| 102 | <h2>Arguments</h2> |
| 103 | <p>The above example contains something awkward. The procedure <i><code><font size=+1>playOctave</font></code></i> requires that the variable <i><code><font size=+1>frequency</font></code></i> is set to an appropriate value, so before calling <i><code><font size=+1>playOctave</font></code></i> you always have to insert a line like</p> |
| 104 | <code> frequency = 440<br></code> |
| 105 | <p>This can be improved upon. In the following version of the script, the procedure <i><code><font size=+1>playOctave</font></code></i> requires an explicit <i>argument</i>:</p> |
| 106 | <code> @playOctave: 440<br></code> |
| 107 | <code> @playOctave: 400<br></code> |
| 108 | <code> @playOctave: 500<br></code> |
| 109 | <code> <b>procedure</b> playOctave: frequency<br></code> |
| 110 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01<br></code> |
| 111 | <code> Play<br></code> |
| 112 | <code> Remove<br></code> |
| 113 | <code> octaveHigher = 2 * frequency<br></code> |
| 114 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 115 | <code> Play<br></code> |
| 116 | <code> Remove<br></code> |
| 117 | <code> <b>endproc</b><br></code> |
| 118 | <p>This works as follows. The first line of the procedure now not only contains the name (<i><code><font size=+1>playOctave</font></code></i>), but also a list of variables (here only one: <i><code><font size=+1>frequency</font></code></i>). In the first line of the script, the procedure <i><code><font size=+1>playOctave</font></code></i> is called with the <i>argument</i> 440. Execution then jumps to the procedure, where the argument 440 is assigned to the variable <i><code><font size=+1>frequency</font></code></i>, which is then used in the body of the procedure.</p> |
| 119 | <h2>Encapsulation and local variables</h2> |
| 120 | <p>Although the size of the script has now been reduced to 12 lines, which cannot be further improved upon, there is still something wrong with it. Imagine the following script:</p> |
| 121 | <code> frequency = 300<br></code> |
| 122 | <code> @playOctave: 440<br></code> |
| 123 | <code> @playOctave: 400<br></code> |
| 124 | <code> @playOctave: 500<br></code> |
| 125 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: frequency<br></code> |
| 126 | <code> <b>procedure</b> playOctave: frequency<br></code> |
| 127 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01<br></code> |
| 128 | <code> Play<br></code> |
| 129 | <code> Remove<br></code> |
| 130 | <code> octaveHigher = 2 * frequency<br></code> |
| 131 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 132 | <code> Play<br></code> |
| 133 | <code> Remove<br></code> |
| 134 | <code> <b>endproc</b><br></code> |
| 135 | <p>You might have thought that this script will write “300” to the Info window, because that is what you expect if you look at the first five lines. However, the procedure will assign the values 440, 400, and 500 to the variable <i><code><font size=+1>frequency</font></code></i>, so that the script will actually write “500” to the Info window, because 500 is the last (fourth!) value that was assigned to the variable <i><code><font size=+1>frequency</font></code></i>.</p> |
| 136 | <p>What you would want is that variables that are used inside procedures, such as <i><code><font size=+1>frequency</font></code></i> and <i><code><font size=+1>octaveHigher</font></code></i> here, could somehow be made not to “clash” with variable names used outside the procedure. A trick that works would be to include the procedure name into the names of these variables:</p> |
| 137 | <code> frequency = 300<br></code> |
| 138 | <code> @playOctave: 440<br></code> |
| 139 | <code> @playOctave: 400<br></code> |
| 140 | <code> @playOctave: 500<br></code> |
| 141 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: frequency<br></code> |
| 142 | <code> <b>procedure</b> playOctave: playOctave.frequency<br></code> |
| 143 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, playOctave.frequency, 0.2, 0.01, 0.01<br></code> |
| 144 | <code> Play<br></code> |
| 145 | <code> Remove<br></code> |
| 146 | <code> playOctave.octaveHigher = 2 * playOctave.frequency<br></code> |
| 147 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, playOctave.octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 148 | <code> Play<br></code> |
| 149 | <code> Remove<br></code> |
| 150 | <code> <b>endproc</b><br></code> |
| 151 | <p>This works. The six tones will be played, and 00"will be written to the Info window. But the formulation is a bit wordy, isn’t it?</p> |
| 152 | <p>Fortunately, Praat allows an abbreviated version of these long names: just leave “playOctave” off from the names of the variables, but keep the period (<code><font size=+1>.</font></code>):</p> |
| 153 | <code> frequency = 300<br></code> |
| 154 | <code> @playOctave: 440<br></code> |
| 155 | <code> @playOctave: 400<br></code> |
| 156 | <code> @playOctave: 500<br></code> |
| 157 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: frequency<br></code> |
| 158 | <code> <b>procedure</b> playOctave: .frequency<br></code> |
| 159 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, .frequency, 0.2, 0.01, 0.01<br></code> |
| 160 | <code> Play<br></code> |
| 161 | <code> Remove<br></code> |
| 162 | <code> .octaveHigher = 2 * .frequency<br></code> |
| 163 | <code> Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, .octaveHigher, 0.2, 0.01, 0.01<br></code> |
| 164 | <code> Play<br></code> |
| 165 | <code> Remove<br></code> |
| 166 | <code> <b>endproc</b><br></code> |
| 167 | <p>This is the final version of the script. It works because Praat knows that you are using the variable names <i><code><font size=+1>.frequency</font></code></i> and <i><code><font size=+1>.octaveHigher</font></code></i> in the context of the procedure <i><code><font size=+1>playOctave</font></code></i>, so that Praat knows that by these variable names you actually mean to refer to <i><code><font size=+1>playOctave.frequency</font></code></i> and <i><code><font size=+1>playOctave.octaveHigher</font></code></i>.</p> |
| 168 | <p>It is advisable that you use such “local” variable names for all <i>parameters</i> of a procedure, i.e. for the variables listed after the <b><code><font size=+1>procedure</font></code></b> word (e.g. <i><code><font size=+1>.frequency</font></code></i>), as well as for all variables that you create in the procedure body (e.g. <i><code><font size=+1>.octaveHigher</font></code></i>). In this way, you make sure that you don't inadvertently use a variable name that is also used outside the procedure and thereby perhaps inadvertently change the value of a variable that you expect to be constant across a procedure call.</p> |
| 169 | <h2>A list of numeric and string arguments</h2> |
| 170 | <p>You can use multiple arguments, separated by commas, and string arguments (with a dollar sign in the variable name):</p> |
| 171 | <code> @listSpeaker: “Bart”, 38<br></code> |
| 172 | <code> @listSpeaker: “Katja”, 24<br></code> |
| 173 | <code> <b>procedure</b> listSpeaker: .name$, .age<br></code> |
| 174 | <code> appendInfoLine: “Speaker ”, .name$, “ is ”, .age, “ years old.”<br></code> |
| 175 | <code> <b>endproc</b><br></code> |
| 176 | <code style="color:red">=></code><br> |
| 177 | <code> Speaker Bart is 38 years old.<br></code> |
| 178 | <code> Speaker Katja is 24 years old.<br></code> |
| 179 | <p>or</p> |
| 180 | <code> @conjugateVerb: “be”, “I am”, “you are”, “she is”<br></code> |
| 181 | <code> <b>procedure</b> conjugateVerb: .verb$, .first$, .second$, .third$<br></code> |
| 182 | <code> writeInfoLine: “Conjugation of 'to ”, .verb$, “':”<br></code> |
| 183 | <code> appendInfoLine: “1sg ”, .first$<br></code> |
| 184 | <code> appendInfoLine: “2sg ”, .second$<br></code> |
| 185 | <code> appendInfoLine: “3sg ”, .third$<br></code> |
| 186 | <code> <b>endproc</b><br></code> |
| 187 | <code style="color:red">=></code><br> |
| 188 | <code> Conjugation of 'to be':<br></code> |
| 189 | <code> 1sg I am<br></code> |
| 190 | <code> 2sg you are<br></code> |
| 191 | <code> 3sg she is<br></code> |
| 192 | <p>For the arguments you can use expressions:</p> |
| 193 | <code> @playOctave: 400 + 100<br></code> |
| 194 | <p>As with all string literals, the double quotes in literal string arguments should be doubled:</p> |
| 195 | <code> <b>procedure</b> texts: .top$, .bottom$<br></code> |
| 196 | <code> Text top: “yes”, .top$<br></code> |
| 197 | <code> Text bottom: “yes”, .bottom$<br></code> |
| 198 | <code> <b>endproc</b><br></code> |
| 199 | <code> @texts: """ hello"" at the top", """goodbye"" at the bottom"<br></code> |
| 200 | <p>unless you use curly quotes:</p> |
| 201 | <code> @texts: “" hello" at the top”, “"goodbye" at the bottom”<br></code> |
| 202 | <p>or even</p> |
| 203 | <code> @texts: ““ hello” at the top”, ““goodbye” at the bottom”<br></code> |
| 204 | <h2>Restrictions</h2> |
| 205 | <p>You can have any number of procedures in your script. The name of a procedure can have any number of characters (at least 1, of course). A procedure name can consist of letters (“word characters”), underscores (“<sub>”</sub>), periods (“.”) and digits, but it cannot start with a digit; this is the same restriction as for variable names.</p> |
| 206 | <h2>Functions</h2> |
| 207 | <p>The Praat scripting language does not have the concept of a “function” like some other scripting languages do. A function is a procedure that returns a number, a string, a vector, a matrix, or a string array. For instance, you can imagine the function <code><font size=+1>squareNumber</font></code> (), which takes a number (e.g. 5) as an argument and returns the square of that number (e.g. 25). Here is an example of how you can do that, using the global availability of local variables:</p> |
| 208 | <code> @squareNumber: 5<br></code> |
| 209 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “The square of 5 is ”, squareNumber.result, “.”<br></code> |
| 210 | <code> <b>procedure</b> squareNumber: .number<br></code> |
| 211 | <code> .result = .number ^ 2<br></code> |
| 212 | <code> <b>endproc</b><br></code> |
| 213 | <code style="color:red">=></code><br> |
| 214 | <code> The square of 5 is 25.<br></code> |
| 215 | <p>Another way to emulate functions is to use a variable name as an argument:</p> |
| 216 | <code> @squareNumber2: 5, “square5”<br></code> |
| 217 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “The square of 5 is ”, square5, “.”<br></code> |
| 218 | <code> <b>procedure</b> squareNumber2: .number, .squareVariableName$<br></code> |
| 219 | <code> '.squareVariableName$' = .number ^ 2<br></code> |
| 220 | <code> <b>endproc</b><br></code> |
| 221 | <code style="color:red">=></code><br> |
| 222 | <code> The square of 5 is 25.<br></code> |
| 223 | <p>However, this uses variable substitution, a trick better avoided.</p> |
| 224 | <h3>Links to this page</h3> |
| 225 | <ul> |
| 226 | <li><a href="_endproc_.html"><code><font size=+1>endproc</font></code></a> |
| 227 | <li><a href="_procedure_.html"><code><font size=+1>procedure</font></code></a> |
| 228 | <li><a href="Scripting.html">Scripting</a> |
| 229 | <li><a href="Scripting_5__Language_elements_reference.html">Scripting 5. Language elements reference</a> |
| 230 | </ul> |
| 231 | <hr> |
| 232 | <address> |
| 233 | <p>© Paul Boersma 2017-09-04,2025</p> |
| 234 | </address> |
| 235 | </body> |
| 236 | </html> |