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"
45 te.buttonPressValue = 0
46 samplingFrequency = 44100
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
59 preferencesAppDir$ = "'preferencesDirectory$'/../.'sgc2.demoAppName$'"
61 preferencesAppDir$ = "'preferencesDirectory$'/../'sgc2.demoAppName$'"
64 # It has to go somewhere. Make a subdirectory in the current preferences directory
65 preferencesAppDir$ = "'preferencesDirectory$'/'sgc2.demoAppName$'"
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
77 # Set up button height
83 defaultFont$ = "Helvetica"
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
100 # Start instruction loop
101 while demoWaitForInput()
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$
117 # You cannot select a text field
118 if startsWith(.label$, "$")
124 te.buttonPressValue = 0
126 call Draw_button 'buttons$' '.label$' 1
127 call process_label '.label$' '.clickX' '.clickY' '.pressed$'
129 call Draw_button 'buttons$' '.label$' 'te.buttonPressValue'
136 ########################################################
138 # Definitions of procedures
140 ########################################################
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$'
152 procedure init_buttons
154 call Draw_all_buttons 'buttons$'
160 procedure Draw_all_buttons .table$
161 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
162 select Table '.table$'
163 .numRows = Get number of rows
166 .label$ = Get value... '.row' Label
167 if not startsWith(.label$, "!")
169 # Determine the "pressed" state of a button
170 # A variable with the same name as the button will act as a
172 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
173 # Simple boolean variables
174 if index(.variableName$, "_") <= 0 and variableExists(.variableName$)
176 if '.variableName$' > 0
179 elsif '.variableName$' < 0
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$
188 if variableExists(.generalVar$+"$")
189 .currentVariableName$ = .generalVar$ + "$"
191 # Remove one level of indirection
192 if variableExists(.currentVariableName$)
193 if index(.currentVariableName$, "$")
194 .currentVariableName$ = '.currentVariableName$'
196 .currentValue = '.currentVariableName$'
197 .currentVariableName$ = "'.currentValue'"
199 # Remove next level of indirection
200 .currentContent$ = "'.currentVariableName$'"
201 if .currentContent$ = "_DISABLED_"
204 # Reconstruct label from current values
205 .currentLabelValue$ = .generalVar$ + "_" + .currentContent$
206 # Set PRESSED from label
207 if .variableName$ = .currentLabelValue$
212 # You did erase everything before you started here? So do not do that again
213 call Draw_button_internal 0 '.table$' '.label$' '.pressed'
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'
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
228 .mm = demo Horizontal wc to mm... '.wc'
229 # Allow to overide ! skip directive
231 if startsWith(.label$, "+")
232 .label$ = right$(.label$, length(.label$)-1)
236 select Table '.table$'
237 .row = Search column... Label '.label$'
239 call emergency_table_exit Button Table '.table$' does not have a row with label '.label$'
242 # Perspective shift sizes
247 .shiftDown = 0.1*buttonbevel
248 .shiftX = -0.2*buttonbevel
249 .shiftY = 0.40*buttonbevel
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
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
280 .noDraw = startsWith(.label$, "!") or (.leftX < 0) or (.rightX < 0) or (.lowY < 0) or (.highY < 0)
283 if index_regex(.buttonText$, "^![0-9\.]+!")
284 .rotation = extractNumber(.buttonText$, "!")
285 .buttonText$ = replace_regex$(.buttonText$, "^![0-9\.]+!", "", 0)
288 goto NOBUTTON .noDraw and not .forceDraw
290 # Replace button text with ALERT
292 .buttonText$ = alertText$
295 # Adapt font size to button size
296 .maxWidth = (.rightX - .leftX) - 2
297 .maxHeight = (.highY - .lowY) - 1
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
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
314 .buttonFontSize = adjustFontSizeOnHeight.newFontSize
316 # With non-horizontal text, only change font size
317 call adjustRotatedFontSizeOnBox 'defaultFont$' '.buttonFontSize' '.maxWidth' '.maxHeight' '.rotation' '.buttonText$'
318 .buttonFontSize = adjustRotatedFontSizeOnBox.newFontSize
321 # Reset and erase button area
323 demo Line width... 'defaultLineWidth'
325 .shiftRightX = .rightX - .shiftX
326 .shiftLowY = .lowY - .shiftY
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
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
342 demo Paint rounded rectangle... '.flankBackGroundColorUp$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
343 demo Colour... '.flankLineColorUp$'
344 demo Line width... '.flankLineWidthUp'
346 demo Paint rounded rectangle... '.flankBackGroundColorDown$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
347 demo Colour... '.flankLineColorDown$'
348 demo Line width... '.flankLineWidthDown'
350 demo Draw rounded rectangle... .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
353 # Button Down will shift the top perspective
355 # Draw the button top
357 demo Paint rounded rectangle... '.topBackGroundColorUp$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
358 demo Colour... '.topLineColorUp$'
359 demo Line width... '.topLineWidthUp'
361 demo Paint rounded rectangle... '.topBackGroundColorDisabled$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
362 demo Colour... '.topLineColorDisabled$'
363 demo Line width... '.topLineWidthDisabled'
367 .rightX += .shiftDown
371 demo Paint rounded rectangle... '.topBackGroundColorDown$' .leftX .rightX .lowY .highY '.mm'
372 demo Colour... '.topLineColorDown$'
373 demo Line width... '.topLineWidthDown'
375 demo Draw rounded rectangle... '.leftX' '.rightX' '.lowY' '.highY' '.mm'
378 # The button text and symbol
379 .horWC = demo Horizontal mm to wc... 10.0
380 .verWC = demo Vertical mm to wc... 10.0
382 .verCoeff = .horWC / .verWC
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)
394 .newText$ = .buttonText$
396 # Variable text field, read corresponding variable
398 .fieldName$ = replace_regex$(.label$, "^[!$]", "", 0)
399 printline ['.fieldName$'] ['.label$']
400 .fieldName$ = replace_regex$(.fieldName$, "^(.)", "\l\1", 0)
401 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
402 .newText$ = '.varPrefix$'.'.fieldName$'$
404 if .push = 1 or .push = -1
406 if .buttonColor$ = "Red"
407 .buttonColor$ = "Pink"
408 elsif .buttonColor$ = "Blue"
409 .buttonColor$ = "{0.5,0.5,1}"
411 .buttonColor$ = "Grey"
414 .buttonColor$ = "Maroon"
419 call '.buttonDraw$' '.buttonColor$' '.centerX' '.centerY' '.radius'
420 call set_font_size '.buttonFontSize'
421 demo Colour... '.buttonColor$'
424 .verticalAlignment$ = "Bottom"
426 .anchorY = .lowY + 0.5*(.highY-.lowY)
427 .verticalAlignment$ = "Half"
429 demo Text special... '.centerX' Centre '.anchorY' '.verticalAlignment$' 'defaultFont$' '.buttonFontSize' '.rotation' '.newText$'
433 call set_font_size 'defaultFontSize'
435 demo Line width... 'defaultLineWidth'
440 procedure set_window_title .table$ .addedText$
441 select Table '.table$'
442 .row = Search column... Label !WindowTitle
444 call emergency_table_exit Button Table '.table$' does not have a row with label !WindowTitle
446 .windowText$ = Get value... '.row' Text
447 call convert_praat_to_latin1 '.windowText$'
448 .windowText$ = convert_praat_to_latin1.text$
450 demoWindowTitle(.windowText$+ .addedText$)
453 # Handle language setting
454 procedure processLanguageCodes .table$ .label$
456 call Draw_button 'config$' Language_'config.language$' 0
457 call Draw_button 'config$' '.label$' 2
458 # Someone might have to use more than 2 chars for the language code
459 .numChars = length(.label$) - length("Language_")
460 .lang$ = right$(.label$, .numChars)
462 call set_language '.lang$'
466 procedure set_language .lang$
470 select Table 'buttons$'
475 select Table 'config$'
481 call checkTable 'buttonsTableName$'_'.lang$'
482 if checkTable.available
483 config.language$ = .lang$
485 config.language$ = "EN"
488 # Load buttons tables
489 call loadTable 'buttonsTableName$'
490 buttons$ = selected$("Table")
491 Append column... Text
493 Append column... Helptext
494 .numLabels = Get number of rows
495 call loadTable 'buttonsTableName$'_'config.language$'
496 .buttonsLang$ = selected$("Table")
497 for .row to .numLabels
498 select Table 'buttons$'
499 .label$ = Get value... '.row' Label
500 call findLabel '.buttonsLang$' '.label$'
502 select Table '.buttonsLang$'
503 .valueText$ = Get value... 'findLabel.row' Text
504 .valueKey$ = Get value... 'findLabel.row' Key
505 .valueHelp$ = Get value... 'findLabel.row' Helptext
506 select Table 'buttons$'
507 Set string value... '.row' Text '.valueText$'
508 Set string value... '.row' Key '.valueKey$'
509 Set string value... '.row' Helptext '.valueHelp$'
510 elsif index(.label$, "_")
511 # Load alternative language table
512 .startChar = rindex(.label$, "_")
513 .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
514 call loadTable 'buttonsTableName$'_'.otherLanguage$'
515 .otherbuttonsLang$ = selected$("Table")
516 call findLabel '.otherbuttonsLang$' '.label$'
518 select Table '.buttonsLang$'
519 .valueText$ = Get value... 'findLabel.row' Text
520 .valueKey$ = Get value... 'findLabel.row' Key
521 .valueHelp$ = Get value... 'findLabel.row' Helptext
522 select Table 'buttons$'
523 Set string value... '.row' Text '.valueText$'
524 Set string value... '.row' Key '.valueKey$'
525 Set string value... '.row' Helptext '.valueHelp$'
527 call emergency_table_exit Cannot find Label: '.otherbuttonsLang$' '.label$'
529 select Table '.otherbuttonsLang$'
532 call emergency_table_exit Cannot find Label: '.buttonsLang$' '.label$'
535 select Table '.buttonsLang$'
538 # Load configuration table
539 call loadTable 'configTableName$'
540 config$ = selected$("Table")
541 Append column... Text
543 Append column... Helptext
544 .numLabels = Get number of rows
545 call loadTable 'configTableName$'_'config.language$'
546 .configLang$ = selected$("Table")
547 for .row to .numLabels
548 select Table 'config$'
549 .label$ = Get value... '.row' Label
550 call findLabel '.configLang$' '.label$'
552 select Table '.configLang$'
553 .valueText$ = Get value... 'findLabel.row' Text
554 .valueKey$ = Get value... 'findLabel.row' Key
555 .valueHelp$ = Get value... 'findLabel.row' Helptext
556 select Table 'config$'
557 Set string value... '.row' Text '.valueText$'
558 Set string value... '.row' Key '.valueKey$'
559 Set string value... '.row' Helptext '.valueHelp$'
560 elsif index(.label$, "_")
561 .startChar = rindex(.label$, "_")
562 .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
563 call loadTable 'configTableName$'_'.otherLanguage$'
564 .otherconfigLang$ = selected$("Table")
565 call findLabel '.otherconfigLang$' '.label$'
567 select Table '.otherconfigLang$'
568 .valueText$ = Get value... 'findLabel.row' Text
569 .valueKey$ = Get value... 'findLabel.row' Key
570 .valueHelp$ = Get value... 'findLabel.row' Helptext
571 select Table 'config$'
572 Set string value... '.row' Text '.valueText$'
573 Set string value... '.row' Key '.valueKey$'
574 Set string value... '.row' Helptext '.valueHelp$'
576 call emergency_table_exit Cannot find Label: '.otherconfigLang$' '.label$'
578 select Table '.otherconfigLang$'
581 call emergency_table_exit Cannot find Label: '.configLang$' '.label$'
584 select Table '.configLang$'
587 # Make language change visible
589 call Draw_config_page
594 ###############################################################
596 # Button Drawing Routines
598 ###############################################################
600 # A stub for buttons that do not have a drawing routine (yet)
601 procedure DrawNull .color$ .x .y .size
604 procedure DrawHelp .color$ .x .y .size
605 .currentFontSize = 24
608 call adjustFontSizeOnHeight 'defaultFont$' '.currentFontSize' '.maxHeight'
609 .currentFontSize = adjustFontSizeOnHeight.currentFontSize
610 call set_font_size '.currentFontSize'
611 demo Colour... '.color$'
612 demo Text... '.x' Centre '.y' Bottom ?
613 call set_font_size 'defaultFontSize'
616 ###############################################################
618 # Button Processing Routines
620 ###############################################################
622 # Search row in table on label
623 procedure findKey .table$ .label$
625 select Table '.table$'
626 .to$ = selected$("Table")
628 .numRows = Get number of rows
630 .currentKey$ = '.to$'$[.i, "Key"]
631 if .label$ = .currentKey$
637 if .row <= 0 and index(.label$, "_") <= 0
638 printline "'.label$'" is not a key in '.table$'
642 procedure findLabel .table$ .label$
644 select Table '.table$'
645 .to$ = selected$("Table")
647 .numRows = Get number of rows
649 .currentKey$ = '.to$'$[.i, "Label"]
650 if .label$ = .currentKey$
656 if .row <= 0 and index(.label$, "_") <= 0
657 call emergency_table_exit "'.label$'" is not a key in '.table$'
662 procedure buttonClicked table$ .x .y
664 select Table 'table$'
665 .bo$ = selected$("Table")
667 .numRows = Get number of rows
670 .leftX = '.bo$'[.i, "LeftX"]
671 .rightX = '.bo$'[.i, "RightX"]
672 .lowY = '.bo$'[.i, "LowY"]
673 .highY = '.bo$'[.i, "HighY"]
674 if .x > .leftX and .x < .rightX and .y > .lowY and .y < .highY
675 .label$ = '.bo$'$[.i, "Label"]
681 procedure keyPressed table$ .pressed$
684 if .pressed$ = "" and not demoShiftKeyPressed()
687 .lowerPressed$ = replace_regex$(.pressed$, ".", "\L&", 0)
688 .upperPressed$ = replace_regex$(.pressed$, ".", "\U&", 0)
689 select Table 'table$'
690 .bo$ = selected$("Table")
692 .numRows = Get number of rows
695 .key$ = '.bo$'$[.i, "Key"]
696 if index(.key$, .lowerPressed$) or index(.key$, .upperPressed$)
697 .label$ = '.bo$'$[.i, "Label"]
703 procedure count_syllables
706 select Table 'wordlist$'
707 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
708 .sound$ = Get value... 'te.currentWord' Sound
709 .pinyin$ = Get value... 'te.currentWord' Pinyin
711 call add_missing_neutral_tones '.pinyin$'
712 .pinyin$ = add_missing_neutral_tones.pinyin$
713 if index_regex(.pinyin$, "[0-9]") > 0
714 .number = length(replace_regex$(.pinyin$, "[^\d]+([\d]+)", "1", 0))
720 procedure play_sound .sound$
722 select Sound '.sound$'
727 procedure record_sound .recordingTime
728 if .recordingTime <= 0
729 .recordingTime = recordingTime
732 if sgc2.alignedTextGrid > 0
733 select sgc2.alignedTextGrid
735 sgc2.alignedTextGrid = -1
738 # There is a very nasty delay before the first recording starts, do a dummy record
739 if not variableExists("recordingInitialized")
740 noprogress nowarn Record Sound (fixed time)... 'config.input$' 0.99 0.5 'samplingFrequency' 0.1
742 recordingInitialized = 1
745 demo Paint circle... Red 5 95 2
747 noprogress nowarn Record Sound (fixed time)... 'config.input$' 0.99 0.5 'samplingFrequency' '.recordingTime'
748 demo Paint circle... White 5 95 2.5
749 call wipeArea 'wipeFeedbackArea$'
751 # Feedback on recording level
752 .extremum = Get absolute extremum... 0 0 None
753 .radius = 2 * .extremum
762 elsif .extremum >= 0.49
765 .green = .extremum / 0.5
767 .color$ = "{'.red','.green','.blue'}"
768 demo Colour... '.color$'
770 demo Draw circle... 5 95 '.radius'
774 demo Line width... 'defaultLineWidth'
778 Rename... Pronunciation
779 recordedSound$ = selected$("Sound")
780 te.recordedSound = selected("Sound")
783 select Sound 'recordedSound$'
784 te.recordedSound = selected("Sound")
786 # Cut out real sound from silences/noise
787 call sound_detection 'recordedSound$' 'soundMargin'
788 select Sound 'recordedSound$'
789 te.recordedSound = selected("Sound")
791 # Store audio if requested
792 if sgc.saveAudioOn and sgc.saveAudio$ <> ""
794 select Table 'wordlist$'
795 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
796 .pinyin$ = Get value... 'te.currentWord' Pinyin
797 .outputName$ = "'sgc.saveAudio$'/'.pinyin$'.wav"
798 select te.recordedSound
799 Save as WAV file: .outputName$
806 # Select real sound from recording
807 # Uses some global variable
808 procedure sound_detection .sound$ .margin
809 select Sound '.sound$'
810 .soundlength = Get total duration
811 .internalSilence = 2*.margin
813 # Silence and remove noise, DANGEROUS
814 To TextGrid (silences)... 'minimumPitch' 0 'noiseThresshold' '.internalSilence' 0.1 silent sounding
815 Rename... Input'.sound$'
817 select TextGrid Input'.sound$'
818 .numberofIntervals = Get number of intervals... 1
819 if .numberofIntervals < 2
820 .numberofIntervals = 0
823 # Remove buzzing and other obnoxious sounds (if switched on)
824 for .i from 1 to .numberofIntervals
825 select TextGrid Input'.sound$'
826 .value$ = Get label of interval... 1 '.i'
827 .begintime = Get starting point... 1 '.i'
828 .endtime = Get end point... 1 '.i'
831 if .value$ = "silent"
832 select Sound '.sound$'
833 Set part to zero... '.begintime' '.endtime' at nearest zero crossing
837 # Select target sound
838 .maximumIntensity = -1
840 for i from 1 to .numberofIntervals
841 select TextGrid Input'.sound$'
843 .value$ = Get label of interval... 1 'i'
844 .begintime = Get starting point... 1 'i'
845 .endtime = Get end point... 1 'i'
847 if .value$ != "silent"
848 if .begintime > .margin
849 .begintime -= .margin
853 if .endtime + .margin < .soundlength
856 .endtime = .soundlength
859 select Sound '.sound$'
860 Extract part... '.begintime' '.endtime' Rectangular 1.0 no
861 Rename... Tmp'.sound$'
863 .newIntensity = Get intensity (dB)
864 if .newIntensity > .maximumIntensity
865 if .maximumIntensity > 0
866 select Sound New'.sound$'
869 select Sound Tmp'.sound$'
870 Rename... New'.sound$'
871 .maximumIntensity = .newIntensity
873 select Sound Tmp'.sound$'
879 if .maximumIntensity > minimumIntensity
880 select Sound '.sound$'
882 select Sound New'.sound$'
884 elsif .maximumIntensity > -1
885 select Sound New'.sound$'
888 select TextGrid Input'.sound$'
891 select Sound '.sound$'
894 procedure end_program
895 call write_preferences ""
902 ######################################################
906 ######################################################
907 procedure config_page
909 demoWindowTitle("Speak Good Chinese: Change settings")
911 call Draw_config_page
913 while (.label$ <> "Return") and demoWaitForInput()
921 call buttonClicked 'config$' '.clickX' '.clickY'
922 .label$ = buttonClicked.label$
923 elsif demoKeyPressed()
924 .pressed$ = demoKey$()
925 call keyPressed 'config$' '.pressed$'
926 .label$ = keyPressed.label$
929 # You cannot select a text field
930 if startsWith(.label$, "$")
936 # Handle push button in process_config
937 call process_config '.label$' '.clickX' '.clickY' '.pressed$'
940 if .label$ = "Return"
950 procedure Draw_config_page
953 if config.showBackground
954 call draw_background Background
957 call Draw_all_buttons 'config$'
958 call set_window_title 'config$'
959 # Set correct buttons (alert)
960 call setConfigMainPage
964 procedure process_config .label$ .clickX .clickY .pressed$
965 if .label$ <> "" and not startsWith(.label$,"!")
966 .label$ = replace$(.label$, "_", " ", 0)
967 call process'config$''.label$' '.clickX' '.clickY' '.pressed$'
971 ###############################################################
973 # Presenting help texts
975 ###############################################################
978 procedure help_loop .table$ .redrawProc$
980 call write_help_title '.table$'
983 call Draw_button '.table$' Help 2
985 while (.label$ <> "Help") and demoWaitForInput()
990 call buttonClicked '.table$' '.clickX' '.clickY'
991 .label$ = buttonClicked.label$
992 elsif demoKeyPressed()
993 .pressed$ = demoKey$()
994 call keyPressed '.table$' '.pressed$'
995 .label$ = keyPressed.label$
998 if .label$ != "" and .label$ <> "Help"
1006 call Draw_button '.table$' Help 2
1007 call write_help_title '.table$'
1009 # Handle push button in process_config
1010 call write_help_text '.table$' '.label$'
1016 call Draw_button '.table$' Help 0
1022 procedure write_help_text .table$ .label$
1023 call findLabel '.table$' '.label$'
1024 .row = findLabel.row
1025 select Table '.table$'
1028 call findLabel '.table$' Help
1029 .row = findLabel.row
1030 select Table '.table$'
1032 .helpText$ = Get value... '.row' Helptext
1033 .helpKey$ = Get value... '.row' Key
1034 .helpKey$ = replace$(.helpKey$, "\", "", 0)
1035 .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
1036 if index_regex(.helpKey$, "\S")
1037 .helpText$ = .helpText$+" ("+.helpKey$+")"
1040 .leftX = Get value... '.row' LeftX
1041 .rightX = Get value... '.row' RightX
1042 .lowY = Get value... '.row' LowY
1043 .highY = Get value... '.row' HighY
1046 .currentHelpFontSize = defaultFontSize
1047 call set_font_size '.currentHelpFontSize'
1048 .helpTextSize = demo Text width (wc)... '.helpText$'
1052 .htXright = .htXleft + .helpTextSize + 5
1056 .htXleft = .htXright - .helpTextSize - 5
1061 .htYhigh = .htYlow + 7
1066 .htYlow = .htYhigh - 7
1071 # Adapt font size to horizontal dimensions
1073 call adjustFontSizeOnWidth 'defaultFont$' '.currentHelpFontSize' '.maxWidth' '.helpText$'
1074 .currentHelpFontSize = adjustFontSizeOnWidth.newFontSize
1075 if .htXleft < 0 or .htXright > 100
1077 .htXright = .htXleft + adjustFontSizeOnWidth.textWidth + 5
1079 call set_font_size '.currentHelpFontSize'
1081 # Adapt vertical dimensions to font height
1082 call points_to_wc '.currentHelpFontSize'
1083 .lineHeight = points_to_wc.wc
1084 if .lineHeight > .htYhigh - .htYlow - 4
1085 .htYhigh = .htYlow + .lineHeight + 4
1088 # Determine arrow endpoints
1090 if abs(.htXleft - .xstart) > abs(.htXright - .xstart)
1093 if abs((.htXleft+.htXright)/2 - .xstart) < min(abs(.htXright - .xstart),abs(.htXleft - .xstart))
1094 .xend = (.htXleft+.htXright)/2
1097 .xtext = .htXleft + 2
1098 .ytext = .htYlow + 1
1101 .mm2wc = demo Horizontal mm to wc... 1
1102 .lineWidth = 2/.mm2wc
1103 demo Line width... '.lineWidth'
1104 demo Arrow size... '.lineWidth'
1105 demo Colour... 'sgc2.popUp_bordercolor$'
1106 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.htXleft' '.htXright' '.htYlow' '.htYhigh'
1107 demo Draw rectangle... '.htXleft' '.htXright' '.htYlow' '.htYhigh'
1108 demo Draw arrow... '.xstart' '.ystart' '.xend' '.yend'
1109 demo Line width... 'defaultLineWidth'
1110 demo Arrow size... 1
1112 demo Text... '.xtext' Left '.ytext' Bottom '.helpText$'
1114 call set_font_size 'defaultFontSize'
1118 procedure write_help_title .table$
1119 # Set help text title
1121 call findLabel '.table$' Help
1122 .row = findLabel.row
1123 select Table '.table$'
1124 .helpTitle$ = Get value... '.row' Helptext
1125 .helpKey$ = Get value... '.row' Key
1126 .helpKey$ = replace$(.helpKey$, "\", "", 0)
1127 .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
1128 .helpTitle$ = .helpTitle$+" ("+.helpKey$+")"
1131 .helpTitleFontSize = 14
1132 # Adapt size of button to length of text
1134 call adjustFontSizeOnWidth 'defaultFont$' '.helpTitleFontSize' '.maxWidth' '.helpTitle$'
1135 .helpTitleFontSize = adjustFontSizeOnWidth.newFontSize
1136 call set_font_size '.helpTitleFontSize'
1139 demo Select inner viewport... 0 100 0 100
1140 demo Axes... 0 100 0 100
1141 demo Text... 50 Centre '.helpTop' Top '.helpTitle$'
1142 call set_font_size 'defaultFontSize'
1146 ###############################################################
1148 # Miscelaneous procedures
1150 ###############################################################
1151 procedure printPageToPrinter
1153 demo Print... 'printerName$' 'printerPresets$'
1157 procedure getOpenFile .openDialogue$
1160 call convert_praat_to_latin1 '.openDialogue$'
1161 .openDialogue$ = convert_praat_to_latin1.text$
1162 .filename$ = chooseReadFile$ (.openDialogue$)
1164 if .filename$ <> "" and fileReadable(.filename$)
1165 .tmp = nocheck Read from file... '.filename$'
1166 if .tmp !- undefined and .tmp > 0
1167 call log_fileOpen '.filename$'
1169 # Get only the filename
1170 .startName = rindex_regex(.filename$, "[/\\:]") + 1
1171 .nameLength = rindex(.filename$, ".") - .startName
1172 currentSoundName$ = mid$(.filename$, .startName, .nameLength)
1178 Create Sound from formula... Speech Mono 0 1 44100 0
1180 recordedSound$ = selected$("Sound")
1181 te.recordedSound = selected("Sound")
1182 currentStartTime = 0
1183 currentEndTime = Get total duration
1184 # Reset selected window
1185 selectedStartTime = currentStartTime
1186 selectedEndTime = currentEndTime
1189 procedure points_to_wc .points
1190 .mm = .points * 0.3527777778
1191 .wc = demo Vertical mm to wc... '.mm'
1194 procedure reset_viewport
1195 .low = viewportMargin
1196 .high = 100 - viewportMargin
1197 demo Select inner viewport... '.low' '.high' '.low' '.high'
1198 demo Axes... 0 100 0 100
1201 procedure set_font_size .fontSize
1203 demo Font size... '.fontSize'
1207 procedure wipeArea .areaCommand$
1212 procedure adjustFontSizeOnWidth .font$ .currentFontSize .maxWidth .text$
1214 call set_font_size '.currentFontSize'
1215 .textWidth = demo Text width (wc)... '.text$'
1216 while .textWidth > .maxWidth and .currentFontSize > 2
1217 .currentFontSize -= 0.5
1218 call set_font_size '.currentFontSize'
1219 .textWidth = demo Text width (wc)... '.text$'
1221 .diff = .textWidth - .maxWidth
1222 .newFontSize = .currentFontSize
1226 procedure adjustRotatedFontSizeOnBox .font$ .currentFontSize .maxWidth .maxHeight .rotation .text$
1228 .radians = .rotation/360 * 2 * pi
1229 .horWC = demo Horizontal mm to wc... 10.0
1230 .verWC = demo Vertical mm to wc... 10.0
1232 .verCoeff = .verWC / .horWC
1236 call set_font_size '.currentFontSize'
1237 .textLength = demo Text width (wc)... '.text$'
1238 while (.textLength * .verCoeff * sin(.radians) > .maxHeight or .textLength * cos(.radians) > .maxWidth) and .currentFontSize > 2
1239 .currentFontSize -= 0.5
1240 call set_font_size '.currentFontSize'
1241 .textLength = demo Text width (wc)... '.text$'
1243 .diff = .textLength - .maxHeight
1244 .newFontSize = .currentFontSize
1248 procedure adjustFontSizeOnHeight .font$ .currentFontSize .maxHeight
1250 call points_to_wc '.currentFontSize'
1251 .lineHeight = points_to_wc.wc
1252 while .lineHeight > .maxHeight and .currentFontSize > 2
1253 .currentFontSize -= 0.5
1254 call points_to_wc '.currentFontSize'
1255 .lineHeight = points_to_wc.wc
1257 .diff = .lineHeight - .maxHeight
1258 .newFontSize = .currentFontSize
1262 # Load a table with button info etc.
1263 # Load local tables if present. Else load
1264 # build-in scripted tables
1265 procedure loadTable .tableName$
1266 .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
1267 # Search for the table in local, preference, and global directories
1268 if fileReadable("'localTableDir$'/'.tableName$'.Table")
1269 Read from file... 'localTableDir$'/'.tableName$'.Table
1270 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1271 Read from file... 'preferencesTableDir$'/'.tableName$'.Table
1272 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1273 Read from file... 'globaltablelists$'/'.tableName$'.Table
1274 # Load them from script
1275 elsif variableExists("procCreate'.tableVariableName$'$")
1276 call Create'.tableVariableName$'
1278 call write_text_popup 'defaultFont$' 14 '.tableName$' cannot be found
1280 exit '.tableName$' cannot be found
1284 procedure testLoadTable .tableName$
1286 .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
1287 # Search for the table in local, preference, and global directories
1288 if fileReadable("'localTableDir$'/'.tableName$'.Table")
1290 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1292 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1294 # Load them from script
1295 elsif variableExists("procCreate'.tableVariableName$'$")
1302 procedure checkTable .tableName$
1304 if fileReadable("'localTableDir$'/'.tableName$'.Table")
1306 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1308 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1310 # Load them from script
1311 elsif variableExists("procCreate'.tableName$'$")
1318 # Create a pop-up window with text from a Text Table
1319 procedure write_text_table .table$
1326 # Get table with text and longest line
1328 call testLoadTable '.table$'
1329 if testLoadTable.table > 0
1330 call loadTable '.table$'
1331 .instructionText = selected()
1332 .numLines = Get number of rows
1334 .instructionFontSize = 14
1335 .referenceText$ = ""
1341 select '.instructionText'
1342 .currentText$ = Get value... '.l' text
1343 # Expand variables, eg, 'praatVersion$'
1344 call expand_praat_variables '.currentText$'
1345 .currentText$ = expand_praat_variables.text$
1347 .font$ = Get value... '.l' font
1348 .fontSize = Get value... '.l' size
1349 call set_font_size '.fontSize'
1350 .textWidth = demo Text width (wc)... '.currentText$'
1351 if .fontSize > .maxFontSize
1352 .maxFontSize = .fontSize
1354 if .textWidth > .maxWidth
1355 .maxWidth = .textWidth
1356 .instructionFontSize = .fontSize
1360 select '.instructionText'
1361 .referenceText$ = Get value... '.maxLine' text
1362 .maxLineFont$ = Get value... '.maxLine' font
1363 .instructionFontSize = Get value... '.maxLine' size
1364 call set_font_size '.maxFontSize'
1366 # Adapt size of button to length of text
1367 .maxWidth = (.xright - .xleft) - 4
1368 .origFontSize = .instructionFontSize
1369 call adjustFontSizeOnWidth 'defaultFont$' '.instructionFontSize' '.maxWidth' '.referenceText$'
1370 call adjustFontSizeOnHeight 'defaultFont$' '.maxFontSize' '.lineHeight'
1371 .instructionFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1372 if adjustFontSizeOnWidth.diff > 0
1373 .xright += adjustFontSizeOnWidth.diff/4
1374 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1376 call set_font_size '.instructionFontSize'
1377 .fontSizeFactor = .instructionFontSize / .origFontSize
1379 .numRows = Get number of rows
1380 # Calculate length from number of lines.
1382 .midY = .yhigh - (.yhigh - .ylow)/2
1383 .yhigh = .midY + (.numRows+1) * .dy / 2
1384 .ylow = .yhigh - (.numRows+1) * .dy
1385 .textleft = .xleft + 2
1387 demo Line width... 8
1388 demo Colour... 'sgc2.popUp_bordercolor$'
1389 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1390 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1391 demo Line width... 'defaultLineWidth'
1393 .ytext = .yhigh - 2 - .dy
1395 select '.instructionText'
1396 .font$ = Get value... '.i' font
1397 .fontSize = Get value... '.i' size
1398 .font$ = extractWord$(.font$, "")
1400 .fontSize = floor(.fontSize*.fontSizeFactor)
1404 .line$ = Get value... '.i' text
1405 # Expand variables, eg, 'praatVersion$'
1406 call expand_praat_variables '.line$'
1407 .line$ = expand_praat_variables.text$
1410 demo Text special... '.textleft' Left '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1414 call set_font_size 'defaultFontSize'
1416 select '.instructionText'
1419 label ESCAPEwrite_text_table
1423 # Create a pop-up window with text from an existing Table object
1424 procedure write_tabbed_table .table$ .labelTextTable$
1431 # Get table with text and longest line
1432 call testLoadTable '.table$'
1433 if testLoadTable.table <= 0
1434 call loadTable '.labelTextTable$'
1435 .labelText$ = selected$("Table")
1438 select Table '.table$'
1439 .tabbedText = selected()
1440 .numLines = Get number of rows
1441 .numCols = Get number of columns
1442 .font$ = defaultFont$
1443 .fontSize = defaultFontSize
1445 .widthCanvas = .xright - .xleft
1446 .dx = (.widthCanvas - 4) / (.numCols)
1450 call set_font_size '.fontSize'
1452 for .i from 0 to .numLines
1453 .xtext = .xleft + .dx / 2
1455 select '.tabbedText'
1456 .currentLabel$ = Get column label... '.j'
1458 .line$ = Get value... '.i' '.currentLabel$'
1460 .line$ = .currentLabel$
1461 select Table '.labelText$'
1462 call findLabel '.labelText$' '.line$'
1463 select Table '.labelText$'
1464 .line$ = Get value... 'findLabel.row' Text
1466 # Expand variables, eg, 'praatVersion$'
1467 call expand_praat_variables '.line$'
1468 .line$ = expand_praat_variables.text$
1469 .textWidth = demo Text width (wc)... '.line$'
1470 if .textWidth > .maxWidth
1471 .maxWidth = .textWidth
1475 if .dx > 1.2 * .maxWidth
1476 .widthCanvas = 1.2 * .maxWidth * .numCols + 4
1477 .xleft = 50 - .widthCanvas / 2
1478 .xright = 50 + .widthCanvas / 2
1479 .dx = (.widthCanvas - 4) / (.numCols)
1484 # Calculate length from number of lines.
1485 .dy = .lineHeight + 0.5
1486 .midY = .yhigh - (.yhigh - .ylow)/2
1487 .yhigh = .midY + (.numLines+2) * .dy / 2
1488 .ylow = .yhigh - (.numLines+2) * .dy
1489 .textleft = .xleft + 2
1491 demo Line width... 8
1492 demo Colour... 'sgc2.popUp_bordercolor$'
1493 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1494 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1495 demo Line width... 'defaultLineWidth'
1497 .ytext = .yhigh - 2 - .dy
1498 # First the column names, then the items
1499 for .i from 0 to .numLines
1500 .xtext = .textleft + .dx / 2
1502 select '.tabbedText'
1503 .currentLabel$ = Get column label... '.j'
1505 .line$ = Get value... '.i' '.currentLabel$'
1507 .line$ = .currentLabel$
1508 select Table '.labelText$'
1509 call findLabel '.labelText$' '.line$'
1510 select Table '.labelText$'
1511 .line$ = Get value... 'findLabel.row' Text
1513 # Expand variables, eg, 'praatVersion$'
1514 call expand_praat_variables '.line$'
1515 .line$ = expand_praat_variables.text$
1516 call adjustFontSizeOnWidth '.font$' '.fontSize' '.maxWidth' '.line$'
1517 .currentFontSize = adjustFontSizeOnWidth.newFontSize
1520 demo Text special... '.xtext' Centre '.ytext' Bottom '.font$' '.currentFontSize' 0 '.line$'
1526 call set_font_size 'defaultFontSize'
1527 select Table '.labelText$'
1530 label ESCAPEwrite_tabbed_table
1533 # Create a pop-up window with a given text
1534 procedure write_text_popup .font$ .size .text$
1541 # Adapt size of button to length of text
1542 .maxWidth = (.xright - .xleft) - 4
1543 call adjustFontSizeOnWidth 'defaultFont$' '.size' '.maxWidth' '.text$'
1544 call adjustFontSizeOnHeight 'defaultFont$' '.size' '.lineHeight'
1545 .popupFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1546 if adjustFontSizeOnWidth.diff > 0
1547 .xright += adjustFontSizeOnWidth.diff/4
1548 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1550 .xleft = ((.xright + .xleft) - adjustFontSizeOnWidth.textWidth)/2 - 2
1551 .xright = ((.xright + .xleft) + adjustFontSizeOnWidth.textWidth)/2 + 2
1555 # Calculate length from number of lines.
1557 .midY = .yhigh - (.yhigh - .ylow)/2
1558 .yhigh = .midY + (.numRows+1) * .dy / 2
1559 .ylow = .yhigh - (.numRows+1) * .dy
1560 .textleft = .xleft + 2
1561 .xmid = (.textleft + .xright - 2)/2
1563 demo Line width... 8
1564 demo Colour... 'sgc2.popUp_bordercolor$'
1565 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1566 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1567 demo Line width... 'defaultLineWidth'
1569 .ytext = .yhigh - 2 - .dy
1571 demo Text special... '.xmid' Centre '.ytext' Bottom '.font$' '.popupFontSize' 0 '.text$'
1575 call set_font_size 'defaultFontSize'
1578 # Write the background from a Text Table
1579 procedure draw_background .table$
1585 .defaultColour$ = "{0.9,0.9,0.9}"
1586 .defaultAlign$ = "centre"
1588 # Get table with text and longest line
1589 call loadTable '.table$'
1590 .backgroundText = selected()
1591 .numLines = Get number of rows
1592 .backgroundFontSize = 28
1593 .referenceText$ = ""
1600 select '.backgroundText'
1601 .currentText$ = Get value... '.l' text
1602 # Expand variables, eg, 'praatVersion$'
1603 call expand_praat_variables '.currentText$'
1604 .currentText$ = expand_praat_variables.text$
1606 .font$ = Get value... '.l' font
1607 .fontSize = Get value... '.l' size
1608 if .fontSize > .maxFontSize
1609 .maxFontSize = .fontSize
1611 if not startsWith(.font$, "!")
1612 call set_font_size '.fontSize'
1613 .textWidth = demo Text width (wc)... '.currentText$'
1614 if .textWidth > .maxWidth
1615 .maxWidth = .textWidth
1616 .backgroundFontSize = .fontSize
1624 select '.backgroundText'
1625 .referenceText$ = Get value... '.maxLine' text
1626 .maxLineFont$ = Get value... '.maxLine' font
1627 .backgroundFontSize = Get value... '.maxLine' size
1628 .backgroundFontColour$ = Get value... '.maxLine' colour
1629 call set_font_size '.maxFontSize'
1631 .maxFontSize = .backgroundFontSize
1634 # Adapt size of button to length of text
1635 .maxWidth = (.xright - .xleft) - 4
1636 .origFontSize = .backgroundFontSize
1637 call adjustFontSizeOnWidth 'defaultFont$' '.backgroundFontSize' '.maxWidth' '.referenceText$'
1638 .fontSizeFactor = adjustFontSizeOnWidth.newFontSize / .backgroundFontSize
1639 .backgroundFontSize = adjustFontSizeOnWidth.newFontSize
1640 call set_font_size '.backgroundFontSize'
1642 call adjustFontSizeOnHeight 'defaultFont$' '.backgroundFontSize' '.lineHeight'
1643 .lineHeight /= adjustFontSizeOnHeight.newFontSize / .backgroundFontSize
1644 if adjustFontSizeOnHeight.newFontSize >= .origFontSize and (.textLines+1) * .lineHeight > (.yhigh - .ylow - 4)
1645 .lineHeight = (.yhigh - .ylow - 4)/(.textLines + 1)
1646 call adjustFontSizeOnHeight 'defaultFont$' '.maxFontSize' '.lineHeight'
1647 .fontSizeFactor = adjustFontSizeOnHeight.newFontSize / .backgroundFontSize
1650 .numRows = Get number of rows
1651 # Calculate length from number of lines.
1653 .midY = .yhigh - (.yhigh - .ylow)/2
1654 .yhigh = .midY + (.textLines+1) * .dy / 2
1655 .ylow = .yhigh - (.textLines+1) * .dy
1656 .textleft = .xleft + 2
1657 .textright = .xright - 2
1658 .textmid = (.xright - .xleft)/2
1661 .ytext = .yhigh - 2 - .dy
1663 select '.backgroundText'
1664 .font$ = Get value... '.i' font
1665 .fontSize = Get value... '.i' size
1666 .fontColour$ = Get value... '.i' colour
1667 .fontColour$ = replace_regex$(.fontColour$, "^[\- ]$", ".defaultColour$", 1)
1668 .fontAlign$ = Get value... '.i' align
1669 .fontAlign$ = replace_regex$(.fontAlign$, "^[\- ]$", ".defaultAlign$", 1)
1670 .line$ = Get value... '.i' text
1671 # Expand variables, eg, 'praatVersion$'
1672 call expand_praat_variables '.line$'
1673 .line$ = expand_praat_variables.text$
1676 .fontSize = floor(.fontSize*.fontSizeFactor)
1677 if not startsWith(.font$, "!")
1678 .font$ = extractWord$(.font$, "")
1680 if .fontAlign$ = "centre"
1682 elsif .fontAlign$ = "right"
1691 demo Colour... '.fontColour$'
1692 demo Text special... '.xtext' '.fontAlign$' '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1694 elsif .font$ = "!demo command"
1695 demo Colour... '.fontColour$'
1696 .line$ = replace_regex$(.line$, "\{FONTSIZE\$\}", "'.fontSize'", 0)
1697 .line$ = replace_regex$(.line$, "\{XTEXT\$\}", "'.xtext'", 0)
1698 .line$ = replace_regex$(.line$, "\{YTEXT\$\}", "'.ytext'", 0)
1699 .line$ = replace_regex$(.line$, "\{DY\$\}", "'.dy'", 0)
1700 .line$ = replace_regex$(.line$, "\{[^\}]*\}", "", 0)
1701 while index(.line$, "[[")
1702 .nextBracketOpen = index(.line$, "[[")
1703 .nextBracketOpen += 2
1704 .nextBracketClose = index(.line$, "]]")
1705 .bracketLength = .nextBracketClose - .nextBracketOpen
1707 if .bracketLength > 0
1708 .expression$ = mid$(.line$, .nextBracketOpen, .bracketLength)
1709 .expression$ = replace_regex$(.expression$, "\s", "", 0)
1710 if length(.expression$) > 0
1711 # Test expression for security, only allow explicitely defined functions
1712 .allowedStrings$ = "e|pi|not|and|or|div|mod|abs|round|floor|ceiling"
1713 .allowedStrings$ = .allowedStrings$ + "|sqrt|min|max|imin|imax|sin|cos|tan|arcsin|arccos|arctan|arctan2|sinc|sincpi"
1714 .allowedStrings$ = .allowedStrings$ + "|exp|ln|log10|log2|sinh|cosh|tanh|arcsinh|arccosh|arctanh"
1715 .allowedStrings$ = .allowedStrings$ + "|sigmoid|invSigmoid|erf|erfc|randomUniform|randomInteger|randomGauss|randomPoisson"
1716 .allowedStrings$ = .allowedStrings$ + "|lnGamma|gaussP|gaussQ|invGaussQ|chiSquareP|chiSquareQ"
1717 .allowedStrings$ = .allowedStrings$ + "|invChiSquareP|invChiSquareQ|studentP|studentQ|invStudentP|invStudentQ"
1718 .allowedStrings$ = .allowedStrings$ + "|beta|besselI|besselK"
1719 .testExpression$ = replace_regex$(.expression$, "(^|\W)('.allowedStrings$')(?=$|\W)", "\1\3", 0)
1720 .testExpression$ = replace_regex$(.testExpression$, "[0-9\.,\-+/*^()<>= ]", "", 0)
1721 if .testExpression$ = ""
1722 .calc = '.expression$'
1723 .result$ = "'.calc'"
1728 # Replace expression by result
1729 .lastLeft = .nextBracketOpen - 3
1730 .newLine$ = left$(.line$, .lastLeft)
1731 .newLine$ = .newLine$ + .result$
1732 .numCopy = length(.line$) - .nextBracketClose - 1
1733 .newLine$ = .newLine$ + right$(.line$, .numCopy)
1741 call set_font_size 'defaultFontSize'
1743 select '.backgroundText'
1747 procedure convert_praat_to_utf8 .text$
1748 .text$ = replace_regex$(.text$, "\\a""", "\xc3\xa4", 0)
1749 .text$ = replace_regex$(.text$, "\\A""", "\xc3\x84", 0)
1750 .text$ = replace_regex$(.text$, "\\o""", "\xc3\xb6", 0)
1751 .text$ = replace_regex$(.text$, "\\O""", "\xc3\x96", 0)
1752 .text$ = replace_regex$(.text$, "\\u""", "\xc3\xbc", 0)
1753 .text$ = replace_regex$(.text$, "\\U""", "\xc3\x9c", 0)
1754 .text$ = replace_regex$(.text$, "\\i""", "\xc3\xaf", 0)
1755 .text$ = replace_regex$(.text$, "\\I""", "\xc3\x8f", 0)
1756 .text$ = replace_regex$(.text$, "\\e""", "\xc3\xab", 0)
1757 .text$ = replace_regex$(.text$, "\\E""", "\xc3\x8b", 0)
1758 .text$ = replace_regex$(.text$, "\\y""", "\xc3\xbf", 0)
1759 .text$ = replace_regex$(.text$, "\\e'", "\xc3\xa9", 0)
1760 .text$ = replace_regex$(.text$, "\\E'", "\xc3\x89", 0)
1761 .text$ = replace_regex$(.text$, "\\ss", "\xc3\x9f", 0)
1764 procedure convert_praat_to_latin1 .text$
1765 .text$ = replace_regex$(.text$, "\\a""", "\xe4", 0)
1766 .text$ = replace_regex$(.text$, "\\A""", "\xc4", 0)
1767 .text$ = replace_regex$(.text$, "\\o""", "\xf6", 0)
1768 .text$ = replace_regex$(.text$, "\\O""", "\xd6", 0)
1769 .text$ = replace_regex$(.text$, "\\u""", "\xfc", 0)
1770 .text$ = replace_regex$(.text$, "\\U""", "\xdc", 0)
1771 .text$ = replace_regex$(.text$, "\\i""", "\xef", 0)
1772 .text$ = replace_regex$(.text$, "\\I""", "\xcf", 0)
1773 .text$ = replace_regex$(.text$, "\\e""", "\xeb", 0)
1774 .text$ = replace_regex$(.text$, "\\E""", "\xcb", 0)
1775 .text$ = replace_regex$(.text$, "\\y""", "\xff", 0)
1776 .text$ = replace_regex$(.text$, "\\Y""", "Y", 0)
1777 .text$ = replace_regex$(.text$, "\\e'", "\xe9", 0)
1778 .text$ = replace_regex$(.text$, "\\E'", "\xc9", 0)
1779 .text$ = replace_regex$(.text$, "\\ss", "\xdf", 0)
1782 # Expand 'variable$' into the value of variable$.
1783 # Eg, 'praatVersion$' becomes 5.1.45 or whatever is the current version
1784 # Single quotes can be protected by \'
1785 procedure expand_praat_variables .text$
1786 if index(.text$, "'")
1787 .tempText$ = replace_regex$(.text$, "(^|[^\\])'([\w\$\.]+)'", "\1""+\2+""", 0)
1788 .tempText$ = replace_regex$(.tempText$, "[\\]'", "'", 0)
1789 .tempText$ = """"+.tempText$+""""
1790 # Check whether all the variables actually exist. Ignore any variable that does not exist
1791 .checkVars$ = .tempText$
1792 while length(.checkVars$) > 0 and index(.checkVars$, "+")
1793 .start = index(.checkVars$, "+")
1794 .checkVars$ = right$(.checkVars$, length(.checkVars$) - .start)
1795 .end = index(.checkVars$, "+")
1797 .variable$ = left$(.checkVars$, .end - 1)
1798 if not variableExists(.variable$)
1799 .tempText$ = replace$(.tempText$, """+'.variable$'+""", "'"+.variable$+"'", 0)
1801 .checkVars$ = right$(.checkVars$, length(.checkVars$) - .end)
1806 .text$ = '.tempText$'
1810 # Get a time stamp in normalized format
1811 procedure getTimeStamp
1812 .currentDateTime$ = date$()
1813 .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)
1816 # A table error, can be insiduously caused by an outdate preferences file!
1817 procedure emergency_table_exit .message$
1818 # If you come here as a user, your preferences file is borked
1819 if preferencesAppFile$ <> "" and fileReadable(preferencesAppFile$)
1820 deleteFile(preferencesAppFile$)
1825 # Remove previous files from system
1826 procedure clean_up_sound
1827 if recordedSound$ = ""
1828 te.recordedSound = 0
1830 if te.recordedSound > 0
1831 select te.recordedSound
1834 te.recordedSound = 0
1836 if te.recordedPitch > 0
1837 select te.recordedPitch
1839 te.recordedPitch = 0
1843 # Safely read a table
1844 procedure readTable .filename$
1846 if .filename$ <> "" and fileReadable(.filename$) and index_regex(.filename$, "(?i\.(tsv|table|csv))$") > 0
1847 .tableID = nocheck Read from file... '.filename$'
1848 if .tableID = undefined or .tableID <= 0
1851 .fullName$ = selected$ ()
1852 .type$ = extractWord$(.fullName$, "")
1853 if .type$ <> "Table"
1861 # Read feedback table and get keyed text
1862 procedure get_feedback_text .language$ .key$
1863 if not endsWith(feedbackTableName$, "_'.language$'")
1864 if feedbackTableName$ <> ""
1865 select Table 'feedbackTableName$'
1868 call loadTable 'feedbackTablePrefix$'_'.language$'
1869 feedbackTableName$ = selected$("Table")
1871 call findKey 'feedbackTableName$' '.key$'
1873 select Table 'feedbackTableName$'
1874 .text$ = Get value... '.row' Text
1875 # Expand variables, eg, 'praatVersion$'
1876 call expand_praat_variables '.text$'
1877 .text$ = expand_praat_variables.text$