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 44100 0.01 50 100 100 yes no"
126 sgc2.advancedTTScommand$ = "Create SpeechSynthesizer... Mandarin f3 44100 0.01 50 70 125 yes no"
128 ##############################################################
130 config.shuffleLists = 1
132 defaultFont$ = "Helvetica"
135 # Set up directories if they do not exist already
136 call set_up_directories
138 # Get saved preferences
139 call read_preferences ""
142 speakCommandDir$ = "'preferencesAppDir$'/TTS"
143 speakCommandFile$ = ""
146 # Set inital language
147 call set_language 'config.language$'
150 call load_word_list "'localWordlistDir$'" 0
152 # Set up evaluation table
153 initialiseSGC2.toneevaluation_table$ = ""
154 call initialize_toneevaluation_tables
159 ###############################################################
161 # Obligatory procedures
163 ###############################################################
164 # Initialize Demo Window
165 procedure init_window
167 if config.showBackground
168 call draw_background Background
170 demo Line width... 'defaultLineWidth'
172 call set_font_size 'defaultFontSize'
175 # Define buttons in a table
177 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
178 call set_window_title 'buttons$' '.windowTitle$'
179 # Display the word-list
182 call paint_logging_light
185 # Make sure all Preferences directories are available
186 procedure set_up_directories
187 .dirPath$ = "'preferencesAppDir$'"
188 createDirectory(.dirPath$)
189 .dirPath$ > '.dirPath$'/directory.txt
190 createDirectory("'.dirPath$'/wordlists")
191 .dirPath$ > '.dirPath$'/wordlists/directory.txt
192 createDirectory("'.dirPath$'/log")
193 .dirPath$ > '.dirPath$'/log/directory.txt
194 createDirectory("'.dirPath$'/TTS")
195 .dirPath$ > '.dirPath$'/TTS/directory.txt
199 # Retrieve and store setting between sessions
201 procedure read_preferences .preferencesFile$
202 if not fileReadable(.preferencesFile$)
203 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
205 if fileReadable(.preferencesFile$)
206 Read from file... 'preferencesAppDir$'/sgc2rc.txt
207 .preferenceTable$ = selected$("Table")
208 .numPrefKeys = Get number of rows
209 for .row to .numPrefKeys
210 .variableName$ = Get value... '.row' Key
211 if variableExists(.variableName$)
212 .variableValue = Get value... '.row' Value
213 if .variableValue <> undefined
214 '.variableName$' = '.variableValue'
216 elsif variableExists(.variableName$+"$")
217 .variableValue$ = Get value... '.row' Value
218 .variableName$ = .variableName$+"$"
219 '.variableName$' = "'.variableValue$'"
223 select Table '.preferenceTable$'
228 procedure write_preferences .preferencesFile$
229 Create Table with column names... Preferences 0 Key Value
230 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
231 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
235 for .tableNum from 1 to 2
241 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
243 select Table '.table$'
244 .numTableRows = Get number of rows
245 for .tablerow to .numTableRows
246 select Table '.table$'
247 .label$ = Get value... '.tablerow' Label
248 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
249 .keyName$ = .variableName$
251 if variableExists(.variableName$)
252 .value = '.variableName$'
254 elsif variableExists(.variableName$ + "$")
255 .variableName$ = .variableName$ + "$"
256 .value$ = '.variableName$'
257 elsif index(.variableName$, "_") > 0
258 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
259 .keyName$ = .variableName$
260 select Table Preferences
261 .prefRow = Search column... Key '.keyName$'
263 if variableExists(.variableName$)
264 .value = '.variableName$'
266 elsif variableExists(.variableName$ + "$")
267 .variableName$ = .variableName$ + "$"
268 .value$ = '.variableName$'
274 select Table Preferences
277 Set string value... '.row' Key '.keyName$'
278 Set string value... '.row' Value '.value$'
283 # Some extra settings
284 select Table Preferences
287 Set string value... '.row' Key wordlistDir
288 Set string value... '.row' Value 'localWordlistDir$'
290 select Table Preferences
293 Set string value... '.row' Key wordlistName
294 Set string value... '.row' Value 'wordlistName$'
296 select Table Preferences
297 Write to table file... '.preferencesFile$'
302 ###############################################################
304 # TTS (speech synthesis). Place commands in user info
307 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
308 # speakCommandFile$ = ""
310 if sgc2.synthesizer > 0
311 select sgc2.synthesizer
313 sgc2.synthesizer = -1
316 sgc2.synthesizer = 'sgc2.advancedTTScommand$'
318 sgc2.synthesizer = 'sgc2.normalTTScommand$'
322 .ttscommand$ = "speak"
326 .ttscommand$ = "espeak"
330 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
334 # Global TTS command has precedence
335 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
336 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
337 if config.synthesis$ = "_DISABLED_"
339 config.synthesis$ = "eSpeak"
343 # Local TTS command, if there is no global one
344 # Command does not exist yet, create it if eSpeak is installed
345 # Make sure to quote the path!
346 if speakCommandFile$ = ""
347 # Autodetect synthesizer command
348 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
351 system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
352 .command_path$ < 'speakCommandDir$'/command_path.txt
354 system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
355 .command_path$ < 'speakCommandDir$'/command_path.txt
357 if fileReadable(.ttscommand$)
358 .command_path$ = .ttscommand$
361 # Remove any newlines
362 .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
365 if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
366 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
367 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
368 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
369 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
372 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
373 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
375 deleteFile("'speakCommandDir$'/command_path.txt")
378 # Close off (other TTS maybe a later option)
379 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
380 config.synthesis$ = "eSpeak"
384 ###############################################################
389 # initialiseSGC2.toneevaluation_table$ = ""
390 procedure initialize_toneevaluation_tables
392 if initialiseSGC2.toneevaluation_table$ != ""
393 select Table 'initialiseSGC2.toneevaluation_table$'
396 initialiseSGC2.toneevaluation_table$ = ""
399 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
400 initialiseSGC2.toneevaluation_table$ = "Performance"
401 call get_toneevaluation_row Total
404 procedure get_toneevaluation_row .tones$
405 select Table 'initialiseSGC2.toneevaluation_table$'
406 .row = Search column... Tones '.tones$'
409 .row = Get number of rows
410 Set string value... '.row' Tones '.tones$'
411 Set numeric value... '.row' Correct 0
412 Set numeric value... '.row' Wrong 0
413 Set numeric value... '.row' Total 0
414 Set numeric value... '.row' High 0
415 Set numeric value... '.row' Low 0
416 Set numeric value... '.row' Wide 0
417 Set numeric value... '.row' Narrow 0
418 Set numeric value... '.row' Unknown 0
419 Set numeric value... '.row' Commented 0
421 .row = Search column... Tones '.tones$'
425 procedure increment_toneevaluation_in_row .row .column$
426 select Table 'initialiseSGC2.toneevaluation_table$'
427 .tmp = Get value... '.row' '.column$'
429 Set numeric value... '.row' '.column$' '.tmp'
432 procedure increment_toneevaluation_value .tones$ .column$
433 call get_toneevaluation_row '.tones$'
434 .row = get_toneevaluation_row.row
435 call increment_toneevaluation_in_row '.row' '.column$'
437 call get_toneevaluation_row Total
438 .row = get_toneevaluation_row.row
439 call increment_toneevaluation_in_row '.row' '.column$'
442 procedure add_feedback_to_toneevaluation .table$
443 select Table '.table$'
444 .line1$ = Get value... 1 Text
445 .line2$ = Get value... 2 Text
446 .label$ = Get value... 3 Text
451 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
452 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
455 call increment_toneevaluation_value '.tones$' Total
456 if .tones$ = .recognized$
457 call increment_toneevaluation_value '.tones$' Correct
459 call increment_toneevaluation_value '.tones$' Wrong
461 if index_regex(.label$, "(Correct|Wrong)") <= 0
462 call increment_toneevaluation_value '.tones$' '.label$'
463 call increment_toneevaluation_value '.tones$' Commented
467 procedure CreateSummaryToneEvaluation
469 # Summarize tone evaluation
470 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
472 call get_toneevaluation_row Total
473 .row = get_toneevaluation_row.row
474 select Table 'initialiseSGC2.toneevaluation_table$'
475 .total = Get value... '.row' Total
476 .correct = Get value... '.row' Correct
478 .correctPerc = 100 * .correct / .total
482 .problematic = Get value... '.row' Commented
484 .problematicPerc = 100 * .problematic / .total
489 select Table SummaryToneEvaluation
491 Set numeric value... 1 Total '.total'
492 Set numeric value... 1 Correct '.correctPerc'
493 Set numeric value... 1 Problematic '.problematicPerc'
496 ###############################################################
498 # Miscelaneous procedures
500 ###############################################################
502 # Create a file 'preferencesAppDir$'/log/logPerformance
503 # This file contains the path to the target directory where logging
505 # Note that this procedure is nowhere used in SGC2. It has to
506 # be explicitely called in a separate script.
508 procedure activate_sgc2_logging .targetDirectory$
509 createDirectory("'preferencesAppDir$'")
510 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
511 createDirectory("'preferencesLogDir$'")
512 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
514 if startsWith(.targetDirectory$, "~")
515 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
518 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
519 newline$ >> 'preferencesLogDir$'/logPerformance.txt
523 procedure synthesize_sound .pinyin$
525 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
526 .command$ < 'speakCommandFile$'
527 .command$ = replace$(.command$, "'newline$'", " ", 0)
529 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
531 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
533 system_nocheck call '.command$' "'.pinyin$'"'
535 elsif sgc2.synthesizer > 0
536 select sgc2.synthesizer
537 Play text... '.pinyin$'