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"
125 ##############################################################
127 config.shuffleLists = 1
129 defaultFont$ = "Helvetica"
132 # Set up directories if they do not exist already
133 call set_up_directories
135 # Get saved preferences
136 call read_preferences ""
139 speakCommandDir$ = "'preferencesAppDir$'/TTS"
140 speakCommandFile$ = ""
143 # Set inital language
144 call set_language 'config.language$'
147 call load_word_list "'localWordlistDir$'" 0
149 # Set up evaluation table
150 initialiseSGC2.toneevaluation_table$ = ""
151 call initialize_toneevaluation_tables
156 ###############################################################
158 # Obligatory procedures
160 ###############################################################
161 # Initialize Demo Window
162 procedure init_window
164 if config.showBackground
165 call draw_background Background
167 demo Line width... 'defaultLineWidth'
169 call set_font_size 'defaultFontSize'
172 # Define buttons in a table
174 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
175 call set_window_title 'buttons$' '.windowTitle$'
176 # Display the word-list
179 call paint_logging_light
182 # Make sure all Preferences directories are available
183 procedure set_up_directories
184 .dirPath$ = "'preferencesAppDir$'"
185 createDirectory(.dirPath$)
186 .dirPath$ > '.dirPath$'/directory.txt
187 createDirectory("'.dirPath$'/wordlists")
188 .dirPath$ > '.dirPath$'/wordlists/directory.txt
189 createDirectory("'.dirPath$'/log")
190 .dirPath$ > '.dirPath$'/log/directory.txt
191 createDirectory("'.dirPath$'/TTS")
192 .dirPath$ > '.dirPath$'/TTS/directory.txt
196 # Retrieve and store setting between sessions
198 procedure read_preferences .preferencesFile$
199 if not fileReadable(.preferencesFile$)
200 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
202 if fileReadable(.preferencesFile$)
203 Read from file... 'preferencesAppDir$'/sgc2rc.txt
204 .preferenceTable$ = selected$("Table")
205 .numPrefKeys = Get number of rows
206 for .row to .numPrefKeys
207 .variableName$ = Get value... '.row' Key
208 if variableExists(.variableName$)
209 .variableValue = Get value... '.row' Value
210 if .variableValue <> undefined
211 '.variableName$' = '.variableValue'
213 elsif variableExists(.variableName$+"$")
214 .variableValue$ = Get value... '.row' Value
215 .variableName$ = .variableName$+"$"
216 '.variableName$' = "'.variableValue$'"
220 select Table '.preferenceTable$'
225 procedure write_preferences .preferencesFile$
226 Create Table with column names... Preferences 0 Key Value
227 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
228 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
232 for .tableNum from 1 to 2
238 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
240 select Table '.table$'
241 .numTableRows = Get number of rows
242 for .tablerow to .numTableRows
243 select Table '.table$'
244 .label$ = Get value... '.tablerow' Label
245 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
246 .keyName$ = .variableName$
248 if variableExists(.variableName$)
249 .value = '.variableName$'
251 elsif variableExists(.variableName$ + "$")
252 .variableName$ = .variableName$ + "$"
253 .value$ = '.variableName$'
254 elsif index(.variableName$, "_") > 0
255 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
256 .keyName$ = .variableName$
257 select Table Preferences
258 .prefRow = Search column... Key '.keyName$'
260 if variableExists(.variableName$)
261 .value = '.variableName$'
263 elsif variableExists(.variableName$ + "$")
264 .variableName$ = .variableName$ + "$"
265 .value$ = '.variableName$'
271 select Table Preferences
274 Set string value... '.row' Key '.keyName$'
275 Set string value... '.row' Value '.value$'
280 # Some extra settings
281 select Table Preferences
284 Set string value... '.row' Key wordlistDir
285 Set string value... '.row' Value 'localWordlistDir$'
287 select Table Preferences
290 Set string value... '.row' Key wordlistName
291 Set string value... '.row' Value 'wordlistName$'
293 select Table Preferences
294 Write to table file... '.preferencesFile$'
299 ###############################################################
301 # TTS (speech synthesis). Place commands in user info
304 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
305 # speakCommandFile$ = ""
307 sgc.synthesizer = Create SpeechSynthesizer... Mandarin m1 44100 0.01 50 100 100 yes no
310 .ttscommand$ = "speak"
314 .ttscommand$ = "espeak"
318 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
322 # Global TTS command has precedence
323 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
324 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
325 if config.synthesis$ = "_DISABLED_"
327 config.synthesis$ = "eSpeak"
331 # Local TTS command, if there is no global one
332 # Command does not exist yet, create it if eSpeak is installed
333 # Make sure to quote the path!
334 if speakCommandFile$ = ""
335 # Autodetect synthesizer command
336 if not (sgc.synthesizer > 0 or fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"))
339 system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
340 .command_path$ < 'speakCommandDir$'/command_path.txt
342 system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
343 .command_path$ < 'speakCommandDir$'/command_path.txt
345 if fileReadable(.ttscommand$)
346 .command_path$ = .ttscommand$
349 # Remove any newlines
350 .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
353 if .command_path$ <> "" and fileReadable(.command_path$)
354 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
355 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
356 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
357 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
360 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
361 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
363 deleteFile("'speakCommandDir$'/command_path.txt")
366 # Close off (other TTS maybe a later option)
367 if sgc.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
368 config.synthesis$ = "eSpeak"
372 ###############################################################
377 # initialiseSGC2.toneevaluation_table$ = ""
378 procedure initialize_toneevaluation_tables
380 if initialiseSGC2.toneevaluation_table$ != ""
381 select Table 'initialiseSGC2.toneevaluation_table$'
384 initialiseSGC2.toneevaluation_table$ = ""
387 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
388 initialiseSGC2.toneevaluation_table$ = "Performance"
389 call get_toneevaluation_row Total
392 procedure get_toneevaluation_row .tones$
393 select Table 'initialiseSGC2.toneevaluation_table$'
394 .row = Search column... Tones '.tones$'
397 .row = Get number of rows
398 Set string value... '.row' Tones '.tones$'
399 Set numeric value... '.row' Correct 0
400 Set numeric value... '.row' Wrong 0
401 Set numeric value... '.row' Total 0
402 Set numeric value... '.row' High 0
403 Set numeric value... '.row' Low 0
404 Set numeric value... '.row' Wide 0
405 Set numeric value... '.row' Narrow 0
406 Set numeric value... '.row' Unknown 0
407 Set numeric value... '.row' Commented 0
409 .row = Search column... Tones '.tones$'
413 procedure increment_toneevaluation_in_row .row .column$
414 select Table 'initialiseSGC2.toneevaluation_table$'
415 .tmp = Get value... '.row' '.column$'
417 Set numeric value... '.row' '.column$' '.tmp'
420 procedure increment_toneevaluation_value .tones$ .column$
421 call get_toneevaluation_row '.tones$'
422 .row = get_toneevaluation_row.row
423 call increment_toneevaluation_in_row '.row' '.column$'
425 call get_toneevaluation_row Total
426 .row = get_toneevaluation_row.row
427 call increment_toneevaluation_in_row '.row' '.column$'
430 procedure add_feedback_to_toneevaluation .table$
431 select Table '.table$'
432 .line1$ = Get value... 1 Text
433 .line2$ = Get value... 2 Text
434 .label$ = Get value... 3 Text
439 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
440 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
443 call increment_toneevaluation_value '.tones$' Total
444 if .tones$ = .recognized$
445 call increment_toneevaluation_value '.tones$' Correct
447 call increment_toneevaluation_value '.tones$' Wrong
449 if index_regex(.label$, "(Correct|Wrong)") <= 0
450 call increment_toneevaluation_value '.tones$' '.label$'
451 call increment_toneevaluation_value '.tones$' Commented
455 procedure CreateSummaryToneEvaluation
457 # Summarize tone evaluation
458 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
460 call get_toneevaluation_row Total
461 .row = get_toneevaluation_row.row
462 select Table 'initialiseSGC2.toneevaluation_table$'
463 .total = Get value... '.row' Total
464 .correct = Get value... '.row' Correct
466 .correctPerc = 100 * .correct / .total
470 .problematic = Get value... '.row' Commented
472 .problematicPerc = 100 * .problematic / .total
477 select Table SummaryToneEvaluation
479 Set numeric value... 1 Total '.total'
480 Set numeric value... 1 Correct '.correctPerc'
481 Set numeric value... 1 Problematic '.problematicPerc'
484 ###############################################################
486 # Miscelaneous procedures
488 ###############################################################
490 # Create a file 'preferencesAppDir$'/log/logPerformance
491 # This file contains the path to the target directory where logging
493 # Note that this procedure is nowhere used in SGC2. It has to
494 # be explicitely called in a separate script.
496 procedure activate_sgc2_logging .targetDirectory$
497 createDirectory("'preferencesAppDir$'")
498 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
499 createDirectory("'preferencesLogDir$'")
500 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
502 if startsWith(.targetDirectory$, "~")
503 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
506 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
507 newline$ >> 'preferencesLogDir$'/logPerformance.txt
511 procedure synthesize_sound .pinyin$
513 if sgc.synthesizer > 0
514 select sgc.synthesizer
515 Play text... '.pinyin$'
516 elsif fileReadable(speakCommandFile$)
517 .command$ < 'speakCommandFile$'
518 .command$ = replace$(.command$, "'newline$'", " ", 0)
520 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
522 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
524 system_nocheck call '.command$' "'.pinyin$'"'