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_regex$(.label$, "^[#]", "", 0)
147 .label$ = replace$(.label$, "_", " ", 0)
148 call process'buttons$''.label$' '.clickX' '.clickY' '.pressed$'
153 procedure init_buttons
155 call Draw_all_buttons 'buttons$'
161 procedure Draw_all_buttons .table$
162 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
163 select Table '.table$'
164 .numRows = Get number of rows
167 .label$ = Get value... '.row' Label
168 if not startsWith(.label$, "!")
170 # Determine the "pressed" state of a button
171 # A variable with the same name as the button will act as a
173 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
174 # Simple boolean variables
175 if index(.variableName$, "_") <= 0 and variableExists(.variableName$)
177 if '.variableName$' > 0
180 elsif '.variableName$' < 0
183 elsif index(.variableName$, "_") <= 0 and variableExists("'.variableName$'$")
185 if '.variableName$'$ <> "" and '.variableName$'$ <> "0"
188 # Complex buttons with an variableName+'_'+value structure
189 # varableName$ -> name of button, e.g., "language"
190 elsif index(.variableName$, "_")
191 .generalVar$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
192 .currentVariableName$ = .generalVar$
194 if variableExists(.generalVar$+"$")
195 .currentVariableName$ = .generalVar$ + "$"
197 # Remove one level of indirection
198 if variableExists(.currentVariableName$)
199 if index(.currentVariableName$, "$")
200 .currentVariableName$ = '.currentVariableName$'
202 .currentValue = '.currentVariableName$'
203 .currentVariableName$ = "'.currentValue'"
205 # Remove next level of indirection
206 .currentContent$ = "'.currentVariableName$'"
207 if .currentContent$ = "_DISABLED_"
210 # Reconstruct label from current values
211 .currentLabelValue$ = .generalVar$ + "_" + .currentContent$
212 # Set PRESSED from label
213 if .variableName$ = .currentLabelValue$
218 # You did erase everything before you started here? So do not do that again
219 call Draw_button_internal 0 '.table$' '.label$' '.pressed'
224 # Draw a button from a predefined button table
225 # Normally, erase the area around a button
226 procedure Draw_button .table$ .label$ .push
227 call Draw_button_internal 1 '.table$' '.label$' '.push'
230 # Use this function if you want to control erasing
231 procedure Draw_button_internal .erase_button_area .table$ .label$ .push
232 # Do not draw invisible buttons starting with #
233 goto NOBUTTON startsWith(.label$, "#")
235 # Scale rounding of rounded rectangles
237 .mm = demo Horizontal wc to mm... '.wc'
238 # Allow to overide ! skip directive
240 if startsWith(.label$, "+")
241 .label$ = right$(.label$, length(.label$)-1)
245 select Table '.table$'
246 .row = Search column... Label '.label$'
248 call emergency_table_exit Button Table '.table$' does not have a row with label '.label$'
251 # Perspective shift sizes
256 .shiftDown = 0.1*buttonbevel
257 .shiftX = -0.2*buttonbevel
258 .shiftY = 0.40*buttonbevel
261 # Set drawing parameters
262 .topBackGroundColorUp$ = "{0.93,0.93,0.93}"
263 .topLineColorUp$ = "Black"
264 .topLineWidthUp = 1.5
265 .topBackGroundColorDown$ = "{0.89,0.89,0.94}"
266 .topLineColorDown$ = "{0.2,0.2,0.2}"
267 .topLineWidthDown = 2.0
268 .topBackGroundColorDisabled$ = "{0.85,0.85,0.85}"
269 .topLineColorDisabled$ = "{0.70,0.70,0.70}"
270 .topLineWidthDisabled = 1.5
271 .flankBackGroundColorUp$ = "{0.6,0.6,0.6}"
272 .flankLineColorUp$ = "{0.2,0.2,0.2}"
273 .flankLineWidthUp = 1.5
274 .flankBackGroundColorDown$ = "{0.75,0.75,0.75}"
275 .flankLineColorDown$ = .flankLineColorUp$
276 .flankLineWidthDown = 1.5
277 .buttonFontSize = defaultFontSize
280 .leftX = Get value... '.row' LeftX
281 .rightX = Get value... '.row' RightX
282 .lowY = Get value... '.row' LowY
283 .highY = Get value... '.row' HighY
284 .buttonText$ = Get value... '.row' Text
285 .buttonColor$ = Get value... '.row' Color
286 .buttonDraw$ = Get value... '.row' Draw
287 .buttonKey$ = Get value... '.row' Key
289 .noDraw = startsWith(.label$, "!") or (.leftX < 0) or (.rightX < 0) or (.lowY < 0) or (.highY < 0)
292 if index_regex(.buttonText$, "^![0-9\.]+!")
293 .rotation = extractNumber(.buttonText$, "!")
294 .buttonText$ = replace_regex$(.buttonText$, "^![0-9\.]+!", "", 0)
297 goto NOBUTTON .noDraw and not .forceDraw
299 # Replace button text with ALERT
301 .buttonText$ = alertText$
304 # Adapt font size to button size
305 .maxWidth = (.rightX - .leftX) - 2
306 .maxHeight = (.highY - .lowY) - 1
308 # Adapt size of button to length of text if necessary
309 call adjustFontSizeOnWidth 'defaultFont$' '.buttonFontSize' '.maxWidth' '.buttonText$'
310 .buttonFontSize = adjustFontSizeOnWidth.newFontSize
311 if adjustFontSizeOnWidth.diff > 0
312 .rightX += adjustFontSizeOnWidth.diff/2
313 .leftX -= adjustFontSizeOnWidth.diff/2
315 call set_font_size '.buttonFontSize'
317 # Adapt size of button to length of text
318 call adjustFontSizeOnHeight 'defaultFont$' '.buttonFontSize' '.maxHeight'
319 if adjustFontSizeOnHeight.diff > 0
320 .lowY -= adjustFontSizeOnHeight.diff/2
321 .highY += adjustFontSizeOnHeight.diff/2
323 .buttonFontSize = adjustFontSizeOnHeight.newFontSize
325 # With non-horizontal text, only change font size
326 call adjustRotatedFontSizeOnBox 'defaultFont$' '.buttonFontSize' '.maxWidth' '.maxHeight' '.rotation' '.buttonText$'
327 .buttonFontSize = adjustRotatedFontSizeOnBox.newFontSize
330 # Reset and erase button area
332 demo Line width... 'defaultLineWidth'
334 .shiftRightX = .rightX - .shiftX
335 .shiftLowY = .lowY - .shiftY
337 if .erase_button_area
338 # Make erase area minutely larger
339 .eraseLeft = .shiftLeftX - 0.01
340 .eraseRight = .shiftRightX + 0.01
341 .eraseBottom = .shiftLowY - 0.01
342 .eraseTop = .shiftHighY + 0.01
343 demo Paint rectangle... White .eraseLeft .eraseRight .eraseBottom .eraseTop
346 # If label starts with "$", it is a text field. Then do not draw the button
347 if not startsWith(.label$, "$")
348 # Give some depth to button: Draw flank outline
349 if .shiftDown or .shiftX or .shiftY
351 demo Paint rounded rectangle... '.flankBackGroundColorUp$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
352 demo Colour... '.flankLineColorUp$'
353 demo Line width... '.flankLineWidthUp'
355 demo Paint rounded rectangle... '.flankBackGroundColorDown$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
356 demo Colour... '.flankLineColorDown$'
357 demo Line width... '.flankLineWidthDown'
359 demo Draw rounded rectangle... .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
362 # Button Down will shift the top perspective
364 # Draw the button top
366 demo Paint rounded rectangle... '.topBackGroundColorUp$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
367 demo Colour... '.topLineColorUp$'
368 demo Line width... '.topLineWidthUp'
370 demo Paint rounded rectangle... '.topBackGroundColorDisabled$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
371 demo Colour... '.topLineColorDisabled$'
372 demo Line width... '.topLineWidthDisabled'
376 .rightX += .shiftDown
380 demo Paint rounded rectangle... '.topBackGroundColorDown$' .leftX .rightX .lowY .highY '.mm'
381 demo Colour... '.topLineColorDown$'
382 demo Line width... '.topLineWidthDown'
384 demo Draw rounded rectangle... '.leftX' '.rightX' '.lowY' '.highY' '.mm'
387 # The button text and symbol
388 .horWC = demo Horizontal mm to wc... 10.0
389 .verWC = demo Vertical mm to wc... 10.0
391 .verCoeff = .horWC / .verWC
396 .centerX = (.leftX + .rightX)/2
397 .centerY = .lowY + 0.6*(.highY-.lowY)
398 .radius = min(.verCoeff * (.highY - .lowY ), (.rightX - .leftX))/3
399 .buttonKey$ = replace$(.buttonKey$, "\", "\\", 0)
400 .buttonKey$ = replace$(.buttonKey$, """", "\""""", 0)
401 .newText$ = replace_regex$(.buttonText$, "['.buttonKey$']", "#%&", 1)
403 .newText$ = .buttonText$
405 # Variable text field, read corresponding variable
406 if index(.newText$, "$$$")
407 .fieldName$ = replace_regex$(.label$, "^[!$#]", "", 0)
408 .fieldName$ = replace_regex$(.fieldName$, "^(.)", "\l\1", 0)
409 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
410 .newText$ = replace$(.newText$, "$$$", '.varPrefix$'.'.fieldName$'$, 0)
412 if .push = 1 or .push = -1
414 if .buttonColor$ = "Red"
415 .buttonColor$ = "Pink"
416 elsif .buttonColor$ = "Blue"
417 .buttonColor$ = "{0.5,0.5,1}"
419 .buttonColor$ = "Grey"
422 .buttonColor$ = "Maroon"
427 call '.buttonDraw$' '.buttonColor$' '.centerX' '.centerY' '.radius'
428 call set_font_size '.buttonFontSize'
429 demo Colour... '.buttonColor$'
432 .verticalAlignment$ = "Bottom"
434 .anchorY = .lowY + 0.5*(.highY-.lowY)
435 .verticalAlignment$ = "Half"
437 demo Text special... '.centerX' Centre '.anchorY' '.verticalAlignment$' 'defaultFont$' '.buttonFontSize' '.rotation' '.newText$'
441 call set_font_size 'defaultFontSize'
443 demo Line width... 'defaultLineWidth'
448 procedure set_window_title .table$ .addedText$
449 select Table '.table$'
450 .row = Search column... Label !WindowTitle
452 call emergency_table_exit Button Table '.table$' does not have a row with label !WindowTitle
454 .windowText$ = Get value... '.row' Text
455 call convert_praat_to_latin1 '.windowText$'
456 .windowText$ = convert_praat_to_latin1.text$
458 demoWindowTitle(.windowText$+ .addedText$)
461 # Handle language setting
462 procedure processLanguageCodes .table$ .label$
464 call Draw_button 'config$' Language_'config.language$' 0
465 call Draw_button 'config$' '.label$' 2
466 # Someone might have to use more than 2 chars for the language code
467 .numChars = length(.label$) - length("Language_")
468 .lang$ = right$(.label$, .numChars)
470 call set_language '.lang$'
474 procedure set_language .lang$
478 select Table 'buttons$'
483 select Table 'config$'
489 call checkTable 'buttonsTableName$'_'.lang$'
490 if checkTable.available
491 config.language$ = .lang$
493 config.language$ = "EN"
496 # Load buttons tables
497 call loadTable 'buttonsTableName$'
498 buttons$ = selected$("Table")
499 Append column... Text
501 Append column... Helptext
502 .numLabels = Get number of rows
503 call loadTable 'buttonsTableName$'_'config.language$'
504 .buttonsLang$ = selected$("Table")
505 for .row to .numLabels
506 select Table 'buttons$'
507 .label$ = Get value... '.row' Label
508 call findLabel '.buttonsLang$' '.label$'
510 select Table '.buttonsLang$'
511 .valueText$ = Get value... 'findLabel.row' Text
512 .valueKey$ = Get value... 'findLabel.row' Key
513 .valueHelp$ = Get value... 'findLabel.row' Helptext
514 select Table 'buttons$'
515 Set string value... '.row' Text '.valueText$'
516 Set string value... '.row' Key '.valueKey$'
517 Set string value... '.row' Helptext '.valueHelp$'
518 elsif index(.label$, "_")
519 # Load alternative language table
520 .startChar = rindex(.label$, "_")
521 .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
522 call loadTable 'buttonsTableName$'_'.otherLanguage$'
523 .otherbuttonsLang$ = selected$("Table")
524 call findLabel '.otherbuttonsLang$' '.label$'
526 select Table '.buttonsLang$'
527 .valueText$ = Get value... 'findLabel.row' Text
528 .valueKey$ = Get value... 'findLabel.row' Key
529 .valueHelp$ = Get value... 'findLabel.row' Helptext
530 select Table 'buttons$'
531 Set string value... '.row' Text '.valueText$'
532 Set string value... '.row' Key '.valueKey$'
533 Set string value... '.row' Helptext '.valueHelp$'
535 call emergency_table_exit Cannot find Label: '.otherbuttonsLang$' '.label$'
537 select Table '.otherbuttonsLang$'
540 call emergency_table_exit Cannot find Label: '.buttonsLang$' '.label$'
543 select Table '.buttonsLang$'
546 # Load configuration table
547 call loadTable 'configTableName$'
548 config$ = selected$("Table")
549 Append column... Text
551 Append column... Helptext
552 .numLabels = Get number of rows
553 call loadTable 'configTableName$'_'config.language$'
554 .configLang$ = selected$("Table")
555 for .row to .numLabels
556 select Table 'config$'
557 .label$ = Get value... '.row' Label
558 call findLabel '.configLang$' '.label$'
560 select Table '.configLang$'
561 .valueText$ = Get value... 'findLabel.row' Text
562 .valueKey$ = Get value... 'findLabel.row' Key
563 .valueHelp$ = Get value... 'findLabel.row' Helptext
564 select Table 'config$'
565 Set string value... '.row' Text '.valueText$'
566 Set string value... '.row' Key '.valueKey$'
567 Set string value... '.row' Helptext '.valueHelp$'
568 elsif index(.label$, "_")
569 .startChar = rindex(.label$, "_")
570 .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
571 call loadTable 'configTableName$'_'.otherLanguage$'
572 .otherconfigLang$ = selected$("Table")
573 call findLabel '.otherconfigLang$' '.label$'
575 select Table '.otherconfigLang$'
576 .valueText$ = Get value... 'findLabel.row' Text
577 .valueKey$ = Get value... 'findLabel.row' Key
578 .valueHelp$ = Get value... 'findLabel.row' Helptext
579 select Table 'config$'
580 Set string value... '.row' Text '.valueText$'
581 Set string value... '.row' Key '.valueKey$'
582 Set string value... '.row' Helptext '.valueHelp$'
584 call emergency_table_exit Cannot find Label: '.otherconfigLang$' '.label$'
586 select Table '.otherconfigLang$'
589 call emergency_table_exit Cannot find Label: '.configLang$' '.label$'
592 select Table '.configLang$'
595 # Make language change visible
597 call Draw_config_page
602 ###############################################################
604 # Button Drawing Routines
606 ###############################################################
608 # A stub for buttons that do not have a drawing routine (yet)
609 procedure DrawNull .color$ .x .y .size
612 procedure DrawHelp .color$ .x .y .size
613 .currentFontSize = 24
616 call adjustFontSizeOnHeight 'defaultFont$' '.currentFontSize' '.maxHeight'
617 .currentFontSize = adjustFontSizeOnHeight.currentFontSize
618 call set_font_size '.currentFontSize'
619 demo Colour... '.color$'
620 demo Text... '.x' Centre '.y' Bottom ?
621 call set_font_size 'defaultFontSize'
624 ###############################################################
626 # Button Processing Routines
628 ###############################################################
630 # Search row in table on label
631 procedure findKey .table$ .label$
633 select Table '.table$'
634 .to$ = selected$("Table")
636 .numRows = Get number of rows
638 .currentKey$ = '.to$'$[.i, "Key"]
639 if .label$ = .currentKey$
645 if .row <= 0 and index(.label$, "_") <= 0
646 printline "'.label$'" is not a key in '.table$'
650 procedure findLabel .table$ .label$
652 select Table '.table$'
653 .to$ = selected$("Table")
655 .numRows = Get number of rows
657 .currentKey$ = '.to$'$[.i, "Label"]
658 if .label$ = .currentKey$
664 if .row <= 0 and index(.label$, "_") <= 0
665 call emergency_table_exit "'.label$'" is not a key in '.table$'
670 procedure buttonClicked table$ .x .y
672 select Table 'table$'
673 .bo$ = selected$("Table")
675 .numRows = Get number of rows
678 .leftX = '.bo$'[.i, "LeftX"]
679 .rightX = '.bo$'[.i, "RightX"]
680 .lowY = '.bo$'[.i, "LowY"]
681 .highY = '.bo$'[.i, "HighY"]
682 if .x > .leftX and .x < .rightX and .y > .lowY and .y < .highY
683 .label$ = '.bo$'$[.i, "Label"]
689 procedure keyPressed table$ .pressed$
692 if .pressed$ = "" and not demoShiftKeyPressed()
695 .lowerPressed$ = replace_regex$(.pressed$, ".", "\L&", 0)
696 .upperPressed$ = replace_regex$(.pressed$, ".", "\U&", 0)
697 select Table 'table$'
698 .bo$ = selected$("Table")
700 .numRows = Get number of rows
703 .key$ = '.bo$'$[.i, "Key"]
704 if index(.key$, .lowerPressed$) or index(.key$, .upperPressed$)
705 .label$ = '.bo$'$[.i, "Label"]
711 procedure count_syllables
714 select Table 'wordlist$'
715 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
716 .sound$ = Get value... 'te.currentWord' Sound
717 .pinyin$ = Get value... 'te.currentWord' Pinyin
719 call add_missing_neutral_tones '.pinyin$'
720 .pinyin$ = add_missing_neutral_tones.pinyin$
721 if index_regex(.pinyin$, "[0-9]") > 0
722 .number = length(replace_regex$(.pinyin$, "[^\d]+([\d]+)", "1", 0))
728 procedure play_sound .sound$
730 select Sound '.sound$'
735 procedure record_sound .recordingTime
736 if .recordingTime <= 0
737 .recordingTime = recordingTime
740 if sgc2.alignedTextGrid > 0
741 select sgc2.alignedTextGrid
743 sgc2.alignedTextGrid = -1
746 # There is a very nasty delay before the first recording starts, do a dummy record
747 if not variableExists("recordingInitialized")
748 noprogress nowarn Record Sound (fixed time)... 'config.input$' 0.99 0.5 'samplingFrequency' 0.1
750 recordingInitialized = 1
753 demo Paint circle... Red 5 95 2
755 noprogress nowarn Record Sound (fixed time)... 'config.input$' 0.99 0.5 'samplingFrequency' '.recordingTime'
756 demo Paint circle... White 5 95 2.5
757 call wipeArea 'wipeFeedbackArea$'
759 # Feedback on recording level
760 .extremum = Get absolute extremum... 0 0 None
761 .radius = 2 * .extremum
770 elsif .extremum >= 0.49
773 .green = .extremum / 0.5
775 .color$ = "{'.red','.green','.blue'}"
776 demo Colour... '.color$'
778 demo Draw circle... 5 95 '.radius'
782 demo Line width... 'defaultLineWidth'
786 Rename... Pronunciation
787 recordedSound$ = selected$("Sound")
788 te.recordedSound = selected("Sound")
791 select Sound 'recordedSound$'
792 te.recordedSound = selected("Sound")
794 # Cut out real sound from silences/noise
795 call sound_detection 'recordedSound$' 'soundMargin'
796 select Sound 'recordedSound$'
797 te.recordedSound = selected("Sound")
799 # Store audio if requested
800 if sgc.saveAudioOn and sgc.saveAudio$ <> ""
801 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
803 select Table 'wordlist$'
804 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
805 .pinyin$ = Get value... 'te.currentWord' Pinyin
806 .outputName$ = "'sgc.saveAudio$'/'.pinyin$'.wav"
807 select te.recordedSound
808 Save as WAV file: .outputName$
811 # The Audio directory disappeared
817 config.clearSummary = 0
818 config.audioName$ = ""
825 # Select real sound from recording
826 # Uses some global variable
827 procedure sound_detection .sound$ .margin
828 select Sound '.sound$'
829 .soundlength = Get total duration
830 .internalSilence = 2*.margin
832 # Silence and remove noise, DANGEROUS
833 To TextGrid (silences)... 'minimumPitch' 0 'noiseThresshold' '.internalSilence' 0.1 silent sounding
834 Rename... Input'.sound$'
836 select TextGrid Input'.sound$'
837 .numberofIntervals = Get number of intervals... 1
838 if .numberofIntervals < 2
839 .numberofIntervals = 0
842 # Remove buzzing and other obnoxious sounds (if switched on)
843 for .i from 1 to .numberofIntervals
844 select TextGrid Input'.sound$'
845 .value$ = Get label of interval... 1 '.i'
846 .begintime = Get starting point... 1 '.i'
847 .endtime = Get end point... 1 '.i'
850 if .value$ = "silent"
851 select Sound '.sound$'
852 Set part to zero... '.begintime' '.endtime' at nearest zero crossing
856 # Select target sound
857 .maximumIntensity = -1
859 for i from 1 to .numberofIntervals
860 select TextGrid Input'.sound$'
862 .value$ = Get label of interval... 1 'i'
863 .begintime = Get starting point... 1 'i'
864 .endtime = Get end point... 1 'i'
866 if .value$ != "silent"
867 if .begintime > .margin
868 .begintime -= .margin
872 if .endtime + .margin < .soundlength
875 .endtime = .soundlength
878 select Sound '.sound$'
879 Extract part... '.begintime' '.endtime' Rectangular 1.0 no
880 Rename... Tmp'.sound$'
882 .newIntensity = Get intensity (dB)
883 if .newIntensity > .maximumIntensity
884 if .maximumIntensity > 0
885 select Sound New'.sound$'
888 select Sound Tmp'.sound$'
889 Rename... New'.sound$'
890 .maximumIntensity = .newIntensity
892 select Sound Tmp'.sound$'
898 if .maximumIntensity > minimumIntensity
899 select Sound '.sound$'
901 select Sound New'.sound$'
903 elsif .maximumIntensity > -1
904 select Sound New'.sound$'
907 select TextGrid Input'.sound$'
910 select Sound '.sound$'
913 procedure end_program
914 call write_preferences ""
921 ######################################################
925 ######################################################
926 procedure config_page
928 demoWindowTitle("Speak Good Chinese: Change settings")
930 call Draw_config_page
932 while (.label$ <> "Return") and demoWaitForInput()
940 call buttonClicked 'config$' '.clickX' '.clickY'
941 .label$ = buttonClicked.label$
942 elsif demoKeyPressed()
943 .pressed$ = demoKey$()
944 call keyPressed 'config$' '.pressed$'
945 .label$ = keyPressed.label$
948 # You cannot select a text field
949 if startsWith(.label$, "$")
955 # Handle push button in process_config
956 call process_config '.label$' '.clickX' '.clickY' '.pressed$'
959 if .label$ = "Return"
969 procedure Draw_config_page
972 if config.showBackground
973 call draw_background Background
976 call Draw_all_buttons 'config$'
977 call set_window_title 'config$'
978 # Set correct buttons (alert)
979 call setConfigMainPage
983 procedure process_config .label$ .clickX .clickY .pressed$
984 if .label$ <> "" and not startsWith(.label$,"!")
985 .label$ = replace_regex$(.label$, "^[#]", "", 0)
986 .label$ = replace$(.label$, "_", " ", 0)
987 call process'config$''.label$' '.clickX' '.clickY' '.pressed$'
991 ###############################################################
993 # Presenting help texts
995 ###############################################################
998 procedure help_loop .table$ .redrawProc$
1000 call write_help_title '.table$'
1003 call Draw_button '.table$' Help 2
1005 while (.label$ <> "Help") and demoWaitForInput()
1010 call buttonClicked '.table$' '.clickX' '.clickY'
1011 .label$ = buttonClicked.label$
1012 elsif demoKeyPressed()
1013 .pressed$ = demoKey$()
1014 call keyPressed '.table$' '.pressed$'
1015 .label$ = keyPressed.label$
1018 if .label$ != "" and .label$ <> "Help"
1026 call Draw_button '.table$' Help 2
1027 call write_help_title '.table$'
1029 # Handle push button in process_config
1030 call write_help_text '.table$' '.label$'
1036 call Draw_button '.table$' Help 0
1042 procedure write_help_text .table$ .label$
1043 call findLabel '.table$' '.label$'
1044 .row = findLabel.row
1045 select Table '.table$'
1048 call findLabel '.table$' Help
1049 .row = findLabel.row
1050 select Table '.table$'
1052 .helpText$ = Get value... '.row' Helptext
1053 .helpKey$ = Get value... '.row' Key
1054 .helpKey$ = replace$(.helpKey$, "\", "", 0)
1055 .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
1056 if index_regex(.helpKey$, "\S")
1057 .helpText$ = .helpText$+" ("+.helpKey$+")"
1060 .leftX = Get value... '.row' LeftX
1061 .rightX = Get value... '.row' RightX
1062 .lowY = Get value... '.row' LowY
1063 .highY = Get value... '.row' HighY
1066 .currentHelpFontSize = defaultFontSize
1067 call set_font_size '.currentHelpFontSize'
1068 .helpTextSize = demo Text width (wc)... '.helpText$'
1072 .htXright = .htXleft + .helpTextSize + 5
1076 .htXleft = .htXright - .helpTextSize - 5
1081 .htYhigh = .htYlow + 7
1086 .htYlow = .htYhigh - 7
1091 # Adapt font size to horizontal dimensions
1093 call adjustFontSizeOnWidth 'defaultFont$' '.currentHelpFontSize' '.maxWidth' '.helpText$'
1094 .currentHelpFontSize = adjustFontSizeOnWidth.newFontSize
1095 if .htXleft < 0 or .htXright > 100
1097 .htXright = .htXleft + adjustFontSizeOnWidth.textWidth + 5
1099 call set_font_size '.currentHelpFontSize'
1101 # Adapt vertical dimensions to font height
1102 call points_to_wc '.currentHelpFontSize'
1103 .lineHeight = points_to_wc.wc
1104 if .lineHeight > .htYhigh - .htYlow - 4
1105 .htYhigh = .htYlow + .lineHeight + 4
1108 # Determine arrow endpoints
1110 if abs(.htXleft - .xstart) > abs(.htXright - .xstart)
1113 if abs((.htXleft+.htXright)/2 - .xstart) < min(abs(.htXright - .xstart),abs(.htXleft - .xstart))
1114 .xend = (.htXleft+.htXright)/2
1117 .xtext = .htXleft + 2
1118 .ytext = .htYlow + 1
1121 .mm2wc = demo Horizontal mm to wc... 1
1122 .lineWidth = 2/.mm2wc
1123 demo Line width... '.lineWidth'
1124 demo Arrow size... '.lineWidth'
1125 demo Colour... 'sgc2.popUp_bordercolor$'
1126 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.htXleft' '.htXright' '.htYlow' '.htYhigh'
1127 demo Draw rectangle... '.htXleft' '.htXright' '.htYlow' '.htYhigh'
1128 demo Draw arrow... '.xstart' '.ystart' '.xend' '.yend'
1129 demo Line width... 'defaultLineWidth'
1130 demo Arrow size... 1
1132 demo Text... '.xtext' Left '.ytext' Bottom '.helpText$'
1134 call set_font_size 'defaultFontSize'
1138 procedure write_help_title .table$
1139 # Set help text title
1141 call findLabel '.table$' Help
1142 .row = findLabel.row
1143 select Table '.table$'
1144 .helpTitle$ = Get value... '.row' Helptext
1145 .helpKey$ = Get value... '.row' Key
1146 .helpKey$ = replace$(.helpKey$, "\", "", 0)
1147 .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
1148 .helpTitle$ = .helpTitle$+" ("+.helpKey$+")"
1151 .helpTitleFontSize = 14
1152 # Adapt size of button to length of text
1154 call adjustFontSizeOnWidth 'defaultFont$' '.helpTitleFontSize' '.maxWidth' '.helpTitle$'
1155 .helpTitleFontSize = adjustFontSizeOnWidth.newFontSize
1156 call set_font_size '.helpTitleFontSize'
1159 demo Select inner viewport... 0 100 0 100
1160 demo Axes... 0 100 0 100
1161 demo Text... 50 Centre '.helpTop' Top '.helpTitle$'
1162 call set_font_size 'defaultFontSize'
1166 ###############################################################
1168 # Miscelaneous procedures
1170 ###############################################################
1171 procedure printPageToPrinter
1173 demo Print... 'printerName$' 'printerPresets$'
1177 procedure getOpenFile .openDialogue$
1180 call convert_praat_to_latin1 '.openDialogue$'
1181 .openDialogue$ = convert_praat_to_latin1.text$
1182 .filename$ = chooseReadFile$ (.openDialogue$)
1184 if .filename$ <> "" and fileReadable(.filename$)
1185 .tmp = nocheck Read from file... '.filename$'
1186 if .tmp !- undefined and .tmp > 0
1187 call log_fileOpen '.filename$'
1189 # Get only the filename
1190 .startName = rindex_regex(.filename$, "[/\\:]") + 1
1191 .nameLength = rindex(.filename$, ".") - .startName
1192 currentSoundName$ = mid$(.filename$, .startName, .nameLength)
1198 Create Sound from formula... Speech Mono 0 1 44100 0
1200 recordedSound$ = selected$("Sound")
1201 te.recordedSound = selected("Sound")
1202 currentStartTime = 0
1203 currentEndTime = Get total duration
1204 # Reset selected window
1205 selectedStartTime = currentStartTime
1206 selectedEndTime = currentEndTime
1209 procedure points_to_wc .points
1210 .mm = .points * 0.3527777778
1211 .wc = demo Vertical mm to wc... '.mm'
1214 procedure reset_viewport
1215 .low = viewportMargin
1216 .high = 100 - viewportMargin
1217 demo Select inner viewport... '.low' '.high' '.low' '.high'
1218 demo Axes... 0 100 0 100
1221 procedure set_font_size .fontSize
1223 demo Font size... '.fontSize'
1227 procedure wipeArea .areaCommand$
1232 procedure adjustFontSizeOnWidth .font$ .currentFontSize .maxWidth .text$
1234 call set_font_size '.currentFontSize'
1235 .textWidth = demo Text width (wc)... '.text$'
1236 while .textWidth > .maxWidth and .currentFontSize > 2
1237 .currentFontSize -= 0.5
1238 call set_font_size '.currentFontSize'
1239 .textWidth = demo Text width (wc)... '.text$'
1241 .diff = .textWidth - .maxWidth
1242 .newFontSize = .currentFontSize
1246 procedure adjustRotatedFontSizeOnBox .font$ .currentFontSize .maxWidth .maxHeight .rotation .text$
1248 .radians = .rotation/360 * 2 * pi
1249 .horWC = demo Horizontal mm to wc... 10.0
1250 .verWC = demo Vertical mm to wc... 10.0
1252 .verCoeff = .verWC / .horWC
1256 call set_font_size '.currentFontSize'
1257 .textLength = demo Text width (wc)... '.text$'
1258 while (.textLength * .verCoeff * sin(.radians) > .maxHeight or .textLength * cos(.radians) > .maxWidth) and .currentFontSize > 2
1259 .currentFontSize -= 0.5
1260 call set_font_size '.currentFontSize'
1261 .textLength = demo Text width (wc)... '.text$'
1263 .diff = .textLength - .maxHeight
1264 .newFontSize = .currentFontSize
1268 procedure adjustFontSizeOnHeight .font$ .currentFontSize .maxHeight
1270 call points_to_wc '.currentFontSize'
1271 .lineHeight = points_to_wc.wc
1272 while .lineHeight > .maxHeight and .currentFontSize > 2
1273 .currentFontSize -= 0.5
1274 call points_to_wc '.currentFontSize'
1275 .lineHeight = points_to_wc.wc
1277 .diff = .lineHeight - .maxHeight
1278 .newFontSize = .currentFontSize
1282 # Load a table with button info etc.
1283 # Load local tables if present. Else load
1284 # build-in scripted tables
1285 procedure loadTable .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")
1289 Read from file... 'localTableDir$'/'.tableName$'.Table
1290 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1291 Read from file... 'preferencesTableDir$'/'.tableName$'.Table
1292 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1293 Read from file... 'globaltablelists$'/'.tableName$'.Table
1294 # Load them from script
1295 elsif variableExists("procCreate'.tableVariableName$'$")
1296 call Create'.tableVariableName$'
1298 call write_text_popup 'defaultFont$' 14 '.tableName$' cannot be found
1300 exit '.tableName$' cannot be found
1304 procedure testLoadTable .tableName$
1306 .tableVariableName$ = replace_regex$(.tableName$, "[^\w]", "_", 0);
1307 # Search for the table in local, preference, and global directories
1308 if fileReadable("'localTableDir$'/'.tableName$'.Table")
1310 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1312 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1314 # Load them from script
1315 elsif variableExists("procCreate'.tableVariableName$'$")
1322 procedure checkTable .tableName$
1324 if fileReadable("'localTableDir$'/'.tableName$'.Table")
1326 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
1328 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
1330 # Load them from script
1331 elsif variableExists("procCreate'.tableName$'$")
1338 # Create a pop-up window with text from a Text Table
1339 procedure write_text_table .table$
1346 # Get table with text and longest line
1348 call testLoadTable '.table$'
1349 if testLoadTable.table > 0
1350 call loadTable '.table$'
1351 .instructionText = selected()
1352 .numLines = Get number of rows
1354 .instructionFontSize = 14
1355 .referenceText$ = ""
1361 select '.instructionText'
1362 .currentText$ = Get value... '.l' text
1363 # Expand variables, eg, 'praatVersion$'
1364 call expand_praat_variables '.currentText$'
1365 .currentText$ = expand_praat_variables.text$
1367 .font$ = Get value... '.l' font
1368 .fontSize = Get value... '.l' size
1369 call set_font_size '.fontSize'
1370 .textWidth = demo Text width (wc)... '.currentText$'
1371 if .fontSize > .maxFontSize
1372 .maxFontSize = .fontSize
1374 if .textWidth > .maxWidth
1375 .maxWidth = .textWidth
1376 .instructionFontSize = .fontSize
1380 select '.instructionText'
1381 .referenceText$ = Get value... '.maxLine' text
1382 .maxLineFont$ = Get value... '.maxLine' font
1383 .instructionFontSize = Get value... '.maxLine' size
1384 call set_font_size '.maxFontSize'
1386 # Adapt size of button to length of text
1387 .maxWidth = (.xright - .xleft) - 4
1388 .origFontSize = .instructionFontSize
1389 call adjustFontSizeOnWidth 'defaultFont$' '.instructionFontSize' '.maxWidth' '.referenceText$'
1390 call adjustFontSizeOnHeight 'defaultFont$' '.maxFontSize' '.lineHeight'
1391 .instructionFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1392 if adjustFontSizeOnWidth.diff > 0
1393 .xright += adjustFontSizeOnWidth.diff/4
1394 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1396 call set_font_size '.instructionFontSize'
1397 .fontSizeFactor = .instructionFontSize / .origFontSize
1399 .numRows = Get number of rows
1400 # Calculate length from number of lines.
1402 .midY = .yhigh - (.yhigh - .ylow)/2
1403 .yhigh = .midY + (.numRows+1) * .dy / 2
1404 .ylow = .yhigh - (.numRows+1) * .dy
1405 .textleft = .xleft + 2
1407 demo Line width... 8
1408 demo Colour... 'sgc2.popUp_bordercolor$'
1409 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1410 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1411 demo Line width... 'defaultLineWidth'
1413 .ytext = .yhigh - 2 - .dy
1415 select '.instructionText'
1416 .font$ = Get value... '.i' font
1417 .fontSize = Get value... '.i' size
1418 .font$ = extractWord$(.font$, "")
1420 .fontSize = floor(.fontSize*.fontSizeFactor)
1424 .line$ = Get value... '.i' text
1425 # Expand variables, eg, 'praatVersion$'
1426 call expand_praat_variables '.line$'
1427 .line$ = expand_praat_variables.text$
1430 demo Text special... '.textleft' Left '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1434 call set_font_size 'defaultFontSize'
1436 select '.instructionText'
1439 label ESCAPEwrite_text_table
1443 # Create a pop-up window with text from an existing Table object
1444 procedure write_tabbed_table .table$ .labelTextTable$
1451 # Get table with text and longest line
1452 call testLoadTable '.table$'
1453 if testLoadTable.table <= 0
1454 call loadTable '.labelTextTable$'
1455 .labelText$ = selected$("Table")
1458 select Table '.table$'
1459 .tabbedText = selected()
1460 .numLines = Get number of rows
1461 .numCols = Get number of columns
1462 .font$ = defaultFont$
1463 .fontSize = defaultFontSize
1465 .widthCanvas = .xright - .xleft
1466 .dx = (.widthCanvas - 4) / (.numCols)
1470 call set_font_size '.fontSize'
1472 for .i from 0 to .numLines
1473 .xtext = .xleft + .dx / 2
1475 select '.tabbedText'
1476 .currentLabel$ = Get column label... '.j'
1478 .line$ = Get value... '.i' '.currentLabel$'
1480 .line$ = .currentLabel$
1481 select Table '.labelText$'
1482 call findLabel '.labelText$' '.line$'
1483 select Table '.labelText$'
1484 .line$ = Get value... 'findLabel.row' Text
1486 # Expand variables, eg, 'praatVersion$'
1487 call expand_praat_variables '.line$'
1488 .line$ = expand_praat_variables.text$
1489 .textWidth = demo Text width (wc)... '.line$'
1490 if .textWidth > .maxWidth
1491 .maxWidth = .textWidth
1495 if .dx > 1.2 * .maxWidth
1496 .widthCanvas = 1.2 * .maxWidth * .numCols + 4
1497 .xleft = 50 - .widthCanvas / 2
1498 .xright = 50 + .widthCanvas / 2
1499 .dx = (.widthCanvas - 4) / (.numCols)
1504 # Calculate length from number of lines.
1505 .dy = .lineHeight + 0.5
1506 .midY = .yhigh - (.yhigh - .ylow)/2
1507 .yhigh = .midY + (.numLines+2) * .dy / 2
1508 .ylow = .yhigh - (.numLines+2) * .dy
1509 .textleft = .xleft + 2
1511 demo Line width... 8
1512 demo Colour... 'sgc2.popUp_bordercolor$'
1513 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1514 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1515 demo Line width... 'defaultLineWidth'
1517 .ytext = .yhigh - 2 - .dy
1518 # First the column names, then the items
1519 for .i from 0 to .numLines
1520 .xtext = .textleft + .dx / 2
1522 select '.tabbedText'
1523 .currentLabel$ = Get column label... '.j'
1525 .line$ = Get value... '.i' '.currentLabel$'
1527 .line$ = .currentLabel$
1528 select Table '.labelText$'
1529 call findLabel '.labelText$' '.line$'
1530 select Table '.labelText$'
1531 .line$ = Get value... 'findLabel.row' Text
1533 # Expand variables, eg, 'praatVersion$'
1534 call expand_praat_variables '.line$'
1535 .line$ = expand_praat_variables.text$
1536 call adjustFontSizeOnWidth '.font$' '.fontSize' '.maxWidth' '.line$'
1537 .currentFontSize = adjustFontSizeOnWidth.newFontSize
1540 demo Text special... '.xtext' Centre '.ytext' Bottom '.font$' '.currentFontSize' 0 '.line$'
1546 call set_font_size 'defaultFontSize'
1547 select Table '.labelText$'
1550 label ESCAPEwrite_tabbed_table
1553 # Create a pop-up window with a given text
1554 procedure write_text_popup .font$ .size .text$
1561 # Adapt size of button to length of text
1562 .maxWidth = (.xright - .xleft) - 4
1563 call adjustFontSizeOnWidth 'defaultFont$' '.size' '.maxWidth' '.text$'
1564 call adjustFontSizeOnHeight 'defaultFont$' '.size' '.lineHeight'
1565 .popupFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1566 if adjustFontSizeOnWidth.diff > 0
1567 .xright += adjustFontSizeOnWidth.diff/4
1568 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1570 .xleft = ((.xright + .xleft) - adjustFontSizeOnWidth.textWidth)/2 - 2
1571 .xright = ((.xright + .xleft) + adjustFontSizeOnWidth.textWidth)/2 + 2
1575 # Calculate length from number of lines.
1577 .midY = .yhigh - (.yhigh - .ylow)/2
1578 .yhigh = .midY + (.numRows+1) * .dy / 2
1579 .ylow = .yhigh - (.numRows+1) * .dy
1580 .textleft = .xleft + 2
1581 .xmid = (.textleft + .xright - 2)/2
1583 demo Line width... 8
1584 demo Colour... 'sgc2.popUp_bordercolor$'
1585 demo Paint rectangle... 'sgc2.popUp_backgroundcolor$' '.xleft' '.xright' '.ylow' '.yhigh'
1586 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1587 demo Line width... 'defaultLineWidth'
1589 .ytext = .yhigh - 2 - .dy
1591 demo Text special... '.xmid' Centre '.ytext' Bottom '.font$' '.popupFontSize' 0 '.text$'
1595 call set_font_size 'defaultFontSize'
1598 # Write the background from a Text Table
1599 procedure draw_background .table$
1605 .defaultColour$ = "{0.9,0.9,0.9}"
1606 .defaultAlign$ = "centre"
1608 # Get table with text and longest line
1609 call loadTable '.table$'
1610 .backgroundText = selected()
1611 .numLines = Get number of rows
1612 .backgroundFontSize = 28
1613 .referenceText$ = ""
1620 select '.backgroundText'
1621 .currentText$ = Get value... '.l' text
1622 # Expand variables, eg, 'praatVersion$'
1623 call expand_praat_variables '.currentText$'
1624 .currentText$ = expand_praat_variables.text$
1626 .font$ = Get value... '.l' font
1627 .fontSize = Get value... '.l' size
1628 if .fontSize > .maxFontSize
1629 .maxFontSize = .fontSize
1631 if not startsWith(.font$, "!")
1632 call set_font_size '.fontSize'
1633 .textWidth = demo Text width (wc)... '.currentText$'
1634 if .textWidth > .maxWidth
1635 .maxWidth = .textWidth
1636 .backgroundFontSize = .fontSize
1644 select '.backgroundText'
1645 .referenceText$ = Get value... '.maxLine' text
1646 .maxLineFont$ = Get value... '.maxLine' font
1647 .backgroundFontSize = Get value... '.maxLine' size
1648 .backgroundFontColour$ = Get value... '.maxLine' colour
1649 call set_font_size '.maxFontSize'
1651 .maxFontSize = .backgroundFontSize
1654 # Adapt size of button to length of text
1655 .maxWidth = (.xright - .xleft) - 4
1656 .origFontSize = .backgroundFontSize
1657 call adjustFontSizeOnWidth 'defaultFont$' '.backgroundFontSize' '.maxWidth' '.referenceText$'
1658 .fontSizeFactor = adjustFontSizeOnWidth.newFontSize / .backgroundFontSize
1659 .backgroundFontSize = adjustFontSizeOnWidth.newFontSize
1660 call set_font_size '.backgroundFontSize'
1662 call adjustFontSizeOnHeight 'defaultFont$' '.backgroundFontSize' '.lineHeight'
1663 .lineHeight /= adjustFontSizeOnHeight.newFontSize / .backgroundFontSize
1664 if adjustFontSizeOnHeight.newFontSize >= .origFontSize and (.textLines+1) * .lineHeight > (.yhigh - .ylow - 4)
1665 .lineHeight = (.yhigh - .ylow - 4)/(.textLines + 1)
1666 call adjustFontSizeOnHeight 'defaultFont$' '.maxFontSize' '.lineHeight'
1667 .fontSizeFactor = adjustFontSizeOnHeight.newFontSize / .backgroundFontSize
1670 .numRows = Get number of rows
1671 # Calculate length from number of lines.
1673 .midY = .yhigh - (.yhigh - .ylow)/2
1674 .yhigh = .midY + (.textLines+1) * .dy / 2
1675 .ylow = .yhigh - (.textLines+1) * .dy
1676 .textleft = .xleft + 2
1677 .textright = .xright - 2
1678 .textmid = (.xright - .xleft)/2
1681 .ytext = .yhigh - 2 - .dy
1683 select '.backgroundText'
1684 .font$ = Get value... '.i' font
1685 .fontSize = Get value... '.i' size
1686 .fontColour$ = Get value... '.i' colour
1687 .fontColour$ = replace_regex$(.fontColour$, "^[\- ]$", ".defaultColour$", 1)
1688 .fontAlign$ = Get value... '.i' align
1689 .fontAlign$ = replace_regex$(.fontAlign$, "^[\- ]$", ".defaultAlign$", 1)
1690 .line$ = Get value... '.i' text
1691 # Expand variables, eg, 'praatVersion$'
1692 call expand_praat_variables '.line$'
1693 .line$ = expand_praat_variables.text$
1696 .fontSize = floor(.fontSize*.fontSizeFactor)
1697 if not startsWith(.font$, "!")
1698 .font$ = extractWord$(.font$, "")
1700 if .fontAlign$ = "centre"
1702 elsif .fontAlign$ = "right"
1711 demo Colour... '.fontColour$'
1712 demo Text special... '.xtext' '.fontAlign$' '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1714 elsif .font$ = "!demo command"
1715 demo Colour... '.fontColour$'
1716 .line$ = replace_regex$(.line$, "\{FONTSIZE\$\}", "'.fontSize'", 0)
1717 .line$ = replace_regex$(.line$, "\{XTEXT\$\}", "'.xtext'", 0)
1718 .line$ = replace_regex$(.line$, "\{YTEXT\$\}", "'.ytext'", 0)
1719 .line$ = replace_regex$(.line$, "\{DY\$\}", "'.dy'", 0)
1720 .line$ = replace_regex$(.line$, "\{[^\}]*\}", "", 0)
1721 while index(.line$, "[[")
1722 .nextBracketOpen = index(.line$, "[[")
1723 .nextBracketOpen += 2
1724 .nextBracketClose = index(.line$, "]]")
1725 .bracketLength = .nextBracketClose - .nextBracketOpen
1727 if .bracketLength > 0
1728 .expression$ = mid$(.line$, .nextBracketOpen, .bracketLength)
1729 .expression$ = replace_regex$(.expression$, "\s", "", 0)
1730 if length(.expression$) > 0
1731 # Test expression for security, only allow explicitely defined functions
1732 .allowedStrings$ = "e|pi|not|and|or|div|mod|abs|round|floor|ceiling"
1733 .allowedStrings$ = .allowedStrings$ + "|sqrt|min|max|imin|imax|sin|cos|tan|arcsin|arccos|arctan|arctan2|sinc|sincpi"
1734 .allowedStrings$ = .allowedStrings$ + "|exp|ln|log10|log2|sinh|cosh|tanh|arcsinh|arccosh|arctanh"
1735 .allowedStrings$ = .allowedStrings$ + "|sigmoid|invSigmoid|erf|erfc|randomUniform|randomInteger|randomGauss|randomPoisson"
1736 .allowedStrings$ = .allowedStrings$ + "|lnGamma|gaussP|gaussQ|invGaussQ|chiSquareP|chiSquareQ"
1737 .allowedStrings$ = .allowedStrings$ + "|invChiSquareP|invChiSquareQ|studentP|studentQ|invStudentP|invStudentQ"
1738 .allowedStrings$ = .allowedStrings$ + "|beta|besselI|besselK"
1739 .testExpression$ = replace_regex$(.expression$, "(^|\W)('.allowedStrings$')(?=$|\W)", "\1\3", 0)
1740 .testExpression$ = replace_regex$(.testExpression$, "[0-9\.,\-+/*^()<>= ]", "", 0)
1741 if .testExpression$ = ""
1742 .calc = '.expression$'
1743 .result$ = "'.calc'"
1748 # Replace expression by result
1749 .lastLeft = .nextBracketOpen - 3
1750 .newLine$ = left$(.line$, .lastLeft)
1751 .newLine$ = .newLine$ + .result$
1752 .numCopy = length(.line$) - .nextBracketClose - 1
1753 .newLine$ = .newLine$ + right$(.line$, .numCopy)
1761 call set_font_size 'defaultFontSize'
1763 select '.backgroundText'
1767 procedure convert_praat_to_utf8 .text$
1768 .text$ = replace_regex$(.text$, "\\a""", "\xc3\xa4", 0)
1769 .text$ = replace_regex$(.text$, "\\A""", "\xc3\x84", 0)
1770 .text$ = replace_regex$(.text$, "\\o""", "\xc3\xb6", 0)
1771 .text$ = replace_regex$(.text$, "\\O""", "\xc3\x96", 0)
1772 .text$ = replace_regex$(.text$, "\\u""", "\xc3\xbc", 0)
1773 .text$ = replace_regex$(.text$, "\\U""", "\xc3\x9c", 0)
1774 .text$ = replace_regex$(.text$, "\\i""", "\xc3\xaf", 0)
1775 .text$ = replace_regex$(.text$, "\\I""", "\xc3\x8f", 0)
1776 .text$ = replace_regex$(.text$, "\\e""", "\xc3\xab", 0)
1777 .text$ = replace_regex$(.text$, "\\E""", "\xc3\x8b", 0)
1778 .text$ = replace_regex$(.text$, "\\y""", "\xc3\xbf", 0)
1779 .text$ = replace_regex$(.text$, "\\e'", "\xc3\xa9", 0)
1780 .text$ = replace_regex$(.text$, "\\E'", "\xc3\x89", 0)
1781 .text$ = replace_regex$(.text$, "\\ss", "\xc3\x9f", 0)
1784 procedure convert_praat_to_latin1 .text$
1785 .text$ = replace_regex$(.text$, "\\a""", "\xe4", 0)
1786 .text$ = replace_regex$(.text$, "\\A""", "\xc4", 0)
1787 .text$ = replace_regex$(.text$, "\\o""", "\xf6", 0)
1788 .text$ = replace_regex$(.text$, "\\O""", "\xd6", 0)
1789 .text$ = replace_regex$(.text$, "\\u""", "\xfc", 0)
1790 .text$ = replace_regex$(.text$, "\\U""", "\xdc", 0)
1791 .text$ = replace_regex$(.text$, "\\i""", "\xef", 0)
1792 .text$ = replace_regex$(.text$, "\\I""", "\xcf", 0)
1793 .text$ = replace_regex$(.text$, "\\e""", "\xeb", 0)
1794 .text$ = replace_regex$(.text$, "\\E""", "\xcb", 0)
1795 .text$ = replace_regex$(.text$, "\\y""", "\xff", 0)
1796 .text$ = replace_regex$(.text$, "\\Y""", "Y", 0)
1797 .text$ = replace_regex$(.text$, "\\e'", "\xe9", 0)
1798 .text$ = replace_regex$(.text$, "\\E'", "\xc9", 0)
1799 .text$ = replace_regex$(.text$, "\\ss", "\xdf", 0)
1802 # Expand 'variable$' into the value of variable$.
1803 # Eg, 'praatVersion$' becomes 5.1.45 or whatever is the current version
1804 # Single quotes can be protected by \'
1805 procedure expand_praat_variables .text$
1806 if index(.text$, "'")
1807 .tempText$ = replace_regex$(.text$, "(^|[^\\])'([\w\$\.]+)'", "\1""+\2+""", 0)
1808 .tempText$ = replace_regex$(.tempText$, "[\\]'", "'", 0)
1809 .tempText$ = """"+.tempText$+""""
1810 # Check whether all the variables actually exist. Ignore any variable that does not exist
1811 .checkVars$ = .tempText$
1812 while length(.checkVars$) > 0 and index(.checkVars$, "+")
1813 .start = index(.checkVars$, "+")
1814 .checkVars$ = right$(.checkVars$, length(.checkVars$) - .start)
1815 .end = index(.checkVars$, "+")
1817 .variable$ = left$(.checkVars$, .end - 1)
1818 if not variableExists(.variable$)
1819 .tempText$ = replace$(.tempText$, """+'.variable$'+""", "'"+.variable$+"'", 0)
1821 .checkVars$ = right$(.checkVars$, length(.checkVars$) - .end)
1826 .text$ = '.tempText$'
1830 # Get a time stamp in normalized format
1831 procedure getTimeStamp
1832 .currentDateTime$ = date$()
1833 .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)
1836 # A table error, can be insiduously caused by an outdate preferences file!
1837 procedure emergency_table_exit .message$
1838 # If you come here as a user, your preferences file is borked
1839 if preferencesAppFile$ <> "" and fileReadable(preferencesAppFile$)
1840 deleteFile(preferencesAppFile$)
1845 # Remove previous files from system
1846 procedure clean_up_sound
1847 if recordedSound$ = ""
1848 te.recordedSound = 0
1850 if te.recordedSound > 0
1851 select te.recordedSound
1854 te.recordedSound = 0
1856 if te.recordedPitch > 0
1857 select te.recordedPitch
1859 te.recordedPitch = 0
1863 # Safely read a table
1864 procedure readTable .filename$
1866 if .filename$ <> "" and fileReadable(.filename$) and index_regex(.filename$, "(?i\.(tsv|table|csv))$") > 0
1867 .tableID = nocheck Read from file... '.filename$'
1868 if .tableID = undefined or .tableID <= 0
1871 .fullName$ = selected$ ()
1872 .type$ = extractWord$(.fullName$, "")
1873 if .type$ <> "Table"
1881 # Read feedback table and get keyed text
1882 procedure get_feedback_text .language$ .key$
1883 if not endsWith(feedbackTableName$, "_'.language$'")
1884 if feedbackTableName$ <> ""
1885 select Table 'feedbackTableName$'
1888 call loadTable 'feedbackTablePrefix$'_'.language$'
1889 feedbackTableName$ = selected$("Table")
1891 call findKey 'feedbackTableName$' '.key$'
1893 select Table 'feedbackTableName$'
1894 .text$ = Get value... '.row' Text
1895 # Expand variables, eg, 'praatVersion$'
1896 call expand_praat_variables '.text$'
1897 .text$ = expand_praat_variables.text$