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 config.displayNumbers = 1
62 config.displayPinyin = 1
63 config.displayChar = 0
64 config.displayTrans = 1
65 config.useSoundExample = 1
66 config.synthesis$ = "_DISABLED_"
67 config.input$ = "Microphone"
68 config.showBackground = 1
69 sgc2.noiseThresshold = -30
72 sgc_ToneProt.minimumPitch = 40
73 sgc_ToneProt.maximumPitch = 600
75 # Platform dependent settings
76 if macintosh or windows
77 config.displayChar = 1
78 config.displayNumbers = 0
80 if windows and endsWith(build_SHA$, " XP")
81 config.displayChar = 0
82 config.displayNumbers = 0
85 config.displayChar = 0
86 config.displayNumbers = 1
90 samplingFrequency = 44100
92 samplingFrequency = 44100
94 samplingFrequency = 44100
99 config.language$ = "EN"
100 config.register = 249
101 config.logPerformance = 0
102 preferencesLogDir$ = "'preferencesAppDir$'/log"
103 preferencesTableDir$ = "'preferencesAppDir$'/Data"
104 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
105 localWordlistDir$ = sgc2wordlists$
107 # Global word lists must be installed BY THE ADMINISTRATOR
108 # This means, create the directory 'globalwordlists$' and
109 # fill it with wordlist directories containing wordlist.Table
110 # and audio files. They can be copied from a local directory.
111 globalwordlists$ = ""
112 globaltablelists$ = ""
115 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
116 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
117 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
119 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
120 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
121 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
123 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
124 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
125 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
127 sgc2.synthesizer = -1
128 # Define a Praat TTS command that will set sgc2.synthesizer
129 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
130 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
131 sgc2.alignedTextGrid = -1
133 ##############################################################
135 config.shuffleLists = 1
137 defaultFont$ = "Helvetica"
140 # Set up directories if they do not exist already
141 call set_up_directories
143 # Get saved preferences
144 call read_preferences ""
147 speakCommandDir$ = "'preferencesAppDir$'/TTS"
148 speakCommandFile$ = ""
151 # Set inital language
152 call set_language 'config.language$'
155 call load_word_list "'localWordlistDir$'" 0
157 # Set up evaluation table
158 initialiseSGC2.toneevaluation_table$ = ""
159 call initialize_toneevaluation_tables
164 ###############################################################
166 # Obligatory procedures
168 ###############################################################
169 # Initialize Demo Window
170 procedure init_window
172 if config.showBackground
173 call draw_background Background
175 demo Line width... 'defaultLineWidth'
177 call set_font_size 'defaultFontSize'
180 # Define buttons in a table
182 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
183 call set_window_title 'buttons$' '.windowTitle$'
184 # Display the word-list
187 call paint_logging_light
190 # Make sure all Preferences directories are available
191 procedure set_up_directories
192 .dirPath$ = "'preferencesAppDir$'"
193 createDirectory(.dirPath$)
194 .dirPath$ > '.dirPath$'/directory.txt
195 createDirectory("'.dirPath$'/wordlists")
196 .dirPath$ > '.dirPath$'/wordlists/directory.txt
197 createDirectory("'.dirPath$'/pitchmodels")
198 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
199 createDirectory("'.dirPath$'/log")
200 .dirPath$ > '.dirPath$'/log/directory.txt
201 createDirectory("'.dirPath$'/TTS")
202 .dirPath$ > '.dirPath$'/TTS/directory.txt
206 # Retrieve and store setting between sessions
208 procedure read_preferences .preferencesFile$
209 if not fileReadable(.preferencesFile$)
210 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
212 if fileReadable(.preferencesFile$)
213 Read from file... 'preferencesAppDir$'/sgc2rc.txt
214 .preferenceTable$ = selected$("Table")
215 .numPrefKeys = Get number of rows
216 for .row to .numPrefKeys
217 .variableName$ = Get value... '.row' Key
218 if variableExists(.variableName$)
219 .variableValue = Get value... '.row' Value
220 if .variableValue <> undefined
221 '.variableName$' = '.variableValue'
223 elsif variableExists(.variableName$+"$")
224 .variableValue$ = Get value... '.row' Value
225 .variableName$ = .variableName$+"$"
226 '.variableName$' = "'.variableValue$'"
230 select Table '.preferenceTable$'
235 procedure write_preferences .preferencesFile$
236 Create Table with column names... Preferences 0 Key Value
237 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
238 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
242 for .tableNum from 1 to 2
248 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
250 select Table '.table$'
251 .numTableRows = Get number of rows
252 for .tablerow to .numTableRows
253 select Table '.table$'
254 .label$ = Get value... '.tablerow' Label
255 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
256 .keyName$ = .variableName$
258 if variableExists(.variableName$)
259 .value = '.variableName$'
261 elsif variableExists(.variableName$ + "$")
262 .variableName$ = .variableName$ + "$"
263 .value$ = '.variableName$'
264 elsif index(.variableName$, "_") > 0
265 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
266 .keyName$ = .variableName$
267 select Table Preferences
268 .prefRow = Search column... Key '.keyName$'
270 if variableExists(.variableName$)
271 .value = '.variableName$'
273 elsif variableExists(.variableName$ + "$")
274 .variableName$ = .variableName$ + "$"
275 .value$ = '.variableName$'
281 select Table Preferences
284 Set string value... '.row' Key '.keyName$'
285 Set string value... '.row' Value '.value$'
290 # Some extra settings
291 select Table Preferences
294 Set string value... '.row' Key wordlistDir
295 Set string value... '.row' Value 'localWordlistDir$'
297 select Table Preferences
300 Set string value... '.row' Key wordlistName
301 Set string value... '.row' Value 'wordlistName$'
303 select Table Preferences
304 Write to table file... '.preferencesFile$'
309 ###############################################################
311 # TTS (speech synthesis). Place commands in user info
314 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
315 # speakCommandFile$ = ""
317 # Make sure sgc2.synthesizer is set!
318 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
319 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
320 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
324 if sgc2.synthesizer > 0
325 select sgc2.synthesizer
327 sgc2.synthesizer = -1
329 # Note, the TTScommand must set sgc2.synthesizer!
331 'sgc2.advancedTTScommand$'
333 'sgc2.normalTTScommand$'
337 .ttscommand$ = "speak"
341 .ttscommand$ = "espeak"
345 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
349 # Global TTS command has precedence
350 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
351 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
352 if config.synthesis$ = "_DISABLED_"
354 config.synthesis$ = "eSpeak"
358 # Local TTS command, if there is no global one
359 # Command does not exist yet, create it if eSpeak is installed
360 # Make sure to quote the path!
361 if speakCommandFile$ = ""
362 # Autodetect synthesizer command
363 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
364 # .command_path$ = ""
366 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
367 # .command_path$ < 'speakCommandDir$'/command_path.txt
369 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
370 # .command_path$ < 'speakCommandDir$'/command_path.txt
372 # if fileReadable(.ttscommand$)
373 # .command_path$ = .ttscommand$
376 # # Remove any newlines
377 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
379 # # Command path found
380 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
381 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
382 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
383 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
384 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
387 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
388 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
390 deleteFile("'speakCommandDir$'/command_path.txt")
393 # Close off (other TTS maybe a later option)
394 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
395 config.synthesis$ = "eSpeak"
399 ###############################################################
404 # initialiseSGC2.toneevaluation_table$ = ""
405 procedure initialize_toneevaluation_tables
407 if initialiseSGC2.toneevaluation_table$ != ""
408 select Table 'initialiseSGC2.toneevaluation_table$'
411 initialiseSGC2.toneevaluation_table$ = ""
414 Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
415 initialiseSGC2.toneevaluation_table$ = "Performance"
416 call get_toneevaluation_row Total
419 procedure get_toneevaluation_row .tones$
420 select Table 'initialiseSGC2.toneevaluation_table$'
421 .row = Search column... Tones '.tones$'
424 .row = Get number of rows
425 Set string value... '.row' Tones '.tones$'
426 Set numeric value... '.row' Correct 0
427 Set numeric value... '.row' Wrong 0
428 Set numeric value... '.row' Total 0
429 Set numeric value... '.row' High 0
430 Set numeric value... '.row' Low 0
431 Set numeric value... '.row' Wide 0
432 Set numeric value... '.row' Narrow 0
433 Set numeric value... '.row' Unknown 0
434 Set numeric value... '.row' Commented 0
436 .row = Search column... Tones '.tones$'
440 procedure increment_toneevaluation_in_row .row .column$
441 select Table 'initialiseSGC2.toneevaluation_table$'
442 .tmp = Get value... '.row' '.column$'
444 Set numeric value... '.row' '.column$' '.tmp'
447 procedure increment_toneevaluation_value .tones$ .column$
448 call get_toneevaluation_row '.tones$'
449 .row = get_toneevaluation_row.row
450 call increment_toneevaluation_in_row '.row' '.column$'
452 call get_toneevaluation_row Total
453 .row = get_toneevaluation_row.row
454 call increment_toneevaluation_in_row '.row' '.column$'
457 procedure add_feedback_to_toneevaluation .table$
458 select Table '.table$'
459 .line1$ = Get value... 1 Text
460 .line2$ = Get value... 2 Text
461 .label$ = Get value... 3 Text
466 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
467 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
470 call increment_toneevaluation_value '.tones$' Total
471 if .tones$ = .recognized$
472 call increment_toneevaluation_value '.tones$' Correct
474 call increment_toneevaluation_value '.tones$' Wrong
476 if index_regex(.label$, "(Correct|Wrong)") <= 0
477 call increment_toneevaluation_value '.tones$' '.label$'
478 call increment_toneevaluation_value '.tones$' Commented
482 procedure CreateSummaryToneEvaluation
484 # Summarize tone evaluation
485 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
487 call get_toneevaluation_row Total
488 .row = get_toneevaluation_row.row
489 select Table 'initialiseSGC2.toneevaluation_table$'
490 .total = Get value... '.row' Total
491 .correct = Get value... '.row' Correct
493 .correctPerc = 100 * .correct / .total
497 .problematic = Get value... '.row' Commented
499 .problematicPerc = 100 * .problematic / .total
504 select Table SummaryToneEvaluation
506 Set numeric value... 1 Total '.total'
507 Set numeric value... 1 Correct '.correctPerc'
508 Set numeric value... 1 Problematic '.problematicPerc'
511 ###############################################################
513 # Miscelaneous procedures
515 ###############################################################
517 # Create a file 'preferencesAppDir$'/log/logPerformance
518 # This file contains the path to the target directory where logging
520 # Note that this procedure is nowhere used in SGC2. It has to
521 # be explicitely called in a separate script.
523 procedure activate_sgc2_logging .targetDirectory$
524 createDirectory("'preferencesAppDir$'")
525 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
526 createDirectory("'preferencesLogDir$'")
527 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
529 if startsWith(.targetDirectory$, "~")
530 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
533 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
534 newline$ >> 'preferencesLogDir$'/logPerformance.txt
538 procedure synthesize_sound .pinyin$
540 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
541 .command$ < 'speakCommandFile$'
542 .command$ = replace$(.command$, "'newline$'", " ", 0)
544 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
546 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
548 system_nocheck call '.command$' "'.pinyin$'"'
550 elsif sgc2.synthesizer > 0
551 select sgc2.synthesizer
552 Play text... '.pinyin$'
556 procedure align_recordedSound .pinyin$
557 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
558 select Sound 'recordedSound$'
559 .recordedTextGrid = To TextGrid... pinyin
560 Set interval text... 1 1 '.pinyin$'
561 select sgc2.synthesizer
562 plus Sound 'recordedSound$'
563 plus .recordedTextGrid
564 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
565 Rename... AlignedTextGrid
566 select .recordedTextGrid