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 # Do not store these variables as preferences
30 sgc.nonPreferences$ = " config.savePerf config.openPerf config.clearSummary config.audioName "
32 # Information for logging
33 currentDate$ = date$()
34 dateYear$ = right$(currentDate$, length(currentDate$) - rindex(currentDate$, " "))
35 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
36 currentLogDirectory$ = ""
37 feedbackTablePrefix$ = "Feedback"
38 feedbackTableName$ = ""
43 yFeedback = yWordlist + 5
44 yPinyin = yFeedback + 10
45 yContour = yPinyin + 15
46 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
47 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
48 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
49 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
51 # Pop-Up window colors
52 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
53 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
55 # Initialize parameters
70 config.deleteWordlist = -1
71 config.displayNumbers = 1
72 config.displayPinyin = 1
73 config.displayChar = 0
74 config.displayTrans = 1
75 config.useSoundExample = 1
76 config.synthesis$ = "_DISABLED_"
77 config.input$ = "Microphone"
78 config.showBackground = 1
79 sgc2.noiseThresshold = -30
83 config.clearSummary = 0
84 config.audioName$ = ""
87 mainPage.saveAudio$ = ""
89 sgc_ToneProt.minimumPitch = 40
90 sgc_ToneProt.maximumPitch = 600
92 # Platform dependent settings
93 if macintosh or windows
94 config.displayChar = 1
95 config.displayNumbers = 0
97 if windows and endsWith(build_SHA$, " XP")
98 config.displayChar = 0
99 config.displayNumbers = 0
102 config.displayChar = 0
103 config.displayNumbers = 1
107 samplingFrequency = 44100
109 samplingFrequency = 44100
111 samplingFrequency = 44100
116 config.language$ = "EN"
117 config.register = 249
118 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
119 preferencesLogDir$ = "'preferencesAppDir$'/log"
120 preferencesTableDir$ = "'preferencesAppDir$'/Data"
121 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
122 localWordlistDir$ = sgc2wordlists$
124 # Global word lists must be installed BY THE ADMINISTRATOR
125 # This means, create the directory 'globalwordlists$' and
126 # fill it with wordlist directories containing wordlist.Table
127 # and audio files. They can be copied from a local directory.
128 globalwordlists$ = ""
129 globaltablelists$ = ""
132 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
133 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
134 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
136 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
137 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
138 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
140 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
141 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
142 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
144 sgc2.synthesizer = -1
145 # Define a Praat TTS command that will set sgc2.synthesizer
146 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
147 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
148 sgc2.alignedTextGrid = -1
150 ##############################################################
152 config.shuffleLists = 1
154 defaultFont$ = "Helvetica"
157 # Set up directories if they do not exist already
158 call set_up_directories
160 # Get saved preferences
161 call read_preferences ""
164 speakCommandDir$ = "'preferencesAppDir$'/TTS"
165 speakCommandFile$ = ""
168 # Set inital language
169 call set_language 'config.language$'
172 call load_word_list "'localWordlistDir$'" 0
174 # Set up evaluation table
175 initialiseSGC2.toneevaluation_table$ = ""
176 call initialize_toneevaluation_tables
181 ###############################################################
183 # Obligatory procedures
185 ###############################################################
186 # Initialize Demo Window
187 procedure init_window
189 demo Line width... 'defaultLineWidth'
191 call set_font_size 'defaultFontSize'
194 # If there is no recorded sound, try to read stored recording
195 if recordedSound$ = "" and sgc.saveAudio$ <> ""
197 select Table 'wordlist$'
198 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
199 sgc.pinyin$ = Get value... 'te.currentWord' Pinyin
200 .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
201 if fileReadable(.outputName$)
202 te.recordedSound = Read from file: .outputName$
203 recordedSound$ = selected$("Sound")
206 # Set config.audioName
207 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/]+/?$", "", 0)
208 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
214 # Set mainPage.saveAudio$ grade
215 call set_grade_display 'sgc.pinyin$'
219 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
220 call set_window_title 'buttons$' '.windowTitle$'
221 # Display the word-list
223 # Wipe screen and draw background
224 call wipeArea 'wipeContourArea$'
225 if config.showBackground
226 call draw_background Background
228 # Define buttons in a table
234 call draw_tone_contour
236 call paint_saveAudio_light
241 # Make sure all Preferences directories are available
242 procedure set_up_directories
243 .dirPath$ = "'preferencesAppDir$'"
244 createDirectory(.dirPath$)
245 .dirPath$ > '.dirPath$'/directory.txt
246 createDirectory("'.dirPath$'/wordlists")
247 .dirPath$ > '.dirPath$'/wordlists/directory.txt
248 createDirectory("'.dirPath$'/pitchmodels")
249 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
250 createDirectory("'.dirPath$'/log")
251 .dirPath$ > '.dirPath$'/log/directory.txt
252 createDirectory("'.dirPath$'/TTS")
253 .dirPath$ > '.dirPath$'/TTS/directory.txt
257 # Retrieve and store setting between sessions
259 procedure read_preferences .preferencesFile$
260 if not fileReadable(.preferencesFile$)
261 .preferencesFile$ = preferencesAppFile$
263 if fileReadable(.preferencesFile$)
264 Read from file... 'preferencesAppFile$'
265 .preferenceTable$ = selected$("Table")
266 .numPrefKeys = Get number of rows
267 for .row to .numPrefKeys
268 .variableName$ = Get value... '.row' Key
269 if variableExists(.variableName$)
270 .variableValue = Get value... '.row' Value
271 if .variableValue <> undefined
272 '.variableName$' = '.variableValue'
274 elsif variableExists(.variableName$+"$")
275 .variableValue$ = Get value... '.row' Value
276 .variableName$ = .variableName$+"$"
277 '.variableName$' = "'.variableValue$'"
281 select Table '.preferenceTable$'
286 procedure write_preferences .preferencesFile$
287 Create Table with column names... Preferences 0 Key Value
288 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
289 .preferencesFile$ = preferencesAppFile$
293 for .tableNum from 1 to 2
299 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
301 select Table '.table$'
302 .numTableRows = Get number of rows
303 for .tablerow to .numTableRows
304 select Table '.table$'
305 .label$ = Get value... '.tablerow' Label
306 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
307 .keyName$ = .variableName$
309 if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
311 elsif variableExists(.variableName$)
312 .value = '.variableName$'
314 elsif variableExists(.variableName$ + "$")
315 .variableName$ = .variableName$ + "$"
316 .value$ = '.variableName$'
317 elsif index(.variableName$, "_") > 0
318 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
319 .keyName$ = .variableName$
320 select Table Preferences
321 .prefRow = Search column... Key '.keyName$'
323 if variableExists(.variableName$)
324 .value = '.variableName$'
326 elsif variableExists(.variableName$ + "$")
327 .variableName$ = .variableName$ + "$"
328 .value$ = '.variableName$'
334 select Table Preferences
337 Set string value... '.row' Key '.keyName$'
338 Set string value... '.row' Value '.value$'
343 # Some extra settings
344 select Table Preferences
347 Set string value... '.row' Key wordlistDir
348 Set string value... '.row' Value 'localWordlistDir$'
350 select Table Preferences
353 Set string value... '.row' Key wordlistName
354 Set string value... '.row' Value 'wordlistName$'
356 select Table Preferences
357 Write to table file... '.preferencesFile$'
362 ###############################################################
364 # TTS (speech synthesis). Place commands in user info
367 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
368 # speakCommandFile$ = ""
370 # Make sure sgc2.synthesizer is set!
371 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
372 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
373 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
377 if sgc2.synthesizer > 0
378 select sgc2.synthesizer
380 sgc2.synthesizer = -1
382 # Note, the TTScommand must set sgc2.synthesizer!
384 'sgc2.advancedTTScommand$'
386 'sgc2.normalTTScommand$'
390 .ttscommand$ = "speak"
394 .ttscommand$ = "espeak"
398 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
402 # Global TTS command has precedence
403 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
404 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
405 if config.synthesis$ = "_DISABLED_"
407 config.synthesis$ = "eSpeak"
411 # Local TTS command, if there is no global one
412 # Command does not exist yet, create it if eSpeak is installed
413 # Make sure to quote the path!
414 if speakCommandFile$ = ""
415 # Autodetect synthesizer command
416 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
417 # .command_path$ = ""
419 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
420 # .command_path$ < 'speakCommandDir$'/command_path.txt
422 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
423 # .command_path$ < 'speakCommandDir$'/command_path.txt
425 # if fileReadable(.ttscommand$)
426 # .command_path$ = .ttscommand$
429 # # Remove any newlines
430 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
432 # # Command path found
433 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
434 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
435 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
436 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
437 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
440 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
441 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
443 deleteFile("'speakCommandDir$'/command_path.txt")
446 # Close off (other TTS maybe a later option)
447 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
448 config.synthesis$ = "eSpeak"
452 ###############################################################
457 # initialiseSGC2.toneevaluation_table$ = ""
458 procedure initialize_toneevaluation_tables
460 if initialiseSGC2.toneevaluation_table$ != ""
461 select Table 'initialiseSGC2.toneevaluation_table$'
464 initialiseSGC2.toneevaluation_table$ = ""
466 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
467 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
468 Rename: "Performance"
471 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Time
473 initialiseSGC2.toneevaluation_table$ = "Performance"
474 call get_toneevaluation_row Total
477 procedure get_toneevaluation_row .pinyin$
478 select Table 'initialiseSGC2.toneevaluation_table$'
479 .row = Search column... Pinyin '.pinyin$'
482 .row = Get number of rows
483 Set string value... '.row' Pinyin '.pinyin$'
484 Set numeric value... '.row' Correct 0
485 Set numeric value... '.row' Wrong 0
486 Set numeric value... '.row' Total 0
487 Set numeric value... '.row' High 0
488 Set numeric value... '.row' Low 0
489 Set numeric value... '.row' Wide 0
490 Set numeric value... '.row' Narrow 0
491 Set numeric value... '.row' Unknown 0
492 Set numeric value... '.row' Commented 0
493 Set numeric value... '.row' Time 0
495 .row = Search column... Pinyin '.pinyin$'
499 procedure increment_toneevaluation_in_row .row .column$
500 select Table 'initialiseSGC2.toneevaluation_table$'
501 .tmp = Get value... '.row' '.column$'
503 Set numeric value... '.row' '.column$' '.tmp'
507 Set string value: .row, "Time", .dateTime$
511 # Update existing performance file with toneevaluation
512 procedure update_toneevaluation_file
513 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
514 select Table 'initialiseSGC2.toneevaluation_table$'
515 Write to table file... 'sgc.savePerf$'
519 procedure increment_toneevaluation_value .pinyin$ .column$
520 call get_toneevaluation_row '.pinyin$'
521 .row = get_toneevaluation_row.row
522 call increment_toneevaluation_in_row '.row' '.column$'
524 call get_toneevaluation_row Total
525 .row = get_toneevaluation_row.row
526 call increment_toneevaluation_in_row '.row' '.column$'
529 procedure add_feedback_to_toneevaluation .table$
530 select Table '.table$'
531 .line1$ = Get value... 1 Text
532 .line2$ = Get value... 2 Text
533 .label$ = Get value... 3 Text
538 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
539 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
542 call increment_toneevaluation_value 'sgc.pinyin$' Total
543 if .tones$ = .recognized$
544 call increment_toneevaluation_value 'sgc.pinyin$' Correct
546 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
548 if index_regex(.label$, "(Correct|Wrong)") <= 0
549 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
550 call increment_toneevaluation_value 'sgc.pinyin$' Commented
553 # Update performance table when SaveAudio is on
555 call update_toneevaluation_file
559 procedure setGrade .grade$
560 select Table 'wordlist$'
561 if recordedSound$ <> "" and sgc.pinyin$ <> ""
562 call get_toneevaluation_row 'sgc.pinyin$'
563 .row = get_toneevaluation_row.row
564 select Table 'initialiseSGC2.toneevaluation_table$'
565 .col = Get column index... Grade
567 Insert column... 2 Grade
570 Set numeric value: .row, "Grade", '.grade$'
571 .numRows = Get number of rows
574 for .r from 2 to .numRows
575 .tmp = Get value: .r, "Grade"
583 .meanGrade = .sumGrade / .sumN
585 Set numeric value: 1, "Grade", .meanGrade
587 # Store current performance table
588 call update_toneevaluation_file
592 # Set mainPage.saveAudio$ to Grade
593 procedure set_grade_display .pinyin$
594 mainPage.saveAudio$ = ""
595 select Table 'initialiseSGC2.toneevaluation_table$'
597 .col = Get column index: "Grade"
598 if .pinyin$ <> "" and .col > 0
599 call get_toneevaluation_row '.pinyin$'
600 .row = get_toneevaluation_row.row
602 .grade$ = Get value: .row, "Grade"
603 if index_regex(.grade$, "[0-9]") <= 0
609 mainPage.saveAudio$ = .grade$
613 ###############################################################
615 # Miscelaneous procedures
617 ###############################################################
620 procedure synthesize_sound .pinyin$
622 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
623 .command$ < 'speakCommandFile$'
624 .command$ = replace$(.command$, "'newline$'", " ", 0)
626 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
628 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
630 system_nocheck call '.command$' "'.pinyin$'"'
632 elsif sgc2.synthesizer > 0
633 select sgc2.synthesizer
634 Play text... '.pinyin$'
638 procedure align_recordedSound .pinyin$
639 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
640 select Sound 'recordedSound$'
641 .recordedTextGrid = To TextGrid... pinyin
642 Set interval text... 1 1 '.pinyin$'
643 select sgc2.synthesizer
644 plus Sound 'recordedSound$'
645 plus .recordedTextGrid
646 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
647 Rename... AlignedTextGrid
648 select .recordedTextGrid