Goal: Write up your first Praat Script to automatically extract the info of duration from TextGrid files in a folder.
Bear in mind that whatever can be manually extracted in Praat can be also automated in a Praat Script!
I usually write up a Praat Script in four steps:
Figure out how to extract the acoustic parameter manually
Convert the manual operations into Praat syntax by using the "Paste history" function in the Praat script
Extract a list of files and loop over the list to extract each file name. Represent the file name with a variable
Substitute any specific filenames you have in the output of "Paste history" with the variables you created
1. Extract duration info manually from one TextGrid file
Before writing up a script, we want to consider how we would extract duration info for a specific TextGrid file manually.
Step 1.1 Create a Praat Table: "Select a TextGrid Object -> Tabulate -> Down to Table -> OK"
If you view the generated Table Object, it will provide the following info for each of the annotated intervals: the starting time, the tier name, the annotated text, and the ending time.
Step 1.2 Create a difference column: We want to calculate the duration of each interval by calculating the difference between the starting and the ending time.
Now if we view our Table Object again, there is a fifth column of duration info (in seconds).
Step 1.3 Save the Table locally: "Select (the Table Object) -> Save -> Save as comma separated file...". Remember to change the extension name from ".Table" to ".csv" to match the format of the file.
Congratulations, you have manually extracted the duration info from file Pearson_Pin_Pen.TextGrid!
2. Use a PraatScript to get the duration info from the same TextGrid file
Praat has a very neat function called "paste history", which will translate what we have done manually to the syntax of Praat as a documentation of each command that we have implemented.
Now we are just going to use that function to record our commands and see how they are represented in Praat language. The first step is to create a blank Praat Script.
Step 2.1 Create a blank Praat Script and save it to the local folder that contains the TextGrid: "Praat -> New Praat Script -> (in untitled script) command/contrl + s -> Duration.praat".
Keep in mind that the extension is ".praat" for all praat script files.
Step 2.2 Clear history: "Edit -> Clear History".
Step 2.3 Repeat Manual operations and "paste history"
Repeat what we did manually to extract the duration information: Recall that we first imported the TextGrid file. Then we used the "Tabulate..." button to transfer it to a Table. Next, we appended a difference column to that table using the "Modify" button. Lastly, we saved it as a CSV file on our local computer.
Don't make any additional move in this process such as playing a random sound or visualizing the interim output of the table file. Every command needs to be directly related to the generation of the output .csv file.
After having finished these operations, we can go to the window of the Praat script and click on "Edit -> Paste history". This command has given me four lines of operation history, as follows:
Step 2.4 Try out the generated PraatScript: "Run -> Run"
Remember to delete all the objects in the object window, including the TextGrid file we imported earlier. Also, delete the CSV file that we saved in our local folder. We want to see whether we can generate it again with the script that we came up with.
Click on "Run" and choose "Run" as shown in the following figure.
Now, check in your local folder whether a CSV file has been generated. If so, congratulations! You have written up your first Praat Script that. we'll give you the duration information of a specific TextGrid file!
3. Modify the PraatScript to extract duration info from ALL the TextGrid files in a folder
The last question is how to generalize our TextGrid. What if we have a dozen of task word files in a folder and we don't want to manually type in the name of those files? We want the script to automatically identify the names of the TextGrid files and the directory of the folder they are in, and to loop over each file and generate a corresponding .csv file.
Step 3.1 Automatically extract names of TextGrid files in the same folder of the script
You can do it with the following commands, where "./" refers to the folder where the script is in, and *.TextGrid means anything in the format ending with ".TextGrid".
Create Strings as file list: "gridlist", "./*.TextGrid"
Step 3.2 Loop over the list to obtain the names of the TextGrid file and their corresponding .csv output.
We first need to get the number of TextGrid names on that list:
select Strings gridlist
n = Get number of strings
Then we use a for loop to iterate over the gridlist to get the names of the input files and output files. We will create a variable called grid$ to stand for the input file and another file csv$ to stand for the output file.
for i from 1 to n
select Strings gridlist
grid$ = Get string: i
csv$ = grid$- ".TextGrid" + "_duration.csv"
endfor
Step 3.3 Loop over each file to generate a .csv
The last step is to use those variables we created to replace the specific filenames we have in our previous Praat script.
After substitution, the final Script looks like this:
Create Strings as file list: "gridlist", "./*.TextGrid"
select Strings gridlist
n = Get number of strings
for i from 1 to n
select Strings gridlist
grid$ = Get string: i
csv$ = grid$- ".TextGrid" + "_duration.csv"
Read from file: "./'grid$'"
Down to Table: "no", 6, "yes", "no"
Append difference column: "tmax", "tmin", "Duration"
Save as comma-separated file: "./'csv$'"
endfor
Congrats! Now you have a Praat Script that you may use to extract the duration info from any TextGrid files in any folder. You can do so by simply by putting the script file into that folder, open it, and run it!
Exercise: Extracting vowel formants using Praat script
Now that we have gone through the logistics of writing up a praat script, hopefully running an existing praat script would seem less intimidating.
There is another praat script in your folder: vowel_formant.praat. Open that script in Praat, then in the script window, hit Run -> Run. See what happens!
Resources
TextGrid file format: https://www.fon.hum.uva.nl/praat/manual/TextGrid_file_formats.html
Vowel formant measurements: https://www.youtube.com/watch?v=5gVraRxer6o
Evaluation and feedback
Please provide your feedback for this tutorial via this link: https://qfreeaccountssjc1.az1.qualtrics.com/jfe/form/SV_cObYY8IoTOetEh0