Workshop
Scripting Environments

Introduction

In this workshop you vary the macro that you developed in the previous workshops. Now, we aim at creating pictures of sound selections, e.g. vowels, and associated formant contours. You'll open the target sound in an editor and assemble your first editor script, including several environment switches.

Preparation

Open a sound object in an editor (View & Edit). Configure formant settings and activate Show formants. Select a relevant part of the sound (e.g. a vowel) and zoom to selection.

Exercise

  • In sound editor, choose File > New editor script to create an editor script
  • Clear the history
  • Arrange settings for the oscillogram in the Picture window:
    • Edit > Erase all
    • Pen > Black
    • Pen > Line width...; specify 1 and confirm
    • Pen > Solid line
    • Select the viewport with the mouse; horizontal: 0 to 6, vertical: 3 to 5
  • Back in the sound editor, choose File > Draw visible sound, set parameters as follows, and confirm:
  • Draw visible sound: Parameter input form
    Draw visible sound: Parameter input form
  • Go to the script editor and paste the history

The result should look like this:

Erase all Black Line width: 1 Solid line Select outer viewport: 0, 6, 3, 5 Draw visible sound: "no", "yes", 0, 0, "no", "no", "no", "yes"

If you run that script an error message pops up right away:

Error message: Command not available in SoundEditor
Error message: Command not available in SoundEditor

What's the problem?

Erase all is a menu command in the Picture window and the Picture window—together with the Objects window—belongs to the Praat shell. Therefore the command is not readily available in an editor script.

If you want to use shell commands in editor scripts you must tell the script to switch temporarily to the shell environment:

# switch to shell endeditor # configure Picture window (shell commands) Erase all Black Line width: 1 Solid line Select outer viewport: 0, 6, 3, 5 Draw visible sound: "no", "yes", 0, 0, "no", "no", "no", "yes"

With endeditor the script abandons the editor environment and enters the shell environment. All shell commands are executed without complaints, but the script is aborted again. Run the script, try to make sense of the error message, and think about a solution.

Error message: Command not available
Error message: Command not available
At first sight, the error message seems cryptic. But if you think about it it's perfectly logical: After endeditor, the script is working in the shell and executes the Picture window commands without grumbling. Then it comes up against Draw visible sound and tries to apply the command to the current selected object, which is the standard behavior of shell commands. But look for yourself: With a sound object selected you won't find a button or menu command called Draw visible sound anywhere in the Objects window. The solution, obviously, is to steer the script back to the editor environment where it can find the command.
# switch to shell endeditor # configure Picture window (shell commands) Erase all Black Line width: 1 Solid line Select outer viewport: 0, 6, 3, 5 # switch back to editor editor # draw waveform (editor command) Draw visible sound: "no", "yes", 0, 0, "no", "no", "no", "yes"

Now that you've covered the waveform, you'll deal with the formants.

  • Clear the history
  • In the Picture window, choose Pen > Red (or any other color)
  • Select the viewport with the mouse; horizontal: 0 to 6, vertical: 0 to 3
  • In the sound editor, choose Formant > Draw visible formant contour..., set parameters as follows, and confirm:
  • Draw visible formant contour: Parameter input form
    Draw visible formant contour: Parameter input form
  • Go to the script editor and paste the history

The result:

# switch to shell endeditor # configure Picture window (shell commands) Erase all Black Line width: 1 Solid line Select outer viewport: 0, 6, 3, 5 # switch to editor editor # draw oscillogram (editor command) Draw visible sound: "no", "yes", 0, 0, "no", "no", "no", "yes" Red Select outer viewport: 0, 6, 0, 3 Draw visible formant contour: "no", "far", "no", "no", "yes"

What do you think, is the script going to work as expected? Or are we going to look at yet another error message? In the latter case, try to make it work.

To make the script work you must add more environment switching instructions:

# switch to shell endeditor # configure Picture window (shell commands) Erase all Black Line width: 1 Solid line Select outer viewport: 0, 6, 3, 5 # switch to editor editor # draw oscillogram (editor command) Draw visible sound: "no", "yes", 0, 0, "no", "no", "no", "yes" # switch to shell endeditor # configure Picture window (shell commands) Red Select outer viewport: 0, 6, 0, 3 # switch to editor and draw formants editor Draw visible formant contour: "no", "far", "no", "no", "yes"

Because the Praat command history is indifferent regarding environment switches (it's a pity…), the script author alone is responsible for guiding the script through the environment labyrinth. Fortunately, Praat supports us. The two sets of commands—shell commands and editor commands—don't overlap, so Praat will invariably remind us with an error message every time we forget to steer a script to the right environment.

To conclude this exercise, you'll fit the viewport to the entire picture as in the previous exercises. You can apply the familiar history procedure (clear history—adjust viewport—paste history) or you just copy and paste one of the Select outer viewport statements to the end of the script and adjust its arguments (0, 6, 0, 5). Be aware of environment issues…

# switch to shell endeditor # configure Picture window (shell commands) Erase all Black Line width: 1 Solid line Select outer viewport: 0, 6, 3, 5 # switch to editor editor # draw oscillogram (editor command) Draw visible sound: "no", "yes", 0, 0, "no", "no", "no", "yes" # switch to shell endeditor # configure Picture window (shell commands) Red Select outer viewport: 0, 6, 0, 3 # switch to editor and draw formants editor Draw visible formant contour: "no", "far", "no", "no", "yes" # switch to shell endeditor # fit viewport to picture Select outer viewport: 0, 6, 0, 5
Next: Session 4: Generating Output