A lot of tutorials at this point would jump right in and start showing you shortcuts to start Praat programming, but please resist the urge. We will do that soon, but I think that will be much easier to explain and understand in a more profound way if we talk first about variables and data types in Praat
A fundamental part of programming is manipulating variables. Variables are essentially containers that hold information. Let's jump right into using them:
age = 34
In programmer parlance, we've assigned the number 34 to a variable called "age". which means we've made a container named "age" and have filled it with the number thirty-four. There are a few key details to note here.
- "age" is a name I made up. It might as well have been asdfg (although that's a stupid idea). When I first started learning to program, I found it hard to know what parts in a script were made up by the programmer, and what parts were built-in Praat commands. Well... no easy answer to that now, hang in there.
- Secondly, note that the variable (age) is on the left hand side and the value (34) is on the right. In Praat's language this is a requirement.
- We use the "=" sign, although there's a subtle difference between the way we normally think of it and the way it's used here. It doesn't really mean "equals", it's actually called the assignment operator. Its job is to take values and assign them to variables.
- Another thing is that there is a single space on either side of the assignment operator. This isn't really required, but is considered good style and supposedly improves readability of your code.
Well-formed variable names
Let's look at another variable:
numKids = 2
Hopefully it's easy to guess that this variable represents "number of kids". I've chosen a name that is short and meaningful. Short so I won't get mad about typing it a bunch of times and am less likely to type it wrong; and meaningful so I won't have trouble remembering what it means when I come back to it a few weeks later.
But why didn't I just write "Number of kids = 2"? Besides being long, this would cause an error and Praat would not finish the script, because it has restrictions on what a variable name can look like. Open a new scripting window, type "Number of kids = 2" (without the quotes), and run it. Check out the error message, and be happy that you understand it.
Here are the rules for variable names in Praat (hopefully I haven't left anything out):
A variable can be composed of a combination of letters and numbers, but Praat requires that it start with a lowercase letter. It cannot contain spaces or other "weird" characters like accents or pound signs or the like. Variables are case sensitive (rollsRoyce is not the same variable as rollsroyce).
Those of us used to a Roman script have a hard time reading words without spaces (not a problem for you, Thailand!!! Whaddup China!!!). In order to make variable names more readable there are two common practices used to make up for the lack of spaces. You can either use underscores instead of spaces, or use what's known as camel case, where every subsequent word begins with a capital letter:
beers_drunk = 4 beersDrunk = 4
I personally prefer camel case because I find it easier to type, but it really doesn't matter. Just pick one. You should always stick to one naming convention throughout a script to simplify your life.
Which of these are legal variable names in Praat, and why?
1. Gang 2. street 3. beers 4. imperial IPA 5. fish_and_chips 6. fishAndFries 7. 2ndSt 8. name$
1. Incorrect 2. Correct 3. Correct 4. Incorrect 5. Correct 6. Correct 7. Incorrect 8. Correct.
The last one was a trick. The dollar sign at the end of a variable name is legal, but it has a special meaning in Praat. We'll talk more about this in the next section.
What's the point?Who cares? Why are we talking about variables? Well, VARIABLES ARE INCREDIBLY USEFUL AND ARE BASICALLY THE WHOLE POINT OF PROGRAMMING. You abstract away from a specific number and just have an entity that represents a number, which can be changed and reused in a series of commands, no matter what it's value is: We can treat this variable just like a number, make calculations with it, change its value, rinse and repeat.
Some more examplesHere we assign two variables, add them up, and print the result to the Info Window.
lunchBeers = 3 dinnerBeers = 8 totalBeers = lunchBeers + dinnerBeers appendInfoLine: totalBeers
You could have written appendInfoLine: 3 + 8, but that would be short-sighted. Variables are where the true power and flexibility of programming come to play. Entering the number 3 or 8 directly in an equation instead of using variables like in the example is called "hard-coding", and you should generally avoid this. It may seem trivial now, but when you get a longer script that you want to modify, you just change a value once at the moment you assign it (i.e.change lunchBeers = 3 to lunchBeers = 7), and it works for all subsequent times in the script that you use its name, instead of changing a meaningless number a bajillion times in a longer script. This will cause you many headaches. Avoid hard-coding!
# Hard-coding: in longer scripts this will # ruin your life. I already have to change # it multiple times if Jim orders another # beer: appendInfoLine: "Dan drank", 3, "beers" appendInfoLine: "Jim drank", 8, "beers" totalBeers = 3 + 8 appendInfoLine: totalBeers # The (usually) better way: using variables # that can be changed once. danBeers = 3 jimBeers = 8 totalBeers = danBeers + jimBeers appendInfoLine: totalBeers
Note what happens in the following script:
danBeers = 3 danBeers = danBeers + 1 appendInfoLine: danBeers
What is the value of danBeers now? Print it to the info window with appendInfoLine to find out. Is that what you were expecting?
Whenever we change the value of a simple variable like a number, it resets the variable's value and keeps only the most recent change, so the last time we changed danBeers is the number we get. Hopefully you expected the value you got. Let's look at another:
#Let's order some beers. We're in England in the early 2000's, and they #have an early last call, but the pub will remain open, so as is the custom, #we load the table with beers to drink after last call #I'm ordering three danBeers = 3 #Jim ordered eight jimBeers = 8 #Jim went to the bathroom. I'm commandeering #his beers danBeers = danBeers + jimBeers
How many beers does Jim have now? As far as our script is concerned, he still has 8. It remained unchanged since it was only used and not reassigned, or changed, using the "=" assignment operator. In the line 'danBeers = danBeers + jimBeers', the variable jimBeers was used in the equation, but I never actually did anything to change its value (I didn't reassign it with the assignment operator '=' like I did with danBeers), so the value of jimBeers will remain the same until the end of the script, and jimBeers still equals 8. Let's complete the crime and remove his beers by resetting the value of jimBeers to 0.
#Let's order some beers. We're in England in the 2000's, and they #have last call but the pub will remain open, so as is the custom, #we load the table with beers to drink after last call clearinfo #I'm ordering three danBeers = 3 #Jim ordered eight jimBeers = 8 #Jim went to the bathroom. I'm commandeering #his beers danBeers = danBeers + jimBeers jimBeers = 0 appendInfoLine: "Dan has ", danBeers, " beers." appendInfoLine: "Jim has ", jimBeers, " beers."
There is a shorthand common among programming languages to increment the value of a variable +=, so instead of the line 'danBeers = danBeers + jimBeers', I could write 'danBeers += jimBeers' to not have to type danBeers twice (-=, *=, and /= also work by the way).
Important Praat-specific note!
If you're using a value less than one (the number begins with a decimal), Praat requires that there be a leading zero before the decimal point:
#Let's say I changed my mind, and just #drank a third of one of jim's beers to #mess with him. He's bigger than #me and will not let me keep his beers. #he starts off with eight jimBeers = 8 # this will generate an error, # because it's a decimal number that # doesn't start with 0. Comment it out # with a hashtag jimBeers -= .3 # this will not generate an error jimBeers -= 0.3 # Notice the -= shortcut above. # It's the same as this: #jimBeers = jimBeers - 0.3
1. Go to Praat's scripting manual and see what other mathematical operators are available. How do you get a square root? How do you do exponents, division, and multiplication?
2. Just mess around. Create some variables, give them values, and do mathematical operations with those variables. Print the results to the Info Window. Don't hard-code any numbers once you start doing the operations! Try to break things down and see where they go wrong.
Next page: String variables
This work is licensed under a Creative Commons Attribution 4.0 International License.