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
77 sgc_ToneProt.minimumPitch = 40
78 sgc_ToneProt.maximumPitch = 600
80 # Platform dependent settings
81 if macintosh or windows
82 config.displayChar = 1
83 config.displayNumbers = 0
85 if windows and endsWith(build_SHA$, " XP")
86 config.displayChar = 0
87 config.displayNumbers = 0
90 config.displayChar = 0
91 config.displayNumbers = 1
95 samplingFrequency = 44100
97 samplingFrequency = 44100
99 samplingFrequency = 44100
104 config.language$ = "EN"
105 config.register = 249
106 config.logPerformance = 0
107 preferencesLogDir$ = "'preferencesAppDir$'/log"
108 preferencesTableDir$ = "'preferencesAppDir$'/Data"
109 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
110 localWordlistDir$ = sgc2wordlists$
112 # Global word lists must be installed BY THE ADMINISTRATOR
113 # This means, create the directory 'globalwordlists$' and
114 # fill it with wordlist directories containing wordlist.Table
115 # and audio files. They can be copied from a local directory.
116 globalwordlists$ = ""
117 globaltablelists$ = ""
120 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
121 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
122 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
124 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
125 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
126 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
128 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
129 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
130 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
132 sgc2.synthesizer = -1
133 # Define a Praat TTS command that will set sgc2.synthesizer
134 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
135 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
136 sgc2.alignedTextGrid = -1
138 ##############################################################
140 config.shuffleLists = 1
142 defaultFont$ = "Helvetica"
145 # Set up directories if they do not exist already
146 call set_up_directories
148 # Get saved preferences
149 call read_preferences ""
152 speakCommandDir$ = "'preferencesAppDir$'/TTS"
153 speakCommandFile$ = ""
156 # Set inital language
157 call set_language 'config.language$'
160 call load_word_list "'localWordlistDir$'" 0
162 # Set up evaluation table
163 initialiseSGC2.toneevaluation_table$ = ""
164 call initialize_toneevaluation_tables
169 ###############################################################
171 # Obligatory procedures
173 ###############################################################
174 # Initialize Demo Window
175 procedure init_window
177 demo Line width... 'defaultLineWidth'
179 call set_font_size 'defaultFontSize'
182 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
183 call set_window_title 'buttons$' '.windowTitle$'
184 # Display the word-list
186 # Wipe screen and draw background
187 call wipeArea 'wipeContourArea$'
188 if config.showBackground
189 call draw_background Background
191 # Define buttons in a table
194 call draw_tone_contour
196 call paint_logging_light
199 # Make sure all Preferences directories are available
200 procedure set_up_directories
201 .dirPath$ = "'preferencesAppDir$'"
202 createDirectory(.dirPath$)
203 .dirPath$ > '.dirPath$'/directory.txt
204 createDirectory("'.dirPath$'/wordlists")
205 .dirPath$ > '.dirPath$'/wordlists/directory.txt
206 createDirectory("'.dirPath$'/pitchmodels")
207 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
208 createDirectory("'.dirPath$'/log")
209 .dirPath$ > '.dirPath$'/log/directory.txt
210 createDirectory("'.dirPath$'/TTS")
211 .dirPath$ > '.dirPath$'/TTS/directory.txt
215 # Retrieve and store setting between sessions
217 procedure read_preferences .preferencesFile$
218 if not fileReadable(.preferencesFile$)
219 .preferencesFile$ = preferencesAppDir$+"/sgc2rc.txt"
221 if fileReadable(.preferencesFile$)
222 Read from file... 'preferencesAppDir$'/sgc2rc.txt
223 .preferenceTable$ = selected$("Table")
224 .numPrefKeys = Get number of rows
225 for .row to .numPrefKeys
226 .variableName$ = Get value... '.row' Key
227 if variableExists(.variableName$)
228 .variableValue = Get value... '.row' Value
229 if .variableValue <> undefined
230 '.variableName$' = '.variableValue'
232 elsif variableExists(.variableName$+"$")
233 .variableValue$ = Get value... '.row' Value
234 .variableName$ = .variableName$+"$"
235 '.variableName$' = "'.variableValue$'"
239 select Table '.preferenceTable$'
244 procedure write_preferences .preferencesFile$
245 Create Table with column names... Preferences 0 Key Value
246 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
247 .preferencesFile$ = preferencesAppDir$ + "/sgc2rc.txt"
251 for .tableNum from 1 to 2
257 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
259 select Table '.table$'
260 .numTableRows = Get number of rows
261 for .tablerow to .numTableRows
262 select Table '.table$'
263 .label$ = Get value... '.tablerow' Label
264 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
265 .keyName$ = .variableName$
267 if variableExists(.variableName$)
268 .value = '.variableName$'
270 elsif variableExists(.variableName$ + "$")
271 .variableName$ = .variableName$ + "$"
272 .value$ = '.variableName$'
273 elsif index(.variableName$, "_") > 0
274 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
275 .keyName$ = .variableName$
276 select Table Preferences
277 .prefRow = Search column... Key '.keyName$'
279 if variableExists(.variableName$)
280 .value = '.variableName$'
282 elsif variableExists(.variableName$ + "$")
283 .variableName$ = .variableName$ + "$"
284 .value$ = '.variableName$'
290 select Table Preferences
293 Set string value... '.row' Key '.keyName$'
294 Set string value... '.row' Value '.value$'
299 # Some extra settings
300 select Table Preferences
303 Set string value... '.row' Key wordlistDir
304 Set string value... '.row' Value 'localWordlistDir$'
306 select Table Preferences
309 Set string value... '.row' Key wordlistName
310 Set string value... '.row' Value 'wordlistName$'
312 select Table Preferences
313 Write to table file... '.preferencesFile$'
318 ###############################################################
320 # TTS (speech synthesis). Place commands in user info
323 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
324 # speakCommandFile$ = ""
326 # Make sure sgc2.synthesizer is set!
327 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
328 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
329 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
333 if sgc2.synthesizer > 0
334 select sgc2.synthesizer
336 sgc2.synthesizer = -1
338 # Note, the TTScommand must set sgc2.synthesizer!
340 'sgc2.advancedTTScommand$'
342 'sgc2.normalTTScommand$'
346 .ttscommand$ = "speak"
350 .ttscommand$ = "espeak"
354 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
358 # Global TTS command has precedence
359 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
360 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
361 if config.synthesis$ = "_DISABLED_"
363 config.synthesis$ = "eSpeak"
367 # Local TTS command, if there is no global one
368 # Command does not exist yet, create it if eSpeak is installed
369 # Make sure to quote the path!
370 if speakCommandFile$ = ""
371 # Autodetect synthesizer command
372 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
373 # .command_path$ = ""
375 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
376 # .command_path$ < 'speakCommandDir$'/command_path.txt
378 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
379 # .command_path$ < 'speakCommandDir$'/command_path.txt
381 # if fileReadable(.ttscommand$)
382 # .command_path$ = .ttscommand$
385 # # Remove any newlines
386 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
388 # # Command path found
389 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
390 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
391 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
392 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
393 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
396 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
397 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
399 deleteFile("'speakCommandDir$'/command_path.txt")
402 # Close off (other TTS maybe a later option)
403 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
404 config.synthesis$ = "eSpeak"
408 ###############################################################
413 # initialiseSGC2.toneevaluation_table$ = ""
414 procedure initialize_toneevaluation_tables
416 if initialiseSGC2.toneevaluation_table$ != ""
417 select Table 'initialiseSGC2.toneevaluation_table$'
420 initialiseSGC2.toneevaluation_table$ = ""
422 if config.savePerf$ <> "" and fileReadable(config.savePerf$)
423 sgc2.performanceTable = Read from file: config.savePerf$
424 Rename: "Performance"
427 sgc2.performanceTable = Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
429 initialiseSGC2.toneevaluation_table$ = "Performance"
430 call get_toneevaluation_row Total
433 procedure get_toneevaluation_row .tones$
434 select Table 'initialiseSGC2.toneevaluation_table$'
435 .row = Search column... Tones '.tones$'
438 .row = Get number of rows
439 Set string value... '.row' Tones '.tones$'
440 Set numeric value... '.row' Correct 0
441 Set numeric value... '.row' Wrong 0
442 Set numeric value... '.row' Total 0
443 Set numeric value... '.row' High 0
444 Set numeric value... '.row' Low 0
445 Set numeric value... '.row' Wide 0
446 Set numeric value... '.row' Narrow 0
447 Set numeric value... '.row' Unknown 0
448 Set numeric value... '.row' Commented 0
450 .row = Search column... Tones '.tones$'
454 procedure increment_toneevaluation_in_row .row .column$
455 select Table 'initialiseSGC2.toneevaluation_table$'
456 .tmp = Get value... '.row' '.column$'
458 Set numeric value... '.row' '.column$' '.tmp'
461 procedure increment_toneevaluation_value .tones$ .column$
462 call get_toneevaluation_row '.tones$'
463 .row = get_toneevaluation_row.row
464 call increment_toneevaluation_in_row '.row' '.column$'
466 call get_toneevaluation_row Total
467 .row = get_toneevaluation_row.row
468 call increment_toneevaluation_in_row '.row' '.column$'
471 procedure add_feedback_to_toneevaluation .table$
472 select Table '.table$'
473 .line1$ = Get value... 1 Text
474 .line2$ = Get value... 2 Text
475 .label$ = Get value... 3 Text
480 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^a-z0-9]", "", 0)
481 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
484 call increment_toneevaluation_value '.tones$' Total
485 if .tones$ = .recognized$
486 call increment_toneevaluation_value '.tones$' Correct
488 call increment_toneevaluation_value '.tones$' Wrong
490 if index_regex(.label$, "(Correct|Wrong)") <= 0
491 call increment_toneevaluation_value '.tones$' '.label$'
492 call increment_toneevaluation_value '.tones$' Commented
496 procedure CreateSummaryToneEvaluation
497 .font$ = defaultFont$
498 # Summarize tone evaluation
499 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
501 call get_toneevaluation_row Total
502 .row = get_toneevaluation_row.row
503 select Table 'initialiseSGC2.toneevaluation_table$'
504 .total = Get value... '.row' Total
505 .correct = Get value... '.row' Correct
507 .correctPerc = 100 * .correct / .total
511 .problematic = Get value... '.row' Commented
513 .problematicPerc = 100 * .problematic / .total
518 select Table SummaryToneEvaluation
520 Set numeric value... 1 Total '.total'
521 Set numeric value... 1 Correct '.correctPerc'
522 Set numeric value... 1 Problematic '.problematicPerc'
525 ###############################################################
527 # Miscelaneous procedures
529 ###############################################################
531 # Create a file 'preferencesAppDir$'/log/logPerformance
532 # This file contains the path to the target directory where logging
534 # Note that this procedure is nowhere used in SGC2. It has to
535 # be explicitely called in a separate script.
537 procedure activate_sgc2_logging .targetDirectory$
538 createDirectory("'preferencesAppDir$'")
539 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
540 createDirectory("'preferencesLogDir$'")
541 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
543 if startsWith(.targetDirectory$, "~")
544 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
547 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
548 newline$ >> 'preferencesLogDir$'/logPerformance.txt
552 procedure synthesize_sound .pinyin$
554 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
555 .command$ < 'speakCommandFile$'
556 .command$ = replace$(.command$, "'newline$'", " ", 0)
558 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
560 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
562 system_nocheck call '.command$' "'.pinyin$'"'
564 elsif sgc2.synthesizer > 0
565 select sgc2.synthesizer
566 Play text... '.pinyin$'
570 procedure align_recordedSound .pinyin$
571 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
572 select Sound 'recordedSound$'
573 .recordedTextGrid = To TextGrid... pinyin
574 Set interval text... 1 1 '.pinyin$'
575 select sgc2.synthesizer
576 plus Sound 'recordedSound$'
577 plus .recordedTextGrid
578 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
579 Rename... AlignedTextGrid
580 select .recordedTextGrid