Debian Codegraph @

fon/manual_programming.cpp

raw symbols SCIP
1 /* manual_programming.cpp
2 *
3 * Copyright (C) 1992-2010,2011,2013,2015-2025 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "ManPagesM.h"
20
21 void manual_programming_init (ManPages me);
22 void manual_programming_init (ManPages me) {
23
24 MAN_PAGES_BEGIN
25 R"~~~(
26 ################################################################################
27 "Interoperability"
28 © Paul Boersma 2022-01-15
29
30 You can use Praat in workflows that involve other programs.
31
32 1. General ways to access Praat from other programs
33 ======================================
34
35 • To send messages from another program to a running Praat,
36 use @@sendpraat@ or @@Scripting 6.9. Calling from the command line|praat --send@.
37 This method is used by the programs CHAT (Childes) and Phon to view a sound in a Sound window.
38
39 • To execute a Praat script as a subprocess of another program, see @@Scripting 6.9. Calling from the command line@.
40
41 2. General ways to access other programs from Praat
42 ======================================
43
44 • To execute another program as a subprocess of Praat, see @@Scripting 6.5. Calling system commands@.
45
46 3. Files
47 ========
48
49 • Many programs read and/or write Praat TextGrid files. If you want create a TextGrid reading or writing procedure,
50 consult @@TextGrid file formats@.
51
52 ################################################################################
53 "TextGrid file formats"
54 © Paul Boersma 2018-08-21,2026
55
56 This page describes the syntax and semantics of TextGrid files that Praat can read and/or write.
57
58 1. The full text format of a minimal TextGrid
59 ======================================
60
61 If you record a Sound with a duration of 2.3 seconds, and then do ##To TextGrid...#,
62 you are asked to provide tier names and to say which of these tiers are point tiers.
63 If you click OK without changing the settings from their standard values,
64 you obtain a TextGrid with two interval tiers, called “Mary” and “John”, and one point tier called %bell.
65 When you save this TextGrid to disk by choosing @@Save as text file...@ from the #New menu,
66 the resulting text file, when opened in a text editor, will look as follows:
67 `
68 File type = "ooTextFile"
69 Object class = "TextGrid"
70
71 xmin = 0
72 xmax = 2.3
73 tiers? <exists>
74 size = 3
75 item []:
76 item [1]:
77 class = "IntervalTier"
78 name = "Mary"
79 xmin = 0
80 xmax = 2.3
81 intervals: size = 1
82 intervals [1]:
83 xmin = 0
84 xmax = 2.3
85 text = ""
86 item [2]:
87 class = "IntervalTier"
88 name = "John"
89 xmin = 0
90 xmax = 2.3
91 intervals: size = 1
92 intervals [1]:
93 xmin = 0
94 xmax = 2.3
95 text = ""
96 item [3]:
97 class = "TextTier"
98 name = "bell"
99 xmin = 0
100 xmax = 2.3
101 points: size = 0
102 `
103 This text file contains the following pieces of data, which Praat will use when reading this file from disk
104 and turning it into a TextGrid object again:
105
106 • The text "ooTextFile", which appears in the first line of any text file that you write with @@Save as text file...@,
107 i.e. not only in text files created from a TextGrid object, but also in text files created from e.g. a Pitch
108 or ExperimentMFC object or from any other of the hundreds of types of objects that Praat can have in its Objects list.
109
110 • The text "TextGrid", which designates the type of the object that has been saved to this file.
111
112 • The real number 0, which is the starting time (in seconds) of this TextGrid.
113
114 • The real number 2.3, which is the end time (in seconds) of the TextGrid.
115
116 • The flag <exists>, which tells us that this TextGrid contains tiers (this value would be <absent>
117 if the TextGrid contained no tiers, in which case the file would end here; however, you cannot really create TextGrid objects
118 without tiers in Praat, so this issue can be ignored).
119
120 • The integer number 3, which is the number of tiers that you created.
121
122 • The text "IntervalTier", which designates the type of the first tier (an interval tier, therefore).
123
124 • The text "Mary", which is the name you gave to the first tier.
125
126 • The real number 0, which is the starting time (in seconds) of the first tier.
127
128 • The real number 2.3, which is the end time (in seconds) of the first tier.
129 When you create a TextGrid with an interval tier, the time domain of the interval tier is automatically set equal
130 to the time domain of the whole TextGrid.
131
132 • The integer number 1, which is the number of intervals in the first tier.
133 Although you did not add any intervals and did not add any text to the first tier,
134 all interval tiers always contain at least one interval, which is created when you create the TextGrid.
135
136 • The real number 0, which is the starting time (in seconds) of the first (only) interval of the first tier.
137
138 • The real number 2.3, which is the end time (in seconds) of the first interval of the first tier.
139 The interval that is automatically created for you when you create a TextGrid with an interval tier,
140 spans the whole tier.
141
142 • The text "", which is the contents of the interval on the first tier. This text is empty.
143
144 • The text "IntervalTier", which gives the type of the second tier (again an interval tier).
145
146 • The text "John", which is the name you gave to the second tier.
147
148 • The real number 0, which is the starting time (in seconds) of the second tier.
149
150 • The real number 2.3, which is the end time (in seconds) of the second tier.
151
152 • The integer number 1, which is the number of intervals in the second tier.
153
154 • The real number 0, which is the starting time (in seconds) of the first (only) interval of the second tier.
155
156 • The real number 2.3, which is the end time (in seconds) of the first interval of the second tier.
157
158 • The text "", which is the contents of the interval on the second tier. This text is empty.
159
160 • The text "TextTier", which designates the type of the third tier (a point tier this time).
161
162 • The text "bell", which is the name you gave to the third tier.
163
164 • The real number 0, which is the starting time (in seconds) of the third tier.
165
166 • The real number 2.3, which is the end time (in seconds) of the third tier.
167
168 • The integer number 0, which is the number of points in the third tier.
169 A newly created point tier contains no points yet.
170
171 You will have noticed that the file contains a lot of stuff that was not mentioned in this list.
172 All of that stuff are %comments that are present only to help the human reader understand the contents
173 of the file: labels for all tiers (such as `item [2]`), labels for the starting times of the TextGrid or
174 a tier or an interval (`xmin`), labels for end times (`xmax`); labels for the number of tiers or
175 intervals or points (`size`), and little numbers enclosed in square brackets to tell the reader where they are (`[2]`).
176
177 When reading a text file containing a TextGrid (or any other object), Praat totally ignores these comments,
178 so if you e.g. replace `[2]` with `[4]` somewhere, Praat will not notice. Praat will consider as data only the following
179 types of information in the file:
180 • free-standing numbers, such as `0` and `2.3` above, but not `[1]` or `[3]`;
181 • free-standing text enclosed within double quotes, such as `"TextGrid"` and `""` above;
182 • free-standing flags, such as `<exists>` above (this is the only flag that appears in TextGrid files;
183 see @ExperimentMFC for a much broader use of flags).
184
185 In this list, "free-standing" means that the number, text or flag is preceded by the beginning of the file,
186 the beginning of a line, or a space, and that it is followed by the end of the file, the end of a line, or a space.
187
188 2. The shortest text format of a minimal TextGrid
189 ======================================
190
191 From the description above of what in the file is considered data (namely free-standing numbers, texts and flags)
192 and what is not (namely everything else), you can conclude that Praat will be able to read a much shorter version of
193 the file above. And indeed, when you choose @@Save as short text file...@ from the #New menu, your file will consist of
194 the following text,
195 with every piece of data alone on a separate line:
196 `
197 File type = "ooTextFile"
198 Object class = "TextGrid"
199
200 0
201 2.3
202 <exists>
203 3
204 "IntervalTier"
205 "Mary"
206 0
207 2.3
208 1
209 0
210 2.3
211 ""
212 "IntervalTier"
213 "John"
214 0
215 2.3
216 1
217 0
218 2.3
219 ""
220 "TextTier"
221 "bell"
222 0
223 2.3
224 0
225 `
226 That’s much less human-readable than before, but equally computer-readable, at least by Praat.
227 When you write your own TextGrid file recognizer, you should be able to interpret both versions shown above,
228 and also any version that is intermediate between the two, as long as it has the data written as freestanding numbers,
229 texts, and flags. For instance, a human-readable TextGrid file that can be interpreted by Praat could look
230 as follows:
231 `
232 "ooTextFile"
233 "TextGrid"
234 0 2.3 ! time domain of TextGrid
235 <exists>
236 3 tiers
237 "IntervalTier" "Mary" ! type and name of tier 1
238 0 2.3 ! time domain of tier 1
239 1 interval coming
240 0 2.3 "" ! interval 1 on tier 1
241 "IntervalTier" "John" ! type and name of tier 2
242 0 2.3 ! time domain of tier 2
243 1 interval coming
244 0 2.3 "" ! interval 1 on tier 2
245 "TextTier" "bell" ! type and name of tier 3
246 0 2.3 ! time domain of tier 3
247 0 points coming
248 `
249 Here we see that multiple pieces of data can be together on a line, as long as each of them stands free;
250 the only layout requirement is that the text `"ooTextFile"` has to be alone on the first line.
251
252 Another thing we see is that there is an additional type of comment: everything that follows an
253 exclamation mark on the same line is considered a comment. Thus, although some lines seem to end in a free-standing
254 number (1, 2, or 3), those numbers do not count as data, because they are part of a comment that follows
255 an exclamation mark.
256
257 3. Reading a TextGrid file
258 ==========================
259
260 Reading the contents of a TextGrid file into your own data structure is fairly easy.
261 For instance, to figure out how many intervals there are in a tier, you do not have to read multiple lines and then backtrack
262 to see where the list of intervals ends. Instead, the number of intervals is given before the intervals are enumerated.
263
264 4. Writing a TextGrid file
265 ==========================
266
267 Writing the contents of a TextGrid file involves deciding on a level of human readability.
268
269 5. A TextGrid file with more than the minimal content
270 ======================================
271
272 The above example was about a rather uninteresting TextGrid object, with no text in it. Suppose instead that
273 the sound was a recording of speaker saying a sentence, and your TextGrid annotates the sentence orthographically
274 as well as phonetically, and also annotates two chimes of the bell:
275 {- 6.0x3.0
276 textgrid = Create TextGrid: 0, 2.3, "sentence phonemes bell", "bell"
277 Set interval text: 1, 1, "říkej ""ahoj"" dvakrát"
278 Insert boundary: 2, 0.7
279 Insert boundary: 2, 1.6
280 Set interval text: 2, 1, "r̝iːkɛj"
281 Set interval text: 2, 2, "ʔaɦɔj"
282 Set interval text: 2, 3, "dʋakraːt"
283 Insert point: 3, 0.9, "ding"
284 Insert point: 3, 1.3, "dong"
285 Draw: 0.0, 0.0, 1, 1, 1
286 }
287 When you save this as a full text file, it will look as follows:
288 `
289 File type = "ooTextFile"
290 Object class = "TextGrid"
291
292 xmin = 0
293 xmax = 2.3
294 tiers? <exists>
295 size = 3
296 item []:
297 item [1]:
298 class = "IntervalTier"
299 name = "sentence"
300 xmin = 0
301 xmax = 2.3
302 intervals: size = 1
303 intervals [1]:
304 xmin = 0
305 xmax = 2.3
306 text = "říkej ""ahoj"" dvakrát"
307 item [2]:
308 class = "IntervalTier"
309 name = "phonemes"
310 xmin = 0
311 xmax = 2.3
312 intervals: size = 3
313 intervals [1]:
314 xmin = 0
315 xmax = 0.7
316 text = "r̝iːkɛj"
317 intervals [2]:
318 xmin = 0.7
319 xmax = 1.6
320 text = "ʔaɦɔj"
321 intervals [3]:
322 xmin = 1.6
323 xmax = 2.3
324 text = "dʋakraːt"
325 item [3]:
326 class = "TextTier"
327 name = "bell"
328 xmin = 0
329 xmax = 2.3
330 points: size = 2
331 points [1]:
332 number = 0.9
333 mark = "ding"
334 points [2]:
335 number = 1.3
336 mark = "dong"
337 `
338 We now see how points on a point tier are written: with their time (`number`) and their text (`mark`).
339 As usual, most of the contents of the file is comments; for instance, older TextGrid files may call
340 the time of the point $time instead of $number, but as these are just comments, Praat will ignore these words
341 when reading the file, and so should anybody who programs a TextGrid file parser.
342
343 A thing to note is the doubling of double quotes. The sentence in tier 1 contains double quotes around %ahoj,
344 as you can see in the picture above, and you typed only these two double quotes in the TextGrid window to start with.
345 However, the file format uses double quotes to mark texts, so if a text itself contains a double quote,
346 something special has to be done about it lest Praat think that you’re ending the text. The solution that Praat chooses
347 is that a double quote that appears in a text is written as a %doubled double quote in the text file, as above on tier 1.
348
349 6. Restrictions in a TextGrid object
350 ====================================
351
352 TextGrid objects maintain several invariants, some stronger and some weaker.
353
354 The two strongest invariants within an interval tier are %%positive duration% and %adjacency.
355 That is, the end time of each interval has to be greater than the starting time of that same interval,
356 and the starting time of each interval (except the first) has to be equal to the end time of the previous interval.
357 As a result, the union of the time domains of the set of intervals in a tier is a contiguous stretch of time,
358 and no intervals overlap. If a TextGrid file violates these invariants, Praat may refuse to read the file and give
359 an error message instead (or Praat may try to repair the TextGrid, but that is not guaranteed).
360
361 A weaker invariant is that the starting time of the first interval on a tier equals the starting time of that tier,
362 and the end time of the last interval equals the end time of the tier. When you create a TextGrid with Praat,
363 this invariant is automatically maintained, and most types of modifications also maintain it, except sometimes the commands
364 that combine multiple TextGrid objects with different durations into a new TextGrid.
365 Praat will happily read TextGrid files that do not honour this weak invariant, and will also display such a TextGrid
366 reasonably well in a TextGrid window. It is nevertheless advisable for other programs that create TextGrids
367 to honour this weak invariant.
368
369 For a point tier, a strong invariant is that the time of each point (except the first) has to be greater than the time
370 of the previous point. Praat maintains this invariant for instance by refusing to insert a point at the time of an existing
371 point. TextGrid files that violate this invariant may or may not be read by Praat, and may cause strange behaviour in Praat
372 if they are read.
373
374 A further weak invariant is that the starting and end times of each tier equal the starting and end times of the whole
375 TextGrid. This can be violated by combining multiple TextGrids into one, but other programs are advised to create TextGrids
376 that honour this invariant, because TextGrids that violate it may look strange to the user.
377
378 7. Text encoding
379 ================
380
381 Existing TextGrid text files come in various encodings. When creating a parser for TextGrid text files,
382 you should be prepared for reading it in UTF-8 encoding (without Byte Order Mark), or in UTF-16 encoding
383 (either Big-Endian or Little-Endian, with Byte Order Mark). Pre-Unicode TextGrid text files may have a Latin-1 encoding
384 if they were created on Windows or Linux, or a MacRoman encoding if they were created on a Mac,
385 so it would be good to prepare for such old files as well, although it may be difficult to figure out which is which
386 (line separators, as described below, may help).
387
388 When writing a TextGrid text file, you can use UTF-8 encoding (without Byte Order Mark), or UTF-16 encoding
389 (either Big-Endian or Little-Endian, with Byte Order Mark).
390 Please never write a limited encoding such as Latin-1 or MacRoman, which do not support international characters
391 or phonetic characters as in “`ʔaɦɔj`”.
392
393 The lines in the file are typically separated by a newline symbol (Linux or modern Mac),
394 or by a Return symbol (old Mac), or by a Return symbol followed by a newline symbol (Windows).
395 When reading a TextGrid text file, you should be prepared for each of these line separators.
396 When writing a TextGrid text file, you can use any of these line separators, because most text editors
397 on all platforms can meanwhile open and view all these versions correctly.
398
399 8. Interpreting trigraphs
400 =========================
401
402 The example above contains several phonetic symbols, and it is not always easy to type those into a text field.
403 For this reason, Praat provides %trigraphs for most phonetic characters, as well as for many non-ASCII characters
404 used in the languages of the world. For instance, the vowel “ɔ” (a “turned c”) can be typed as `\\ct` into
405 the TextGrid window as well as anywhere else in Praat where you want to draw graphical text
406 (see @@Special symbols@ for all trigraphs). Thus, the file above could have looked as follows:
407 `
408 File type = "ooTextFile"
409 Object class = "TextGrid"
410
411 xmin = 0
412 xmax = 2.3
413 tiers? <exists>
414 size = 3
415 item []:
416 item [1]:
417 class = "IntervalTier"
418 name = "sentence"
419 xmin = 0
420 xmax = 2.3
421 intervals: size = 1
422 intervals [1]:
423 xmin = 0
424 xmax = 2.3
425 text = "\r<\i'kej ""ahoj"" dvakr\a't"
426 item [2]:
427 class = "IntervalTier"
428 name = "phonemes"
429 xmin = 0
430 xmax = 2.3
431 intervals: size = 3
432 intervals [1]:
433 xmin = 0
434 xmax = 0.7
435 text = "r\T^i\:fk\efj"
436 intervals [2]:
437 xmin = 0.7
438 xmax = 1.6
439 text = "\?ga\h^\ctj"
440 intervals [3]:
441 xmin = 1.6
442 xmax = 2.3
443 text = "d\vsakra\:ft"
444 item [3]:
445 class = "TextTier"
446 name = "bell"
447 xmin = 0
448 xmax = 2.3
449 points: size = 2
450 points [1]:
451 number = 0.9
452 mark = "ding"
453 points [2]:
454 number = 1.3
455 mark = "dong"
456 `
457 On the screen, this TextGrid looks the same as before:
458 {- 6.0x3.0
459 textgrid = Create TextGrid: 0, 2.3, "sentence phonemes bell", "bell"
460 Set interval text: 1, 1, "\r<\i'kej ""ahoj"" dvakr\a't"
461 Insert boundary: 2, 0.7
462 Insert boundary: 2, 1.6
463 Set interval text: 2, 1, "r\T^i\:fk\efj"
464 Set interval text: 2, 2, "\?ga\h^\ctj"
465 Set interval text: 2, 3, "d\vsakra\:ft"
466 Insert point: 3, 0.9, "ding"
467 Insert point: 3, 1.3, "dong"
468 Draw: 0.0, 0.0, 1, 1, 1
469 }
470 There are several hundreds of such trigraphs, and a few more might be added in the future.
471 When you create a TextGrid file parser and want to interpret the trigraphs in the correct way,
472 it is advisable to have Praat do the conversion for you: read the TextGrid file into Praat,
473 call the menu command ##Convert to Unicode#, and save the TextGrid as a new text file.
474
475 9. The binary TextGrid file format
476 ==================================
477
478 Besides the TextGrid text file format described above, TextGrid objects can also be saved as binary files
479 with @@Save as binary file...@. If you need a description of this format, we can add it here on request.
480 When you want to create only a TextGrid %text file parser, and still want to support binary TextGrid files,
481 you can have Praat do the conversion for you: read the binary TextGrid file into Praat with @@Read from file...@
482 and save the resulting TextGrid object as a text file with @@Save as text file...@.
483
484 ################################################################################
485 "Programming with Praat"
486 © Paul Boersma 2023-04-09, 2024, 2025
487
488 You can extend the functionality of the Praat program
489 by adding modules written in C or C++ to it. All of Praat's source code
490 is available under the General Public Licence.
491
492 1. Warning
493 ==========
494
495 Before trying the task of learning how to write Praat extensions in C or C++,
496 you should be well aware of the possibilities of @scripting.
497 Many built-in commands in Praat have started their
498 lives as Praat scripts, and scripts are easier to write than extensions in C or C++.
499 If you have a set of scripts, you can distribute them as a @@plug-ins|plug-in@.
500
501 2. Getting the existing source code
502 ===================================
503
504 You obtain the Praat source code from GitHub (https://github.com/praat), in a file with a name like
505 `praat6499_sources.zip` or `praat6499_sources.tar.gz` (depending on the Praat version), and unpack this by double-clicking.
506 The result will be a set of directories
507 called `kar`, `melder`, `external` (with `clapack`, `gsl`, `glpk`, `flac`, `mp3`, `portaudio`, `espeak`, `vorbis` and `opusfile` in it),
508 `sys`, `dwsys`, `stat`, `fon`, `sensors`, `dwtools`, `LPC`, `FFNet`, `gram`, `artsynth`, `EEG`, `main`, `makefiles`, `test`, `dwtest`, and `generate`,
509 plus a makefile and Xcode project for macOS and a README.md file.
510
511 3. Building Praat
512 =================
513
514 Consult the README file on GitHub for directions to compile and link Praat for your platform.
515
516 4. Extending Praat
517 ==================
518
519 To start extending Praat’s functionality, you can edit `main/main_Praat.cpp`.
520 This example shows you how to create a very simple program with all the functionality
521 of the Praat program, and a single bit more (namely an additional command in the New menu):
522 `
523 #include "praatM.h" // for macros such as DIRECT and INCLUDE_LIBRARY
524
525 DIRECT (INFO_HelloFromJane) { // a macro to include commands without parameters
526 Melder_information (U"Hello, I am Jane.");
527 }
528
529 int main (int argc, char **argv) {
530 praat_init (
531 U"Praat_Jane", // the name of your app
532 U"1.2.01", // the version of your app, as a string
533 1201, // the version of your app, as a number
534 2025, // the year of your app's version
535 9, // the month of your app's version (here: September)
536 18, // the day of your app's version (between 1 and 31)
537 U"jane.doe", // the first part of your contact email address
538 U"univ.edu", // the second part of your contact email address
539 argc, // the number of command line arguments (0 if started from GUI)
540 argv // the command line arguments (empty if started from GUI)
541 );
542 INCLUDE_LIBRARY (praat_uvafon_init) // include all Praat functionality
543 praat_addMenuCommand (
544 U"Objects", // the window in which this command will appear
545 U"New", // the menu under which this command will appear
546 U"Hello from Jane...", // the title of the menu command
547 nullptr, // to insert after another command (unused here)
548 0, // the depth at which this command will appear (0 for top level)
549 INFO_HelloFromJane // the command defined above
550 );
551 praat_run ();
552 return 0; // obligatory
553 }
554 `
555 5. Learning how to program
556 ==========================
557
558 To see how objects are defined, take a look at `sys/Thing.h`, `sys/Daata.h`,
559 `sys/oo.h`, the `XXX_def.h` files in the `fon` folder, and the corresponding
560 `XXX.cpp` files in the `fon` folder. To see how commands show up on the buttons
561 in the fixed and dynamic menus, take a look at the large interface description file
562 `fon/praat_Fon.cpp`.
563
564 6. Using the Praat shell only
565 =============================
566
567 For building the Praat shell (the Objects and Picture windows) only, you need only the code in the nine directories
568 `kar`, `melder`, `external/{clapack,gsl,flac,mp3,portaudio,lame,opusfile,vorbis}`, `sys`, and `dwsys`. You delete the inclusion of `praat_uvafon_init` from `main`.
569 You will be able to build a Praat shell, i.e. an Objects and a Picture window,
570 which has no knowledge of the world, i.e., which does not know any objects
571 that can be included in the list of objects. You could use this Praat shell
572 for modelling your own world and defining your own classes of objects. For advanced programmers only.
573
574 )~~~"
575 MAN_PAGES_END
576
577 }
578
579 /* End of file manual_programming.cpp */