4 # Intializing Praat script
6 # SpeakGoodChinese: InitializeSGC2.praat defines and sets the global variables
7 # and loads general SGC2 code
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
29 # Information for logging
30 currentDate$ = date$()
31 dateYear$ = right$(currentDate$, length(currentDate$) - rindex(currentDate$, " "))
32 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
33 currentLogDirectory$ = ""
38 yFeedback = yWordlist + 5
39 yPinyin = yFeedback + 10
40 yContour = yPinyin + 15
41 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
42 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
43 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
44 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
46 # Pop-Up window colors
47 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
48 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
50 # Initialize parameters
60 config.deleteWordlist = -1
61 config.displayNumbers = 1
62 config.displayPinyin = 1
63 config.displayChar = 0
64 config.displayTrans = 1
65 config.useSoundExample = 1
66 config.synthesis$ = "_DISABLED_"
67 config.input$ = "Microphone"
68 config.showBackground = 1
69 sgc2.noiseThresshold = -30
72 # Platform dependent settings
73 if macintosh or windows
74 config.displayChar = 1
75 config.displayNumbers = 0
77 if windows and endsWith(build_SHA$, " XP")
78 config.displayChar = 0
79 config.displayNumbers = 0
82 config.displayChar = 0
83 config.displayNumbers = 1
87 samplingFrequency = 44100
89 samplingFrequency = 44100
91 samplingFrequency = 44100
96 config.language$ = "EN"
98 config.logPerformance = 0
99 preferencesLogDir$ = "'preferencesAppDir$'/log"
100 preferencesTableDir$ = "'preferencesAppDir$'/Data"
101 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
102 localWordlistDir$ = sgc2wordlists$
104 # Global word lists must be installed BY THE ADMINISTRATOR
105 # This means, create the directory 'globalwordlists$' and
106 # fill it with wordlist directories containing wordlist.Table
107 # and audio files. They can be copied from a local directory.
108 globalwordlists$ = ""
109 globaltablelists$ = ""
112 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
113 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
114 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
116 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
117 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
118 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
120 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
121 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
122 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
124 sgc2.synthesizer = -1
125 sgc2.normalTTScommand$ = "Create SpeechSynthesizer... Mandarin f3 10000 0.01 50 100 100 yes no"
126 sgc2.advancedTTScommand$ = "Create SpeechSynthesizer... Mandarin f3 10000 0.01 50 70 125 yes no"
127 sgc2.alignedTextGrid = -1
129 ##############################################################
131 config.shuffleLists = 1
133 defaultFont$ = "Helvetica"
136 # Set up directories if they do not exist already
137 call set_up_directories
139 # Get saved preferences
140 call read_preferences ""
143 speakCommandDir$ = "'preferencesAppDir$'/TTS"
144 speakCommandFile$ = ""
147 # Set inital language
148 call set_language 'config.language$'
151 call load_word_list "'localWordlistDir$'" 0
153 # Set up evaluation table
154 initialiseSGC2.toneevaluation_table$ = ""
155 call initialize_toneevaluation_tables
160 ###############################################################
162 # Obligatory procedures
164 ###############################################################
165 # Initialize Demo Window
166 procedure init_window
168 if config.showBackground
169 call draw_background Background
171 demo Line width... 'defaultLineWidth'
173 call set_font_size 'defaultFontSize'
176 # Define buttons in a table
178 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
179 call set_window_title 'buttons$' '.windowTitle$'
180 # Display the word-list
183 call paint_logging_light
186 # Make sure all Preferences directories are available
187 procedure set_up_directories
188 .dirPath$ = "'preferencesAppDir$'"
189 createDirectory(.dirPath$)
190 .dirPath$ > '.dirPath$'/directory.txt
191 createDirectory("'.dirPath$'/wordlists")
192 .dirPath$ > '.dirPath$'/wordlists/directory.txt
193 createDirectory("'.dirPath$'/log")
194 .dirPath$ > '.dirPath$'/log/directory.txt
195 createDirectory("'.dirPath$'/TTS")
196 .dirPath$ > '.dirPath$'/TTS/directory.txt
200 # Retrieve and store setting between sessions
202 procedure read_preferences .preferencesFile$
203 if not fileReadable(.preferencesFile$)
204 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
206 if fileReadable(.preferencesFile$)
207 Read from file... 'preferencesAppDir$'/sgc2rc.txt
208 .preferenceTable$ = selected$("Table")
209 .numPrefKeys = Get number of rows
210 for .row to .numPrefKeys
211 .variableName$ = Get value... '.row' Key
212 if variableExists(.variableName$)
213 .variableValue = Get value... '.row' Value
214 if .variableValue <> undefined
215 '.variableName$' = '.variableValue'
217 elsif variableExists(.variableName$+"$")
218 .variableValue$ = Get value... '.row' Value
219 .variableName$ = .variableName$+"$"
220 '.variableName$' = "'.variableValue$'"
224 select Table '.preferenceTable$'
229 procedure write_preferences .preferencesFile$
230 Create Table with column names... Preferences 0 Key Value
231 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
232 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
236 for .tableNum from 1 to 2
242 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
244 select Table '.table$'
245 .numTableRows = Get number of rows
246 for .tablerow to .numTableRows
247 select Table '.table$'
248 .label$ = Get value... '.tablerow' Label
249 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
250 .keyName$ = .variableName$
252 if variableExists(.variableName$)
253 .value = '.variableName$'
255 elsif variableExists(.variableName$ + "$")
256 .variableName$ = .variableName$ + "$"
257 .value$ = '.variableName$'
258 elsif index(.variableName$, "_") > 0
259 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
260 .keyName$ = .variableName$
261 select Table Preferences
262 .prefRow = Search column... Key '.keyName$'
264 if variableExists(.variableName$)
265 .value = '.variableName$'
267 elsif variableExists(.variableName$ + "$")
268 .variableName$ = .variableName$ + "$"
269 .value$ = '.variableName$'
275 select Table Preferences
278 Set string value... '.row' Key '.keyName$'
279 Set string value... '.row' Value '.value$'
284 # Some extra settings
285 select Table Preferences
288 Set string value... '.row' Key wordlistDir
289 Set string value... '.row' Value 'localWordlistDir$'
291 select Table Preferences
294 Set string value... '.row' Key wordlistName
295 Set string value... '.row' Value 'wordlistName$'
297 select Table Preferences
298 Write to table file... '.preferencesFile$'
303 ###############################################################
305 # TTS (speech synthesis). Place commands in user info
308 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
309 # speakCommandFile$ = ""
311 if sgc2.synthesizer > 0
312 select sgc2.synthesizer
314 sgc2.synthesizer = -1
317 sgc2.synthesizer = 'sgc2.advancedTTScommand$'
319 sgc2.synthesizer = 'sgc2.normalTTScommand$'
323 .ttscommand$ = "speak"
327 .ttscommand$ = "espeak"
331 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
335 # Global TTS command has precedence
336 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
337 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
338 if config.synthesis$ = "_DISABLED_"
340 config.synthesis$ = "eSpeak"
344 # Local TTS command, if there is no global one
345 # Command does not exist yet, create it if eSpeak is installed
346 # Make sure to quote the path!
347 if speakCommandFile$ = ""
348 # Autodetect synthesizer command
349 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
352 system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
353 .command_path$ < 'speakCommandDir$'/command_path.txt
355 system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
356 .command_path$ < 'speakCommandDir$'/command_path.txt
358 if fileReadable(.ttscommand$)
359 .command_path$ = .ttscommand$
362 # Remove any newlines
363 .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
366 if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
367 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
368 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
369 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
370 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
373 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
374 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
376 deleteFile("'speakCommandDir$'/command_path.txt")
379 # Close off (other TTS maybe a later option)
380 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
381 config.synthesis$ = "eSpeak"
385 ###############################################################
390 # initialiseSGC2.toneevaluation_table$ = ""
391 procedure initialize_toneevaluation_tables
393 if initialiseSGC2.toneevaluation_table$ != ""
394 select Table 'initialiseSGC2.toneevaluation_table$'
397 initialiseSGC2.toneevaluation_table$ = ""
400 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
401 initialiseSGC2.toneevaluation_table$ = "Performance"
402 call get_toneevaluation_row Total
405 procedure get_toneevaluation_row .tones$
406 select Table 'initialiseSGC2.toneevaluation_table$'
407 .row = Search column... Tones '.tones$'
410 .row = Get number of rows
411 Set string value... '.row' Tones '.tones$'
412 Set numeric value... '.row' Correct 0
413 Set numeric value... '.row' Wrong 0
414 Set numeric value... '.row' Total 0
415 Set numeric value... '.row' High 0
416 Set numeric value... '.row' Low 0
417 Set numeric value... '.row' Wide 0
418 Set numeric value... '.row' Narrow 0
419 Set numeric value... '.row' Unknown 0
420 Set numeric value... '.row' Commented 0
422 .row = Search column... Tones '.tones$'
426 procedure increment_toneevaluation_in_row .row .column$
427 select Table 'initialiseSGC2.toneevaluation_table$'
428 .tmp = Get value... '.row' '.column$'
430 Set numeric value... '.row' '.column$' '.tmp'
433 procedure increment_toneevaluation_value .tones$ .column$
434 call get_toneevaluation_row '.tones$'
435 .row = get_toneevaluation_row.row
436 call increment_toneevaluation_in_row '.row' '.column$'
438 call get_toneevaluation_row Total
439 .row = get_toneevaluation_row.row
440 call increment_toneevaluation_in_row '.row' '.column$'
443 procedure add_feedback_to_toneevaluation .table$
444 select Table '.table$'
445 .line1$ = Get value... 1 Text
446 .line2$ = Get value... 2 Text
447 .label$ = Get value... 3 Text
452 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
453 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
456 call increment_toneevaluation_value '.tones$' Total
457 if .tones$ = .recognized$
458 call increment_toneevaluation_value '.tones$' Correct
460 call increment_toneevaluation_value '.tones$' Wrong
462 if index_regex(.label$, "(Correct|Wrong)") <= 0
463 call increment_toneevaluation_value '.tones$' '.label$'
464 call increment_toneevaluation_value '.tones$' Commented
468 procedure CreateSummaryToneEvaluation
470 # Summarize tone evaluation
471 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
473 call get_toneevaluation_row Total
474 .row = get_toneevaluation_row.row
475 select Table 'initialiseSGC2.toneevaluation_table$'
476 .total = Get value... '.row' Total
477 .correct = Get value... '.row' Correct
479 .correctPerc = 100 * .correct / .total
483 .problematic = Get value... '.row' Commented
485 .problematicPerc = 100 * .problematic / .total
490 select Table SummaryToneEvaluation
492 Set numeric value... 1 Total '.total'
493 Set numeric value... 1 Correct '.correctPerc'
494 Set numeric value... 1 Problematic '.problematicPerc'
497 ###############################################################
499 # Miscelaneous procedures
501 ###############################################################
503 # Create a file 'preferencesAppDir$'/log/logPerformance
504 # This file contains the path to the target directory where logging
506 # Note that this procedure is nowhere used in SGC2. It has to
507 # be explicitely called in a separate script.
509 procedure activate_sgc2_logging .targetDirectory$
510 createDirectory("'preferencesAppDir$'")
511 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
512 createDirectory("'preferencesLogDir$'")
513 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
515 if startsWith(.targetDirectory$, "~")
516 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
519 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
520 newline$ >> 'preferencesLogDir$'/logPerformance.txt
524 procedure synthesize_sound .pinyin$
526 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
527 .command$ < 'speakCommandFile$'
528 .command$ = replace$(.command$, "'newline$'", " ", 0)
530 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
532 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
534 system_nocheck call '.command$' "'.pinyin$'"'
536 elsif sgc2.synthesizer > 0
537 select sgc2.synthesizer
538 Play text... '.pinyin$'
542 procedure align_recordedSound .pinyin$
544 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
545 select Sound 'recordedSound$'
546 .recordedTextGrid = To TextGrid... pinyin
547 Set interval text... 1 1 '.pinyin$'
548 select sgc2.synthesizer
549 plus Sound 'recordedSound$'
550 plus .recordedTextGrid
551 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align)... 1 1 1 -35 0.1 0.1
552 Rename AlignedTextGrid
553 select .recordedTextGrid