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$ = ""
34 feedbackTablePrefix$ = "Feedback"
35 feedbackTableName$ = ""
40 yFeedback = yWordlist + 5
41 yPinyin = yFeedback + 10
42 yContour = yPinyin + 15
43 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
44 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
45 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
46 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
48 # Pop-Up window colors
49 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
50 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
52 # Initialize parameters
64 config.deleteWordlist = -1
65 config.displayNumbers = 1
66 config.displayPinyin = 1
67 config.displayChar = 0
68 config.displayTrans = 1
69 config.useSoundExample = 1
70 config.synthesis$ = "_DISABLED_"
71 config.input$ = "Microphone"
72 config.showBackground = 1
73 sgc2.noiseThresshold = -30
76 sgc_ToneProt.minimumPitch = 40
77 sgc_ToneProt.maximumPitch = 600
79 # Platform dependent settings
80 if macintosh or windows
81 config.displayChar = 1
82 config.displayNumbers = 0
84 if windows and endsWith(build_SHA$, " XP")
85 config.displayChar = 0
86 config.displayNumbers = 0
89 config.displayChar = 0
90 config.displayNumbers = 1
94 samplingFrequency = 44100
96 samplingFrequency = 44100
98 samplingFrequency = 44100
103 config.language$ = "EN"
104 config.register = 249
105 config.logPerformance = 0
106 preferencesLogDir$ = "'preferencesAppDir$'/log"
107 preferencesTableDir$ = "'preferencesAppDir$'/Data"
108 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
109 localWordlistDir$ = sgc2wordlists$
111 # Global word lists must be installed BY THE ADMINISTRATOR
112 # This means, create the directory 'globalwordlists$' and
113 # fill it with wordlist directories containing wordlist.Table
114 # and audio files. They can be copied from a local directory.
115 globalwordlists$ = ""
116 globaltablelists$ = ""
119 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
120 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
121 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
123 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
124 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
125 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
127 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
128 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
129 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
131 sgc2.synthesizer = -1
132 # Define a Praat TTS command that will set sgc2.synthesizer
133 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
134 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
135 sgc2.alignedTextGrid = -1
137 ##############################################################
139 config.shuffleLists = 1
141 defaultFont$ = "Helvetica"
144 # Set up directories if they do not exist already
145 call set_up_directories
147 # Get saved preferences
148 call read_preferences ""
151 speakCommandDir$ = "'preferencesAppDir$'/TTS"
152 speakCommandFile$ = ""
155 # Set inital language
156 call set_language 'config.language$'
159 call load_word_list "'localWordlistDir$'" 0
161 # Set up evaluation table
162 initialiseSGC2.toneevaluation_table$ = ""
163 call initialize_toneevaluation_tables
168 ###############################################################
170 # Obligatory procedures
172 ###############################################################
173 # Initialize Demo Window
174 procedure init_window
176 demo Line width... 'defaultLineWidth'
178 call set_font_size 'defaultFontSize'
181 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
182 call set_window_title 'buttons$' '.windowTitle$'
183 # Display the word-list
185 # Wipe screen and draw background
186 call wipeArea 'wipeContourArea$'
187 if config.showBackground
188 call draw_background Background
190 # Define buttons in a table
193 call draw_tone_contour
195 call paint_logging_light
198 # Make sure all Preferences directories are available
199 procedure set_up_directories
200 .dirPath$ = "'preferencesAppDir$'"
201 createDirectory(.dirPath$)
202 .dirPath$ > '.dirPath$'/directory.txt
203 createDirectory("'.dirPath$'/wordlists")
204 .dirPath$ > '.dirPath$'/wordlists/directory.txt
205 createDirectory("'.dirPath$'/pitchmodels")
206 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
207 createDirectory("'.dirPath$'/log")
208 .dirPath$ > '.dirPath$'/log/directory.txt
209 createDirectory("'.dirPath$'/TTS")
210 .dirPath$ > '.dirPath$'/TTS/directory.txt
214 # Retrieve and store setting between sessions
216 procedure read_preferences .preferencesFile$
217 if not fileReadable(.preferencesFile$)
218 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
220 if fileReadable(.preferencesFile$)
221 Read from file... 'preferencesAppDir$'/sgc2rc.txt
222 .preferenceTable$ = selected$("Table")
223 .numPrefKeys = Get number of rows
224 for .row to .numPrefKeys
225 .variableName$ = Get value... '.row' Key
226 if variableExists(.variableName$)
227 .variableValue = Get value... '.row' Value
228 if .variableValue <> undefined
229 '.variableName$' = '.variableValue'
231 elsif variableExists(.variableName$+"$")
232 .variableValue$ = Get value... '.row' Value
233 .variableName$ = .variableName$+"$"
234 '.variableName$' = "'.variableValue$'"
238 select Table '.preferenceTable$'
243 procedure write_preferences .preferencesFile$
244 Create Table with column names... Preferences 0 Key Value
245 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
246 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
250 for .tableNum from 1 to 2
256 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
258 select Table '.table$'
259 .numTableRows = Get number of rows
260 for .tablerow to .numTableRows
261 select Table '.table$'
262 .label$ = Get value... '.tablerow' Label
263 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
264 .keyName$ = .variableName$
266 if variableExists(.variableName$)
267 .value = '.variableName$'
269 elsif variableExists(.variableName$ + "$")
270 .variableName$ = .variableName$ + "$"
271 .value$ = '.variableName$'
272 elsif index(.variableName$, "_") > 0
273 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
274 .keyName$ = .variableName$
275 select Table Preferences
276 .prefRow = Search column... Key '.keyName$'
278 if variableExists(.variableName$)
279 .value = '.variableName$'
281 elsif variableExists(.variableName$ + "$")
282 .variableName$ = .variableName$ + "$"
283 .value$ = '.variableName$'
289 select Table Preferences
292 Set string value... '.row' Key '.keyName$'
293 Set string value... '.row' Value '.value$'
298 # Some extra settings
299 select Table Preferences
302 Set string value... '.row' Key wordlistDir
303 Set string value... '.row' Value 'localWordlistDir$'
305 select Table Preferences
308 Set string value... '.row' Key wordlistName
309 Set string value... '.row' Value 'wordlistName$'
311 select Table Preferences
312 Write to table file... '.preferencesFile$'
317 ###############################################################
319 # TTS (speech synthesis). Place commands in user info
322 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
323 # speakCommandFile$ = ""
325 # Make sure sgc2.synthesizer is set!
326 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
327 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
328 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
332 if sgc2.synthesizer > 0
333 select sgc2.synthesizer
335 sgc2.synthesizer = -1
337 # Note, the TTScommand must set sgc2.synthesizer!
339 'sgc2.advancedTTScommand$'
341 'sgc2.normalTTScommand$'
345 .ttscommand$ = "speak"
349 .ttscommand$ = "espeak"
353 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
357 # Global TTS command has precedence
358 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
359 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
360 if config.synthesis$ = "_DISABLED_"
362 config.synthesis$ = "eSpeak"
366 # Local TTS command, if there is no global one
367 # Command does not exist yet, create it if eSpeak is installed
368 # Make sure to quote the path!
369 if speakCommandFile$ = ""
370 # Autodetect synthesizer command
371 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
372 # .command_path$ = ""
374 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
375 # .command_path$ < 'speakCommandDir$'/command_path.txt
377 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
378 # .command_path$ < 'speakCommandDir$'/command_path.txt
380 # if fileReadable(.ttscommand$)
381 # .command_path$ = .ttscommand$
384 # # Remove any newlines
385 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
387 # # Command path found
388 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
389 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
390 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
391 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
392 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
395 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
396 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
398 deleteFile("'speakCommandDir$'/command_path.txt")
401 # Close off (other TTS maybe a later option)
402 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
403 config.synthesis$ = "eSpeak"
407 ###############################################################
412 # initialiseSGC2.toneevaluation_table$ = ""
413 procedure initialize_toneevaluation_tables
415 if initialiseSGC2.toneevaluation_table$ != ""
416 select Table 'initialiseSGC2.toneevaluation_table$'
419 initialiseSGC2.toneevaluation_table$ = ""
422 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
423 initialiseSGC2.toneevaluation_table$ = "Performance"
424 call get_toneevaluation_row Total
427 procedure get_toneevaluation_row .tones$
428 select Table 'initialiseSGC2.toneevaluation_table$'
429 .row = Search column... Tones '.tones$'
432 .row = Get number of rows
433 Set string value... '.row' Tones '.tones$'
434 Set numeric value... '.row' Correct 0
435 Set numeric value... '.row' Wrong 0
436 Set numeric value... '.row' Total 0
437 Set numeric value... '.row' High 0
438 Set numeric value... '.row' Low 0
439 Set numeric value... '.row' Wide 0
440 Set numeric value... '.row' Narrow 0
441 Set numeric value... '.row' Unknown 0
442 Set numeric value... '.row' Commented 0
444 .row = Search column... Tones '.tones$'
448 procedure increment_toneevaluation_in_row .row .column$
449 select Table 'initialiseSGC2.toneevaluation_table$'
450 .tmp = Get value... '.row' '.column$'
452 Set numeric value... '.row' '.column$' '.tmp'
455 procedure increment_toneevaluation_value .tones$ .column$
456 call get_toneevaluation_row '.tones$'
457 .row = get_toneevaluation_row.row
458 call increment_toneevaluation_in_row '.row' '.column$'
460 call get_toneevaluation_row Total
461 .row = get_toneevaluation_row.row
462 call increment_toneevaluation_in_row '.row' '.column$'
465 procedure add_feedback_to_toneevaluation .table$
466 select Table '.table$'
467 .line1$ = Get value... 1 Text
468 .line2$ = Get value... 2 Text
469 .label$ = Get value... 3 Text
474 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
475 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
478 call increment_toneevaluation_value '.tones$' Total
479 if .tones$ = .recognized$
480 call increment_toneevaluation_value '.tones$' Correct
482 call increment_toneevaluation_value '.tones$' Wrong
484 if index_regex(.label$, "(Correct|Wrong)") <= 0
485 call increment_toneevaluation_value '.tones$' '.label$'
486 call increment_toneevaluation_value '.tones$' Commented
490 procedure CreateSummaryToneEvaluation
491 .font$ = defaultFont$
492 # Summarize tone evaluation
493 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
495 call get_toneevaluation_row Total
496 .row = get_toneevaluation_row.row
497 select Table 'initialiseSGC2.toneevaluation_table$'
498 .total = Get value... '.row' Total
499 .correct = Get value... '.row' Correct
501 .correctPerc = 100 * .correct / .total
505 .problematic = Get value... '.row' Commented
507 .problematicPerc = 100 * .problematic / .total
512 select Table SummaryToneEvaluation
514 Set numeric value... 1 Total '.total'
515 Set numeric value... 1 Correct '.correctPerc'
516 Set numeric value... 1 Problematic '.problematicPerc'
519 ###############################################################
521 # Miscelaneous procedures
523 ###############################################################
525 # Create a file 'preferencesAppDir$'/log/logPerformance
526 # This file contains the path to the target directory where logging
528 # Note that this procedure is nowhere used in SGC2. It has to
529 # be explicitely called in a separate script.
531 procedure activate_sgc2_logging .targetDirectory$
532 createDirectory("'preferencesAppDir$'")
533 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
534 createDirectory("'preferencesLogDir$'")
535 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
537 if startsWith(.targetDirectory$, "~")
538 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
541 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
542 newline$ >> 'preferencesLogDir$'/logPerformance.txt
546 procedure synthesize_sound .pinyin$
548 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
549 .command$ < 'speakCommandFile$'
550 .command$ = replace$(.command$, "'newline$'", " ", 0)
552 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
554 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
556 system_nocheck call '.command$' "'.pinyin$'"'
558 elsif sgc2.synthesizer > 0
559 select sgc2.synthesizer
560 Play text... '.pinyin$'
564 procedure align_recordedSound .pinyin$
565 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
566 select Sound 'recordedSound$'
567 .recordedTextGrid = To TextGrid... pinyin
568 Set interval text... 1 1 '.pinyin$'
569 select sgc2.synthesizer
570 plus Sound 'recordedSound$'
571 plus .recordedTextGrid
572 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
573 Rename... AlignedTextGrid
574 select .recordedTextGrid