Removed interference between SaveAudio and Read performance
[sgc2.git] / sgc2.praat
blob98da9e63ff2c6bb569108a56d2f9ad9bebd6ef6c
2 # SpeakGoodChinese 2.0
3
4 # Master Praat script
6 #     SpeakGoodChinese: sgc2.praat is the master GUI of SpeakGoodChinese
7 #     It is written in Praat script for the Demo window 
8 #     
9 #     Copyright (C) 2007-2010  R.J.J.H. van Son and 2010 the Netherlands Cancer Institute
10 #     The SpeakGoodChinese team are:
11 #     Guangqin Chen, Zhonyan Chen, Stefan de Koning, Eveline van Hagen, 
12 #     Rob van Son, Dennis Vierkant, David Weenink
13
14 #     This program is free software; you can redistribute it and/or modify
15 #     it under the terms of the GNU General Public License as published by
16 #     the Free Software Foundation; either version 2 of the License, or
17 #     (at your option) any later version.
18
19 #     This program is distributed in the hope that it will be useful,
20 #     but WITHOUT ANY WARRANTY; without even the implied warranty of
21 #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 #     GNU General Public License for more details.
23
24 #     You should have received a copy of the GNU General Public License
25 #     along with this program; if not, write to the Free Software
26 #     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
27
28 # The real application name
29 sgc2.demoAppName$ = "SpeakGoodChinese2"
31 # Define variable that might be reset in Initialise*.praat
32 if not variableExists("build_SHA$")
33         build_SHA$ = "-"
34 endif
36 # These are simply "useful" defaults
37 localTableDir$ = "Data"
38 buttonsTableName$ = "MainPage"
39 configTableName$ = "Config"
40 buttons$ = ""
41 config$ = ""
42 recordedSound$ = ""
43 te.recordedSound = 0
44 te.recordedPitch = 0
45 te.buttonPressValue = 0
46 samplingFrequency = 44100
47 recordingTime = 2
49 # Pop-Up window colors
50 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
51 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
53 # If running in a packed script binary
54 if index_regex(preferencesDirectory$, "(?i'sgc2.demoAppName$')$")
55         preferencesAppDir$ = preferencesDirectory$
56 elsif index_regex(preferencesDirectory$, "[pP]raat(\-dir| Prefs)?$")
57         # If running as a Praat script, create a new preferences directory
58         if unix
59                 preferencesAppDir$ = "'preferencesDirectory$'/../.'sgc2.demoAppName$'"
60         else
61                 preferencesAppDir$ = "'preferencesDirectory$'/../'sgc2.demoAppName$'"
62         endif
63 else
64         # It has to go somewhere. Make a subdirectory in the current preferences directory
65         preferencesAppDir$ = "'preferencesDirectory$'/'sgc2.demoAppName$'"
66 endif
69 # Parameters for isolating recorded speech from noise
70 # Should be mostly left alone unless you are using ultra clean
71 # or very noisy recordings
72 noiseThresshold = -30
73 minimumPitch = 60
74 soundMargin = 0.25
75 minimumIntensity = 30
77 # Set up button height
78 buttonbevel = 0
80 # Define canvas
81 viewportMargin = 5
82 defaultFontSize = 12
83 defaultFont$ = "Helvetica"
84 defaultLineWidth = 1
86 # Set up system
87 call reset_viewport
89 # Load supporting scripts
90 # Load tables in script format
91 include CreateTables.praat
92 include CreateWordlists.praat
93 # Set up system and load preferences
94 include InitialiseSGC2.praat
95 # Include the main page buttons and procedures
96 include MainPage.praat
97 # Include the configuration page buttons and procedures
98 include Config.praat
100 # Start instruction loop
101 while demoWaitForInput()
102         .label$ = ""
103         .clickX = -1
104         .clickY = -1
105         .pressed$ = ""
106         if demoClicked()
107                 .clickX = demoX()
108                 .clickY = demoY()
109                 call buttonClicked 'buttons$' '.clickX' '.clickY'
110                 .label$ = buttonClicked.label$
111         elsif demoKeyPressed()
112                 .pressed$ = demoKey$()
113                 call keyPressed 'buttons$' '.pressed$'
114                 .label$ = keyPressed.label$
115         endif
117         # You cannot select a text field
118         if startsWith(.label$, "$")
119                 .label$ = ""
120         endif
121         
122         # Do things
123         if .label$ != ""
124                 te.buttonPressValue = 0
125                 # Push button down
126                 call Draw_button 'buttons$' '.label$' 1
127                 call process_label '.label$' '.clickX' '.clickY' '.pressed$'
128                 # push button up
129                 call Draw_button 'buttons$' '.label$' 'te.buttonPressValue'
130         endif
131 endwhile
133 call end_program
136 ########################################################
138 # Definitions of procedures
140 ########################################################
142 # Do what is asked
143 procedure process_label .label$ .clickX .clickY .pressed$
144         # Prcoess the command
145         if .label$ <> "" and not startsWith(.label$,"!")
146                 .label$ = replace$(.label$, "_", " ", 0)
147                 call process'buttons$''.label$' '.clickX' '.clickY' '.pressed$'
148         endif
149 endproc
151 # Intialize buttons
152 procedure init_buttons
153         noerase = 1
154         call Draw_all_buttons 'buttons$'
155         noerase = 0
156 endproc
158 # Draw all buttons
159 noerase = 0
160 procedure Draw_all_buttons .table$
161         .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
162         select Table '.table$'
163         .numRows = Get number of rows
164         
165         for .row to .numRows
166                 .label$ = Get value... '.row' Label
167         if not startsWith(.label$, "!")
168                         .pressed = 0
169                         # Determine the "pressed" state of a button
170                         # A variable with the same name as the button will act as a
171                         # "pressed state"
172                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
173                         # Simple boolean variables
174                         if index(.variableName$, "_") <= 0 and variableExists(.variableName$)
175                                 # True: Pressed
176                                 if '.variableName$' > 0
177                                         .pressed = 2
178                                 # <0: Grayed out
179                                 elsif '.variableName$' < 0
180                                         .pressed = -1
181                                 endif
182                         # Complex buttons with an variableName+'_'+value structure
183                         # varableName$ -> name of button, e.g., "language"
184                         elsif index(.variableName$, "_")
185                                 .generalVar$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
186                                 .currentVariableName$ = .generalVar$
187                                 # Is it a string?
188                                 if variableExists(.generalVar$+"$")
189                                         .currentVariableName$ = .generalVar$ + "$"
190                                 endif
191                                 # Remove one level of indirection
192                                 if variableExists(.currentVariableName$)
193                                         if index(.currentVariableName$, "$")
194                                                 .currentVariableName$ = '.currentVariableName$'
195                                         else
196                                                 .currentValue = '.currentVariableName$'
197                                                 .currentVariableName$ = "'.currentValue'"
198                                         endif
199                                         # Remove next level of indirection
200                                         .currentContent$ = "'.currentVariableName$'"
201                                         if .currentContent$ = "_DISABLED_"
202                                                 .pressed = -1
203                                         endif
204                                         # Reconstruct label from current values
205                                         .currentLabelValue$ = .generalVar$ + "_" + .currentContent$
206                                         # Set PRESSED from label
207                                         if .variableName$ = .currentLabelValue$
208                                                 .pressed = 2
209                                         endif
210                                 endif
211                         endif
212                         # You did erase everything before you started here? So do not do that again
213                     call Draw_button_internal 0 '.table$' '.label$' '.pressed'
214         endif
215         endfor
216 endproc
218 # Draw a button from a predefined button table
219 # Normally, erase the area around a button
220 procedure Draw_button .table$ .label$ .push
221         call Draw_button_internal 1 '.table$' '.label$' '.push'
222 endproc
224 # Use this function if you want to control erasing
225 procedure Draw_button_internal .erase_button_area .table$ .label$ .push
226         # Scale rounding of rounded rectangles
227         .wc = 1
228         .mm = demo Horizontal wc to mm... '.wc' 
229     # Allow to overide ! skip directive
230     .forceDraw = 0
231     if startsWith(.label$, "+")
232         .label$ = right$(.label$, length(.label$)-1)
233         .forceDraw = 1
234     endif
236     select Table '.table$'
237     .row = Search column... Label '.label$'
238         if .row < 1
239                 call emergency_table_exit Button Table '.table$' does not have a row with label '.label$'
240         endif
241         
242         # Perspective shift sizes
243         .shiftDown = 0
244         .shiftX = 0
245         .shiftY = 0
246         if buttonbevel <> 0
247                 .shiftDown = 0.1*buttonbevel
248         .shiftX = -0.2*buttonbevel
249         .shiftY = 0.40*buttonbevel
250         endif
251         
252         # Set drawing parameters
253         .topBackGroundColorUp$ = "{0.93,0.93,0.93}"
254         .topLineColorUp$ = "Black"
255         .topLineWidthUp = 1.5
256         .topBackGroundColorDown$ = "{0.89,0.89,0.94}"
257         .topLineColorDown$ = "{0.2,0.2,0.2}"
258         .topLineWidthDown = 2.0
259         .topBackGroundColorDisabled$ = "{0.85,0.85,0.85}"
260         .topLineColorDisabled$ = "{0.70,0.70,0.70}"
261         .topLineWidthDisabled = 1.5
262         .flankBackGroundColorUp$ = "{0.6,0.6,0.6}"
263         .flankLineColorUp$ = "{0.2,0.2,0.2}"
264         .flankLineWidthUp = 1.5
265         .flankBackGroundColorDown$ = "{0.75,0.75,0.75}"
266         .flankLineColorDown$ = .flankLineColorUp$
267         .flankLineWidthDown = 1.5
268         .buttonFontSize = defaultFontSize
269         
270         # Get button values
271     .leftX = Get value... '.row' LeftX
272     .rightX = Get value... '.row' RightX
273     .lowY = Get value... '.row' LowY
274     .highY = Get value... '.row' HighY
275     .buttonText$ = Get value... '.row' Text
276     .buttonColor$ = Get value... '.row' Color
277     .buttonDraw$ = Get value... '.row' Draw
278     .buttonKey$ = Get value... '.row' Key
279     
280     .noDraw = startsWith(.label$, "!") or (.leftX < 0) or (.rightX < 0) or (.lowY < 0) or (.highY < 0)
282         .rotation = 0
283         if index_regex(.buttonText$, "^![0-9\.]+!")
284                 .rotation = extractNumber(.buttonText$, "!")
285                 .buttonText$ = replace_regex$(.buttonText$, "^![0-9\.]+!", "", 0)
286         endif
288     goto NOBUTTON .noDraw and not .forceDraw
290     # Replace button text with ALERT
291     if .push = 3
292         .buttonText$ = alertText$
293     endif
294         
295         # Adapt font size to button size
296         .maxWidth = (.rightX - .leftX) - 2
297         .maxHeight = (.highY - .lowY) - 1
298         if .rotation = 0
299                 # Adapt size of button to length of text if necessary
300                 call adjustFontSizeOnWidth 'defaultFont$' '.buttonFontSize' '.maxWidth' '.buttonText$'
301                 .buttonFontSize = adjustFontSizeOnWidth.newFontSize
302                 if adjustFontSizeOnWidth.diff > 0
303                         .rightX += adjustFontSizeOnWidth.diff/2
304                         .leftX -= adjustFontSizeOnWidth.diff/2
305                 endif
306                 call set_font_size '.buttonFontSize'
308                 # Adapt size of button to length of text
309                 call adjustFontSizeOnHeight 'defaultFont$' '.buttonFontSize' '.maxHeight'
310                 if adjustFontSizeOnHeight.diff > 0
311                         .lowY -= adjustFontSizeOnHeight.diff/2
312                         .highY += adjustFontSizeOnHeight.diff/2
313                 endif
314                 .buttonFontSize = adjustFontSizeOnHeight.newFontSize
315         else
316                 # With non-horizontal text, only change font size
317                 call adjustRotatedFontSizeOnBox 'defaultFont$' '.buttonFontSize' '.maxWidth' '.maxHeight' '.rotation' '.buttonText$'
318                 .buttonFontSize = adjustRotatedFontSizeOnBox.newFontSize
319         endif
320         
321         # Reset and erase button area
322         call reset_viewport
323     demo Line width... 'defaultLineWidth'
324     .shiftLeftX = .leftX
325     .shiftRightX = .rightX - .shiftX
326     .shiftLowY = .lowY - .shiftY
327     .shiftHighY = .highY
328         if .erase_button_area
329                 # Make erase area minutely larger
330                 .eraseLeft = .shiftLeftX - 0.01
331                 .eraseRight = .shiftRightX + 0.01
332                 .eraseBottom = .shiftLowY - 0.01
333                 .eraseTop = .shiftHighY + 0.01
334                 demo Paint rectangle... White .eraseLeft .eraseRight .eraseBottom .eraseTop
335         endif
336         
337     # If label starts with "$", it is a text field. Then do not draw the button
338         if not startsWith(.label$, "$")
339         # Give some depth to button: Draw flank outline
340                 if .shiftDown or .shiftX or .shiftY
341                         if .push <= 0
342                         demo Paint rounded rectangle... '.flankBackGroundColorUp$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
343                                 demo Colour... '.flankLineColorUp$'
344                         demo Line width... '.flankLineWidthUp'
345                         else
346                         demo Paint rounded rectangle... '.flankBackGroundColorDown$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
347                                 demo Colour... '.flankLineColorDown$'
348                         demo Line width... '.flankLineWidthDown'
349                         endif
350                 demo Draw rounded rectangle... .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
351                 endif
353                 # Button Down will shift the top perspective
355         # Draw the button top
356                 if .push = 0
357                 demo Paint rounded rectangle... '.topBackGroundColorUp$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
358                         demo Colour... '.topLineColorUp$'
359                 demo Line width... '.topLineWidthUp'
360                 elsif .push < 0
361                 demo Paint rounded rectangle... '.topBackGroundColorDisabled$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
362                         demo Colour... '.topLineColorDisabled$'
363                 demo Line width... '.topLineWidthDisabled'
364                 else
365                         # Button Down
366                         .leftX += .shiftDown
367                         .rightX += .shiftDown
368                         .lowY -= .shiftDown
369                         .highY -= .shiftDown
371                 demo Paint rounded rectangle... '.topBackGroundColorDown$' .leftX .rightX .lowY .highY '.mm'
372                         demo Colour... '.topLineColorDown$'
373                 demo Line width... '.topLineWidthDown'
374                 endif
375         demo Draw rounded rectangle... '.leftX' '.rightX' '.lowY' '.highY' '.mm'
376         endif
377    
378     # The button text and symbol
379         .horWC = demo Horizontal mm to wc... 10.0
380         .verWC = demo Vertical mm to wc... 10.0
381         if .verWC > 0
382                 .verCoeff = .horWC / .verWC
383         else
384                 .verCoeff = 1
385         endif
387     .centerX = (.leftX + .rightX)/2
388     .centerY = .lowY + 0.6*(.highY-.lowY)
389     .radius = min(.verCoeff * (.highY - .lowY ), (.rightX - .leftX))/3
390         .buttonKey$ = replace$(.buttonKey$, "\", "\\", 0)
391         .buttonKey$ = replace$(.buttonKey$, """", "\""""", 0)
392         .newText$ = replace_regex$(.buttonText$, "['.buttonKey$']", "#%&", 1)
393         if .newText$ = ""
394                 .newText$ = .buttonText$
395         endif
396         if .push = 1 or .push = -1
397                 demo Grey
398                 if .buttonColor$ = "Red"
399                         .buttonColor$ = "Pink"
400                 elsif .buttonColor$ = "Blue"
401                         .buttonColor$ = "{0.5,0.5,1}"
402                 else
403                         .buttonColor$ = "Grey"
404                 endif
405     elsif .push >= 2
406         .buttonColor$ = "Maroon"
407         else
408         demo Colour... Black
409         endif
411     call '.buttonDraw$' '.buttonColor$' '.centerX' '.centerY' '.radius' 
412         call set_font_size '.buttonFontSize'
413     demo Colour... '.buttonColor$'
414         if .rotation = 0
415                 .anchorY = .lowY
416                 .verticalAlignment$ = "Bottom"
417         else
418                 .anchorY = .lowY + 0.5*(.highY-.lowY)
419                 .verticalAlignment$ = "Half"
420         endif
421     demo Text special... '.centerX' Centre '.anchorY' '.verticalAlignment$' 'defaultFont$' '.buttonFontSize' '.rotation' '.newText$'
422         demoShow()
424         # Reset
425         call set_font_size 'defaultFontSize'
426     demo Black
427     demo Line width... 'defaultLineWidth'
428     
429     label NOBUTTON
430 endproc
432 procedure set_window_title .table$ .addedText$
433     select Table '.table$'
434     .row = Search column... Label !WindowTitle
435         if .row < 1
436                 call emergency_table_exit Button Table '.table$' does not have a row with label !WindowTitle
437         endif
438         .windowText$ = Get value... '.row' Text
439         call convert_praat_to_latin1 '.windowText$'
440         .windowText$ = convert_praat_to_latin1.text$
442     demoWindowTitle(.windowText$+ .addedText$)
443 endproc
445 # Handle language setting 
446 procedure processLanguageCodes .table$ .label$
447         .table$ = "Config"
448     call Draw_button 'config$' Language_'config.language$' 0
449     call Draw_button 'config$' '.label$' 2
450     # Someone might have to use more than 2 chars for the language code
451     .numChars = length(.label$) - length("Language_")
452         .lang$ = right$(.label$, .numChars)
453     # Load new tables
454     call set_language '.lang$'
455 endproc
457 # Set the language
458 procedure set_language .lang$
459         .redraw_config = 0
460     # Remove old tables
461     if buttons$ <> ""
462         select Table 'buttons$'
463         Remove
464                 .redraw_config = 1
465     endif
466     if config$ <> ""
467         select Table 'config$'
468         Remove
469                 .redraw_config = 1
470     endif
471     
472     # Set language
473         call checkTable 'buttonsTableName$'_'.lang$'
474         if checkTable.available
475                 config.language$ = .lang$
476         else
477                 config.language$ = "EN"
478         endif
479     
480     # Load buttons tables
481     call loadTable 'buttonsTableName$'
482     buttons$ = selected$("Table")
483     Append column... Text
484     Append column... Key
485     Append column... Helptext
486     .numLabels = Get number of rows
487     call loadTable 'buttonsTableName$'_'config.language$'
488     .buttonsLang$ = selected$("Table")
489     for .row to .numLabels
490                 select Table 'buttons$'
491                 .label$ = Get value... '.row' Label
492         call findLabel '.buttonsLang$' '.label$'
493             if findLabel.row > 0
494             select Table '.buttonsLang$'
495                 .valueText$ = Get value... 'findLabel.row' Text
496                 .valueKey$ = Get value... 'findLabel.row' Key
497                 .valueHelp$ = Get value... 'findLabel.row' Helptext
498                 select Table 'buttons$'
499                 Set string value... '.row' Text '.valueText$'
500                 Set string value... '.row' Key '.valueKey$'
501                 Set string value... '.row' Helptext '.valueHelp$'
502                 elsif index(.label$, "_")
503                         # Load alternative language table
504                         .startChar = rindex(.label$, "_")
505                         .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
506                         call loadTable 'buttonsTableName$'_'.otherLanguage$'
507                 .otherbuttonsLang$ = selected$("Table")
508                 call findLabel '.otherbuttonsLang$' '.label$'
509                 if findLabel.row > 0
510                 select Table '.buttonsLang$'
511                         .valueText$ = Get value... 'findLabel.row' Text
512                         .valueKey$ = Get value... 'findLabel.row' Key
513                         .valueHelp$ = Get value... 'findLabel.row' Helptext
514                         select Table 'buttons$'
515                         Set string value... '.row' Text '.valueText$'
516                         Set string value... '.row' Key '.valueKey$'
517                         Set string value... '.row' Helptext '.valueHelp$'
518                 else
519                 call emergency_table_exit Cannot find Label: '.otherbuttonsLang$' '.label$'
520                 endif
521                         select Table '.otherbuttonsLang$'
522                         Remove
523         else
524             call emergency_table_exit Cannot find Label: '.buttonsLang$' '.label$'
525         endif
526     endfor
527     select Table '.buttonsLang$'
528     Remove
529     
530     # Load configuration table
531     call loadTable 'configTableName$'
532     config$ = selected$("Table")
533     Append column... Text
534     Append column... Key
535     Append column... Helptext
536     .numLabels = Get number of rows
537     call loadTable 'configTableName$'_'config.language$'
538     .configLang$ = selected$("Table")
539     for .row to .numLabels
540                 select Table 'config$'
541                 .label$ = Get value... '.row' Label
542         call findLabel '.configLang$' '.label$'
543             if findLabel.row > 0
544             select Table '.configLang$'
545                 .valueText$ = Get value... 'findLabel.row' Text
546                 .valueKey$ = Get value... 'findLabel.row' Key
547                 .valueHelp$ = Get value... 'findLabel.row' Helptext
548                 select Table 'config$'
549                 Set string value... '.row' Text '.valueText$'
550                 Set string value... '.row' Key '.valueKey$'
551                 Set string value... '.row' Helptext '.valueHelp$'
552                 elsif index(.label$, "_")
553                         .startChar = rindex(.label$, "_")
554                         .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
555                         call loadTable 'configTableName$'_'.otherLanguage$'
556                 .otherconfigLang$ = selected$("Table")
557                 call findLabel '.otherconfigLang$' '.label$'
558                 if findLabel.row > 0
559                 select Table '.otherconfigLang$'
560                         .valueText$ = Get value... 'findLabel.row' Text
561                         .valueKey$ = Get value... 'findLabel.row' Key
562                         .valueHelp$ = Get value... 'findLabel.row' Helptext
563                         select Table 'config$'
564                         Set string value... '.row' Text '.valueText$'
565                         Set string value... '.row' Key '.valueKey$'
566                         Set string value... '.row' Helptext '.valueHelp$'
567                 else
568                 call emergency_table_exit Cannot find Label: '.otherconfigLang$' '.label$'
569                 endif
570                         select Table '.otherconfigLang$'
571                         Remove
572         else
573             call emergency_table_exit Cannot find Label: '.configLang$' '.label$'
574         endif
575     endfor
576     select Table '.configLang$'
577     Remove
579         # Make language change visible
580         if .redraw_config
581                 call Draw_config_page
582         endif
584 endproc
586 ###############################################################
588 # Button Drawing Routines
590 ###############################################################
592 # A stub for buttons that do not have a drawing routine (yet)
593 procedure DrawNull .color$ .x .y .size
594 endproc
596 procedure DrawHelp .color$ .x .y .size
597         .currentFontSize = 24
598         .y -= .size
599         .maxHeight = 2*.size
600         call adjustFontSizeOnHeight 'defaultFont$' '.currentFontSize' '.maxHeight'
601         .currentFontSize = adjustFontSizeOnHeight.currentFontSize
602         call set_font_size '.currentFontSize'
603         demo Colour... '.color$'
604         demo Text... '.x' Centre '.y' Bottom ?
605         call set_font_size 'defaultFontSize'
606 endproc
608 ###############################################################
610 # Button Processing Routines
612 ###############################################################
614 # Search row in table on label
615 procedure findKey .table$ .label$
616         .row = 0
617         select Table '.table$'
618         .to$ = selected$("Table")
619         .to$ = "Table_"+.to$
620         .numRows = Get number of rows
621         for .i to .numRows
622                 .currentKey$ = '.to$'$[.i, "Key"]
623                 if .label$ = .currentKey$
624                         .row = .i
625                         goto KEYFOUND
626                 endif
627         endfor
628         label KEYFOUND
629         if .row <= 0 and index(.label$, "_") <= 0
630                 printline "'.label$'" is not a key in '.table$'
631         endif
632 endproc
634 procedure findLabel .table$ .label$
635         .row = 0
636         select Table '.table$'
637         .to$ = selected$("Table")
638         .to$ = "Table_"+.to$
639         .numRows = Get number of rows
640         for .i to .numRows
641                 .currentKey$ = '.to$'$[.i, "Label"]
642                 if .label$ = .currentKey$
643                         .row = .i
644                         goto LABELFOUND
645                 endif
646         endfor
647         label LABELFOUND
648         if .row <= 0 and index(.label$, "_") <= 0
649                 call emergency_table_exit "'.label$'" is not a key in '.table$'
650         endif
651 endproc
653 # Get the label
654 procedure buttonClicked table$ .x .y
655         .label$ = ""
656         select Table 'table$'
657         .bo$ = selected$("Table")
658         .bo$ = "Table_"+.bo$
659         .numRows = Get number of rows
660         for .i to .numRows
661                 if .label$ = ""
662                         .leftX = '.bo$'[.i, "LeftX"]
663                         .rightX = '.bo$'[.i, "RightX"]
664                         .lowY = '.bo$'[.i, "LowY"]
665                         .highY = '.bo$'[.i, "HighY"]
666                         if .x > .leftX and .x < .rightX and .y > .lowY and .y < .highY
667                                 .label$ = '.bo$'$[.i, "Label"]
668                         endif
669                 endif
670         endfor
671 endproc
673 procedure keyPressed table$ .pressed$
674         .label$ = ""
675         # Magic
676         if .pressed$ = "" and not demoShiftKeyPressed()
677                 .label$ = "Refresh"
678         endif
679         .lowerPressed$ = replace_regex$(.pressed$, ".", "\L&", 0)
680         .upperPressed$ = replace_regex$(.pressed$, ".", "\U&", 0)
681         select Table 'table$'
682         .bo$ = selected$("Table")
683         .bo$ = "Table_"+.bo$
684         .numRows = Get number of rows
685         for .i to .numRows
686                 if .label$ = ""
687                         .key$ = '.bo$'$[.i, "Key"]
688                         if index(.key$, .lowerPressed$) or index(.key$, .upperPressed$)
689                                 .label$ = '.bo$'$[.i, "Label"]
690                         endif
691                 endif
692         endfor
693 endproc
695 procedure count_syllables
696         .number = 0
697         .pinyin$ = ""
698         select Table 'wordlist$'
699         if te.currentWord > 0 and te.currentWord <= te.numberOfWords
700                 .sound$ = Get value... 'te.currentWord' Sound
701                 .pinyin$ = Get value... 'te.currentWord' Pinyin
702         endif
703         call add_missing_neutral_tones '.pinyin$'
704         .pinyin$ = add_missing_neutral_tones.pinyin$
705         if index_regex(.pinyin$, "[0-9]") > 0
706                 .number = length(replace_regex$(.pinyin$, "[^\d]+([\d]+)", "1", 0))
707         elsif .pinyin$ <> ""
708                 .number = 1
709         endif
710 endproc
712 procedure play_sound .sound$
713     if .sound$ <> ""
714         select Sound '.sound$'
715         Play
716     endif
717 endproc
719 procedure record_sound .recordingTime
720         if .recordingTime <= 0
721                 .recordingTime = recordingTime
722         endif
723         call clean_up_sound
724         if sgc2.alignedTextGrid > 0
725                 select sgc2.alignedTextGrid
726                 Remove
727                 sgc2.alignedTextGrid = -1
728         endif
729         
730         # There is a very nasty delay before the first recording starts, do a dummy record
731         if not variableExists("recordingInitialized")
732         noprogress nowarn Record Sound (fixed time)... 'config.input$' 0.99 0.5 'samplingFrequency' 0.1
733                 Remove
734                 recordingInitialized = 1
735         endif
736         # Recording light
737     demo Paint circle... Red 5 95 2
738     demoShow()
739     noprogress nowarn Record Sound (fixed time)... 'config.input$' 0.99 0.5 'samplingFrequency' '.recordingTime'
740     demo Paint circle... White 5 95 2.5
741     call wipeArea 'wipeFeedbackArea$'
743     # Feedback on recording level
744     .extremum = Get absolute extremum... 0 0 None
745     .radius = 2 * .extremum
746     if .radius <= 0
747                 .radius = 0.02
748     endif
749     .blue = 0
750     .green = 0
751     .red = 0
752     if .extremum >= 0.95
753             .red = 1
754     elsif .extremum >= 0.49
755             .green = 1
756     else
757             .green = .extremum / 0.5
758     endif
759     .color$ = "{'.red','.green','.blue'}"
760     demo Colour... '.color$'
761     demo Line width... 1
762     demo Draw circle... 5 95 '.radius'
763     # Reset
764     demoShow()
765     demo Colour... Black
766     demo Line width... 'defaultLineWidth'
767     # Process sound
768     Rename... Tmp
769     Resample... 10000 50
770     Rename... Pronunciation
771     recordedSound$ = selected$("Sound")
772     te.recordedSound = selected("Sound")
773     select Sound Tmp
774     Remove
775     select Sound 'recordedSound$'
776     te.recordedSound = selected("Sound")
777         
778     # Cut out real sound from silences/noise
779     call sound_detection 'recordedSound$' 'soundMargin'
780     select Sound 'recordedSound$'
781     te.recordedSound = selected("Sound")
782     
783     # Store audio if requested
784     if sgc.saveAudioOn and sgc.saveAudio$ <> ""
785                 .pinyin$ = ""
786                 select Table 'wordlist$'
787                 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
788                         .pinyin$ = Get value... 'te.currentWord' Pinyin
789                         .outputName$ = "'sgc.saveAudio$'/'.pinyin$'.wav"
790                         select te.recordedSound
791                         Save as WAV file: .outputName$
792                 endif
793     endif
794     
795 endproc
798 # Select real sound from recording
799 # Uses some global variable
800 procedure sound_detection .sound$ .margin
801         select Sound '.sound$'
802         .soundlength = Get total duration
803         .internalSilence = 2*.margin
804         
805         # Silence and remove noise, DANGEROUS
806         To TextGrid (silences)... 'minimumPitch' 0 'noiseThresshold' '.internalSilence' 0.1 silent sounding
807         Rename... Input'.sound$'
809         select TextGrid Input'.sound$'
810         .numberofIntervals = Get number of intervals... 1
811         if .numberofIntervals < 2
812                 .numberofIntervals = 0
813         endif
815         # Remove buzzing and other obnoxious sounds (if switched on)
816         for .i from 1 to .numberofIntervals
817            select TextGrid Input'.sound$'
818            .value$ = Get label of interval... 1 '.i'
819            .begintime = Get starting point... 1 '.i'
820            .endtime = Get end point... 1 '.i'
822                 # Remove noise
823                 if .value$ = "silent"
824                         select Sound '.sound$'
825                         Set part to zero... '.begintime' '.endtime' at nearest zero crossing
826                 endif
827         endfor
829         # Select target sound
830         .maximumIntensity = -1
831         .counter = 1
832         for i from 1 to .numberofIntervals
833            select TextGrid Input'.sound$'
835            .value$ = Get label of interval... 1 'i'
836            .begintime = Get starting point... 1 'i'
837            .endtime = Get end point... 1 'i'
839            if .value$ != "silent"
840            if .begintime > .margin
841                   .begintime -= .margin
842            else
843                    .begintime = 0
844            endif
845            if .endtime + .margin < .soundlength
846                    .endtime += .margin
847            else
848                    .endtime = .soundlength
849            endif
851            select Sound '.sound$'
852            Extract part... '.begintime' '.endtime' Rectangular 1.0 no
853            Rename... Tmp'.sound$'
854            Subtract mean
855            .newIntensity = Get intensity (dB)
856            if .newIntensity > .maximumIntensity
857                    if .maximumIntensity > 0
858                    select Sound New'.sound$'
859                    Remove
860                    endif
861                    select Sound Tmp'.sound$'
862                    Rename... New'.sound$'
863                    .maximumIntensity = .newIntensity
864            else
865                    select Sound Tmp'.sound$'
866                    Remove
867            endif
868            # 
869            endif
870         endfor
871         if .maximumIntensity > minimumIntensity
872                 select Sound '.sound$'
873                 Remove
874                 select Sound New'.sound$'
875                 Rename... '.sound$'
876         elsif .maximumIntensity > -1
877                 select Sound New'.sound$'
878                 Remove
879         endif
880         select TextGrid Input'.sound$'
881         Remove
882         
883         select Sound '.sound$'
884 endproc
886 procedure end_program
887         call write_preferences "" 
888         demo Erase all
889         select all
890         Remove
891         exit
892 endproc
894 ######################################################
896 # Configuration Page
898 ######################################################
899 procedure config_page
900     demo Erase all
901     demoWindowTitle("Speak Good Chinese: Change settings")
902     .label$ = ""
903     call Draw_config_page
905     while (.label$ <> "Return") and demoWaitForInput() 
906                 .clickX = -1
907                 .clickY = -1
908                 .pressed$ = ""
909             .label$ = ""
910             if demoClicked()
911                     .clickX = demoX()
912                     .clickY = demoY()
913                     call buttonClicked 'config$' '.clickX' '.clickY'
914                     .label$ = buttonClicked.label$
915             elsif demoKeyPressed()
916                     .pressed$ = demoKey$()
917                     call keyPressed 'config$' '.pressed$'
918                     .label$ = keyPressed.label$
919             endif
921                 # You cannot select a text field
922                 if startsWith(.label$, "$")
923                         .label$ = ""
924                 endif
925                 
926             # Do things
927             if .label$ != ""
928                     # Handle push button in process_config
929                     call process_config '.label$' '.clickX' '.clickY' '.pressed$'
930             endif
931         
932         if .label$ = "Return"
933             goto GOBACK
934         endif
935     endwhile
937     # Go back
938     label GOBACK
939     call init_window
940 endproc
942 procedure Draw_config_page
943         demo Erase all
944         # Draw background
945         if config.showBackground
946                 call draw_background Background
947         endif
948         # Draw buttons
949     call Draw_all_buttons 'config$'
950         call set_window_title 'config$'  
951     # Set correct buttons (alert)
952         call setConfigMainPage
953 endproc
955 # Do what is asked
956 procedure process_config .label$ .clickX .clickY .pressed$
957         if .label$ <> "" and not startsWith(.label$,"!")
958                 .label$ = replace$(.label$, "_", " ", 0)
959                 call process'config$''.label$' '.clickX' '.clickY' '.pressed$'
960         endif
961 endproc
963 ###############################################################
965 # Presenting help texts
967 ###############################################################
969 # Process Help
970 procedure help_loop .table$ .redrawProc$
971         # General Help text
972         call  write_help_title '.table$'
973         
974     .label$ = ""
975     call Draw_button '.table$' Help 2
976     .redrawScreen = 0
977     while (.label$ <> "Help") and demoWaitForInput() 
978             .label$ = ""
979             if demoClicked()
980                     .clickX = demoX()
981                     .clickY = demoY()
982                     call buttonClicked '.table$' '.clickX' '.clickY'
983                     .label$ = buttonClicked.label$
984             elsif demoKeyPressed()
985                     .pressed$ = demoKey$()
986                     call keyPressed '.table$' '.pressed$'
987                     .label$ = keyPressed.label$
988             endif
990             if .label$ != "" and .label$ <> "Help"
991                         # Redraw screen
992                         if .redrawScreen
993                                 demo Erase all
994                                 call '.redrawProc$'
995                         else
996                         .redrawScreen = 1
997                         endif
998                         call Draw_button '.table$' Help 2
999                         call  write_help_title '.table$'
1001                     # Handle push button in process_config
1002                     call write_help_text '.table$' '.label$'
1003             endif
1004         
1005     endwhile
1006         
1007         # Reset button
1008     call Draw_button '.table$' Help 0
1009         demo Erase all
1010         call '.redrawProc$'
1011 endproc
1013 # Write help text
1014 procedure write_help_text .table$ .label$
1015         call findLabel '.table$' '.label$'
1016         .row = findLabel.row
1017         select Table '.table$'
1018         # Get text
1019         if .row <= 0
1020                 call findLabel '.table$' Help
1021                 .row = findLabel.row
1022                 select Table '.table$'
1023         endif
1024         .helpText$ = Get value... '.row' Helptext
1025         .helpKey$ = Get value... '.row' Key
1026         .helpKey$ = replace$(.helpKey$, "\", "", 0)
1027         .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
1028         if index_regex(.helpKey$, "\S")
1029                 .helpText$ = .helpText$+" ("+.helpKey$+")"
1030         endif
1031         # Get button values
1032     .leftX = Get value... '.row' LeftX
1033     .rightX = Get value... '.row' RightX
1034     .lowY = Get value... '.row' LowY
1035     .highY = Get value... '.row' HighY
1036         
1037         # PopUp dimensions
1038         .currentHelpFontSize = defaultFontSize
1039     call set_font_size '.currentHelpFontSize'
1040         .helpTextSize = demo Text width (wc)... '.helpText$'
1041         .helpTextSize += 4
1042         if .leftX > 50
1043                 .htXleft = 20
1044                 .htXright = .htXleft + .helpTextSize + 5
1045                 .xstart = .leftX
1046         else
1047                 .htXright = 80
1048                 .htXleft = .htXright - .helpTextSize - 5
1049                 .xstart = .rightX
1050         endif
1051         if .lowY > 50
1052                 .htYlow = 40
1053                 .htYhigh = .htYlow + 7
1054                 .ystart = .lowY
1055                 .yend = .htYhigh
1056         else
1057                 .htYhigh = 60
1058                 .htYlow = .htYhigh - 7
1059                 .ystart = .highY
1060                 .yend = .htYlow
1061         endif
1063         # Adapt font size to horizontal dimensions
1064         .maxWidth = 90
1065         call adjustFontSizeOnWidth 'defaultFont$' '.currentHelpFontSize' '.maxWidth' '.helpText$'
1066         .currentHelpFontSize = adjustFontSizeOnWidth.newFontSize
1067         if .htXleft < 0 or .htXright > 100
1068                 .htXleft = 0
1069                 .htXright = .htXleft + adjustFontSizeOnWidth.textWidth + 5
1070         endif
1071         call set_font_size '.currentHelpFontSize'
1073         # Adapt vertical dimensions to font height
1074         call points_to_wc '.currentHelpFontSize'
1075         .lineHeight = points_to_wc.wc
1076         if .lineHeight > .htYhigh - .htYlow - 4
1077                 .htYhigh = .htYlow + .lineHeight + 4
1078         endif
1080         # Determine arrow endpoints
1081         .xend = .htXleft
1082         if abs(.htXleft - .xstart) > abs(.htXright - .xstart)
1083                 .xend = .htXright
1084         endif
1085         if abs((.htXleft+.htXright)/2 - .xstart) < min(abs(.htXright - .xstart),abs(.htXleft - .xstart))
1086                 .xend = (.htXleft+.htXright)/2
1087         endif
1088         
1089         .xtext = .htXleft + 2
1090         .ytext = .htYlow + 1
1091         
1092         # Draw pop-up
1093         .mm2wc = demo Horizontal mm to wc... 1
1094         .lineWidth = 2/.mm2wc
1095         demo Line width... '.lineWidth'
1096         demo Arrow size... '.lineWidth'
1097         demo Colour... 'sgc2.popUp_bordercolor$'
1098         demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.htXleft' '.htXright' '.htYlow' '.htYhigh'
1099         demo Draw rectangle... '.htXleft' '.htXright' '.htYlow' '.htYhigh'
1100         demo Draw arrow... '.xstart' '.ystart' '.xend' '.yend'
1101         demo Line width... 'defaultLineWidth'
1102         demo Arrow size... 1
1103         demo Black
1104         demo Text... '.xtext' Left '.ytext' Bottom '.helpText$'
1105         demoShow()
1106         call set_font_size 'defaultFontSize'
1107         
1108 endproc
1110 procedure write_help_title .table$
1111         # Set help text title
1112         # General Help text
1113         call findLabel '.table$' Help
1114         .row = findLabel.row
1115         select Table '.table$'
1116         .helpTitle$ = Get value... '.row' Helptext
1117         .helpKey$ = Get value... '.row' Key
1118         .helpKey$ = replace$(.helpKey$, "\", "", 0)
1119         .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
1120         .helpTitle$ = .helpTitle$+" ("+.helpKey$+")"
1121         
1122         call reset_viewport
1123         .helpTitleFontSize = 14
1124         # Adapt size of button to length of text
1125         .maxWidth = 80
1126         call adjustFontSizeOnWidth 'defaultFont$' '.helpTitleFontSize' '.maxWidth' '.helpTitle$'
1127         .helpTitleFontSize = adjustFontSizeOnWidth.newFontSize
1128         call set_font_size '.helpTitleFontSize'
1129         .helpTop = 100
1130         
1131         demo Select inner viewport... 0 100 0 100
1132         demo Axes... 0 100 0 100
1133         demo Text... 50 Centre '.helpTop' Top '.helpTitle$'
1134     call set_font_size 'defaultFontSize'
1135         call reset_viewport
1136 endproc
1138 ###############################################################
1140 # Miscelaneous procedures
1142 ###############################################################
1143 procedure printPageToPrinter
1144         call print_window
1145         demo Print... 'printerName$' 'printerPresets$'
1146         call init_window
1147 endproc
1149 procedure getOpenFile .openDialogue$
1150         call clean_up_sound
1152         call convert_praat_to_latin1 '.openDialogue$'
1153         .openDialogue$ = convert_praat_to_latin1.text$
1154         .filename$ = chooseReadFile$ (.openDialogue$)
1155         .tmp = -1
1156         if .filename$ <> "" and fileReadable(.filename$)
1157                 .tmp = nocheck Read from file... '.filename$'
1158                 if .tmp !- undefined and .tmp > 0
1159                         call log_fileOpen '.filename$'
1160                 
1161                         # Get only the filename
1162                         .startName = rindex_regex(.filename$, "[/\\:]") + 1
1163                         .nameLength = rindex(.filename$, ".") - .startName
1164                         currentSoundName$ = mid$(.filename$, .startName, .nameLength)
1165                 else
1166                         .tmp = -1
1167                 endif
1168         endif
1169         if .tmp <= 0
1170                 Create Sound from formula... Speech Mono 0 1 44100 0
1171         endif
1172         recordedSound$ = selected$("Sound")
1173         te.recordedSound = selected("Sound")
1174         currentStartTime = 0
1175         currentEndTime = Get total duration
1176         # Reset selected window
1177         selectedStartTime = currentStartTime
1178         selectedEndTime = currentEndTime
1179 endproc
1181 procedure points_to_wc .points
1182         .mm = .points * 0.3527777778
1183         .wc = demo Vertical mm to wc... '.mm'
1184 endproc
1186 procedure reset_viewport
1187         .low = viewportMargin
1188         .high = 100 - viewportMargin
1189         demo Select inner viewport... '.low' '.high' '.low' '.high'
1190         demo Axes... 0 100 0 100
1191 endproc
1193 procedure set_font_size .fontSize
1194         call reset_viewport
1195         demo Font size... '.fontSize'
1196         call reset_viewport
1197 endproc
1199 procedure wipeArea .areaCommand$
1200         call reset_viewport
1201         '.areaCommand$'
1202 endproc
1204 procedure adjustFontSizeOnWidth .font$ .currentFontSize .maxWidth .text$
1205         demo '.font$'
1206         call set_font_size '.currentFontSize'
1207         .textWidth = demo Text width (wc)... '.text$'
1208         while .textWidth > .maxWidth and .currentFontSize > 2
1209                 .currentFontSize -= 0.5
1210                 call set_font_size '.currentFontSize'
1211                 .textWidth = demo Text width (wc)... '.text$'
1212         endwhile
1213         .diff = .textWidth - .maxWidth
1214         .newFontSize = .currentFontSize 
1215         demo 'defaultFont$'
1216 endproc
1218 procedure adjustRotatedFontSizeOnBox .font$ .currentFontSize .maxWidth .maxHeight .rotation .text$
1219         demo '.font$'
1220         .radians = .rotation/360 * 2 * pi
1221         .horWC = demo Horizontal mm to wc... 10.0
1222         .verWC = demo Vertical mm to wc... 10.0
1223         if .horWC > 0
1224                 .verCoeff = .verWC / .horWC
1225         else
1226                 .verCoeff = 1
1227         endif
1228         call set_font_size '.currentFontSize'
1229         .textLength = demo Text width (wc)... '.text$'
1230         while (.textLength * .verCoeff * sin(.radians) > .maxHeight or .textLength * cos(.radians) > .maxWidth) and .currentFontSize > 2
1231                 .currentFontSize -= 0.5
1232                 call set_font_size '.currentFontSize'
1233                 .textLength = demo Text width (wc)... '.text$'
1234         endwhile
1235         .diff = .textLength - .maxHeight
1236         .newFontSize = .currentFontSize 
1237         demo 'defaultFont$'
1238 endproc
1240 procedure adjustFontSizeOnHeight .font$ .currentFontSize .maxHeight
1241         demo '.font$'
1242         call points_to_wc '.currentFontSize'
1243         .lineHeight = points_to_wc.wc
1244         while .lineHeight > .maxHeight and .currentFontSize > 2
1245                 .currentFontSize -= 0.5
1246                 call points_to_wc '.currentFontSize'
1247                 .lineHeight = points_to_wc.wc
1248         endwhile
1249         .diff = .lineHeight - .maxHeight
1250         .newFontSize = .currentFontSize
1251         demo 'defaultFont$'
1252 endproc
1254 # Load a table with button info etc.
1255 # Load local tables if present. Else load
1256 # build-in scripted tables
1257 procedure loadTable .tableName$
1258         .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
1259         # Search for the table in local, preference, and global directories
1260         if fileReadable("'localTableDir$'/'.tableName$'.Table")
1261         Read from file... 'localTableDir$'/'.tableName$'.Table
1262         elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1263         Read from file... 'preferencesTableDir$'/'.tableName$'.Table
1264         elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1265         Read from file... 'globaltablelists$'/'.tableName$'.Table
1266         # Load them from script
1267         elsif variableExists("procCreate'.tableVariableName$'$")
1268                 call Create'.tableVariableName$'
1269         else
1270                 call write_text_popup 'defaultFont$' 14 '.tableName$' cannot be found
1271                 demoWaitForInput()
1272                 exit '.tableName$' cannot be found
1273         endif
1274 endproc
1276 procedure testLoadTable .tableName$
1277         .table = 0
1278         .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
1279         # Search for the table in local, preference, and global directories
1280         if fileReadable("'localTableDir$'/'.tableName$'.Table")
1281         .table = 1
1282         elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1283         .table = 2
1284         elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1285         .table = 3
1286         # Load them from script
1287         elsif variableExists("procCreate'.tableVariableName$'$")
1288                 .table = 4
1289         else
1290                 .table = 0
1291         endif
1292 endproc
1294 procedure checkTable .tableName$
1295         .available = 0
1296         if fileReadable("'localTableDir$'/'.tableName$'.Table")
1297         .available = 1
1298         elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1299         .available = 1
1300         elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1301         .available = 1
1302         # Load them from script
1303         elsif variableExists("procCreate'.tableName$'$")
1304         .available = 1
1305         else
1306         .available = 0
1307     endif
1308 endproc
1310 # Create a pop-up window with text from a Text Table
1311 procedure write_text_table .table$
1312         .xleft = 10
1313         .xright = 90
1314         .ylow = 20
1315         .yhigh = 85
1316         .lineHeight = 2.5
1318         # Get table with text and longest line
1319         .numLines = 0
1320         call testLoadTable '.table$'
1321         if testLoadTable.table > 0
1322                 call loadTable '.table$'
1323                 .instructionText = selected()
1324                 .numLines = Get number of rows
1325         endif
1326         .instructionFontSize = 14
1327         .referenceText$ = ""
1328         .maxlenght = 0
1329         .maxLine = 0
1330         .maxFontSize = 0
1331         .maxWidth = 0
1332         for .l to .numLines
1333                 select '.instructionText'
1334                 .currentText$ = Get value... '.l' text
1335                 # Expand variables, eg, 'praatVersion$'
1336                 call expand_praat_variables '.currentText$'
1337                 .currentText$ = expand_praat_variables.text$
1338                 
1339                 .font$ = Get value... '.l' font
1340                 .fontSize = Get value... '.l' size
1341                 call set_font_size '.fontSize'
1342                 .textWidth = demo Text width (wc)... '.currentText$'
1343                 if .fontSize > .maxFontSize
1344                         .maxFontSize = .fontSize
1345                 endif
1346                 if .textWidth > .maxWidth
1347                         .maxWidth = .textWidth
1348                         .instructionFontSize = .fontSize
1349                         .maxLine = .l
1350                 endif
1351         endfor
1352         select '.instructionText'
1353         .referenceText$ = Get value... '.maxLine' text
1354         .maxLineFont$ = Get value... '.maxLine' font
1355         .instructionFontSize = Get value... '.maxLine' size
1356         call set_font_size '.maxFontSize'
1357         
1358         # Adapt size of button to length of text
1359         .maxWidth = (.xright - .xleft) - 4
1360         .origFontSize = .instructionFontSize
1361         call adjustFontSizeOnWidth 'defaultFont$' '.instructionFontSize' '.maxWidth' '.referenceText$'
1362         call adjustFontSizeOnHeight 'defaultFont$' '.maxFontSize' '.lineHeight'
1363         .instructionFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1364         if adjustFontSizeOnWidth.diff > 0
1365                 .xright += adjustFontSizeOnWidth.diff/4
1366                 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1367         endif
1368         call set_font_size '.instructionFontSize'
1369         .fontSizeFactor = .instructionFontSize / .origFontSize
1371         .numRows = Get number of rows
1372         # Calculate length from number of lines.
1373         .dy = .lineHeight
1374         .midY = .yhigh - (.yhigh - .ylow)/2
1375         .yhigh = .midY + (.numRows+1) * .dy / 2
1376         .ylow = .yhigh - (.numRows+1) * .dy
1377         .textleft = .xleft + 2
1378         
1379         demo Line width... 8
1380         demo Colour... 'sgc2.popUp_bordercolor$'
1381         demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1382         demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1383         demo Line width... 'defaultLineWidth'
1384         demo Black
1385         .ytext = .yhigh - 2 - .dy
1386         for .i to .numRows
1387                 select '.instructionText'
1388                 .font$ = Get value... '.i' font
1389                 .fontSize = Get value... '.i' size
1390                 .font$ = extractWord$(.font$, "")
1391                 # Scale font
1392                 .fontSize = floor(.fontSize*.fontSizeFactor)
1393                 if .fontSize < 4
1394                         .fontSize = 4
1395                 endif
1396                 .line$ = Get value... '.i' text
1397                 # Expand variables, eg, 'praatVersion$'
1398                 call expand_praat_variables '.line$'
1399                 .line$ = expand_praat_variables.text$
1400                 
1401                 # Display text
1402                 demo Text special... '.textleft' Left '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1403                 .ytext -= .dy
1404         endfor  
1405         demoShow()      
1406         call set_font_size 'defaultFontSize'
1407         
1408         select '.instructionText'
1409         Remove
1410         
1411         label ESCAPEwrite_text_table
1412 endproc
1415 # Create a pop-up window with text from an existing Table object
1416 procedure write_tabbed_table .table$ .labelTextTable$
1417         .xleft = 0
1418         .xright = 100
1419         .ylow = 20
1420         .yhigh = 85
1421         .lineHeight = 2.5
1423         # Get table with text and longest line
1424         call testLoadTable '.table$'
1425         if testLoadTable.table <= 0
1426                 call loadTable '.labelTextTable$'
1427                 .labelText$ = selected$("Table")
1428         endif
1429                 
1430         select Table '.table$'
1431         .tabbedText = selected()
1432         .numLines = Get number of rows
1433         .numCols = Get number of columns
1434         .font$ = defaultFont$
1435         .fontSize = defaultFontSize
1436         # Standard width
1437         .widthCanvas = .xright - .xleft
1438         .dx = (.widthCanvas - 4) / (.numCols)
1440         # Get longest entry
1441         demo '.font$'
1442         call set_font_size '.fontSize'
1443         .maxWidth = 0
1444         for .i from 0 to .numLines
1445                 .xtext = .xleft + .dx / 2
1446                 for .j to .numCols
1447                         select '.tabbedText'
1448                         .currentLabel$ = Get column label... '.j'
1449                         if .i > 0
1450                                 .line$ = Get value... '.i' '.currentLabel$'
1451                         else
1452                                 .line$ = .currentLabel$
1453                                 select Table '.labelText$'
1454                         call findLabel '.labelText$' '.line$'
1455                         select Table '.labelText$'
1456                         .line$ = Get value... 'findLabel.row' Text
1457                         endif
1458                         # Expand variables, eg, 'praatVersion$'
1459                         call expand_praat_variables '.line$'
1460                         .line$ = expand_praat_variables.text$
1461                         .textWidth = demo Text width (wc)... '.line$'
1462                         if .textWidth > .maxWidth
1463                                 .maxWidth = .textWidth
1464                         endif
1465                 endfor
1466         endfor
1467         if .dx > 1.2 * .maxWidth
1468                 .widthCanvas =  1.2 * .maxWidth * .numCols + 4
1469                 .xleft = 50 - .widthCanvas / 2
1470                 .xright = 50 + .widthCanvas / 2
1471                 .dx = (.widthCanvas - 4) / (.numCols)
1472         else
1473                 .maxWidth = .dx - 1
1474         endif
1475         
1476         # Calculate length from number of lines.
1477         .dy = .lineHeight + 0.5
1478         .midY = .yhigh - (.yhigh - .ylow)/2
1479         .yhigh = .midY + (.numLines+2) * .dy / 2
1480         .ylow = .yhigh - (.numLines+2) * .dy
1481         .textleft = .xleft + 2
1482         
1483         demo Line width... 8
1484         demo Colour... 'sgc2.popUp_bordercolor$'
1485         demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1486         demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1487         demo Line width... 'defaultLineWidth'
1488         demo Black
1489         .ytext = .yhigh - 2 - .dy
1490         # First the column names, then the items
1491         for .i from 0 to .numLines
1492                 .xtext = .textleft + .dx / 2
1493                 for .j to .numCols
1494                         select '.tabbedText'
1495                         .currentLabel$ = Get column label... '.j'
1496                         if .i > 0
1497                                 .line$ = Get value... '.i' '.currentLabel$'
1498                         else
1499                                 .line$ = .currentLabel$
1500                                 select Table '.labelText$'
1501                         call findLabel '.labelText$' '.line$'
1502                         select Table '.labelText$'
1503                         .line$ = Get value... 'findLabel.row' Text
1504                         endif
1505                         # Expand variables, eg, 'praatVersion$'
1506                         call expand_praat_variables '.line$'
1507                         .line$ = expand_praat_variables.text$
1508                         call adjustFontSizeOnWidth '.font$' '.fontSize' '.maxWidth' '.line$'
1509                         .currentFontSize = adjustFontSizeOnWidth.newFontSize
1511                         # Display text
1512                         demo Text special... '.xtext' Centre '.ytext' Bottom '.font$' '.currentFontSize' 0 '.line$'
1513                         .xtext += .dx
1514                 endfor
1515                 .ytext -= .dy
1516         endfor  
1517         demoShow()      
1518         call set_font_size 'defaultFontSize'
1519         select Table '.labelText$'
1520         Remove
1521         
1522         label ESCAPEwrite_tabbed_table
1523 endproc
1525 # Create a pop-up window with a given text
1526 procedure write_text_popup .font$ .size .text$
1527         .xleft = 10
1528         .xright = 90
1529         .ylow = 20
1530         .yhigh = 85
1531         .lineHeight = 3
1533         # Adapt size of button to length of text
1534         .maxWidth = (.xright - .xleft) - 4
1535         call adjustFontSizeOnWidth 'defaultFont$' '.size' '.maxWidth' '.text$'
1536         call adjustFontSizeOnHeight 'defaultFont$' '.size' '.lineHeight'
1537         .popupFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1538         if adjustFontSizeOnWidth.diff > 0
1539                 .xright += adjustFontSizeOnWidth.diff/4
1540                 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1541         else
1542                 .xleft = ((.xright + .xleft) - adjustFontSizeOnWidth.textWidth)/2 - 2
1543                 .xright = ((.xright + .xleft) + adjustFontSizeOnWidth.textWidth)/2 + 2
1544         endif
1546         .numRows = 1
1547         # Calculate length from number of lines.
1548         .dy = .lineHeight
1549         .midY = .yhigh - (.yhigh - .ylow)/2
1550         .yhigh = .midY + (.numRows+1) * .dy / 2
1551         .ylow = .yhigh - (.numRows+1) * .dy
1552         .textleft = .xleft + 2
1553         .xmid = (.textleft + .xright - 2)/2
1554         
1555         demo Line width... 8
1556         demo Colour... 'sgc2.popUp_bordercolor$'
1557         demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1558         demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1559         demo Line width... 'defaultLineWidth'
1560         demo Black
1561         .ytext = .yhigh - 2 - .dy
1562         # Write text
1563         demo Text special... '.xmid' Centre '.ytext' Bottom '.font$' '.popupFontSize' 0 '.text$'
1565         demoShow()      
1566         demo 'defaultFont$'
1567         call set_font_size 'defaultFontSize'
1568 endproc
1570 # Write the background from a Text Table
1571 procedure draw_background .table$
1572         .xleft = 0
1573         .xright = 100
1574         .ylow = 0
1575         .yhigh = 100
1576         .lineHeight = 5
1577         .defaultColour$ = "{0.9,0.9,0.9}"
1578         .defaultAlign$ = "centre"
1580         # Get table with text and longest line
1581         call loadTable '.table$'
1582         .backgroundText = selected()
1583         .numLines = Get number of rows
1584         .backgroundFontSize = 28
1585         .referenceText$ = ""
1586         .maxlenght = 0
1587         .maxLine = 0
1588         .maxFontSize = 0
1589         .maxWidth = 0
1590         .textLines = 0
1591         for .l to .numLines
1592                 select '.backgroundText'
1593                 .currentText$ = Get value... '.l' text
1594                 # Expand variables, eg, 'praatVersion$'
1595                 call expand_praat_variables '.currentText$'
1596                 .currentText$ = expand_praat_variables.text$            
1597                 
1598                 .font$ = Get value... '.l' font
1599                 .fontSize = Get value... '.l' size
1600                 if .fontSize > .maxFontSize
1601                         .maxFontSize = .fontSize
1602                 endif
1603                 if not startsWith(.font$, "!")
1604                         call set_font_size '.fontSize'
1605                         .textWidth = demo Text width (wc)... '.currentText$'
1606                         if .textWidth > .maxWidth
1607                                 .maxWidth = .textWidth
1608                                 .backgroundFontSize = .fontSize
1609                                 .maxLine = .l
1610                         endif
1612                         .textLines += 1
1613                 endif
1614         endfor
1615         if .maxLine > 0
1616                 select '.backgroundText'
1617                 .referenceText$ = Get value... '.maxLine' text
1618                 .maxLineFont$ = Get value... '.maxLine' font
1619                 .backgroundFontSize = Get value... '.maxLine' size
1620                 .backgroundFontColour$ = Get value... '.maxLine' colour
1621                 call set_font_size '.maxFontSize'
1622         else
1623                 .maxFontSize = .backgroundFontSize
1624         endif
1625         
1626         # Adapt size of button to length of text
1627         .maxWidth = (.xright - .xleft) - 4
1628         .origFontSize = .backgroundFontSize
1629         call adjustFontSizeOnWidth 'defaultFont$' '.backgroundFontSize' '.maxWidth' '.referenceText$'
1630         .fontSizeFactor = adjustFontSizeOnWidth.newFontSize / .backgroundFontSize
1631         .backgroundFontSize = adjustFontSizeOnWidth.newFontSize
1632         call set_font_size '.backgroundFontSize'
1633         
1634         call adjustFontSizeOnHeight 'defaultFont$' '.backgroundFontSize' '.lineHeight'
1635         .lineHeight /= adjustFontSizeOnHeight.newFontSize / .backgroundFontSize
1636         if adjustFontSizeOnHeight.newFontSize >= .origFontSize and (.textLines+1) * .lineHeight > (.yhigh - .ylow - 4)
1637                 .lineHeight = (.yhigh - .ylow - 4)/(.textLines + 1)
1638                 call adjustFontSizeOnHeight 'defaultFont$' '.maxFontSize' '.lineHeight'
1639                 .fontSizeFactor = adjustFontSizeOnHeight.newFontSize / .backgroundFontSize
1640         endif
1642         .numRows = Get number of rows
1643         # Calculate length from number of lines.
1644         .dy = .lineHeight
1645         .midY = .yhigh - (.yhigh - .ylow)/2
1646         .yhigh = .midY + (.textLines+1) * .dy / 2
1647         .ylow = .yhigh - (.textLines+1) * .dy
1648         .textleft = .xleft + 2
1649         .textright = .xright - 2
1650         .textmid = (.xright - .xleft)/2
1651         
1652         demo Black
1653         .ytext = .yhigh - 2 - .dy
1654         for .i to .numRows
1655                 select '.backgroundText'
1656                 .font$ = Get value... '.i' font
1657                 .fontSize = Get value... '.i' size
1658                 .fontColour$ = Get value... '.i' colour
1659                 .fontColour$ = replace_regex$(.fontColour$, "^[\- ]$", ".defaultColour$", 1)
1660                 .fontAlign$ = Get value... '.i' align
1661                 .fontAlign$ = replace_regex$(.fontAlign$, "^[\- ]$", ".defaultAlign$", 1)
1662                 .line$ = Get value... '.i' text
1663                 # Expand variables, eg, 'praatVersion$'
1664                 call expand_praat_variables '.line$'
1665                 .line$ = expand_praat_variables.text$
1666                                 
1667                  # Scale font
1668                  .fontSize = floor(.fontSize*.fontSizeFactor)
1669                 if not startsWith(.font$, "!")
1670                         .font$ = extractWord$(.font$, "")
1672                         if .fontAlign$ = "centre"
1673                                 .xtext = .textmid
1674                         elsif .fontAlign$ = "right"
1675                                 .xtext = .textright
1676                         else
1677                                 .xtext = .textleft
1678                         endif
1679                         if .fontSize < 4
1680                                 .fontSize = 4
1681                         endif
1682                         # Clean up text
1683                         demo Colour... '.fontColour$'
1684                         demo Text special... '.xtext' '.fontAlign$' '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1685                         .ytext -= .dy
1686                 elsif .font$ = "!demo command"
1687                         demo Colour... '.fontColour$'
1688                         .line$ = replace_regex$(.line$, "\{FONTSIZE\$\}", "'.fontSize'", 0)
1689                         .line$ = replace_regex$(.line$, "\{XTEXT\$\}", "'.xtext'", 0)
1690                         .line$ = replace_regex$(.line$, "\{YTEXT\$\}", "'.ytext'", 0)
1691                         .line$ = replace_regex$(.line$, "\{DY\$\}", "'.dy'", 0)
1692                         .line$ = replace_regex$(.line$, "\{[^\}]*\}", "", 0)
1693                         while index(.line$, "[[")
1694                                 .nextBracketOpen = index(.line$, "[[")
1695                                 .nextBracketOpen += 2
1696                                 .nextBracketClose = index(.line$, "]]")
1697                                 .bracketLength = .nextBracketClose - .nextBracketOpen
1698                                 .result$ = ""
1699                                 if .bracketLength > 0
1700                                         .expression$ = mid$(.line$, .nextBracketOpen, .bracketLength)
1701                                         .expression$ = replace_regex$(.expression$, "\s", "", 0)
1702                                         if length(.expression$) > 0
1703                                                 # Test expression for security, only allow explicitely defined functions
1704                                                 .allowedStrings$ = "e|pi|not|and|or|div|mod|abs|round|floor|ceiling"
1705                                                 .allowedStrings$ = .allowedStrings$ + "|sqrt|min|max|imin|imax|sin|cos|tan|arcsin|arccos|arctan|arctan2|sinc|sincpi"
1706                                                 .allowedStrings$ = .allowedStrings$ + "|exp|ln|log10|log2|sinh|cosh|tanh|arcsinh|arccosh|arctanh"
1707                                                 .allowedStrings$ = .allowedStrings$ + "|sigmoid|invSigmoid|erf|erfc|randomUniform|randomInteger|randomGauss|randomPoisson"
1708                                                 .allowedStrings$ = .allowedStrings$ + "|lnGamma|gaussP|gaussQ|invGaussQ|chiSquareP|chiSquareQ"
1709                                                 .allowedStrings$ = .allowedStrings$ + "|invChiSquareP|invChiSquareQ|studentP|studentQ|invStudentP|invStudentQ"
1710                                                 .allowedStrings$ = .allowedStrings$ + "|beta|besselI|besselK"
1711                                                 .testExpression$ = replace_regex$(.expression$, "(^|\W)('.allowedStrings$')(?=$|\W)", "\1\3", 0)
1712                                                 .testExpression$ = replace_regex$(.testExpression$, "[0-9\.,\-+/*^()<>= ]", "", 0)
1713                                                 if .testExpression$ = ""
1714                                                         .calc = '.expression$'
1715                                                         .result$ = "'.calc'"
1716                                                 endif
1717                                         endif
1718                                 endif
1719                                 
1720                                 # Replace expression by result
1721                                 .lastLeft = .nextBracketOpen - 3
1722                                 .newLine$ = left$(.line$, .lastLeft)  
1723                                 .newLine$ =  .newLine$ + .result$
1724                                 .numCopy = length(.line$) - .nextBracketClose - 1
1725                                 .newLine$ =  .newLine$ + right$(.line$, .numCopy)
1726                                 .line$ = .newLine$
1727                         endwhile
1728                         demo '.line$'
1729                 endif
1730         endfor  
1731         demo Black
1732         demoShow()      
1733         call set_font_size 'defaultFontSize'
1734         
1735         select '.backgroundText'
1736         Remove
1737 endproc
1739 procedure convert_praat_to_utf8 .text$
1740         .text$ = replace_regex$(.text$, "\\a""", "\xc3\xa4", 0)
1741         .text$ = replace_regex$(.text$, "\\A""", "\xc3\x84", 0)
1742         .text$ = replace_regex$(.text$, "\\o""", "\xc3\xb6", 0)
1743         .text$ = replace_regex$(.text$, "\\O""", "\xc3\x96", 0)
1744         .text$ = replace_regex$(.text$, "\\u""", "\xc3\xbc", 0)
1745         .text$ = replace_regex$(.text$, "\\U""", "\xc3\x9c", 0)
1746         .text$ = replace_regex$(.text$, "\\i""", "\xc3\xaf", 0)
1747         .text$ = replace_regex$(.text$, "\\I""", "\xc3\x8f", 0)
1748         .text$ = replace_regex$(.text$, "\\e""", "\xc3\xab", 0)
1749         .text$ = replace_regex$(.text$, "\\E""", "\xc3\x8b", 0)
1750         .text$ = replace_regex$(.text$, "\\y""", "\xc3\xbf", 0)
1751         .text$ = replace_regex$(.text$, "\\e'", "\xc3\xa9", 0)
1752         .text$ = replace_regex$(.text$, "\\E'", "\xc3\x89", 0)
1753         .text$ = replace_regex$(.text$, "\\ss", "\xc3\x9f", 0)
1754 endproc
1756 procedure convert_praat_to_latin1 .text$
1757         .text$ = replace_regex$(.text$, "\\a""", "\xe4", 0)
1758         .text$ = replace_regex$(.text$, "\\A""", "\xc4", 0)
1759         .text$ = replace_regex$(.text$, "\\o""", "\xf6", 0)
1760         .text$ = replace_regex$(.text$, "\\O""", "\xd6", 0)
1761         .text$ = replace_regex$(.text$, "\\u""", "\xfc", 0)
1762         .text$ = replace_regex$(.text$, "\\U""", "\xdc", 0)
1763         .text$ = replace_regex$(.text$, "\\i""", "\xef", 0)
1764         .text$ = replace_regex$(.text$, "\\I""", "\xcf", 0)
1765         .text$ = replace_regex$(.text$, "\\e""", "\xeb", 0)
1766         .text$ = replace_regex$(.text$, "\\E""", "\xcb", 0)
1767         .text$ = replace_regex$(.text$, "\\y""", "\xff", 0)
1768         .text$ = replace_regex$(.text$, "\\Y""", "Y", 0)
1769         .text$ = replace_regex$(.text$, "\\e'", "\xe9", 0)
1770         .text$ = replace_regex$(.text$, "\\E'", "\xc9", 0)
1771         .text$ = replace_regex$(.text$, "\\ss", "\xdf", 0)
1772 endproc
1774 # Expand 'variable$' into the value of variable$.
1775 # Eg, 'praatVersion$' becomes 5.1.45 or whatever is the current version
1776 # Single quotes can be protected by \'
1777 procedure expand_praat_variables .text$
1778         if index(.text$, "'")
1779                 .tempText$ = replace_regex$(.text$, "(^|[^\\])'([\w\$\.]+)'", "\1""+\2+""", 0)
1780                 .tempText$ = replace_regex$(.tempText$, "[\\]'", "'", 0)
1781                 .tempText$ = """"+.tempText$+""""
1782                 # Check whether all the variables actually exist. Ignore any variable that does not exist
1783                 .checkVars$ = .tempText$
1784                 while length(.checkVars$) > 0 and index(.checkVars$, "+")
1785                         .start = index(.checkVars$, "+")
1786                         .checkVars$ = right$(.checkVars$, length(.checkVars$) - .start)
1787                         .end = index(.checkVars$, "+")
1788                         if .end
1789                                 .variable$ = left$(.checkVars$, .end - 1)
1790                                 if not variableExists(.variable$)
1791                                         .tempText$ = replace$(.tempText$, """+'.variable$'+""", "'"+.variable$+"'", 0)
1792                                 endif
1793                                 .checkVars$ = right$(.checkVars$, length(.checkVars$) - .end)
1794                         else
1795                                 .checkVars$ = ""
1796                         endif
1797                 endwhile
1798                 .text$ = '.tempText$'
1799         endif
1800 endproc
1802 # Get a time stamp in normalized format
1803 procedure getTimeStamp
1804         .currentDateTime$ = date$()
1805         .string$ = replace_regex$(.currentDateTime$, "[A-Z][a-z]+\s+([A-Z][a-z]+)\s+(\d+)\s+(\d+)\W(\d+)\W(\d+)\s+(\d+)$", "\6-\1-\2T\3-\4-\5", 0)
1806 endproc
1808 # A table error, can be insiduously caused by an outdate preferences file!
1809 procedure emergency_table_exit .message$
1810         # If you come here as a user, your preferences file is borked
1811         if preferencesAppFile$ <> "" and fileReadable(preferencesAppFile$)
1812                 deleteFile(preferencesAppFile$)
1813         endif
1814         exit '.message$'
1815 endproc
1817 # Remove previous files from system
1818 procedure clean_up_sound
1819         if recordedSound$ = ""
1820                 te.recordedSound = 0
1821         endif
1822     if te.recordedSound > 0
1823         select te.recordedSound
1824         Remove
1825         recordedSound$ = ""
1826         te.recordedSound = 0
1827     endif
1828     if te.recordedPitch > 0
1829         select te.recordedPitch
1830         Remove
1831                 te.recordedPitch = 0
1832     endif
1833 endproc
1835 # Safely read a table
1836 procedure readTable .filename$
1837         .tableID = -1
1838         if .filename$ <> "" and fileReadable(.filename$) and index_regex(.filename$, "(?i\.(tsv|table|csv))$") > 0
1839                 .tableID = nocheck Read from file... '.filename$'
1840                 if .tableID = undefined or .tableID <= 0
1841                         .tableID = -1
1842                 else
1843                         .fullName$ = selected$ ()
1844                         .type$ = extractWord$(.fullName$, "")
1845                         if .type$ <> "Table"
1846                                 Remove
1847                                 .tableID = -1
1848                         endif
1849                 endif
1850         endif
1851 endproc
1853 # Read feedback table and get keyed text
1854 procedure get_feedback_text .language$ .key$
1855         if not endsWith(feedbackTableName$, "_'.language$'")
1856                 if feedbackTableName$ <> ""
1857                         select Table 'feedbackTableName$'
1858                         Remove
1859                 endif
1860                 call loadTable 'feedbackTablePrefix$'_'.language$'
1861                 feedbackTableName$ = selected$("Table")
1862         endif
1863         call findKey 'feedbackTableName$' '.key$'
1864         .row = findKey.row
1865         select Table 'feedbackTableName$'
1866         .text$ = Get value... '.row' Text
1867         # Expand variables, eg, 'praatVersion$'
1868         call expand_praat_variables '.text$'
1869         .text$ = expand_praat_variables.text$   
1870 endproc