| 1 |
<html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 |
<title>Scripting 7.2. Scripting an editor from within</title> |
| 3 |
<style> |
| 4 |
td { padding-left: 5pt; padding-right: 5pt; } |
| 5 |
th { padding-left: 5pt; padding-right: 5pt; } |
| 6 |
code { white-space: pre-wrap; } |
| 7 |
dd { white-space: pre-wrap; } |
| 8 |
</style> |
| 9 |
</head><body bgcolor="#FFFFFF"> |
| 10 |
|
| 11 |
<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 |
Scripting 7.2. Scripting an editor from within |
| 13 |
</b></font></table></table> |
| 14 |
<p>This section shows how you can permanently extend the functionality of an editor.</p> |
| 15 |
<p>As an example, consider the following problem: you want to see a graphic representation of the spectrum of the sound around the cursor position in the SoundEditor. To achieve this, follow these steps:</p> |
| 16 |
<dl> |
| 17 |
<dd style="position:relative;padding-left:1em;text-indent:-2em">1. Create a Sound.</dd> |
| 18 |
<dd style="position:relative;padding-left:1em;text-indent:-2em">2. View it in a SoundEditor by clicking <a href="View___Edit.html">View & Edit</a>.</dd> |
| 19 |
<dd style="position:relative;padding-left:1em;text-indent:-2em">3. Choose <b>New editor script</b> from the <a href="File_menu.html">File menu</a> in the SoundEditor. The resulting <a href="ScriptEditor.html">ScriptEditor</a> will have a name like “untitled script [Sound hallo]”.</dd> |
| 20 |
<dd style="position:relative;padding-left:1em;text-indent:-2em">4. Type the following lines into the ScriptEditor:</dd> |
| 21 |
</dl> |
| 22 |
<code> cursor = Get cursor<br></code> |
| 23 |
<code> Select: cursor - 0.02, cursor + 0.02<br></code> |
| 24 |
<code> Extract selected sound (windowed): "slice", "Kaiser2", 2, "no"<br></code> |
| 25 |
<code> <b><a href="_endeditor_.html">endeditor</a></b><br></code> |
| 26 |
<code> To Spectrum: "yes"<br></code> |
| 27 |
<code> <a href="View___Edit.html">View & Edit</a><br></code> |
| 28 |
<p>If you choose <b>Run</b> from the <b>Run</b> menu in the ScriptEditor, a region of 40 milliseconds around the current cursor position in the SoundEditor will become selected. This piece will be copied to the list of objects, after applying a double Kaiser window (total length 80 ms). Thus, a Sound named “slice” will appear in the list. Subsequently, a Spectrum object also called “slice” will appear in the list, and a SpectrumEditor titled “Spectrum slice” will finally appear on your screen.</p> |
| 29 |
<dl> |
| 30 |
<dd style="position:relative;padding-left:1em;text-indent:-2em">5. Save the script to disk, e.g. as <code><font size=+1>/us/miep/spectrum.praat</font></code>. The title of the ScriptEditor will change accordingly.</dd> |
| 31 |
<dd style="position:relative;padding-left:1em;text-indent:-2em">6. Since you will want this script to be available in all future SoundEditors, you choose <b>Add to menu...</b> from the <b>File</b> menu. For the <i>Window</i>, you specify “SoundEditor” (this is preset). For the <i>Menu</i>, you may want to choose “Spectrum” instead of the preset value (“File”). For the name of the <i>Command</i>, you type something like “Show spectrum at cursor” (instead of “Do it...”). Then you click <b>OK</b>.</dd> |
| 32 |
</dl> |
| 33 |
<p>The command will be visible in every SoundEditor that you create from now on. To see this, close the one visible SoundEditor, select the original Sound, choose <b>View & Edit</b> again, and inspect the <b>Spectrogram</b> menu. You can now view the spectrum around the cursor just by choosing this menu command.</p> |
| 34 |
<p>After you leave Praat and start it again, the command will continue to appear in the SoundEditor. If you don't like the command any longer, you can remove it with the <a href="ButtonEditor.html">ButtonEditor</a>, which you can start by choosing <b>Buttons</b> from the <b>Settings</b> submenu of the <a href="Praat_menu.html">Praat menu</a>.</p> |
| 35 |
<h2>Improving your script</h2> |
| 36 |
<p>The above spectrum-viewing example has a number of disadvantages. It clutters the object list with a number of indiscriminable Sounds and Spectra called "slice", and the spectrum is shown up to the Nyquist frequency while we may just be interested in the lower 5000 Hz. Furthermore, the original selection in the SoundEditor is lost.</p> |
| 37 |
<p>To improve the script, we open it again with <b>Open editor script...</b> from the <b>File</b> menu in the SoundEditor. After every change, we can run it with <b>Run</b> from the <b>Run</b> menu again; alternatively, we could save it (with <b>Save</b> from the <b>File</b> menu) and choose our new <b>Show spectrum at cursor</b> button (this button will always run the version on disk, never the one viewed in a ScriptEditor).</p> |
| 38 |
<p>To zoom in on the first 5000 Hz, we add the following code at the end of our script:</p> |
| 39 |
<code> <b><a href="_editor_.html">editor</a></b>: "Spectrum slice"<br></code> |
| 40 |
<code> Zoom: 0, 5000<br></code> |
| 41 |
<p>To get rid of the “Sound slice”, we can add:</p> |
| 42 |
<code> <b><a href="_endeditor_.html">endeditor</a></b><br></code> |
| 43 |
<code> <a href="_removeObject_.html">removeObject</a>: "Sound slice"<br></code> |
| 44 |
<p>Note that <a href="_endeditor_.html"><b><code><font size=+1>endeditor</font></code></b></a> is needed to change from the environment of a SpectrumEditor to the environment of the object & picture windows.</p> |
| 45 |
<p>If you now choose the <b>Show spectrum at cursor</b> button for several cursor positions, you will notice that all those editors have the same name. To remedy the ambiguity of the line <code><font size=+1>editor Spectrum slice</font></code>, we give each slice a better name. For example, if the cursor was at 635 milliseconds, the slice could be named “635ms”. We can achieve this by changing the extraction in the following way:</p> |
| 46 |
<code> milliseconds = round (cursor*1000)<br></code> |
| 47 |
<code> Extract selection sound (windowed): string$ (milliseconds) + "ms", "Kaiser2", 2, "no"<br></code> |
| 48 |
<p>The names of the Sound and Spectrum objects will now have more chance of being unique. Two lines will have to be edited trivially.</p> |
| 49 |
<p>Finally, we will reset the selection to the original. At the top of the script, we add two lines to remember the positions of the selection markers:</p> |
| 50 |
<code> start = Get start of selection<br></code> |
| 51 |
<code> end = Get end of selection<br></code> |
| 52 |
<p>At the bottom, we reset the selection:</p> |
| 53 |
<code> <b><a href="_editor_.html">editor</a></b><br></code> |
| 54 |
<code> Select: start, end<br></code> |
| 55 |
<p>Note that the <b><code><font size=+1>editor</font></code></b> directive if not followed by the name of an editor, returns the script to the original environment.</p> |
| 56 |
<p>The complete script is:</p> |
| 57 |
<code> start = Get start of selection<br></code> |
| 58 |
<code> end = Get end of selection<br></code> |
| 59 |
<code> cursor = Get cursor<br></code> |
| 60 |
<code> Select: cursor - 0.02, cursor + 0.02<br></code> |
| 61 |
<code> # Create a name. E.g. "670ms" means at 670 milliseconds.<br></code> |
| 62 |
<code> milliseconds = round (cursor*1000)<br></code> |
| 63 |
<code> Extract windowed selection: string$ (milliseconds) + "ms", "Kaiser2", 2, "no"<br></code> |
| 64 |
<code> <b><a href="_endeditor_.html">endeditor</a></b><br></code> |
| 65 |
<code> To Spectrum: "yes"<br></code> |
| 66 |
<code> View & Edit<br></code> |
| 67 |
<code> <b><a href="_editor_.html">editor</a></b>: "Spectrum " + string$ (milliseconds) + "ms"<br></code> |
| 68 |
<code> Zoom: 0, 5000<br></code> |
| 69 |
<code> <b><a href="_endeditor_.html">endeditor</a></b><br></code> |
| 70 |
<code> <a href="_removeObject_.html">removeObject</a>: "Sound " + string$ (milliseconds) + "ms"<br></code> |
| 71 |
<code> <b><a href="_editor_.html">editor</a></b><br></code> |
| 72 |
<code> Select: start, end<br></code> |
| 73 |
<p>This script is useful as it stands. It is good enough for safe use. For instance, if the created Sound object has the same name as an already existing Sound object, it will be the newly created Sound object that will be removed by <a href="_removeObject_.html"><b><code><font size=+1>removeObject</font></code></b></a>, because in case of ambiguity <a href="_removeObject_.html"><b><code><font size=+1>removeObject</font></code></b></a> always removes the most recently created object of that name.</p> |
| 74 |
<h3>Links to this page</h3> |
| 75 |
<ul> |
| 76 |
<li><a href="Scripting.html">Scripting</a> |
| 77 |
<li><a href="Scripting_7__Scripting_the_editors.html">Scripting 7. Scripting the editors</a> |
| 78 |
</ul> |
| 79 |
<hr> |
| 80 |
<address> |
| 81 |
<p>© Paul Boersma 2023</p> |
| 82 |
</address> |
| 83 |
</body> |
| 84 |
</html> |