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 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
32 currentLogDirectory$ = ""
37 yFeedback = yWordlist + 5
38 yPinyin = yFeedback + 10
39 yContour = yPinyin + 15
40 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
41 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
42 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
43 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
45 # Pop-Up window colors
46 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
47 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
49 # Initialize parameters
59 config.deleteWordlist = -1
60 localWordlistDir$ = "shellDirectory$/wordlists"
61 config.displayNumbers = 1
62 config.displayPinyin = 1
63 config.displayChar = 1
64 config.displayTrans = 1
65 config.useSoundExample = 1
66 config.synthesis$ = "_DISABLED_"
67 config.input$ = "Microphone"
68 config.showBackground = 1
69 sgc2.noiseThresshold = -30
73 samplingFrequency = 44100
75 samplingFrequency = 44100
77 samplingFrequency = 44100
81 config.language$ = "EN"
83 config.logPerformance = 0
84 preferencesLogDir$ = "'preferencesAppDir$'/log"
85 preferencesTableDir$ = "'preferencesAppDir$'/Data"
86 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
88 # Global word lists must be installed BY THE ADMINISTRATOR
89 # This means, create the directory 'globalwordlists$' and
90 # fill it with wordlist directories containing wordlist.Table
91 # and audio files. They can be copied from a local directory.
93 globaltablelists$ = ""
96 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
97 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
98 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
100 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
101 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
102 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
104 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
105 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
106 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
109 ##############################################################
111 config.shuffleLists = 1
113 defaultFont$ = "Helvetica"
116 # Set up directories if they do not exist already
117 call set_up_directories
119 # Get saved preferences
120 call read_preferences ""
123 speakCommandDir$ = "'preferencesAppDir$'/TTS"
124 speakCommandFile$ = ""
127 # Set inital language
128 call set_language 'config.language$'
131 call load_word_list "'localWordlistDir$'" 0
133 # Set up evaluation table
134 initialiseSGC2.toneevaluation_table$ = ""
135 call initialize_toneevaluation_tables
140 ###############################################################
142 # Obligatory procedures
144 ###############################################################
146 # Initialize Demo Window
147 procedure init_window
149 if config.showBackground
150 call draw_background Background
152 demo Line width... 'defaultLineWidth'
154 call set_font_size 'defaultFontSize'
157 # Define buttons in a table
159 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
160 call set_window_title 'buttons$' '.windowTitle$'
161 # Display the word-list
164 call paint_logging_light
167 # Make sure all Preferences directories are available
168 procedure set_up_directories
169 .dirPath$ = "'preferencesAppDir$'"
170 createDirectory(.dirPath$)
171 createDirectory("'.dirPath$'/wordlists")
172 createDirectory("'.dirPath$'/log")
173 createDirectory("'.dirPath$'/TTS")
177 # Retrieve and store setting between sessions
179 procedure read_preferences .preferencesFile$
180 if not fileReadable(.preferencesFile$)
181 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
183 if fileReadable(.preferencesFile$)
184 Read from file... 'preferencesAppDir$'/sgc2rc.txt
185 .preferenceTable$ = selected$("Table")
186 .numPrefKeys = Get number of rows
187 for .row to .numPrefKeys
188 .variableName$ = Get value... '.row' Key
189 if variableExists(.variableName$)
190 .variableValue = Get value... '.row' Value
191 if .variableValue <> undefined
192 '.variableName$' = '.variableValue'
194 elsif variableExists(.variableName$+"$")
195 .variableValue$ = Get value... '.row' Value
196 .variableName$ = .variableName$+"$"
197 '.variableName$' = "'.variableValue$'"
201 select Table '.preferenceTable$'
206 procedure write_preferences .preferencesFile$
207 Create Table with column names... Preferences 0 Key Value
208 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
209 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
213 for .tableNum from 1 to 2
219 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
221 select Table '.table$'
222 .numTableRows = Get number of rows
223 for .tablerow to .numTableRows
224 select Table '.table$'
225 .label$ = Get value... '.tablerow' Label
226 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
227 .keyName$ = .variableName$
229 if variableExists(.variableName$)
230 .value = '.variableName$'
232 elsif variableExists(.variableName$ + "$")
233 .variableName$ = .variableName$ + "$"
234 .value$ = '.variableName$'
235 elsif index(.variableName$, "_") > 0
236 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
237 .keyName$ = .variableName$
238 select Table Preferences
239 .prefRow = Search column... Key '.keyName$'
241 if variableExists(.variableName$)
242 .value = '.variableName$'
244 elsif variableExists(.variableName$ + "$")
245 .variableName$ = .variableName$ + "$"
246 .value$ = '.variableName$'
252 select Table Preferences
255 Set string value... '.row' Key '.keyName$'
256 Set string value... '.row' Value '.value$'
261 # Some extra settings
262 select Table Preferences
265 Set string value... '.row' Key wordlistDir
266 Set string value... '.row' Value 'localWordlistDir$'
268 select Table Preferences
271 Set string value... '.row' Key wordlistName
272 Set string value... '.row' Value 'wordlistName$'
274 select Table Preferences
275 Write to table file... '.preferencesFile$'
280 ###############################################################
282 # TTS (speech synthesis). Place commands in user info
285 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
286 # speakCommandFile$ = ""
289 .ttscommand$ = "speak"
293 .ttscommand$ = "espeak"
297 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
301 # Global TTS command has precedence
302 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
303 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
304 if config.synthesis$ = "_DISABLED_"
306 config.synthesis$ = "eSpeak"
310 # Local TTS command, if there is no global one
311 # Command does not exist yet, create it if eSpeak is installed
312 # Make sure to quote the path!
313 if speakCommandFile$ = ""
314 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
316 system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
317 .command_path$ < 'speakCommandDir$'/command_path.txt
319 system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
320 .command_path$ < 'speakCommandDir$'/command_path.txt
322 if fileReadable(.ttscommand$)
323 .command_path$ = .ttscommand$
326 # Remove any newlines
327 .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
330 if .command_path$ <> "" and fileReadable(.command_path$)
331 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
332 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
333 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
334 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
337 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
338 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
340 deleteFile("'speakCommandDir$'/command_path.txt")
343 # Close off (other TTS maybe a later option)
344 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
345 config.synthesis$ = "eSpeak"
349 ###############################################################
354 # initialiseSGC2.toneevaluation_table$ = ""
355 procedure initialize_toneevaluation_tables
357 if initialiseSGC2.toneevaluation_table$ != ""
358 select Table 'initialiseSGC2.toneevaluation_table$'
361 initialiseSGC2.toneevaluation_table$ = ""
364 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
365 initialiseSGC2.toneevaluation_table$ = "Performance"
366 call get_toneevaluation_row Total
369 procedure get_toneevaluation_row .tones$
370 select Table 'initialiseSGC2.toneevaluation_table$'
371 .row = Search column... Tones '.tones$'
374 .row = Get number of rows
375 Set string value... '.row' Tones '.tones$'
376 Set numeric value... '.row' Correct 0
377 Set numeric value... '.row' Wrong 0
378 Set numeric value... '.row' Total 0
379 Set numeric value... '.row' High 0
380 Set numeric value... '.row' Low 0
381 Set numeric value... '.row' Wide 0
382 Set numeric value... '.row' Narrow 0
383 Set numeric value... '.row' Unknown 0
384 Set numeric value... '.row' Commented 0
386 .row = Search column... Tones '.tones$'
390 procedure increment_toneevaluation_in_row .row .column$
391 select Table 'initialiseSGC2.toneevaluation_table$'
392 .tmp = Get value... '.row' '.column$'
394 Set numeric value... '.row' '.column$' '.tmp'
397 procedure increment_toneevaluation_value .tones$ .column$
398 call get_toneevaluation_row '.tones$'
399 .row = get_toneevaluation_row.row
400 call increment_toneevaluation_in_row '.row' '.column$'
402 call get_toneevaluation_row Total
403 .row = get_toneevaluation_row.row
404 call increment_toneevaluation_in_row '.row' '.column$'
407 procedure add_feedback_to_toneevaluation .table$
408 select Table '.table$'
409 .line1$ = Get value... 1 Text
410 .line2$ = Get value... 2 Text
411 .label$ = Get value... 3 Text
416 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
417 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
420 call increment_toneevaluation_value '.tones$' Total
421 if .tones$ = .recognized$
422 call increment_toneevaluation_value '.tones$' Correct
424 call increment_toneevaluation_value '.tones$' Wrong
426 if index_regex(.label$, "(Correct|Wrong)") <= 0
427 call increment_toneevaluation_value '.tones$' '.label$'
428 call increment_toneevaluation_value '.tones$' Commented
432 procedure CreateSummaryToneEvaluation
434 # Summarize tone evaluation
435 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
437 call get_toneevaluation_row Total
438 .row = get_toneevaluation_row.row
439 select Table 'initialiseSGC2.toneevaluation_table$'
440 .total = Get value... '.row' Total
441 .correct = Get value... '.row' Correct
443 .correctPerc = 100 * .correct / .total
447 .problematic = Get value... '.row' Commented
449 .problematicPerc = 100 * .problematic / .total
454 select Table SummaryToneEvaluation
456 Set numeric value... 1 Total '.total'
457 Set numeric value... 1 Correct '.correctPerc'
458 Set numeric value... 1 Problematic '.problematicPerc'
461 ###############################################################
463 # Miscelaneous procedures
465 ###############################################################
467 # Create a file 'preferencesAppDir$'/log/logPerformance
468 # This file contains the path to the target directory where logging
470 # Note that this procedure is nowhere used in SGC2. It has to
471 # be explicitely called in a separate script.
473 procedure activate_sgc2_logging .targetDirectory$
474 createDirectory("'preferencesAppDir$'")
475 createDirectory("'preferencesLogDir$'")
477 if startsWith(.targetDirectory$, "~")
478 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
481 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
482 newline$ >> 'preferencesLogDir$'/logPerformance.txt
486 procedure synthesize_sound .pinyin$
488 if fileReadable(speakCommandFile$)
489 .command$ < 'speakCommandFile$'
490 .command$ = replace$(.command$, "'newline$'", " ", 0)
492 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
494 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
496 system_nocheck call '.command$' "'.pinyin$'"'