This page is last updated in October 2013.
PSL Tutorial/Programming Challenges | praat script archiveThis page offers some simple praat scripting language tutorial and exercises that demonstrates some practical skills of praat scripting, basic to advanced.It assumes that you're familiar with the Praat Scripting official manual(formulas page is hidden from the scripting manual page but is nonetheless very crucial reference):
Who should use this tutorialThis tutorial is written for people with relatively little programming experience in general. If you are a proficient programmer in any language, you should be able to read the manual from above and start writing on your own, or just to consult the existing scripts for some examples,but do not need to walk through this tutorial. (Of course one advantage of going through this tutorial is you'll get a hint of what functions are needed to perform certain tasks, which is not always obvious when you try to figure it out yourself, given that the PSL community is rather small and some things aren't that well documented).
AcknowledgementThank you to Rafael Caro, Music Technology Group, DTIC, UPF, Barcelona, Spain, whose generous support and interests have led me to writing this tutorial, and thinking about PSL in a different way.
Exercise 1In this task, you are going to write a basic script that automatically generates a textgrid for each of the sound files loaded into the Praat Object Window. To do this, load some simple sound files into the praat object window, and your script should be able to go through a loop and generate a textgrid file for each sound file. The textgrid should contain, for instance, two tiers, one interval tier and one point tier, named vowel and F1 respectively.
Tips:
Exercise 2In this task, your goal is to get a measurement of the duration of each non-empty interval in a given textgrid (download by clicking or right click to save it to your computer).Open this textgrid, and study closely for things like how many tiers it has, and also study what functions are needed in order to extract the duration and label of each interval.
Tips:
Exercise 3Congrats on successfully completing the first two tasks. In this task you will need to do some simple computation as well as automation. This folder contains a sound file and a textgrid file of two words, each repeated three times:fala, and shala (from an African language Setswana). When you view them in Praat, you can see that the fricatives /f/ or /sh/ of each word are labeled on the fricatives tier. Now your goal is to obtain a statistical property for each labeled fricative on the fricative tier. To do this, we need to:
(1)find the center 50% of the fricative, where it is the most stable(yes, we're not measuring a point but the middle 50% of the total duration of the fricative); (2)extract a Spectral Slice for this middle 50% portion (this can be manually done in the praat editor window by going to Spectrum->view spectral slice, but in script, you will need to figure out the function to do this); (3)Get these three statistical property: standard deviation, center of gravity, and skewness of this spectral slice. Hint:
Exercise 4From now on we go beyond the automating of manual operations, and focus a little more on the elements of programming. Writing codes needs a lot of planning, and thinking through what will happen exactly when your script is running. Sometimes we may miss something that only comes to our attention when there is a problem in our results. That is why finding a bug, understanding it, and debugging (fix the bug) is always necessary and important in any sort of programming language, almost for any body from a novice to an expert. The reason for the existence of bugs can be a lot. Some times it is simply due to the fact that we have not anticipated the sort of irregularities or messiness in the data from the user, which will always render your results wrong(even though you have tested your script against a lot of data that turned out to be correct). And beware, because the messiness of the data exists everywhere in any sort of tasks you find in real world.
In this task we are going to process some mock-up data extracted from audio signals-pitch tracks. You will first read in this file (you can read in this file manually or automatically). This is called a tab-separated (or tab-delimited) file that is generated from an Excel spreadsheet. You can use 'Read Table from tab-separated file...' in the Open menu of Praat to read this file, which is very useful when you read any sort of excel-like file.When you view this file,you can see that it takes the first row of the file as the header line. The rest looks pretty familiar. It looks like this. Suppose this is a pitch track file. The two columns are 'time' and 'pitch', which gives you one pitch for each timestamp. If you observe this pitch track, you will find one thing strange, that is, some time points are 0 due to the mistakes from pitch tracking (notice that if a time point is 0, the pitch is also 0, but not vice versa). Our first task is to eliminate these rows wherever the time point is 0. In these rows, since pitch is also 0, there is no use in keeping these rows since they provide no information except for taking up our processing resources. So to sum up, your script should scan through all rows, and eliminate any rows where the time value is 0. hint:this task may be a little tricky, depending on what kind of loop you use. Exercise 5If you have successfully completed the last challenge, this one should be not that hard. Basically we use a pitch track that is slightly different from last exercise, and we see that in some cases, there is a pitch that is preceded by a zero and followed by a zero, such as 0,8,0, in the pitch column. In these case the pitch is not continuous and this kind of short-lived pitch value will cause problem for our synthesis (suppose we synthesize a melody from this pitch track). Therefore we want to eliminate this kind of pitch--but we don't need to eliminate this row.We can simply set this pitch value to zero as well.
To sum up, if a pitch value is non-zero,but is preceded by a zero and followed by a zero, we want to set this pitch to zero as well. As part of this task, you should write a main program (or called a main method, which is the main body of your code), and a procedure. This procedure is called by a line in the main method every time you want to set a value to 0. Exercise 6In this task, you simply will need to combine the scripts from exercise 4 and 5 into a single script. This script will take the same pitch track in Exercise 5, and it will apply the two steps of preprocessing to this pitch track file:(1)eliminate zero-value time points;(2)set the short-lived non-zero pitch values to zero.You can be creative in the choice of using procedures, loops, conditionals(if/then),etc., and make the whole thing looks effective,clean,and elegant.
Exercise 7 [edited AUG.22,2013]This is a classic programming challenge. Perhaps you've done it in Python: A sorting problem. The task is simple: given a unsorted list of numbers, such as 1,4,1,7,3,8,10,2, you should write a script that can return a sorted one, preserving any duplicate members: 1,1,2,3,4,7,8,10. In python, this is can be done with a list.sort() built-in function, but it is a classic problem of sorting algorithm that is worth working out yourself.
In Praat Scripting Language, you cannot read in a list like Python in this simple way: a=[2,6,1,5,1] However, the alternate is more practical and useful in real tasks: you need read in the numbers from a text file like this one. Because Praat doesn't support the list like the one above, and it doesn't have real array operations (such as insert an element, remove, pop, etc), you will have to set up your machinery yourself and take care of everything. (That's why the same sorting algorithm implemented in PSL could be at least twice as long as one implemented in Python). Here is a hint of steps you need to go through:
(1) Read in the text file using the Read Matrix from raw text file... function, which can read in numbers from the file provided above. In this case, the list of 14 numbers can be seen as a Matrix with 14 rows, 1 column. (2) Use a loop to assign each of these numbers to a numeric array, a[i]. (3) Now you've got your array, and you can sort this array, print the final results--you don't have to modify the matrix or the original text file itself. (4) Implement your sorting algorithm on this array. The tricky thing about array in PSL is that it doesn't support real array operations like those in Python, so you'll have to be extra careful what kind of operations you will have to manually handle (such as in the 'insert' operation). (5) Print your sorted array to the console, ranked from low to high. More hints: I have written a python version and a PSL version for the solution of this task based on the same algorithm (by no means the only correct algorithm). Exercise 8 (SEP.11,2013)In this exercise you will learn to use the regular expressions. First you can read an intro on this topic here. You can also google it, since regular expression is implemented in major programming languages like java or python (perhaps you can find a complete guide for Perl). The implementation in Praat is not that powerful, but there are still a lot to explore.
Regular expression is useful to deal with patterns in strings. In this exercise, you will work with this file (if you view this in browser you need correct character encoding to display the Chinese characters), which contains some track titles of audio files. Each title contains a mixture of numbers and text (English or Chinese), and your task is to only extract the part of the text, and print it out to the user. This is useful because track names are often a mixture, but say if you are searching for a track name in a database, you would not know their track number and you just want your query to match the text part of the string. NOTE:I have discovered that regular expression in Praat Script does not work well for Chinese characters (or non-ASCII characters in general) on Linux Ubuntu 1204 machine. I have communicated with Paul Boersma, creator of Praat, who confirmed this and will try to fix it. Otherwise, it works just fine for Chinese texts on my Mac OSX machine, and presumably on Windows. The problem on Linux is that the algorithm thinks that the Chinese characters are digits, not text. Exercise 9(OCT.8,2013)In this exercise you will generate some string combinations. Suppose you are looking at some machine learning algorithms and to see how they perform to classify (predict) certain data. So you've got your classic 'weather' data, a data set where you have training data that covers some combinations of certain weather conditions, and the algorithm is going to predict under a particular condition, whether you're going to play the game or not (maybe a golf game outdoor). There are five weather parameters you will consider, each of which can take on these values in the brackets:
outlook {sunny, overcast, rainy} temperature {hot, mild, cool} humidity {high, normal} windy {TRUE, FALSE} play {yes, no} These are just the context of this task, but if this sounds strange to you, it is not required to perform the task. Your real task is that, since you want to test different combinations of these five weather parameters, you want to generate different combinations of strings, such as these: sunny,cool,normal,FALSE,yes sunny,cool,normal,FALSE,no overcast,hot,high,TRUE,yes overcast,hot,high,TRUE,no rainy,hot,high,TRUE,yes rainy,hot,high,TRUE,no ... ... Just as a quick mental exercise, how many different combinations can you generate? 3*3*2*2*2=72 different combinations in total. So now you have your real task. Write a script that can generate all 72 possible combinations of these strings, and then print them to the console. (Optional: try to write them to a file that is saved as a .txt file, you can use the 'writeFileLine()' function) This kind of task is useful because it helps you simplify manual task and serve as an automated string file generator script. Enjoy! Hint: I have written this script here Download some solutions (written by Rafael Caro, MTG, UPF, updates available soon)
|