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 localWordlistDir$ = "shellDirectory$/wordlists"
62 config.displayNumbers = 1
63 config.displayPinyin = 1
64 config.displayChar = 1
65 config.displayTrans = 1
66 config.useSoundExample = 1
67 config.synthesis$ = "_DISABLED_"
68 config.input$ = "Microphone"
69 config.showBackground = 1
70 sgc2.noiseThresshold = -30
74 samplingFrequency = 44100
76 samplingFrequency = 44100
78 samplingFrequency = 44100
82 config.language$ = "EN"
84 config.logPerformance = 0
85 preferencesLogDir$ = "'preferencesAppDir$'/log"
86 preferencesTableDir$ = "'preferencesAppDir$'/Data"
87 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
89 # Global word lists must be installed BY THE ADMINISTRATOR
90 # This means, create the directory 'globalwordlists$' and
91 # fill it with wordlist directories containing wordlist.Table
92 # and audio files. They can be copied from a local directory.
94 globaltablelists$ = ""
97 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
98 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
99 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
101 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
102 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
103 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
105 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
106 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
107 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
110 ##############################################################
112 config.shuffleLists = 1
114 defaultFont$ = "Helvetica"
117 # Set up directories if they do not exist already
118 call set_up_directories
120 # Get saved preferences
121 call read_preferences ""
124 speakCommandDir$ = "'preferencesAppDir$'/TTS"
125 speakCommandFile$ = ""
128 # Set inital language
129 call set_language 'config.language$'
132 call load_word_list "'localWordlistDir$'" 0
134 # Set up evaluation table
135 initialiseSGC2.toneevaluation_table$ = ""
136 call initialize_toneevaluation_tables
141 ###############################################################
143 # Obligatory procedures
145 ###############################################################
146 if not variableExists("build_SHA$")
150 # Initialize Demo Window
151 procedure init_window
153 if config.showBackground
154 call draw_background Background
156 demo Line width... 'defaultLineWidth'
158 call set_font_size 'defaultFontSize'
161 # Define buttons in a table
163 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
164 call set_window_title 'buttons$' '.windowTitle$'
165 # Display the word-list
168 call paint_logging_light
171 # Make sure all Preferences directories are available
172 procedure set_up_directories
173 .dirPath$ = "'preferencesAppDir$'"
174 createDirectory(.dirPath$)
175 createDirectory("'.dirPath$'/wordlists")
176 createDirectory("'.dirPath$'/log")
177 createDirectory("'.dirPath$'/TTS")
181 # Retrieve and store setting between sessions
183 procedure read_preferences .preferencesFile$
184 if not fileReadable(.preferencesFile$)
185 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
187 if fileReadable(.preferencesFile$)
188 Read from file... 'preferencesAppDir$'/sgc2rc.txt
189 .preferenceTable$ = selected$("Table")
190 .numPrefKeys = Get number of rows
191 for .row to .numPrefKeys
192 .variableName$ = Get value... '.row' Key
193 if variableExists(.variableName$)
194 .variableValue = Get value... '.row' Value
195 if .variableValue <> undefined
196 '.variableName$' = '.variableValue'
198 elsif variableExists(.variableName$+"$")
199 .variableValue$ = Get value... '.row' Value
200 .variableName$ = .variableName$+"$"
201 '.variableName$' = "'.variableValue$'"
205 select Table '.preferenceTable$'
210 procedure write_preferences .preferencesFile$
211 Create Table with column names... Preferences 0 Key Value
212 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
213 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
217 for .tableNum from 1 to 2
223 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
225 select Table '.table$'
226 .numTableRows = Get number of rows
227 for .tablerow to .numTableRows
228 select Table '.table$'
229 .label$ = Get value... '.tablerow' Label
230 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
231 .keyName$ = .variableName$
233 if variableExists(.variableName$)
234 .value = '.variableName$'
236 elsif variableExists(.variableName$ + "$")
237 .variableName$ = .variableName$ + "$"
238 .value$ = '.variableName$'
239 elsif index(.variableName$, "_") > 0
240 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
241 .keyName$ = .variableName$
242 select Table Preferences
243 .prefRow = Search column... Key '.keyName$'
245 if variableExists(.variableName$)
246 .value = '.variableName$'
248 elsif variableExists(.variableName$ + "$")
249 .variableName$ = .variableName$ + "$"
250 .value$ = '.variableName$'
256 select Table Preferences
259 Set string value... '.row' Key '.keyName$'
260 Set string value... '.row' Value '.value$'
265 # Some extra settings
266 select Table Preferences
269 Set string value... '.row' Key wordlistDir
270 Set string value... '.row' Value 'localWordlistDir$'
272 select Table Preferences
275 Set string value... '.row' Key wordlistName
276 Set string value... '.row' Value 'wordlistName$'
278 select Table Preferences
279 Write to table file... '.preferencesFile$'
284 ###############################################################
286 # TTS (speech synthesis). Place commands in user info
289 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
290 # speakCommandFile$ = ""
293 .ttscommand$ = "speak"
297 .ttscommand$ = "espeak"
301 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
305 # Global TTS command has precedence
306 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
307 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
308 if config.synthesis$ = "_DISABLED_"
310 config.synthesis$ = "eSpeak"
314 # Local TTS command, if there is no global one
315 # Command does not exist yet, create it if eSpeak is installed
316 # Make sure to quote the path!
317 if speakCommandFile$ = ""
318 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
320 system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
321 .command_path$ < 'speakCommandDir$'/command_path.txt
323 system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
324 .command_path$ < 'speakCommandDir$'/command_path.txt
326 if fileReadable(.ttscommand$)
327 .command_path$ = .ttscommand$
330 # Remove any newlines
331 .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
334 if .command_path$ <> "" and fileReadable(.command_path$)
335 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
336 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
337 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
338 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
341 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
342 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
344 deleteFile("'speakCommandDir$'/command_path.txt")
347 # Close off (other TTS maybe a later option)
348 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
349 config.synthesis$ = "eSpeak"
353 ###############################################################
358 # initialiseSGC2.toneevaluation_table$ = ""
359 procedure initialize_toneevaluation_tables
361 if initialiseSGC2.toneevaluation_table$ != ""
362 select Table 'initialiseSGC2.toneevaluation_table$'
365 initialiseSGC2.toneevaluation_table$ = ""
368 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
369 initialiseSGC2.toneevaluation_table$ = "Performance"
370 call get_toneevaluation_row Total
373 procedure get_toneevaluation_row .tones$
374 select Table 'initialiseSGC2.toneevaluation_table$'
375 .row = Search column... Tones '.tones$'
378 .row = Get number of rows
379 Set string value... '.row' Tones '.tones$'
380 Set numeric value... '.row' Correct 0
381 Set numeric value... '.row' Wrong 0
382 Set numeric value... '.row' Total 0
383 Set numeric value... '.row' High 0
384 Set numeric value... '.row' Low 0
385 Set numeric value... '.row' Wide 0
386 Set numeric value... '.row' Narrow 0
387 Set numeric value... '.row' Unknown 0
388 Set numeric value... '.row' Commented 0
390 .row = Search column... Tones '.tones$'
394 procedure increment_toneevaluation_in_row .row .column$
395 select Table 'initialiseSGC2.toneevaluation_table$'
396 .tmp = Get value... '.row' '.column$'
398 Set numeric value... '.row' '.column$' '.tmp'
401 procedure increment_toneevaluation_value .tones$ .column$
402 call get_toneevaluation_row '.tones$'
403 .row = get_toneevaluation_row.row
404 call increment_toneevaluation_in_row '.row' '.column$'
406 call get_toneevaluation_row Total
407 .row = get_toneevaluation_row.row
408 call increment_toneevaluation_in_row '.row' '.column$'
411 procedure add_feedback_to_toneevaluation .table$
412 select Table '.table$'
413 .line1$ = Get value... 1 Text
414 .line2$ = Get value... 2 Text
415 .label$ = Get value... 3 Text
420 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
421 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
424 call increment_toneevaluation_value '.tones$' Total
425 if .tones$ = .recognized$
426 call increment_toneevaluation_value '.tones$' Correct
428 call increment_toneevaluation_value '.tones$' Wrong
430 if index_regex(.label$, "(Correct|Wrong)") <= 0
431 call increment_toneevaluation_value '.tones$' '.label$'
432 call increment_toneevaluation_value '.tones$' Commented
436 procedure CreateSummaryToneEvaluation
438 # Summarize tone evaluation
439 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
441 call get_toneevaluation_row Total
442 .row = get_toneevaluation_row.row
443 select Table 'initialiseSGC2.toneevaluation_table$'
444 .total = Get value... '.row' Total
445 .correct = Get value... '.row' Correct
447 .correctPerc = 100 * .correct / .total
451 .problematic = Get value... '.row' Commented
453 .problematicPerc = 100 * .problematic / .total
458 select Table SummaryToneEvaluation
460 Set numeric value... 1 Total '.total'
461 Set numeric value... 1 Correct '.correctPerc'
462 Set numeric value... 1 Problematic '.problematicPerc'
465 ###############################################################
467 # Miscelaneous procedures
469 ###############################################################
471 # Create a file 'preferencesAppDir$'/log/logPerformance
472 # This file contains the path to the target directory where logging
474 # Note that this procedure is nowhere used in SGC2. It has to
475 # be explicitely called in a separate script.
477 procedure activate_sgc2_logging .targetDirectory$
478 createDirectory("'preferencesAppDir$'")
479 createDirectory("'preferencesLogDir$'")
481 if startsWith(.targetDirectory$, "~")
482 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
485 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
486 newline$ >> 'preferencesLogDir$'/logPerformance.txt
490 procedure synthesize_sound .pinyin$
492 if fileReadable(speakCommandFile$)
493 .command$ < 'speakCommandFile$'
494 .command$ = replace$(.command$, "'newline$'", " ", 0)
496 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
498 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
500 system_nocheck call '.command$' "'.pinyin$'"'