Most of the examples in the previous sessions were not particularly useful, but served to illustrate a feature of the Praat scripting language. But now that you are familiar with most of the important concepts and features of Praat scripting, I would like to conclude this tutorial with a real-life example: a script that does something useful and that combines (almost) all the bits and pieces that have been discussed in the previous sessions:
- object selection
- commands & functions
- input forms
The script is presented in the next section, together with a detailed explanation of every step. But let's start with a description of what the script does.
The purpose of the script is to generate isochronous segments (e.g. vowels, syllables etc.) by manipulation of the duration of the segments. It expects a Sound object and an associated TextGrid object with at least one interval tier. In the interval tier, all of the segments that are to be isochronous at the end (the target segments) must be specified with a unique label.
When the script is launched, the user can choose the interval tier to use, the label of the target segments, and the desired duration of the target segments. The desired duration is either a positive real number or 0. Numbers other then 0 are interpreted as target duration in seconds (i.e. 0.3 means: all target segments should end up being 300 milliseconds long). If the user chooses 0, the script will use the average duration of all the target segments in the original sound as the target duration.
Since duration manipulation in Praat operates with relative durations rather than absolute durations, the script calculates duration ratios for each target segment. These ratios are applied to the original Sound object to lengthen or shorten the target segments. The result is then resythesized to a new Sound object. Finally, a new TextGrid object with updated segment boundaries is created and intervals are populated with the duration ratios.
From the user's perspective, the script works like this:
(1) The user has an annotated sound. All target segments (syllables in this example) are labeled with "s"; non-target segments have an empty label:
(2) The user starts the script and fills the input form:
(3) If the user specified 0 as desired duration this is the result:
All syllables have the same duration (0.238818s), namely the average duration of all annotated syllables in the original sound. The TextGrid shows applied duration ratios for each segment; lengthened segments have ratios > 1, shortened segments have ratios < 1, unmanipulated segments have ratios = 1. Consider the eighth syllable: In the original sound (figure above) it's duration was 0.380666s, now its 0.238818s so a duration ratio of 0.238818 ÷ 0.380666 = 0.627368875 was applied (ratios in the TextGrid are rounded to 3 decimal places, therefore the ratio for the eighth syllable is given as 0.627).
Now let us take the programmer's perspective and have a look at the inner workings of the script. First, an overview of the the script flow:
- Get selected objects (Sound and TextGrid required)
- Show input form; user specifies:
- number of relevant interval tier
- label of target segments
- desired duration of target segments
- Create Manipulation object and DurationTier (see Praat Help to learn how duration manipulation is done in Praat)
- Loop through intervals and:
- calculate duration ratios and new boundaries
- add points to DurationTier
- collect duration ratios and new boundaries for later use (new TextGrid)
- Resynthesize manipulated sound
- Create new TextGrid with fitting boundaries and duration ratios as labels