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 # Initialize parameters
55 config.deleteWordlist = -1
56 localWordlistDir$ = "shellDirectory$/wordlists"
57 config.displayNumbers = 1
58 config.displayPinyin = 1
59 config.displayChar = 1
60 config.displayTrans = 1
61 config.useSoundExample = 1
62 config.synthesis$ = "_DISABLED_"
63 config.input$ = "Microphone"
64 config.showBackground = 1
65 sgc2.noiseThresshold = -30
69 samplingFrequency = 44100
71 samplingFrequency = 44100
73 samplingFrequency = 44100
77 config.language$ = "EN"
79 config.logPerformance = 0
80 preferencesLogDir$ = "'preferencesDirectory$'/sgc2/log"
81 preferencesTableDir$ = "'preferencesDirectory$'/sgc2/Data"
82 sgc2wordlists$ = "'preferencesDirectory$'/sgc2/wordlists"
83 # Global word lists must be installed BY THE ADMINISTRATOR
84 # This means, create the directory 'globalwordlists$' and
85 # fill it with wordlist directories containing wordlist.Table
86 # and audio files. They can be copied from a local directory.
88 globaltablelists$ = ""
91 globalwordlists$ = "/etc/praat/sgc2/wordlists"
92 globaltablelists$ = "/etc/praat/sgc2/Data"
93 globalTTSdir$ = "/etc/praat/sgc2/TTS"
95 globalwordlists$ = "/Library/Praat/sgc2/wordlists"
96 globaltablelists$ = "/Library/Praat/sgc2/Data"
97 globalTTSdir$ = "/Library/Praat/sgc2/TTS"
99 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/wordlists"
100 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/Data"
101 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/praat/sgc2/TTS"
104 ##############################################################
106 config.shuffleLists = 1
108 defaultFont$ = "Helvetica"
111 # Set up directories if they do not exist already
112 call set_up_directories
114 # Get saved preferences
115 call read_preferences ""
118 speakCommandDir$ = "'preferencesDirectory$'/sgc2/TTS"
119 speakCommandFile$ = ""
122 # Set inital language
123 call set_language 'config.language$'
126 call load_word_list "'localWordlistDir$'" 0
128 # Set up evaluation table
129 evaluation_table$ = ""
130 call initialize_evaluation_tables
135 ###############################################################
137 # Obligatory procedures
139 ###############################################################
141 # Initialize Demo Window
142 procedure init_window
144 if config.showBackground
145 call draw_background Background
147 demo Line width... 'defaultLineWidth'
149 call set_font_size 'defaultFontSize'
152 # Define buttons in a table
154 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
155 call set_window_title 'buttons$' '.windowTitle$'
156 # Display the word-list
159 call paint_logging_light
162 # Make sure all Preferences directories are available
163 procedure set_up_directories
164 .dirPath$ = "'preferencesDirectory$'/sgc2"
165 createDirectory(.dirPath$)
166 createDirectory("'.dirPath$'/wordlists")
167 createDirectory("'.dirPath$'/log")
168 createDirectory("'.dirPath$'/TTS")
172 # Retrieve and store setting between sessions
174 procedure read_preferences .preferencesFile$
175 if not fileReadable(.preferencesFile$)
176 .preferencesFile$ = preferencesDirectory$+"/sgc2/sgc2rc.txt"
178 if fileReadable(.preferencesFile$)
179 Read from file... 'preferencesDirectory$'/sgc2/sgc2rc.txt
180 .preferenceTable$ = selected$("Table")
181 .numPrefKeys = Get number of rows
182 for .row to .numPrefKeys
183 .variableName$ = Get value... '.row' Key
184 if variableExists(.variableName$)
185 .variableValue = Get value... '.row' Value
186 if .variableValue <> undefined
187 '.variableName$' = '.variableValue'
189 elsif variableExists(.variableName$+"$")
190 .variableValue$ = Get value... '.row' Value
191 .variableName$ = .variableName$+"$"
192 '.variableName$' = "'.variableValue$'"
196 select Table '.preferenceTable$'
201 procedure write_preferences .preferencesFile$
202 Create Table with column names... Preferences 0 Key Value
203 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
204 .preferencesFile$ = preferencesDirectory$ + "/sgc2/sgc2rc.txt"
208 for .tableNum from 1 to 2
214 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
216 select Table '.table$'
217 .numTableRows = Get number of rows
218 for .tablerow to .numTableRows
219 select Table '.table$'
220 .label$ = Get value... '.tablerow' Label
221 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
222 .keyName$ = .variableName$
224 if variableExists(.variableName$)
225 .value = '.variableName$'
227 elsif variableExists(.variableName$ + "$")
228 .variableName$ = .variableName$ + "$"
229 .value$ = '.variableName$'
230 elsif index(.variableName$, "_") > 0
231 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
232 .keyName$ = .variableName$
233 select Table Preferences
234 .prefRow = Search column... Key '.keyName$'
236 if variableExists(.variableName$)
237 .value = '.variableName$'
239 elsif variableExists(.variableName$ + "$")
240 .variableName$ = .variableName$ + "$"
241 .value$ = '.variableName$'
247 select Table Preferences
250 Set string value... '.row' Key '.keyName$'
251 Set string value... '.row' Value '.value$'
256 # Some extra settings
257 select Table Preferences
260 Set string value... '.row' Key wordlistDir
261 Set string value... '.row' Value 'localWordlistDir$'
263 select Table Preferences
266 Set string value... '.row' Key wordlistName
267 Set string value... '.row' Value 'wordlistName$'
269 select Table Preferences
270 Write to table file... '.preferencesFile$'
275 ###############################################################
277 # TTS (speech synthesis). Place commands in user info
280 # speakCommandDir$ = "'preferencesDirectory$'/sgc2/TTS"
281 # speakCommandFile$ = ""
284 .ttscommand$ = "speak"
288 .ttscommand$ = "espeak"
292 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
296 # Global TTS command has precedence
297 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
298 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
299 if config.synthesis$ = "_DISABLED_"
301 config.synthesis$ = "eSpeak"
305 # Local TTS command, if there is no global one
306 # Command does not exist yet, create it if eSpeak is installed
307 # Make sure to quote the path!
308 if speakCommandFile$ = ""
309 if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
311 system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
312 .command_path$ < 'speakCommandDir$'/command_path.txt
314 system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
315 .command_path$ < 'speakCommandDir$'/command_path.txt
317 if fileReadable(.ttscommand$)
318 .command_path$ = .ttscommand$
321 # Remove any newlines
322 .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
325 if .command_path$ <> "" and fileReadable(.command_path$)
326 deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
327 fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
328 ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
329 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
332 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
333 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
335 deleteFile("'speakCommandDir$'/command_path.txt")
338 # Close off (other TTS maybe a later option)
339 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
340 config.synthesis$ = "eSpeak"
344 ###############################################################
349 # evaluation_table$ = ""
350 procedure initialize_evaluation_tables
352 if evaluation_table$ != ""
353 select Table 'evaluation_table$'
356 evaluation_table$ = ""
359 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
360 evaluation_table$ = "Performance"
361 call get_evaluation_row Total
364 procedure get_evaluation_row .tones$
365 select Table 'evaluation_table$'
366 .row = Search column... Tones '.tones$'
369 .row = Get number of rows
370 Set string value... '.row' Tones '.tones$'
371 Set numeric value... '.row' Correct 0
372 Set numeric value... '.row' Wrong 0
373 Set numeric value... '.row' Total 0
374 Set numeric value... '.row' High 0
375 Set numeric value... '.row' Low 0
376 Set numeric value... '.row' Wide 0
377 Set numeric value... '.row' Narrow 0
378 Set numeric value... '.row' Unknown 0
379 Set numeric value... '.row' Commented 0
381 .row = Search column... Tones '.tones$'
385 procedure increment_evaluation_in_row .row .column$
386 select Table 'evaluation_table$'
387 .tmp = Get value... '.row' '.column$'
389 Set numeric value... '.row' '.column$' '.tmp'
392 procedure increment_evaluation_value .tones$ .column$
393 call get_evaluation_row '.tones$'
394 .row = get_evaluation_row.row
395 call increment_evaluation_in_row '.row' '.column$'
397 call get_evaluation_row Total
398 .row = get_evaluation_row.row
399 call increment_evaluation_in_row '.row' '.column$'
402 procedure add_feedback_to_evaluation .table$
403 select Table '.table$'
404 .line1$ = Get value... 1 Text
405 .line2$ = Get value... 2 Text
406 .label$ = Get value... 3 Text
411 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
412 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
415 call increment_evaluation_value '.tones$' Total
416 if .tones$ = .recognized$
417 call increment_evaluation_value '.tones$' Correct
419 call increment_evaluation_value '.tones$' Wrong
421 if index_regex(.label$, "(Correct|Wrong)") <= 0
422 call increment_evaluation_value '.tones$' '.label$'
423 call increment_evaluation_value '.tones$' Commented
427 ###############################################################
429 # Miscelaneous procedures
431 ###############################################################
433 # Create a file 'preferencesDirectory$'/sgc2/log/logPerformance
434 # This file contains the path to the target directory where logging
436 # Note that this procedure is nowhere used in SGC2. It has to
437 # be explicitely called in a separate script.
439 procedure activate_sgc2_logging .targetDirectory$
440 createDirectory("'preferencesDirectory$'/sgc2")
441 createDirectory("'preferencesLogDir$'")
443 if startsWith(.targetDirectory$, "~")
444 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
447 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
448 newline$ >> 'preferencesLogDir$'/logPerformance.txt
452 procedure synthesize_sound .pinyin$
454 if fileReadable(speakCommandFile$)
455 .command$ < 'speakCommandFile$'
456 .command$ = replace$(.command$, "'newline$'", " ", 0)
458 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
460 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
462 system_nocheck call '.command$' "'.pinyin$'"'