6 # SpeakGoodChinese: sgc2.praat is the master GUI of SpeakGoodChinese
7 # It is written in Praat script for the Demo window
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
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.
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.
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
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$")
36 # These are simply "useful" defaults
37 localTableDir$ = "Data"
38 buttonsTableName$ = "MainPage"
39 configTableName$ = "Config"
43 samplingFrequency = 44100
46 # Pop-Up window colors
47 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
48 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
50 # If running in a packed script binary
51 if index_regex(preferencesDirectory$, "(?i'sgc2.demoAppName$')$")
52 preferencesAppDir$ = preferencesDirectory$
53 elsif index_regex(preferencesDirectory$, "[pP]raat(\-dir| Prefs)?$")
54 # If running as a Praat script, create a new preferences directory
56 preferencesAppDir$ = "'preferencesDirectory$'/../.'sgc2.demoAppName$'"
58 preferencesAppDir$ = "'preferencesDirectory$'/../'sgc2.demoAppName$'"
61 # It has to go somewhere. Make a subdirectory in the current preferences directory
62 preferencesAppDir$ = "'preferencesDirectory$'/'sgc2.demoAppName$'"
66 # Parameters for isolating recorded speech from noise
67 # Should be mostly left alone unless you are using ultra clean
68 # or very noisy recordings
74 # Set up button height
80 defaultFont$ = "Helvetica"
86 # Load supporting scripts
87 # Load tables in script format
88 include CreateTables.praat
89 include CreateWordlists.praat
90 # Set up system and load preferences
91 include InitialiseSGC2.praat
92 # Include the main page buttons and procedures
93 include MainPage.praat
94 # Include the configuration page buttons and procedures
97 # Start instruction loop
98 while demoWaitForInput()
106 call buttonClicked 'buttons$' '.clickX' '.clickY'
107 .label$ = buttonClicked.label$
108 elsif demoKeyPressed()
109 .pressed$ = demoKey$()
110 call keyPressed 'buttons$' '.pressed$'
111 .label$ = keyPressed.label$
114 # You cannot select a text field
115 if startsWith(.label$, "$")
122 call Draw_button 'buttons$' '.label$' 1
123 call process_label '.label$' '.clickX' '.clickY' '.pressed$'
125 call Draw_button 'buttons$' '.label$' 0
132 ########################################################
134 # Definitions of procedures
136 ########################################################
139 procedure process_label .label$ .clickX .clickY .pressed$
140 # Log raw commands to replay in file
141 call log_command 'buttons$' '.label$' '.clickX' '.clickY' '.pressed$'
143 # Prcoess the command
144 if .label$ <> "" and not startsWith(.label$,"!")
145 .label$ = replace$(.label$, "_", " ", 0)
146 call process'buttons$''.label$' '.clickX' '.clickY' '.pressed$'
151 procedure init_buttons
153 call Draw_all_buttons 'buttons$'
159 procedure Draw_all_buttons .table$
160 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
161 select Table '.table$'
162 .numRows = Get number of rows
165 .label$ = Get value... '.row' Label
166 if not startsWith(.label$, "!")
168 # Determine the "pressed" state of a button
169 # A variable with the same name as the button will act as a
171 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
172 # Simple boolean vairables
173 if index(.variableName$, "_") <= 0 and variableExists(.variableName$)
175 if '.variableName$' > 0
178 elsif '.variableName$' < 0
181 # Complex buttons with an variableName+'_'+value structure
182 # varableName$ -> name of button, e.g., "language"
183 elsif index(.variableName$, "_")
184 .generalVar$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
185 .currentVariableName$ = .generalVar$
187 if variableExists(.generalVar$+"$")
188 .currentVariableName$ = .generalVar$ + "$"
190 # Remove one level of indirection
191 if variableExists(.currentVariableName$)
192 if index(.currentVariableName$, "$")
193 .currentVariableName$ = '.currentVariableName$'
195 .currentValue = '.currentVariableName$'
196 .currentVariableName$ = "'.currentValue'"
198 # Remove next level of indirection
199 .currentContent$ = "'.currentVariableName$'"
200 if .currentContent$ = "_DISABLED_"
203 # Reconstruct label from current values
204 .currentLabelValue$ = .generalVar$ + "_" + .currentContent$
205 # Set PRESSED from label
206 if .variableName$ = .currentLabelValue$
211 # You did erase everything before you started here? So do not do that again
212 call Draw_button_internal 0 '.table$' '.label$' '.pressed'
217 # Draw a button from a predefined button table
218 # Normally, erase the area around a button
219 procedure Draw_button .table$ .label$ .push
220 call Draw_button_internal 1 '.table$' '.label$' '.push'
223 # Use this function if you want to control erasing
224 procedure Draw_button_internal .erase_button_area .table$ .label$ .push
225 # Scale rounding of rounded rectangles
227 .mm = demo Horizontal wc to mm... '.wc'
228 # Allow to overide ! skip directive
230 if startsWith(.label$, "+")
231 .label$ = right$(.label$, length(.label$)-1)
235 select Table '.table$'
236 .row = Search column... Label '.label$'
238 call emergency_table_exit Button Table '.table$' does not have a row with label '.label$'
241 # Perspective shift sizes
251 # Set drawing parameters
252 .topBackGroundColorUp$ = "{0.93,0.93,0.93}"
253 .topLineColorUp$ = "Black"
254 .topLineWidthUp = 1.5
255 .topBackGroundColorDown$ = "{0.89,0.89,0.94}"
256 .topLineColorDown$ = "{0.3,0.3,0.3}"
257 .topLineWidthDown = 1.5
258 .topBackGroundColorDisabled$ = "{0.85,0.85,0.85}"
259 .topLineColorDisabled$ = "{0.70,0.70,0.70}"
260 .topLineWidthDisabled = 1.5
261 .flankBackGroundColorUp$ = "{0.6,0.6,0.6}"
262 .flankLineColorUp$ = "{0.2,0.2,0.2}"
263 .flankLineWidthUp = 1.5
264 .flankBackGroundColorDown$ = "{0.75,0.75,0.75}"
265 .flankLineColorDown$ = .flankLineColorUp$
266 .flankLineWidthDown = 1.5
267 .buttonFontSize = defaultFontSize
270 .leftX = Get value... '.row' LeftX
271 .rightX = Get value... '.row' RightX
272 .lowY = Get value... '.row' LowY
273 .highY = Get value... '.row' HighY
274 .buttonText$ = Get value... '.row' Text
275 .buttonColor$ = Get value... '.row' Color
276 .buttonDraw$ = Get value... '.row' Draw
277 .buttonKey$ = Get value... '.row' Key
280 if index_regex(.buttonText$, "^![0-9\.]+!")
281 .rotation = extractNumber(.buttonText$, "!")
282 .buttonText$ = replace_regex$(.buttonText$, "^![0-9\.]+!", "", 0)
285 goto NOBUTTON startsWith(.label$, "!") and not .forceDraw
287 # Replace button text with ALERT
289 .buttonText$ = alertText$
292 # Adapt font size to button size
293 .maxWidth = (.rightX - .leftX) - 2
294 .maxHeight = (.highY - .lowY) - 1
296 # Adapt size of button to length of text if necessary
297 call adjustFontSizeOnWidth 'defaultFont$' '.buttonFontSize' '.maxWidth' '.buttonText$'
298 .buttonFontSize = adjustFontSizeOnWidth.newFontSize
299 if adjustFontSizeOnWidth.diff > 0
300 .rightX += adjustFontSizeOnWidth.diff/2
301 .leftX -= adjustFontSizeOnWidth.diff/2
303 call set_font_size '.buttonFontSize'
305 # Adapt size of button to length of text
306 call adjustFontSizeOnHeight 'defaultFont$' '.buttonFontSize' '.maxHeight'
307 if adjustFontSizeOnHeight.diff > 0
308 .lowY -= adjustFontSizeOnHeight.diff/2
309 .highY += adjustFontSizeOnHeight.diff/2
311 .buttonFontSize = adjustFontSizeOnHeight.newFontSize
313 # With non-horizontal text, only change font size
314 call adjustRotatedFontSizeOnBox 'defaultFont$' '.buttonFontSize' '.maxWidth' '.maxHeight' '.rotation' '.buttonText$'
315 .buttonFontSize = adjustRotatedFontSizeOnBox.newFontSize
318 # Reset and erase button area
320 demo Line width... 'defaultLineWidth'
321 .shiftLeftX = .leftX - .shiftX
322 .shiftRightX = .rightX
323 .shiftLowY = .lowY - .shiftY
325 if .erase_button_area
326 # Make erase area minutely larger
327 .eraseLeft = .shiftLeftX - 0.01
328 .eraseRight = .shiftRightX + 0.01
329 .eraseBottom = .shiftLowY - 0.01
330 .eraseTop = .shiftHighY + 0.01
331 demo Paint rectangle... White .eraseLeft .eraseRight .eraseBottom .eraseTop
334 # If label starts with "$", it is a text field. Then do not draw the button
335 if not startsWith(.label$, "$")
336 # Give some depth to button: Draw flank outline
337 if .shiftDown or .shiftX or .shiftY
339 demo Paint rounded rectangle... '.flankBackGroundColorUp$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
340 demo Colour... '.flankLineColorUp$'
341 demo Line width... '.flankLineWidthUp'
343 demo Paint rounded rectangle... '.flankBackGroundColorDown$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
344 demo Colour... '.flankLineColorDown$'
345 demo Line width... '.flankLineWidthDown'
347 demo Draw rounded rectangle... .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
350 # Button Down will shift the top perspective
352 # Draw the button top
354 demo Paint rounded rectangle... '.topBackGroundColorUp$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
355 demo Colour... '.topLineColorUp$'
356 demo Line width... '.topLineWidthUp'
358 demo Paint rounded rectangle... '.topBackGroundColorDisabled$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
359 demo Colour... '.topLineColorDisabled$'
360 demo Line width... '.topLineWidthDisabled'
364 .rightX -= .shiftDown
368 demo Paint rounded rectangle... '.topBackGroundColorDown$' .leftX .rightX .lowY .highY '.mm'
369 demo Colour... '.topLineColorDown$'
370 demo Line width... '.topLineWidthDown'
372 demo Draw rounded rectangle... '.leftX' '.rightX' '.lowY' '.highY' '.mm'
375 # The button text and symbol
376 .horWC = demo Horizontal mm to wc... 10.0
377 .verWC = demo Vertical mm to wc... 10.0
379 .verCoeff = .horWC / .verWC
384 .centerX = (.leftX + .rightX)/2
385 .centerY = .lowY + 0.6*(.highY-.lowY)
386 .radius = min(.verCoeff * (.highY - .lowY ), (.rightX - .leftX))/3
387 .buttonKey$ = replace$(.buttonKey$, "\", "\\", 0)
388 .buttonKey$ = replace$(.buttonKey$, """", "\""""", 0)
389 .newText$ = replace_regex$(.buttonText$, "['.buttonKey$']", "#%&", 1)
391 .newText$ = .buttonText$
393 if .push = 1 or .push = -1
395 if .buttonColor$ = "Red"
396 .buttonColor$ = "Pink"
397 elsif .buttonColor$ = "Blue"
398 .buttonColor$ = "{0.5,0.5,1}"
400 .buttonColor$ = "Grey"
403 .buttonColor$ = "Maroon"
408 call '.buttonDraw$' '.buttonColor$' '.centerX' '.centerY' '.radius'
409 call set_font_size '.buttonFontSize'
410 demo Colour... '.buttonColor$'
413 .verticalAlignment$ = "Bottom"
415 .anchorY = .lowY + 0.5*(.highY-.lowY)
416 .verticalAlignment$ = "Half"
418 demo Text special... '.centerX' Centre '.anchorY' '.verticalAlignment$' 'defaultFont$' '.buttonFontSize' '.rotation' '.newText$'
422 call set_font_size 'defaultFontSize'
424 demo Line width... 'defaultLineWidth'
429 procedure set_window_title .table$ .addedText$
430 select Table '.table$'
431 .row = Search column... Label !WindowTitle
433 call emergency_table_exit Button Table '.table$' does not have a row with label !WindowTitle
435 .windowText$ = Get value... '.row' Text
436 call convert_praat_to_latin1 '.windowText$'
437 .windowText$ = convert_praat_to_latin1.text$
439 demoWindowTitle(.windowText$+ .addedText$)
442 # Handle language setting
443 procedure processLanguageCodes .table$ .label$
445 call Draw_button 'config$' Language_'config.language$' 0
446 call Draw_button 'config$' '.label$' 2
447 # Someone might have to use more than 2 chars for the language code
448 .numChars = length(.label$) - length("Language_")
449 .lang$ = right$(.label$, .numChars)
451 call set_language '.lang$'
455 procedure set_language .lang$
459 select Table 'buttons$'
464 select Table 'config$'
470 call checkTable 'buttonsTableName$'_'.lang$'
471 if checkTable.available
472 config.language$ = .lang$
474 config.language$ = "EN"
477 # Load buttons tables
478 call loadTable 'buttonsTableName$'
479 buttons$ = selected$("Table")
480 Append column... Text
482 Append column... Helptext
483 .numLabels = Get number of rows
484 call loadTable 'buttonsTableName$'_'config.language$'
485 .buttonsLang$ = selected$("Table")
486 for .row to .numLabels
487 select Table 'buttons$'
488 .label$ = Get value... '.row' Label
489 call findLabel '.buttonsLang$' '.label$'
491 select Table '.buttonsLang$'
492 .valueText$ = Get value... 'findLabel.row' Text
493 .valueKey$ = Get value... 'findLabel.row' Key
494 .valueHelp$ = Get value... 'findLabel.row' Helptext
495 select Table 'buttons$'
496 Set string value... '.row' Text '.valueText$'
497 Set string value... '.row' Key '.valueKey$'
498 Set string value... '.row' Helptext '.valueHelp$'
499 elsif index(.label$, "_")
500 # Load alternative language table
501 .startChar = rindex(.label$, "_")
502 .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
503 call loadTable 'buttonsTableName$'_'.otherLanguage$'
504 .otherbuttonsLang$ = selected$("Table")
505 call findLabel '.otherbuttonsLang$' '.label$'
507 select Table '.buttonsLang$'
508 .valueText$ = Get value... 'findLabel.row' Text
509 .valueKey$ = Get value... 'findLabel.row' Key
510 .valueHelp$ = Get value... 'findLabel.row' Helptext
511 select Table 'buttons$'
512 Set string value... '.row' Text '.valueText$'
513 Set string value... '.row' Key '.valueKey$'
514 Set string value... '.row' Helptext '.valueHelp$'
516 call emergency_table_exit Cannot find Label: '.otherbuttonsLang$' '.label$'
518 select Table '.otherbuttonsLang$'
521 call emergency_table_exit Cannot find Label: '.buttonsLang$' '.label$'
524 select Table '.buttonsLang$'
527 # Load configuration table
528 call loadTable 'configTableName$'
529 config$ = selected$("Table")
530 Append column... Text
532 Append column... Helptext
533 .numLabels = Get number of rows
534 call loadTable 'configTableName$'_'config.language$'
535 .configLang$ = selected$("Table")
536 for .row to .numLabels
537 select Table 'config$'
538 .label$ = Get value... '.row' Label
539 call findLabel '.configLang$' '.label$'
541 select Table '.configLang$'
542 .valueText$ = Get value... 'findLabel.row' Text
543 .valueKey$ = Get value... 'findLabel.row' Key
544 .valueHelp$ = Get value... 'findLabel.row' Helptext
545 select Table 'config$'
546 Set string value... '.row' Text '.valueText$'
547 Set string value... '.row' Key '.valueKey$'
548 Set string value... '.row' Helptext '.valueHelp$'
549 elsif index(.label$, "_")
550 .startChar = rindex(.label$, "_")
551 .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
552 call loadTable 'configTableName$'_'.otherLanguage$'
553 .otherconfigLang$ = selected$("Table")
554 call findLabel '.otherconfigLang$' '.label$'
556 select Table '.otherconfigLang$'
557 .valueText$ = Get value... 'findLabel.row' Text
558 .valueKey$ = Get value... 'findLabel.row' Key
559 .valueHelp$ = Get value... 'findLabel.row' Helptext
560 select Table 'config$'
561 Set string value... '.row' Text '.valueText$'
562 Set string value... '.row' Key '.valueKey$'
563 Set string value... '.row' Helptext '.valueHelp$'
565 call emergency_table_exit Cannot find Label: '.otherconfigLang$' '.label$'
567 select Table '.otherconfigLang$'
570 call emergency_table_exit Cannot find Label: '.configLang$' '.label$'
573 select Table '.configLang$'
576 # Make language change visible
578 call Draw_config_page
583 ###############################################################
585 # Button Drawing Routines
587 ###############################################################
589 # A stub for buttons that do not have a drawing routine (yet)
590 procedure DrawNull .color$ .x .y .size
593 procedure DrawHelp .color$ .x .y .size
594 .currentFontSize = 24
597 call adjustFontSizeOnHeight 'defaultFont$' '.currentFontSize' '.maxHeight'
598 .currentFontSize = adjustFontSizeOnHeight.currentFontSize
599 call set_font_size '.currentFontSize'
600 demo Colour... '.color$'
601 demo Text... '.x' Centre '.y' Bottom ?
602 call set_font_size 'defaultFontSize'
605 ###############################################################
607 # Button Processing Routines
609 ###############################################################
611 # Search row in table on label
612 procedure findKey .table$ .label$
614 select Table '.table$'
615 .to$ = selected$("Table")
617 .numRows = Get number of rows
619 .currentKey$ = '.to$'$[.i, "Key"]
620 if .label$ = .currentKey$
626 if .row <= 0 and index(.label$, "_") <= 0
627 printline "'.label$'" is not a key in '.table$'
631 procedure findLabel .table$ .label$
633 select Table '.table$'
634 .to$ = selected$("Table")
636 .numRows = Get number of rows
638 .currentKey$ = '.to$'$[.i, "Label"]
639 if .label$ = .currentKey$
645 if .row <= 0 and index(.label$, "_") <= 0
646 call emergency_table_exit "'.label$'" is not a key in '.table$'
651 procedure buttonClicked table$ .x .y
653 select Table 'table$'
654 .bo$ = selected$("Table")
656 .numRows = Get number of rows
659 .leftX = '.bo$'[.i, "LeftX"]
660 .rightX = '.bo$'[.i, "RightX"]
661 .lowY = '.bo$'[.i, "LowY"]
662 .highY = '.bo$'[.i, "HighY"]
663 if .x > .leftX and .x < .rightX and .y > .lowY and .y < .highY
664 .label$ = '.bo$'$[.i, "Label"]
670 procedure keyPressed table$ .pressed$
673 if .pressed$ = "" and not demoShiftKeyPressed()
676 .lowerPressed$ = replace_regex$(.pressed$, ".", "\L&", 0)
677 .upperPressed$ = replace_regex$(.pressed$, ".", "\U&", 0)
678 select Table 'table$'
679 .bo$ = selected$("Table")
681 .numRows = Get number of rows
684 .key$ = '.bo$'$[.i, "Key"]
685 if index(.key$, .lowerPressed$) or index(.key$, .upperPressed$)
686 .label$ = '.bo$'$[.i, "Label"]
693 procedure play_sound .sound$
695 select Sound '.sound$'
700 procedure record_sound
701 if recordedSound$ != ""
702 select Sound 'recordedSound$'
706 if sgc2.alignedTextGrid > 0
707 select sgc2.alignedTextGrid
709 sgc2.alignedTextGrid = -1
712 # There is a very nasty delay before the first recording starts, do a dummy record
713 if not variableExists("recordingInitialized")
714 noprogress nowarn Record Sound (fixed time)... 'config.input$' 0.99 0.5 'samplingFrequency' 0.1
716 recordingInitialized = 1
719 demo Paint circle... Red 5 95 2
721 # In Windows interaction between demoWaitForInput and Record Sound blocks drawing the feedback
722 if windows and endsWith(build_SHA$, " XP")
723 # Display a pause window to flush the graphics buffer
724 beginPause ("DESTROY WINDOW ")
726 endPause ("DESTROY WINDOW ", 1)
728 demo Paint circle... Red 5 95 2
731 noprogress nowarn Record Sound (fixed time)... 'config.input$' 0.99 0.5 'samplingFrequency' 'recordingTime'
732 demo Paint circle... White 5 95 2.5
733 call wipeArea 'wipeFeedbackArea$'
735 # Feedback on recording level
736 .extremum = Get absolute extremum... 0 0 None
737 .radius = 2 * .extremum
743 elsif .extremum >= 0.49
746 .green = .extremum / 0.5
748 .color$ = "{'.red','.green','.blue'}"
749 demo Colour... '.color$'
751 demo Draw circle... 5 95 '.radius'
755 demo Line width... 'defaultLineWidth'
759 Rename... Pronunciation
760 recordedSound$ = selected$("Sound")
763 select Sound 'recordedSound$'
765 # Cut out real sound from silences/noise
766 call sound_detection 'recordedSound$' 'soundMargin'
767 select Sound 'recordedSound$'
771 # Select real sound from recording
772 # Uses some global variable
773 procedure sound_detection .sound$ .margin
774 select Sound '.sound$'
775 .soundlength = Get total duration
776 .internalSilence = 2*.margin
778 # Silence and remove noise, DANGEROUS
779 To TextGrid (silences)... 'minimumPitch' 0 'noiseThresshold' '.internalSilence' 0.1 silent sounding
780 Rename... Input'.sound$'
782 select TextGrid Input'.sound$'
783 .numberofIntervals = Get number of intervals... 1
785 # Remove buzzing and other obnoxious sounds (if switched on)
786 for .i from 1 to .numberofIntervals
787 select TextGrid Input'.sound$'
788 .value$ = Get label of interval... 1 '.i'
789 .begintime = Get starting point... 1 '.i'
790 .endtime = Get end point... 1 '.i'
793 if .value$ = "silent"
794 select Sound '.sound$'
795 Set part to zero... '.begintime' '.endtime' at nearest zero crossing
799 # Select target sound
800 .maximumIntensity = -1
802 for i from 1 to .numberofIntervals
803 select TextGrid Input'.sound$'
805 .value$ = Get label of interval... 1 'i'
806 .begintime = Get starting point... 1 'i'
807 .endtime = Get end point... 1 'i'
809 if .value$ != "silent"
810 if .begintime > .margin
811 .begintime -= .margin
815 if .endtime + .margin < .soundlength
818 .endtime = .soundlength
821 select Sound '.sound$'
822 Extract part... '.begintime' '.endtime' Rectangular 1.0 no
823 Rename... Tmp'.sound$'
825 .newIntensity = Get intensity (dB)
826 if .newIntensity > .maximumIntensity
827 if .maximumIntensity > 0
828 select Sound New'.sound$'
831 select Sound Tmp'.sound$'
832 Rename... New'.sound$'
833 .maximumIntensity = .newIntensity
835 select Sound Tmp'.sound$'
841 if .maximumIntensity > minimumIntensity
842 select Sound '.sound$'
844 select Sound New'.sound$'
846 elsif .maximumIntensity > -1
847 select Sound New'.sound$'
850 select TextGrid Input'.sound$'
854 procedure end_program
855 call write_preferences ""
862 ######################################################
866 ######################################################
867 procedure config_page
869 demoWindowTitle("Speak Good Chinese: Change settings")
871 call Draw_config_page
873 while (.label$ <> "Return") and demoWaitForInput()
881 call buttonClicked 'config$' '.clickX' '.clickY'
882 .label$ = buttonClicked.label$
883 elsif demoKeyPressed()
884 .pressed$ = demoKey$()
885 call keyPressed 'config$' '.pressed$'
886 .label$ = keyPressed.label$
889 # You cannot select a text field
890 if startsWith(.label$, "$")
896 # Handle push button in process_config
897 call process_config '.label$' '.clickX' '.clickY' '.pressed$'
900 if .label$ = "Return"
910 procedure Draw_config_page
913 if config.showBackground
914 call draw_background Background
917 call Draw_all_buttons 'config$'
918 call set_window_title 'config$'
919 # Set correct buttons (alert)
920 call setConfigMainPage
924 procedure process_config .label$ .clickX .clickY .pressed$
925 if .label$ = "!Logging"
926 config.logPerformance = not config.logPerformance
927 .displayButton = config.logPerformance
928 call Draw_button 'config$' +'.label$' '.displayButton'
929 if config.logPerformance
932 elsif .label$ <> "" and not startsWith(.label$,"!")
933 .label$ = replace$(.label$, "_", " ", 0)
934 call process'config$''.label$' '.clickX' '.clickY' '.pressed$'
938 ###############################################################
940 # Presenting help texts
942 ###############################################################
945 procedure help_loop .table$ .redrawProc$
947 call write_help_title '.table$'
950 call Draw_button '.table$' Help 2
952 while (.label$ <> "Help") and demoWaitForInput()
957 call buttonClicked '.table$' '.clickX' '.clickY'
958 .label$ = buttonClicked.label$
959 elsif demoKeyPressed()
960 .pressed$ = demoKey$()
961 call keyPressed '.table$' '.pressed$'
962 .label$ = keyPressed.label$
965 if .label$ != "" and .label$ <> "Help"
973 call Draw_button '.table$' Help 2
974 call write_help_title '.table$'
976 # Handle push button in process_config
977 call write_help_text '.table$' '.label$'
983 call Draw_button '.table$' Help 0
989 procedure write_help_text .table$ .label$
990 call findLabel '.table$' '.label$'
992 select Table '.table$'
995 call findLabel '.table$' Help
997 select Table '.table$'
999 .helpText$ = Get value... '.row' Helptext
1000 .helpKey$ = Get value... '.row' Key
1001 .helpKey$ = replace$(.helpKey$, "\", "", 0)
1002 .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
1003 if index_regex(.helpKey$, "\S")
1004 .helpText$ = .helpText$+" ("+.helpKey$+")"
1007 .leftX = Get value... '.row' LeftX
1008 .rightX = Get value... '.row' RightX
1009 .lowY = Get value... '.row' LowY
1010 .highY = Get value... '.row' HighY
1013 .currentHelpFontSize = defaultFontSize
1014 call set_font_size '.currentHelpFontSize'
1015 .helpTextSize = demo Text width (wc)... '.helpText$'
1019 .htXright = .htXleft + .helpTextSize + 5
1023 .htXleft = .htXright - .helpTextSize - 5
1028 .htYhigh = .htYlow + 7
1033 .htYlow = .htYhigh - 7
1038 # Adapt font size to horizontal dimensions
1040 call adjustFontSizeOnWidth 'defaultFont$' '.currentHelpFontSize' '.maxWidth' '.helpText$'
1041 .currentHelpFontSize = adjustFontSizeOnWidth.newFontSize
1042 if .htXleft < 0 or .htXright > 100
1044 .htXright = .htXleft + adjustFontSizeOnWidth.textWidth + 5
1046 call set_font_size '.currentHelpFontSize'
1048 # Adapt vertical dimensions to font height
1049 call points_to_wc '.currentHelpFontSize'
1050 .lineHeight = points_to_wc.wc
1051 if .lineHeight > .htYhigh - .htYlow - 4
1052 .htYhigh = .htYlow + .lineHeight + 4
1055 # Determine arrow endpoints
1057 if abs(.htXleft - .xstart) > abs(.htXright - .xstart)
1060 if abs((.htXleft+.htXright)/2 - .xstart) < min(abs(.htXright - .xstart),abs(.htXleft - .xstart))
1061 .xend = (.htXleft+.htXright)/2
1064 .xtext = .htXleft + 2
1065 .ytext = .htYlow + 1
1068 .mm2wc = demo Horizontal mm to wc... 1
1069 .lineWidth = 2/.mm2wc
1070 demo Line width... '.lineWidth'
1071 demo Arrow size... '.lineWidth'
1072 demo Colour... 'sgc2.popUp_bordercolor$'
1073 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.htXleft' '.htXright' '.htYlow' '.htYhigh'
1074 demo Draw rectangle... '.htXleft' '.htXright' '.htYlow' '.htYhigh'
1075 demo Draw arrow... '.xstart' '.ystart' '.xend' '.yend'
1076 demo Line width... 'defaultLineWidth'
1077 demo Arrow size... 1
1079 demo Text... '.xtext' Left '.ytext' Bottom '.helpText$'
1081 call set_font_size 'defaultFontSize'
1085 procedure write_help_title .table$
1086 # Set help text title
1088 call findLabel '.table$' Help
1089 .row = findLabel.row
1090 select Table '.table$'
1091 .helpTitle$ = Get value... '.row' Helptext
1092 .helpKey$ = Get value... '.row' Key
1093 .helpKey$ = replace$(.helpKey$, "\", "", 0)
1094 .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
1095 .helpTitle$ = .helpTitle$+" ("+.helpKey$+")"
1098 .helpTitleFontSize = 14
1099 # Adapt size of button to length of text
1101 call adjustFontSizeOnWidth 'defaultFont$' '.helpTitleFontSize' '.maxWidth' '.helpTitle$'
1102 .helpTitleFontSize = adjustFontSizeOnWidth.newFontSize
1103 call set_font_size '.helpTitleFontSize'
1106 demo Select inner viewport... 0 100 0 100
1107 demo Axes... 0 100 0 100
1108 demo Text... 50 Centre '.helpTop' Top '.helpTitle$'
1109 call set_font_size 'defaultFontSize'
1113 ###############################################################
1115 # Miscelaneous procedures
1117 ###############################################################
1118 procedure printPageToPrinter
1120 demo Print... 'printerName$' 'printerPresets$'
1124 procedure getOpenFile .openDialogue$
1125 call convert_praat_to_latin1 '.openDialogue$'
1126 .openDialogue$ = convert_praat_to_latin1.text$
1127 .filename$ = chooseReadFile$ (.openDialogue$)
1128 if .filename$ <> "" and fileReadable(.filename$)
1129 Read from file... '.filename$'
1130 call log_fileOpen '.filename$'
1132 # Get only the filename
1133 .startName = rindex_regex(.filename$, "[/\\:]") + 1
1134 .nameLength = rindex(.filename$, ".") - .startName
1135 currentSoundName$ = mid$(.filename$, .startName, .nameLength)
1137 Create Sound from formula... Speech Mono 0 1 44100 0
1139 recordedSound$ = selected$("Sound")
1140 currentStartTime = 0
1141 currentEndTime = Get total duration
1142 # Reset selected window
1143 selectedStartTime = currentStartTime
1144 selectedEndTime = currentEndTime
1147 procedure points_to_wc .points
1148 .mm = .points * 0.3527777778
1149 .wc = demo Vertical mm to wc... '.mm'
1152 procedure reset_viewport
1153 .low = viewportMargin
1154 .high = 100 - viewportMargin
1155 demo Select inner viewport... '.low' '.high' '.low' '.high'
1156 demo Axes... 0 100 0 100
1159 procedure set_font_size .fontSize
1161 demo Font size... '.fontSize'
1165 procedure wipeArea .areaCommand$
1170 procedure adjustFontSizeOnWidth .font$ .currentFontSize .maxWidth .text$
1172 call set_font_size '.currentFontSize'
1173 .textWidth = demo Text width (wc)... '.text$'
1174 while .textWidth > .maxWidth and .currentFontSize > 2
1175 .currentFontSize -= 0.5
1176 call set_font_size '.currentFontSize'
1177 .textWidth = demo Text width (wc)... '.text$'
1179 .diff = .textWidth - .maxWidth
1180 .newFontSize = .currentFontSize
1184 procedure adjustRotatedFontSizeOnBox .font$ .currentFontSize .maxWidth .maxHeight .rotation .text$
1186 .radians = .rotation/360 * 2 * pi
1187 .horWC = demo Horizontal mm to wc... 10.0
1188 .verWC = demo Vertical mm to wc... 10.0
1190 .verCoeff = .verWC / .horWC
1194 call set_font_size '.currentFontSize'
1195 .textLength = demo Text width (wc)... '.text$'
1196 while (.textLength * .verCoeff * sin(.radians) > .maxHeight or .textLength * cos(.radians) > .maxWidth) and .currentFontSize > 2
1197 .currentFontSize -= 0.5
1198 call set_font_size '.currentFontSize'
1199 .textLength = demo Text width (wc)... '.text$'
1201 .diff = .textLength - .maxHeight
1202 .newFontSize = .currentFontSize
1206 procedure adjustFontSizeOnHeight .font$ .currentFontSize .maxHeight
1208 call points_to_wc '.currentFontSize'
1209 .lineHeight = points_to_wc.wc
1210 while .lineHeight > .maxHeight and .currentFontSize > 2
1211 .currentFontSize -= 0.5
1212 call points_to_wc '.currentFontSize'
1213 .lineHeight = points_to_wc.wc
1215 .diff = .lineHeight - .maxHeight
1216 .newFontSize = .currentFontSize
1220 # Load a table with button info etc.
1221 # Load local tables if present. Else load
1222 # build-in scripted tables
1223 procedure loadTable .tableName$
1224 .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
1225 # Search for the table in local, preference, and global directories
1226 if fileReadable("'localTableDir$'/'.tableName$'.Table")
1227 Read from file... 'localTableDir$'/'.tableName$'.Table
1228 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1229 Read from file... 'preferencesTableDir$'/'.tableName$'.Table
1230 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1231 Read from file... 'globaltablelists$'/'.tableName$'.Table
1232 # Load them from script
1233 elsif variableExists("procCreate'.tableVariableName$'$")
1234 call Create'.tableVariableName$'
1236 call write_text_popup Helvetica 14 '.tableName$' cannot be found
1238 exit '.tableName$' cannot be found
1242 procedure testLoadTable .tableName$
1244 .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
1245 # Search for the table in local, preference, and global directories
1246 if fileReadable("'localTableDir$'/'.tableName$'.Table")
1248 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1250 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1252 # Load them from script
1253 elsif variableExists("procCreate'.tableVariableName$'$")
1260 procedure checkTable .tableName$
1262 if fileReadable("'localTableDir$'/'.tableName$'.Table")
1264 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1266 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1268 # Load them from script
1269 elsif variableExists("procCreate'.tableName$'$")
1276 # Create a pop-up window with text from a Text Table
1277 procedure write_text_table .table$
1284 # Get table with text and longest line
1286 call testLoadTable '.table$'
1287 if testLoadTable.table > 0
1288 call loadTable '.table$'
1289 .instructionText = selected()
1290 .numLines = Get number of rows
1292 .instructionFontSize = 14
1293 .referenceText$ = ""
1299 select '.instructionText'
1300 .currentText$ = Get value... '.l' text
1301 # Expand variables, eg, 'praatVersion$'
1302 call expand_praat_variables '.currentText$'
1303 .currentText$ = expand_praat_variables.text$
1305 .font$ = Get value... '.l' font
1306 .fontSize = Get value... '.l' size
1307 call set_font_size '.fontSize'
1308 .textWidth = demo Text width (wc)... '.currentText$'
1309 if .fontSize > .maxFontSize
1310 .maxFontSize = .fontSize
1312 if .textWidth > .maxWidth
1313 .maxWidth = .textWidth
1314 .instructionFontSize = .fontSize
1318 select '.instructionText'
1319 .referenceText$ = Get value... '.maxLine' text
1320 .maxLineFont$ = Get value... '.maxLine' font
1321 .instructionFontSize = Get value... '.maxLine' size
1322 call set_font_size '.maxFontSize'
1324 # Adapt size of button to length of text
1325 .maxWidth = (.xright - .xleft) - 4
1326 .origFontSize = .instructionFontSize
1327 call adjustFontSizeOnWidth 'defaultFont$' '.instructionFontSize' '.maxWidth' '.referenceText$'
1328 call adjustFontSizeOnHeight 'defaultFont$' '.maxFontSize' '.lineHeight'
1329 .instructionFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1330 if adjustFontSizeOnWidth.diff > 0
1331 .xright += adjustFontSizeOnWidth.diff/4
1332 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1334 call set_font_size '.instructionFontSize'
1335 .fontSizeFactor = .instructionFontSize / .origFontSize
1337 .numRows = Get number of rows
1338 # Calculate length from number of lines.
1340 .midY = .yhigh - (.yhigh - .ylow)/2
1341 .yhigh = .midY + (.numRows+1) * .dy / 2
1342 .ylow = .yhigh - (.numRows+1) * .dy
1343 .textleft = .xleft + 2
1345 demo Line width... 8
1346 demo Colour... 'sgc2.popUp_bordercolor$'
1347 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1348 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1349 demo Line width... 'defaultLineWidth'
1351 .ytext = .yhigh - 2 - .dy
1353 select '.instructionText'
1354 .font$ = Get value... '.i' font
1355 .fontSize = Get value... '.i' size
1356 .font$ = extractWord$(.font$, "")
1358 .fontSize = floor(.fontSize*.fontSizeFactor)
1362 .line$ = Get value... '.i' text
1363 # Expand variables, eg, 'praatVersion$'
1364 call expand_praat_variables '.line$'
1365 .line$ = expand_praat_variables.text$
1368 demo Text special... '.textleft' Left '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1372 call set_font_size 'defaultFontSize'
1374 select '.instructionText'
1377 label ESCAPEwrite_text_table
1381 # Create a pop-up window with text from an existing Table object
1382 procedure write_tabbed_table .table$ .labelTextTable$
1389 # Get table with text and longest line
1390 call testLoadTable '.table$'
1391 if testLoadTable.table <= 0
1392 call loadTable '.labelTextTable$'
1393 .labelText$ = selected$("Table")
1396 select Table '.table$'
1397 .tabbedText = selected()
1398 .numLines = Get number of rows
1399 .numCols = Get number of columns
1400 .font$ = defaultFont$
1401 .fontSize = defaultFontSize
1403 .widthCanvas = .xright - .xleft
1404 .dx = (.widthCanvas - 4) / (.numCols)
1408 call set_font_size '.fontSize'
1410 for .i from 0 to .numLines
1411 .xtext = .xleft + .dx / 2
1413 select '.tabbedText'
1414 .currentLabel$ = Get column label... '.j'
1416 .line$ = Get value... '.i' '.currentLabel$'
1418 .line$ = .currentLabel$
1419 select Table '.labelText$'
1420 call findLabel '.labelText$' '.line$'
1421 select Table '.labelText$'
1422 .line$ = Get value... 'findLabel.row' Text
1424 # Expand variables, eg, 'praatVersion$'
1425 call expand_praat_variables '.line$'
1426 .line$ = expand_praat_variables.text$
1427 .textWidth = demo Text width (wc)... '.line$'
1428 if .textWidth > .maxWidth
1429 .maxWidth = .textWidth
1433 if .dx > 1.2 * .maxWidth
1434 .widthCanvas = 1.2 * .maxWidth * .numCols + 4
1435 .xleft = 50 - .widthCanvas / 2
1436 .xright = 50 + .widthCanvas / 2
1437 .dx = (.widthCanvas - 4) / (.numCols)
1442 # Calculate length from number of lines.
1443 .dy = .lineHeight + 0.5
1444 .midY = .yhigh - (.yhigh - .ylow)/2
1445 .yhigh = .midY + (.numLines+2) * .dy / 2
1446 .ylow = .yhigh - (.numLines+2) * .dy
1447 .textleft = .xleft + 2
1449 demo Line width... 8
1450 demo Colour... 'sgc2.popUp_bordercolor$'
1451 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1452 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1453 demo Line width... 'defaultLineWidth'
1455 .ytext = .yhigh - 2 - .dy
1456 # First the column names, then the items
1457 for .i from 0 to .numLines
1458 .xtext = .textleft + .dx / 2
1460 select '.tabbedText'
1461 .currentLabel$ = Get column label... '.j'
1463 .line$ = Get value... '.i' '.currentLabel$'
1465 .line$ = .currentLabel$
1466 select Table '.labelText$'
1467 call findLabel '.labelText$' '.line$'
1468 select Table '.labelText$'
1469 .line$ = Get value... 'findLabel.row' Text
1471 # Expand variables, eg, 'praatVersion$'
1472 call expand_praat_variables '.line$'
1473 .line$ = expand_praat_variables.text$
1474 call adjustFontSizeOnWidth '.font$' '.fontSize' '.maxWidth' '.line$'
1475 .currentFontSize = adjustFontSizeOnWidth.newFontSize
1478 demo Text special... '.xtext' Centre '.ytext' Bottom '.font$' '.currentFontSize' 0 '.line$'
1484 call set_font_size 'defaultFontSize'
1485 select Table '.labelText$'
1488 label ESCAPEwrite_tabbed_table
1491 # Create a pop-up window with a given text
1492 procedure write_text_popup .font$ .size .text$
1499 # Adapt size of button to length of text
1500 .maxWidth = (.xright - .xleft) - 4
1501 call adjustFontSizeOnWidth 'defaultFont$' '.size' '.maxWidth' '.text$'
1502 call adjustFontSizeOnHeight 'defaultFont$' '.size' '.lineHeight'
1503 .popupFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1504 if adjustFontSizeOnWidth.diff > 0
1505 .xright += adjustFontSizeOnWidth.diff/4
1506 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1508 .xleft = ((.xright + .xleft) - adjustFontSizeOnWidth.textWidth)/2 - 2
1509 .xright = ((.xright + .xleft) + adjustFontSizeOnWidth.textWidth)/2 + 2
1513 # Calculate length from number of lines.
1515 .midY = .yhigh - (.yhigh - .ylow)/2
1516 .yhigh = .midY + (.numRows+1) * .dy / 2
1517 .ylow = .yhigh - (.numRows+1) * .dy
1518 .textleft = .xleft + 2
1519 .xmid = (.textleft + .xright - 2)/2
1521 demo Line width... 8
1522 demo Colour... 'sgc2.popUp_bordercolor$'
1523 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1524 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1525 demo Line width... 'defaultLineWidth'
1527 .ytext = .yhigh - 2 - .dy
1529 demo Text special... '.xmid' Centre '.ytext' Bottom '.font$' '.popupFontSize' 0 '.text$'
1533 call set_font_size 'defaultFontSize'
1536 # Write the background from a Text Table
1537 procedure draw_background .table$
1543 .defaultColour$ = "{0.9,0.9,0.9}"
1544 .defaultAlign$ = "centre"
1546 # Get table with text and longest line
1547 call loadTable '.table$'
1548 .backgroundText = selected()
1549 .numLines = Get number of rows
1550 .backgroundFontSize = 28
1551 .referenceText$ = ""
1558 select '.backgroundText'
1559 .currentText$ = Get value... '.l' text
1560 # Expand variables, eg, 'praatVersion$'
1561 call expand_praat_variables '.currentText$'
1562 .currentText$ = expand_praat_variables.text$
1564 .font$ = Get value... '.l' font
1565 .fontSize = Get value... '.l' size
1566 if .fontSize > .maxFontSize
1567 .maxFontSize = .fontSize
1569 if not startsWith(.font$, "!")
1570 call set_font_size '.fontSize'
1571 .textWidth = demo Text width (wc)... '.currentText$'
1572 if .textWidth > .maxWidth
1573 .maxWidth = .textWidth
1574 .backgroundFontSize = .fontSize
1582 select '.backgroundText'
1583 .referenceText$ = Get value... '.maxLine' text
1584 .maxLineFont$ = Get value... '.maxLine' font
1585 .backgroundFontSize = Get value... '.maxLine' size
1586 .backgroundFontColour$ = Get value... '.maxLine' colour
1587 call set_font_size '.maxFontSize'
1589 .maxFontSize = .backgroundFontSize
1592 # Adapt size of button to length of text
1593 .maxWidth = (.xright - .xleft) - 4
1594 .origFontSize = .backgroundFontSize
1595 call adjustFontSizeOnWidth 'defaultFont$' '.backgroundFontSize' '.maxWidth' '.referenceText$'
1596 .fontSizeFactor = adjustFontSizeOnWidth.newFontSize / .backgroundFontSize
1597 .backgroundFontSize = adjustFontSizeOnWidth.newFontSize
1598 call set_font_size '.backgroundFontSize'
1600 call adjustFontSizeOnHeight 'defaultFont$' '.backgroundFontSize' '.lineHeight'
1601 .lineHeight /= adjustFontSizeOnHeight.newFontSize / .backgroundFontSize
1602 if adjustFontSizeOnHeight.newFontSize >= .origFontSize and (.textLines+1) * .lineHeight > (.yhigh - .ylow - 4)
1603 .lineHeight = (.yhigh - .ylow - 4)/(.textLines + 1)
1604 call adjustFontSizeOnHeight 'defaultFont$' '.maxFontSize' '.lineHeight'
1605 .fontSizeFactor = adjustFontSizeOnHeight.newFontSize / .backgroundFontSize
1608 .numRows = Get number of rows
1609 # Calculate length from number of lines.
1611 .midY = .yhigh - (.yhigh - .ylow)/2
1612 .yhigh = .midY + (.textLines+1) * .dy / 2
1613 .ylow = .yhigh - (.textLines+1) * .dy
1614 .textleft = .xleft + 2
1615 .textright = .xright - 2
1616 .textmid = (.xright - .xleft)/2
1619 .ytext = .yhigh - 2 - .dy
1621 select '.backgroundText'
1622 .font$ = Get value... '.i' font
1623 .fontSize = Get value... '.i' size
1624 .fontColour$ = Get value... '.i' colour
1625 .fontColour$ = replace_regex$(.fontColour$, "^[\- ]$", ".defaultColour$", 1)
1626 .fontAlign$ = Get value... '.i' align
1627 .fontAlign$ = replace_regex$(.fontAlign$, "^[\- ]$", ".defaultAlign$", 1)
1628 .line$ = Get value... '.i' text
1629 # Expand variables, eg, 'praatVersion$'
1630 call expand_praat_variables '.line$'
1631 .line$ = expand_praat_variables.text$
1634 .fontSize = floor(.fontSize*.fontSizeFactor)
1635 if not startsWith(.font$, "!")
1636 .font$ = extractWord$(.font$, "")
1638 if .fontAlign$ = "centre"
1640 elsif .fontAlign$ = "right"
1649 demo Colour... '.fontColour$'
1650 demo Text special... '.xtext' '.fontAlign$' '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1652 elsif .font$ = "!demo command"
1653 demo Colour... '.fontColour$'
1654 .line$ = replace_regex$(.line$, "\{FONTSIZE\$\}", "'.fontSize'", 0)
1655 .line$ = replace_regex$(.line$, "\{XTEXT\$\}", "'.xtext'", 0)
1656 .line$ = replace_regex$(.line$, "\{YTEXT\$\}", "'.ytext'", 0)
1657 .line$ = replace_regex$(.line$, "\{DY\$\}", "'.dy'", 0)
1658 .line$ = replace_regex$(.line$, "\{[^\}]*\}", "", 0)
1659 while index(.line$, "[[")
1660 .nextBracketOpen = index(.line$, "[[")
1661 .nextBracketOpen += 2
1662 .nextBracketClose = index(.line$, "]]")
1663 .bracketLength = .nextBracketClose - .nextBracketOpen
1665 if .bracketLength > 0
1666 .expression$ = mid$(.line$, .nextBracketOpen, .bracketLength)
1667 .expression$ = replace_regex$(.expression$, "\s", "", 0)
1668 if length(.expression$) > 0
1669 # Test expression for security, only allow explicitely defined functions
1670 .allowedStrings$ = "e|pi|not|and|or|div|mod|abs|round|floor|ceiling"
1671 .allowedStrings$ = .allowedStrings$ + "|sqrt|min|max|imin|imax|sin|cos|tan|arcsin|arccos|arctan|arctan2|sinc|sincpi"
1672 .allowedStrings$ = .allowedStrings$ + "|exp|ln|log10|log2|sinh|cosh|tanh|arcsinh|arccosh|arctanh"
1673 .allowedStrings$ = .allowedStrings$ + "|sigmoid|invSigmoid|erf|erfc|randomUniform|randomInteger|randomGauss|randomPoisson"
1674 .allowedStrings$ = .allowedStrings$ + "|lnGamma|gaussP|gaussQ|invGaussQ|chiSquareP|chiSquareQ"
1675 .allowedStrings$ = .allowedStrings$ + "|invChiSquareP|invChiSquareQ|studentP|studentQ|invStudentP|invStudentQ"
1676 .allowedStrings$ = .allowedStrings$ + "|beta|besselI|besselK"
1677 .testExpression$ = replace_regex$(.expression$, "(^|\W)('.allowedStrings$')(?=$|\W)", "\1\3", 0)
1678 .testExpression$ = replace_regex$(.testExpression$, "[0-9\.,\-+/*^()<>= ]", "", 0)
1679 if .testExpression$ = ""
1680 .calc = '.expression$'
1681 .result$ = "'.calc'"
1686 # Replace expression by result
1687 .lastLeft = .nextBracketOpen - 3
1688 .newLine$ = left$(.line$, .lastLeft)
1689 .newLine$ = .newLine$ + .result$
1690 .numCopy = length(.line$) - .nextBracketClose - 1
1691 .newLine$ = .newLine$ + right$(.line$, .numCopy)
1699 call set_font_size 'defaultFontSize'
1701 select '.backgroundText'
1705 procedure convert_praat_to_utf8 .text$
1706 .text$ = replace_regex$(.text$, "\\a""", "\xc3\xa4", 0)
1707 .text$ = replace_regex$(.text$, "\\A""", "\xc3\x84", 0)
1708 .text$ = replace_regex$(.text$, "\\o""", "\xc3\xb6", 0)
1709 .text$ = replace_regex$(.text$, "\\O""", "\xc3\x96", 0)
1710 .text$ = replace_regex$(.text$, "\\u""", "\xc3\xbc", 0)
1711 .text$ = replace_regex$(.text$, "\\U""", "\xc3\x9c", 0)
1712 .text$ = replace_regex$(.text$, "\\i""", "\xc3\xaf", 0)
1713 .text$ = replace_regex$(.text$, "\\I""", "\xc3\x8f", 0)
1714 .text$ = replace_regex$(.text$, "\\e""", "\xc3\xab", 0)
1715 .text$ = replace_regex$(.text$, "\\E""", "\xc3\x8b", 0)
1716 .text$ = replace_regex$(.text$, "\\y""", "\xc3\xbf", 0)
1717 .text$ = replace_regex$(.text$, "\\e'", "\xc3\xa9", 0)
1718 .text$ = replace_regex$(.text$, "\\E'", "\xc3\x89", 0)
1719 .text$ = replace_regex$(.text$, "\\ss", "\xc3\x9f", 0)
1722 procedure convert_praat_to_latin1 .text$
1723 .text$ = replace_regex$(.text$, "\\a""", "\xe4", 0)
1724 .text$ = replace_regex$(.text$, "\\A""", "\xc4", 0)
1725 .text$ = replace_regex$(.text$, "\\o""", "\xf6", 0)
1726 .text$ = replace_regex$(.text$, "\\O""", "\xd6", 0)
1727 .text$ = replace_regex$(.text$, "\\u""", "\xfc", 0)
1728 .text$ = replace_regex$(.text$, "\\U""", "\xdc", 0)
1729 .text$ = replace_regex$(.text$, "\\i""", "\xef", 0)
1730 .text$ = replace_regex$(.text$, "\\I""", "\xcf", 0)
1731 .text$ = replace_regex$(.text$, "\\e""", "\xeb", 0)
1732 .text$ = replace_regex$(.text$, "\\E""", "\xcb", 0)
1733 .text$ = replace_regex$(.text$, "\\y""", "\xff", 0)
1734 .text$ = replace_regex$(.text$, "\\Y""", "Y", 0)
1735 .text$ = replace_regex$(.text$, "\\e'", "\xe9", 0)
1736 .text$ = replace_regex$(.text$, "\\E'", "\xc9", 0)
1737 .text$ = replace_regex$(.text$, "\\ss", "\xdf", 0)
1740 # Expand 'variable$' into the value of variable$.
1741 # Eg, 'praatVersion$' becomes 5.1.45 or whatever is the current version
1742 # Single quotes can be protected by \'
1743 procedure expand_praat_variables .text$
1744 if index(.text$, "'")
1745 .tempText$ = replace_regex$(.text$, "(^|[^\\])'([\w\$\.]+)'", "\1""+\2+""", 0)
1746 .tempText$ = replace_regex$(.tempText$, "[\\]'", "'", 0)
1747 .tempText$ = """"+.tempText$+""""
1748 # Check whether all the variables actually exist. Ignore any variable that does not exist
1749 .checkVars$ = .tempText$
1750 while length(.checkVars$) > 0 and index(.checkVars$, "+")
1751 .start = index(.checkVars$, "+")
1752 .checkVars$ = right$(.checkVars$, length(.checkVars$) - .start)
1753 .end = index(.checkVars$, "+")
1755 .variable$ = left$(.checkVars$, .end - 1)
1756 if not variableExists(.variable$)
1757 .tempText$ = replace$(.tempText$, """+'.variable$'+""", "'"+.variable$+"'", 0)
1759 .checkVars$ = right$(.checkVars$, length(.checkVars$) - .end)
1764 .text$ = '.tempText$'
1768 # Get a time stamp in normalized format
1769 procedure getTimeStamp
1770 .currentDateTime$ = date$()
1771 .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)
1774 # A table error, can be insiduously caused by an outdate preferences file!
1775 procedure emergency_table_exit .message$
1776 # If you come here as a user, your preferences file is borked
1777 if preferencesAppFile$ <> "" and fileReadable(preferencesAppFile$)
1778 deleteFile(preferencesAppFile$)