Script Editor & Command History
In this workshop you'll learn how to record and adapt macros. Let's assume you often need to create pictures of sound objects and their accompanying pitch contour; it's necessary that the pictures have a specific and uniform look.
The GUI approach would be, to arrange settings in the Picture window (viewport, line width, color etc.), select the sound, and draw it. Then, re-arrange settings in the Picture window, generate and select the pitch object, and draw it. To maintain consistency and uniformity you probably make notes to remember the pitch analysis parameters and the picture settings for future occasions.
With a simple macro, on the other hand, consistency is built-in and guaranteed and in the future uniform looking pictures are only one mouse click away. We'll assemble the macro step by step and suggest some possible adaptions.
Open Praat (we'll need the Objects window and the Picture window) and load a couple of sound objects. Select one sound object.
- Create a new Praat script (
Praat > New Praat script)
- Clear the history (
Edit > Clear history)
- Arrange settings for the sound in the Picture window:
Edit > Erase all Pen > Black Pen > Line width...; specify 1and confirm Pen > Solid line
- Select the viewport with the mouse; horizontal: 0 to 6, vertical: 4 to 6
- In the Objects window choose
Draw > Draw...; click Standardsand confirm
- Back in the script editor, choose
Edit > Paste historyto create a script including all commands from above
All GUI actions you performed as suggested above (mouse clicks and keyboard input) are translated into syntactically correct statements and inserted in the script. The script should look like this:
Erase all Black Line width: 1 Solid line Select outer viewport: 0, 6, 4, 6 Draw: 0, 0, 0, 0, "yes", "Curve"
Before you do anything else save the script! In the script editor choose
Next, you generate a pitch object and draw it above the waveform. Make sure a sound object is selected.
- In the script editor, choose
Edit > Clear historyto erase the commands that we've already inserted
- Arrange settings for the pitch contour in the Picture window:
- Select the viewport with the mouse; horizontal: 0 to 6, vertical: 0 to 4.5
Pen > Blue Pen > Line width...; specify 3 and confirm
- In the Objects window choose
Analyse periodicity > To Pitch...; click Standardsand confirm
Draw > Draw...; click Standardsthen specify the frequency range 50 to 600 Hz, uncheck Garnishand confirm
- Back in the Picture window add some garnish:
Pen > Black Pen > Line width...; specify 1and confirm Margins > Draw inner box Margins > Marks left every...; click Standardsthen specify Distance: 50, uncheck Draw dotted linesand confirm Margins > Text left...; click Standardsthen add Frequency (Hz)to the text field and confirm
- Finally, select a viewport including the entire picture so that it can be saved or copied to the clipboard; horizontal: 0 to 6, vertical: 0 to 6
- Back in the script editor, put the cursor in the last empty line (create one if necessary) and choose
Edit > Paste historyto append the new commands to the script
Now, the script should look like this (don't forget to save it!):
Erase all Black Line width: 1 Solid line Select outer viewport: 0, 6, 4, 6 Draw: 0, 0, 0, 0, "yes", "Curve" Select outer viewport: 0, 6, 0, 4.5 Blue Line width: 3 To Pitch: 0, 75, 600 Draw: 0, 0, 50, 600, "no" Black Line width: 1 Draw inner box Marks left every: 1, 50, "yes", "yes", "no" Text left: "yes", "Frequency (Hz)" Select outer viewport: 0, 6, 0, 6
That's it. From now on, every time you need a similar picture of a signal and it's pitch contour just select the sound object and run the script. If the script is not open in the script editor, choose
If you like your scripts neat and tidy, I would suggest one small addition: If a script generates new objects only for internal use, these objects should be removed at the end. Otherwise the objects list would be cluttered with unexplained objects after each script run. In our case that means if the pitch object is only used for drawing it should be removed after drawing. The easiest way to do that is to add a new line at the end of the script and type Remove. This command removes selected objects, which suits us since the pitch object is automatically selected after generation. If you don't dare typing commands yet, apply command history: Clear history, select an object, click
There are many other ways to adapt the script. To do so, just edit the script, save the new version, and run it. Suggestions:
- To generate a red pitch contour instead of a blue one substitute Blue (line 8) with Red (see the
Penmenu for other colors).
- Adapt the line width of the pitch contour (3 in the example, line 9) to your needs.
- Adapt the pitch range for analysis (line 10) and/or for the drawing (line 11)
Practice often! If you encounter a task that has to be repeated more then once or twice wait a moment, think about the task, and if it is suitable for a simple macro, generate a macro! Edit your macros: change commands and parameters or delete and rearrange lines. Make test runs, assess the effects, and adjust the script until it fits your needs. Internalize the macro workflow: Clear history → perform action(s) → paste history.