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 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
108 preferencesLogDir$ = "'preferencesAppDir$'/log"
109 preferencesTableDir$ = "'preferencesAppDir$'/Data"
110 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
111 localWordlistDir$ = sgc2wordlists$
113 # Global word lists must be installed BY THE ADMINISTRATOR
114 # This means, create the directory 'globalwordlists$' and
115 # fill it with wordlist directories containing wordlist.Table
116 # and audio files. They can be copied from a local directory.
117 globalwordlists$ = ""
118 globaltablelists$ = ""
121 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
122 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
123 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
125 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
126 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
127 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
129 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
130 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
131 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
133 sgc2.synthesizer = -1
134 # Define a Praat TTS command that will set sgc2.synthesizer
135 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
136 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
137 sgc2.alignedTextGrid = -1
139 ##############################################################
141 config.shuffleLists = 1
143 defaultFont$ = "Helvetica"
146 # Set up directories if they do not exist already
147 call set_up_directories
149 # Get saved preferences
150 call read_preferences ""
153 speakCommandDir$ = "'preferencesAppDir$'/TTS"
154 speakCommandFile$ = ""
157 # Set inital language
158 call set_language 'config.language$'
161 call load_word_list "'localWordlistDir$'" 0
163 # Set up evaluation table
164 initialiseSGC2.toneevaluation_table$ = ""
165 call initialize_toneevaluation_tables
170 ###############################################################
172 # Obligatory procedures
174 ###############################################################
175 # Initialize Demo Window
176 procedure init_window
178 demo Line width... 'defaultLineWidth'
180 call set_font_size 'defaultFontSize'
183 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
184 call set_window_title 'buttons$' '.windowTitle$'
185 # Display the word-list
187 # Wipe screen and draw background
188 call wipeArea 'wipeContourArea$'
189 if config.showBackground
190 call draw_background Background
192 # Define buttons in a table
195 call draw_tone_contour
197 call paint_logging_light
200 # Make sure all Preferences directories are available
201 procedure set_up_directories
202 .dirPath$ = "'preferencesAppDir$'"
203 createDirectory(.dirPath$)
204 .dirPath$ > '.dirPath$'/directory.txt
205 createDirectory("'.dirPath$'/wordlists")
206 .dirPath$ > '.dirPath$'/wordlists/directory.txt
207 createDirectory("'.dirPath$'/pitchmodels")
208 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
209 createDirectory("'.dirPath$'/log")
210 .dirPath$ > '.dirPath$'/log/directory.txt
211 createDirectory("'.dirPath$'/TTS")
212 .dirPath$ > '.dirPath$'/TTS/directory.txt
216 # Retrieve and store setting between sessions
218 procedure read_preferences .preferencesFile$
219 if not fileReadable(.preferencesFile$)
220 .preferencesFile$ = preferencesAppFile$
222 if fileReadable(.preferencesFile$)
223 Read from file... 'preferencesAppFile$'
224 .preferenceTable$ = selected$("Table")
225 .numPrefKeys = Get number of rows
226 for .row to .numPrefKeys
227 .variableName$ = Get value... '.row' Key
228 if variableExists(.variableName$)
229 .variableValue = Get value... '.row' Value
230 if .variableValue <> undefined
231 '.variableName$' = '.variableValue'
233 elsif variableExists(.variableName$+"$")
234 .variableValue$ = Get value... '.row' Value
235 .variableName$ = .variableName$+"$"
236 '.variableName$' = "'.variableValue$'"
240 select Table '.preferenceTable$'
245 procedure write_preferences .preferencesFile$
246 Create Table with column names... Preferences 0 Key Value
247 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
248 .preferencesFile$ = preferencesAppFile$
252 for .tableNum from 1 to 2
258 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
260 select Table '.table$'
261 .numTableRows = Get number of rows
262 for .tablerow to .numTableRows
263 select Table '.table$'
264 .label$ = Get value... '.tablerow' Label
265 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
266 .keyName$ = .variableName$
268 if variableExists(.variableName$)
269 .value = '.variableName$'
271 elsif variableExists(.variableName$ + "$")
272 .variableName$ = .variableName$ + "$"
273 .value$ = '.variableName$'
274 elsif index(.variableName$, "_") > 0
275 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
276 .keyName$ = .variableName$
277 select Table Preferences
278 .prefRow = Search column... Key '.keyName$'
280 if variableExists(.variableName$)
281 .value = '.variableName$'
283 elsif variableExists(.variableName$ + "$")
284 .variableName$ = .variableName$ + "$"
285 .value$ = '.variableName$'
291 select Table Preferences
294 Set string value... '.row' Key '.keyName$'
295 Set string value... '.row' Value '.value$'
300 # Some extra settings
301 select Table Preferences
304 Set string value... '.row' Key wordlistDir
305 Set string value... '.row' Value 'localWordlistDir$'
307 select Table Preferences
310 Set string value... '.row' Key wordlistName
311 Set string value... '.row' Value 'wordlistName$'
313 select Table Preferences
314 Write to table file... '.preferencesFile$'
319 ###############################################################
321 # TTS (speech synthesis). Place commands in user info
324 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
325 # speakCommandFile$ = ""
327 # Make sure sgc2.synthesizer is set!
328 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
329 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
330 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
334 if sgc2.synthesizer > 0
335 select sgc2.synthesizer
337 sgc2.synthesizer = -1
339 # Note, the TTScommand must set sgc2.synthesizer!
341 'sgc2.advancedTTScommand$'
343 'sgc2.normalTTScommand$'
347 .ttscommand$ = "speak"
351 .ttscommand$ = "espeak"
355 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
359 # Global TTS command has precedence
360 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
361 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
362 if config.synthesis$ = "_DISABLED_"
364 config.synthesis$ = "eSpeak"
368 # Local TTS command, if there is no global one
369 # Command does not exist yet, create it if eSpeak is installed
370 # Make sure to quote the path!
371 if speakCommandFile$ = ""
372 # Autodetect synthesizer command
373 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
374 # .command_path$ = ""
376 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
377 # .command_path$ < 'speakCommandDir$'/command_path.txt
379 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
380 # .command_path$ < 'speakCommandDir$'/command_path.txt
382 # if fileReadable(.ttscommand$)
383 # .command_path$ = .ttscommand$
386 # # Remove any newlines
387 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
389 # # Command path found
390 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
391 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
392 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
393 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
394 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
397 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
398 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
400 deleteFile("'speakCommandDir$'/command_path.txt")
403 # Close off (other TTS maybe a later option)
404 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
405 config.synthesis$ = "eSpeak"
409 ###############################################################
414 # initialiseSGC2.toneevaluation_table$ = ""
415 procedure initialize_toneevaluation_tables
417 if initialiseSGC2.toneevaluation_table$ != ""
418 select Table 'initialiseSGC2.toneevaluation_table$'
421 initialiseSGC2.toneevaluation_table$ = ""
423 if config.savePerf$ <> "" and fileReadable(config.savePerf$)
424 sgc2.performanceTable = Read from file: config.savePerf$
425 Rename: "Performance"
428 sgc2.performanceTable = Create Table with column names... Performance 0 Tones Correct Wrong Total High Low Wide Narrow Unknown Commented
430 initialiseSGC2.toneevaluation_table$ = "Performance"
431 call get_toneevaluation_row Total
434 procedure get_toneevaluation_row .tones$
435 select Table 'initialiseSGC2.toneevaluation_table$'
436 .row = Search column... Tones '.tones$'
439 .row = Get number of rows
440 Set string value... '.row' Tones '.tones$'
441 Set numeric value... '.row' Correct 0
442 Set numeric value... '.row' Wrong 0
443 Set numeric value... '.row' Total 0
444 Set numeric value... '.row' High 0
445 Set numeric value... '.row' Low 0
446 Set numeric value... '.row' Wide 0
447 Set numeric value... '.row' Narrow 0
448 Set numeric value... '.row' Unknown 0
449 Set numeric value... '.row' Commented 0
451 .row = Search column... Tones '.tones$'
455 procedure increment_toneevaluation_in_row .row .column$
456 select Table 'initialiseSGC2.toneevaluation_table$'
457 .tmp = Get value... '.row' '.column$'
459 Set numeric value... '.row' '.column$' '.tmp'
462 procedure increment_toneevaluation_value .tones$ .column$
463 call get_toneevaluation_row '.tones$'
464 .row = get_toneevaluation_row.row
465 call increment_toneevaluation_in_row '.row' '.column$'
467 call get_toneevaluation_row Total
468 .row = get_toneevaluation_row.row
469 call increment_toneevaluation_in_row '.row' '.column$'
472 procedure add_feedback_to_toneevaluation .table$
473 select Table '.table$'
474 .line1$ = Get value... 1 Text
475 .line2$ = Get value... 2 Text
476 .label$ = Get value... 3 Text
481 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^a-z0-9]", "", 0)
482 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
485 call increment_toneevaluation_value '.tones$' Total
486 if .tones$ = .recognized$
487 call increment_toneevaluation_value '.tones$' Correct
489 call increment_toneevaluation_value '.tones$' Wrong
491 if index_regex(.label$, "(Correct|Wrong)") <= 0
492 call increment_toneevaluation_value '.tones$' '.label$'
493 call increment_toneevaluation_value '.tones$' Commented
497 procedure CreateSummaryToneEvaluation
498 .font$ = defaultFont$
499 # Summarize tone evaluation
500 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
502 call get_toneevaluation_row Total
503 .row = get_toneevaluation_row.row
504 select Table 'initialiseSGC2.toneevaluation_table$'
505 .total = Get value... '.row' Total
506 .correct = Get value... '.row' Correct
508 .correctPerc = 100 * .correct / .total
512 .problematic = Get value... '.row' Commented
514 .problematicPerc = 100 * .problematic / .total
519 select Table SummaryToneEvaluation
521 Set numeric value... 1 Total '.total'
522 Set numeric value... 1 Correct '.correctPerc'
523 Set numeric value... 1 Problematic '.problematicPerc'
526 ###############################################################
528 # Miscelaneous procedures
530 ###############################################################
532 # Create a file 'preferencesAppDir$'/log/logPerformance
533 # This file contains the path to the target directory where logging
535 # Note that this procedure is nowhere used in SGC2. It has to
536 # be explicitely called in a separate script.
538 procedure activate_sgc2_logging .targetDirectory$
539 createDirectory("'preferencesAppDir$'")
540 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
541 createDirectory("'preferencesLogDir$'")
542 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
544 if startsWith(.targetDirectory$, "~")
545 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
548 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
549 newline$ >> 'preferencesLogDir$'/logPerformance.txt
553 procedure synthesize_sound .pinyin$
555 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
556 .command$ < 'speakCommandFile$'
557 .command$ = replace$(.command$, "'newline$'", " ", 0)
559 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
561 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
563 system_nocheck call '.command$' "'.pinyin$'"'
565 elsif sgc2.synthesizer > 0
566 select sgc2.synthesizer
567 Play text... '.pinyin$'
571 procedure align_recordedSound .pinyin$
572 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
573 select Sound 'recordedSound$'
574 .recordedTextGrid = To TextGrid... pinyin
575 Set interval text... 1 1 '.pinyin$'
576 select sgc2.synthesizer
577 plus Sound 'recordedSound$'
578 plus .recordedTextGrid
579 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
580 Rename... AlignedTextGrid
581 select .recordedTextGrid