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
61 config.deleteWordlist = -1
62 config.displayNumbers = 1
63 config.displayPinyin = 1
64 config.displayChar = 0
65 config.displayTrans = 1
66 config.useSoundExample = 1
67 config.synthesis$ = "_DISABLED_"
68 config.input$ = "Microphone"
69 config.showBackground = 1
70 sgc2.noiseThresshold = -30
73 sgc_ToneProt.minimumPitch = 40
74 sgc_ToneProt.maximumPitch = 600
76 # Platform dependent settings
77 if macintosh or windows
78 config.displayChar = 1
79 config.displayNumbers = 0
81 if windows and endsWith(build_SHA$, " XP")
82 config.displayChar = 0
83 config.displayNumbers = 0
86 config.displayChar = 0
87 config.displayNumbers = 1
91 samplingFrequency = 44100
93 samplingFrequency = 44100
95 samplingFrequency = 44100
100 config.language$ = "EN"
101 config.register = 249
102 config.logPerformance = 0
103 preferencesLogDir$ = "'preferencesAppDir$'/log"
104 preferencesTableDir$ = "'preferencesAppDir$'/Data"
105 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
106 localWordlistDir$ = sgc2wordlists$
108 # Global word lists must be installed BY THE ADMINISTRATOR
109 # This means, create the directory 'globalwordlists$' and
110 # fill it with wordlist directories containing wordlist.Table
111 # and audio files. They can be copied from a local directory.
112 globalwordlists$ = ""
113 globaltablelists$ = ""
116 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
117 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
118 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
120 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
121 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
122 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
124 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
125 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
126 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
128 sgc2.synthesizer = -1
129 # Define a Praat TTS command that will set sgc2.synthesizer
130 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
131 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
132 sgc2.alignedTextGrid = -1
134 ##############################################################
136 config.shuffleLists = 1
138 defaultFont$ = "Helvetica"
141 # Set up directories if they do not exist already
142 call set_up_directories
144 # Get saved preferences
145 call read_preferences ""
148 speakCommandDir$ = "'preferencesAppDir$'/TTS"
149 speakCommandFile$ = ""
152 # Set inital language
153 call set_language 'config.language$'
156 call load_word_list "'localWordlistDir$'" 0
158 # Set up evaluation table
159 initialiseSGC2.toneevaluation_table$ = ""
160 call initialize_toneevaluation_tables
165 ###############################################################
167 # Obligatory procedures
169 ###############################################################
170 # Initialize Demo Window
171 procedure init_window
173 if config.showBackground
174 call draw_background Background
176 demo Line width... 'defaultLineWidth'
178 call set_font_size 'defaultFontSize'
181 # Define buttons in a table
183 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
184 call set_window_title 'buttons$' '.windowTitle$'
185 # Display the word-list
188 call draw_tone_contour
190 call paint_logging_light
193 # Make sure all Preferences directories are available
194 procedure set_up_directories
195 .dirPath$ = "'preferencesAppDir$'"
196 createDirectory(.dirPath$)
197 .dirPath$ > '.dirPath$'/directory.txt
198 createDirectory("'.dirPath$'/wordlists")
199 .dirPath$ > '.dirPath$'/wordlists/directory.txt
200 createDirectory("'.dirPath$'/pitchmodels")
201 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
202 createDirectory("'.dirPath$'/log")
203 .dirPath$ > '.dirPath$'/log/directory.txt
204 createDirectory("'.dirPath$'/TTS")
205 .dirPath$ > '.dirPath$'/TTS/directory.txt
209 # Retrieve and store setting between sessions
211 procedure read_preferences .preferencesFile$
212 if not fileReadable(.preferencesFile$)
213 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
215 if fileReadable(.preferencesFile$)
216 Read from file... 'preferencesAppDir$'/sgc2rc.txt
217 .preferenceTable$ = selected$("Table")
218 .numPrefKeys = Get number of rows
219 for .row to .numPrefKeys
220 .variableName$ = Get value... '.row' Key
221 if variableExists(.variableName$)
222 .variableValue = Get value... '.row' Value
223 if .variableValue <> undefined
224 '.variableName$' = '.variableValue'
226 elsif variableExists(.variableName$+"$")
227 .variableValue$ = Get value... '.row' Value
228 .variableName$ = .variableName$+"$"
229 '.variableName$' = "'.variableValue$'"
233 select Table '.preferenceTable$'
238 procedure write_preferences .preferencesFile$
239 Create Table with column names... Preferences 0 Key Value
240 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
241 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
245 for .tableNum from 1 to 2
251 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
253 select Table '.table$'
254 .numTableRows = Get number of rows
255 for .tablerow to .numTableRows
256 select Table '.table$'
257 .label$ = Get value... '.tablerow' Label
258 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
259 .keyName$ = .variableName$
261 if variableExists(.variableName$)
262 .value = '.variableName$'
264 elsif variableExists(.variableName$ + "$")
265 .variableName$ = .variableName$ + "$"
266 .value$ = '.variableName$'
267 elsif index(.variableName$, "_") > 0
268 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
269 .keyName$ = .variableName$
270 select Table Preferences
271 .prefRow = Search column... Key '.keyName$'
273 if variableExists(.variableName$)
274 .value = '.variableName$'
276 elsif variableExists(.variableName$ + "$")
277 .variableName$ = .variableName$ + "$"
278 .value$ = '.variableName$'
284 select Table Preferences
287 Set string value... '.row' Key '.keyName$'
288 Set string value... '.row' Value '.value$'
293 # Some extra settings
294 select Table Preferences
297 Set string value... '.row' Key wordlistDir
298 Set string value... '.row' Value 'localWordlistDir$'
300 select Table Preferences
303 Set string value... '.row' Key wordlistName
304 Set string value... '.row' Value 'wordlistName$'
306 select Table Preferences
307 Write to table file... '.preferencesFile$'
312 ###############################################################
314 # TTS (speech synthesis). Place commands in user info
317 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
318 # speakCommandFile$ = ""
320 # Make sure sgc2.synthesizer is set!
321 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
322 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
323 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
327 if sgc2.synthesizer > 0
328 select sgc2.synthesizer
330 sgc2.synthesizer = -1
332 # Note, the TTScommand must set sgc2.synthesizer!
334 'sgc2.advancedTTScommand$'
336 'sgc2.normalTTScommand$'
340 .ttscommand$ = "speak"
344 .ttscommand$ = "espeak"
348 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
352 # Global TTS command has precedence
353 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
354 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
355 if config.synthesis$ = "_DISABLED_"
357 config.synthesis$ = "eSpeak"
361 # Local TTS command, if there is no global one
362 # Command does not exist yet, create it if eSpeak is installed
363 # Make sure to quote the path!
364 if speakCommandFile$ = ""
365 # Autodetect synthesizer command
366 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
367 # .command_path$ = ""
369 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
370 # .command_path$ < 'speakCommandDir$'/command_path.txt
372 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
373 # .command_path$ < 'speakCommandDir$'/command_path.txt
375 # if fileReadable(.ttscommand$)
376 # .command_path$ = .ttscommand$
379 # # Remove any newlines
380 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
382 # # Command path found
383 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
384 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
385 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
386 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
387 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
390 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
391 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
393 deleteFile("'speakCommandDir$'/command_path.txt")
396 # Close off (other TTS maybe a later option)
397 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
398 config.synthesis$ = "eSpeak"
402 ###############################################################
407 # initialiseSGC2.toneevaluation_table$ = ""
408 procedure initialize_toneevaluation_tables
410 if initialiseSGC2.toneevaluation_table$ != ""
411 select Table 'initialiseSGC2.toneevaluation_table$'
414 initialiseSGC2.toneevaluation_table$ = ""
417 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
418 initialiseSGC2.toneevaluation_table$ = "Performance"
419 call get_toneevaluation_row Total
422 procedure get_toneevaluation_row .tones$
423 select Table 'initialiseSGC2.toneevaluation_table$'
424 .row = Search column... Tones '.tones$'
427 .row = Get number of rows
428 Set string value... '.row' Tones '.tones$'
429 Set numeric value... '.row' Correct 0
430 Set numeric value... '.row' Wrong 0
431 Set numeric value... '.row' Total 0
432 Set numeric value... '.row' High 0
433 Set numeric value... '.row' Low 0
434 Set numeric value... '.row' Wide 0
435 Set numeric value... '.row' Narrow 0
436 Set numeric value... '.row' Unknown 0
437 Set numeric value... '.row' Commented 0
439 .row = Search column... Tones '.tones$'
443 procedure increment_toneevaluation_in_row .row .column$
444 select Table 'initialiseSGC2.toneevaluation_table$'
445 .tmp = Get value... '.row' '.column$'
447 Set numeric value... '.row' '.column$' '.tmp'
450 procedure increment_toneevaluation_value .tones$ .column$
451 call get_toneevaluation_row '.tones$'
452 .row = get_toneevaluation_row.row
453 call increment_toneevaluation_in_row '.row' '.column$'
455 call get_toneevaluation_row Total
456 .row = get_toneevaluation_row.row
457 call increment_toneevaluation_in_row '.row' '.column$'
460 procedure add_feedback_to_toneevaluation .table$
461 select Table '.table$'
462 .line1$ = Get value... 1 Text
463 .line2$ = Get value... 2 Text
464 .label$ = Get value... 3 Text
469 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
470 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
473 call increment_toneevaluation_value '.tones$' Total
474 if .tones$ = .recognized$
475 call increment_toneevaluation_value '.tones$' Correct
477 call increment_toneevaluation_value '.tones$' Wrong
479 if index_regex(.label$, "(Correct|Wrong)") <= 0
480 call increment_toneevaluation_value '.tones$' '.label$'
481 call increment_toneevaluation_value '.tones$' Commented
485 procedure CreateSummaryToneEvaluation
487 # Summarize tone evaluation
488 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
490 call get_toneevaluation_row Total
491 .row = get_toneevaluation_row.row
492 select Table 'initialiseSGC2.toneevaluation_table$'
493 .total = Get value... '.row' Total
494 .correct = Get value... '.row' Correct
496 .correctPerc = 100 * .correct / .total
500 .problematic = Get value... '.row' Commented
502 .problematicPerc = 100 * .problematic / .total
507 select Table SummaryToneEvaluation
509 Set numeric value... 1 Total '.total'
510 Set numeric value... 1 Correct '.correctPerc'
511 Set numeric value... 1 Problematic '.problematicPerc'
514 ###############################################################
516 # Miscelaneous procedures
518 ###############################################################
520 # Create a file 'preferencesAppDir$'/log/logPerformance
521 # This file contains the path to the target directory where logging
523 # Note that this procedure is nowhere used in SGC2. It has to
524 # be explicitely called in a separate script.
526 procedure activate_sgc2_logging .targetDirectory$
527 createDirectory("'preferencesAppDir$'")
528 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
529 createDirectory("'preferencesLogDir$'")
530 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
532 if startsWith(.targetDirectory$, "~")
533 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
536 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
537 newline$ >> 'preferencesLogDir$'/logPerformance.txt
541 procedure synthesize_sound .pinyin$
543 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
544 .command$ < 'speakCommandFile$'
545 .command$ = replace$(.command$, "'newline$'", " ", 0)
547 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
549 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
551 system_nocheck call '.command$' "'.pinyin$'"'
553 elsif sgc2.synthesizer > 0
554 select sgc2.synthesizer
555 Play text... '.pinyin$'
559 procedure align_recordedSound .pinyin$
560 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
561 select Sound 'recordedSound$'
562 .recordedTextGrid = To TextGrid... pinyin
563 Set interval text... 1 1 '.pinyin$'
564 select sgc2.synthesizer
565 plus Sound 'recordedSound$'
566 plus .recordedTextGrid
567 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
568 Rename... AlignedTextGrid
569 select .recordedTextGrid