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
66 config.deleteWordlist = -1
67 config.displayNumbers = 1
68 config.displayPinyin = 1
69 config.displayChar = 0
70 config.displayTrans = 1
71 config.useSoundExample = 1
72 config.synthesis$ = "_DISABLED_"
73 config.input$ = "Microphone"
74 config.showBackground = 1
75 sgc2.noiseThresshold = -30
78 config.saveAudio$ = ""
80 sgc_ToneProt.minimumPitch = 40
81 sgc_ToneProt.maximumPitch = 600
83 # Platform dependent settings
84 if macintosh or windows
85 config.displayChar = 1
86 config.displayNumbers = 0
88 if windows and endsWith(build_SHA$, " XP")
89 config.displayChar = 0
90 config.displayNumbers = 0
93 config.displayChar = 0
94 config.displayNumbers = 1
98 samplingFrequency = 44100
100 samplingFrequency = 44100
102 samplingFrequency = 44100
107 config.language$ = "EN"
108 config.register = 249
109 config.logPerformance = 0
110 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
111 preferencesLogDir$ = "'preferencesAppDir$'/log"
112 preferencesTableDir$ = "'preferencesAppDir$'/Data"
113 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
114 localWordlistDir$ = sgc2wordlists$
116 # Global word lists must be installed BY THE ADMINISTRATOR
117 # This means, create the directory 'globalwordlists$' and
118 # fill it with wordlist directories containing wordlist.Table
119 # and audio files. They can be copied from a local directory.
120 globalwordlists$ = ""
121 globaltablelists$ = ""
124 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
125 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
126 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
128 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
129 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
130 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
132 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
133 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
134 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
136 sgc2.synthesizer = -1
137 # Define a Praat TTS command that will set sgc2.synthesizer
138 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
139 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
140 sgc2.alignedTextGrid = -1
142 ##############################################################
144 config.shuffleLists = 1
146 defaultFont$ = "Helvetica"
149 # Set up directories if they do not exist already
150 call set_up_directories
152 # Get saved preferences
153 call read_preferences ""
156 speakCommandDir$ = "'preferencesAppDir$'/TTS"
157 speakCommandFile$ = ""
160 # Set inital language
161 call set_language 'config.language$'
164 call load_word_list "'localWordlistDir$'" 0
166 # Set up evaluation table
167 initialiseSGC2.toneevaluation_table$ = ""
168 call initialize_toneevaluation_tables
173 ###############################################################
175 # Obligatory procedures
177 ###############################################################
178 # Initialize Demo Window
179 procedure init_window
181 demo Line width... 'defaultLineWidth'
183 call set_font_size 'defaultFontSize'
186 # If there is no recorded sound, try to read stored recording
187 if recordedSound$ = "" and config.saveAudio$ <> ""
189 select Table 'wordlist$'
190 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
191 .pinyin$ = Get value... 'te.currentWord' Pinyin
192 .outputName$ = "'config.saveAudio$'/'.pinyin$'.wav"
193 if fileReadable(.outputName$)
194 te.recordedSound = Read from file: .outputName$
195 recordedSound$ = selected$("Sound")
203 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
204 call set_window_title 'buttons$' '.windowTitle$'
205 # Display the word-list
207 # Wipe screen and draw background
208 call wipeArea 'wipeContourArea$'
209 if config.showBackground
210 call draw_background Background
212 # Define buttons in a table
218 call draw_tone_contour
220 call paint_saveAudio_light
225 # Make sure all Preferences directories are available
226 procedure set_up_directories
227 .dirPath$ = "'preferencesAppDir$'"
228 createDirectory(.dirPath$)
229 .dirPath$ > '.dirPath$'/directory.txt
230 createDirectory("'.dirPath$'/wordlists")
231 .dirPath$ > '.dirPath$'/wordlists/directory.txt
232 createDirectory("'.dirPath$'/pitchmodels")
233 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
234 createDirectory("'.dirPath$'/log")
235 .dirPath$ > '.dirPath$'/log/directory.txt
236 createDirectory("'.dirPath$'/TTS")
237 .dirPath$ > '.dirPath$'/TTS/directory.txt
241 # Retrieve and store setting between sessions
243 procedure read_preferences .preferencesFile$
244 if not fileReadable(.preferencesFile$)
245 .preferencesFile$ = preferencesAppFile$
247 if fileReadable(.preferencesFile$)
248 Read from file... 'preferencesAppFile$'
249 .preferenceTable$ = selected$("Table")
250 .numPrefKeys = Get number of rows
251 for .row to .numPrefKeys
252 .variableName$ = Get value... '.row' Key
253 if variableExists(.variableName$)
254 .variableValue = Get value... '.row' Value
255 if .variableValue <> undefined
256 '.variableName$' = '.variableValue'
258 elsif variableExists(.variableName$+"$")
259 .variableValue$ = Get value... '.row' Value
260 .variableName$ = .variableName$+"$"
261 '.variableName$' = "'.variableValue$'"
265 select Table '.preferenceTable$'
270 procedure write_preferences .preferencesFile$
271 Create Table with column names... Preferences 0 Key Value
272 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
273 .preferencesFile$ = preferencesAppFile$
277 for .tableNum from 1 to 2
283 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
285 select Table '.table$'
286 .numTableRows = Get number of rows
287 for .tablerow to .numTableRows
288 select Table '.table$'
289 .label$ = Get value... '.tablerow' Label
290 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
291 .keyName$ = .variableName$
293 if variableExists(.variableName$)
294 .value = '.variableName$'
296 elsif variableExists(.variableName$ + "$")
297 .variableName$ = .variableName$ + "$"
298 .value$ = '.variableName$'
299 elsif index(.variableName$, "_") > 0
300 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
301 .keyName$ = .variableName$
302 select Table Preferences
303 .prefRow = Search column... Key '.keyName$'
305 if variableExists(.variableName$)
306 .value = '.variableName$'
308 elsif variableExists(.variableName$ + "$")
309 .variableName$ = .variableName$ + "$"
310 .value$ = '.variableName$'
316 select Table Preferences
319 Set string value... '.row' Key '.keyName$'
320 Set string value... '.row' Value '.value$'
325 # Some extra settings
326 select Table Preferences
329 Set string value... '.row' Key wordlistDir
330 Set string value... '.row' Value 'localWordlistDir$'
332 select Table Preferences
335 Set string value... '.row' Key wordlistName
336 Set string value... '.row' Value 'wordlistName$'
338 select Table Preferences
339 Write to table file... '.preferencesFile$'
344 ###############################################################
346 # TTS (speech synthesis). Place commands in user info
349 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
350 # speakCommandFile$ = ""
352 # Make sure sgc2.synthesizer is set!
353 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
354 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
355 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
359 if sgc2.synthesizer > 0
360 select sgc2.synthesizer
362 sgc2.synthesizer = -1
364 # Note, the TTScommand must set sgc2.synthesizer!
366 'sgc2.advancedTTScommand$'
368 'sgc2.normalTTScommand$'
372 .ttscommand$ = "speak"
376 .ttscommand$ = "espeak"
380 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
384 # Global TTS command has precedence
385 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
386 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
387 if config.synthesis$ = "_DISABLED_"
389 config.synthesis$ = "eSpeak"
393 # Local TTS command, if there is no global one
394 # Command does not exist yet, create it if eSpeak is installed
395 # Make sure to quote the path!
396 if speakCommandFile$ = ""
397 # Autodetect synthesizer command
398 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
399 # .command_path$ = ""
401 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
402 # .command_path$ < 'speakCommandDir$'/command_path.txt
404 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
405 # .command_path$ < 'speakCommandDir$'/command_path.txt
407 # if fileReadable(.ttscommand$)
408 # .command_path$ = .ttscommand$
411 # # Remove any newlines
412 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
414 # # Command path found
415 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
416 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
417 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
418 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
419 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
422 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
423 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
425 deleteFile("'speakCommandDir$'/command_path.txt")
428 # Close off (other TTS maybe a later option)
429 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
430 config.synthesis$ = "eSpeak"
434 ###############################################################
439 # initialiseSGC2.toneevaluation_table$ = ""
440 procedure initialize_toneevaluation_tables
442 if initialiseSGC2.toneevaluation_table$ != ""
443 select Table 'initialiseSGC2.toneevaluation_table$'
446 initialiseSGC2.toneevaluation_table$ = ""
448 if config.savePerf$ <> "" and fileReadable(config.savePerf$)
449 sgc2.performanceTable = Read from file: config.savePerf$
450 Rename: "Performance"
453 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Time
455 initialiseSGC2.toneevaluation_table$ = "Performance"
456 call get_toneevaluation_row Total
459 procedure get_toneevaluation_row .tones$
460 select Table 'initialiseSGC2.toneevaluation_table$'
461 .row = Search column... Pinyin '.tones$'
464 .row = Get number of rows
465 Set string value... '.row' Pinyin '.tones$'
466 Set numeric value... '.row' Correct 0
467 Set numeric value... '.row' Wrong 0
468 Set numeric value... '.row' Total 0
469 Set numeric value... '.row' High 0
470 Set numeric value... '.row' Low 0
471 Set numeric value... '.row' Wide 0
472 Set numeric value... '.row' Narrow 0
473 Set numeric value... '.row' Unknown 0
474 Set numeric value... '.row' Commented 0
475 Set numeric value... '.row' Time 0
477 .row = Search column... Pinyin '.tones$'
481 procedure increment_toneevaluation_in_row .row .column$
482 select Table 'initialiseSGC2.toneevaluation_table$'
483 .tmp = Get value... '.row' '.column$'
485 Set numeric value... '.row' '.column$' '.tmp'
489 Set string value: .row, "Time", .dateTime$
492 procedure increment_toneevaluation_value .tones$ .column$
493 call get_toneevaluation_row '.tones$'
494 .row = get_toneevaluation_row.row
495 call increment_toneevaluation_in_row '.row' '.column$'
497 call get_toneevaluation_row Total
498 .row = get_toneevaluation_row.row
499 call increment_toneevaluation_in_row '.row' '.column$'
502 procedure add_feedback_to_toneevaluation .table$
503 select Table '.table$'
504 .line1$ = Get value... 1 Text
505 .line2$ = Get value... 2 Text
506 .label$ = Get value... 3 Text
511 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^a-z0-9]", "", 0)
512 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
515 call increment_toneevaluation_value '.tones$' Total
516 if .tones$ = .recognized$
517 call increment_toneevaluation_value '.tones$' Correct
519 call increment_toneevaluation_value '.tones$' Wrong
521 if index_regex(.label$, "(Correct|Wrong)") <= 0
522 call increment_toneevaluation_value '.tones$' '.label$'
523 call increment_toneevaluation_value '.tones$' Commented
527 procedure CreateSummaryToneEvaluation
528 .font$ = defaultFont$
529 # Summarize tone evaluation
530 Create Table with column names... SummaryToneEvaluation 0 Total Correct Problematic
532 call get_toneevaluation_row Total
533 .row = get_toneevaluation_row.row
534 select Table 'initialiseSGC2.toneevaluation_table$'
535 .total = Get value... '.row' Total
536 .correct = Get value... '.row' Correct
538 .correctPerc = 100 * .correct / .total
542 .problematic = Get value... '.row' Commented
544 .problematicPerc = 100 * .problematic / .total
549 select Table SummaryToneEvaluation
551 Set numeric value... 1 Total '.total'
552 Set numeric value... 1 Correct '.correctPerc'
553 Set numeric value... 1 Problematic '.problematicPerc'
556 procedure setGrade .grade$
557 select Table 'wordlist$'
558 if recordedSound$ <> "" and te.currentWord > 0 and te.currentWord <= te.numberOfWords
559 .pinyin$ = Get value: te.currentWord, "Pinyin"
561 call get_toneevaluation_row '.pinyin$'
562 .row = get_toneevaluation_row.row
563 select Table 'initialiseSGC2.toneevaluation_table$'
564 .col = Get column index... Grade
566 Append column... Grade
569 Set numeric value: .row, "Grade", '.grade$'
570 .numRows = Get number of rows
572 for .r from 2 to .numRows
573 .tmp = Get value: .r, "Grade"
576 .meanGrade = .sumGrade / (.numRows - 1)
577 Set numeric value: 1, "Grade", .meanGrade
582 ###############################################################
584 # Miscelaneous procedures
586 ###############################################################
588 # Create a file 'preferencesAppDir$'/log/logPerformance
589 # This file contains the path to the target directory where logging
591 # Note that this procedure is nowhere used in SGC2. It has to
592 # be explicitely called in a separate script.
594 procedure activate_sgc2_logging .targetDirectory$
595 createDirectory("'preferencesAppDir$'")
596 preferencesAppDir$ > 'preferencesAppDir$'/directory.txt
597 createDirectory("'preferencesLogDir$'")
598 preferencesLogDir$ > 'preferencesLogDir$'/directory.txt
600 if startsWith(.targetDirectory$, "~")
601 .targetDirectory$ = replace$(.targetDirectory$, "~", homeDirectory$, 1)
604 .targetDirectory$ > 'preferencesLogDir$'/logPerformance.txt
605 newline$ >> 'preferencesLogDir$'/logPerformance.txt
609 procedure synthesize_sound .pinyin$
611 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
612 .command$ < 'speakCommandFile$'
613 .command$ = replace$(.command$, "'newline$'", " ", 0)
615 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
617 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
619 system_nocheck call '.command$' "'.pinyin$'"'
621 elsif sgc2.synthesizer > 0
622 select sgc2.synthesizer
623 Play text... '.pinyin$'
627 procedure align_recordedSound .pinyin$
628 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
629 select Sound 'recordedSound$'
630 .recordedTextGrid = To TextGrid... pinyin
631 Set interval text... 1 1 '.pinyin$'
632 select sgc2.synthesizer
633 plus Sound 'recordedSound$'
634 plus .recordedTextGrid
635 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
636 Rename... AlignedTextGrid
637 select .recordedTextGrid