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
71 config.deleteWordlist = -1
72 config.displayNumbers = 1
73 config.displayPinyin = 1
74 config.displayChar = 0
75 config.displayTrans = 1
76 config.useSoundExample = 1
77 config.synthesis$ = "_DISABLED_"
78 config.input$ = "Microphone"
79 config.showBackground = 1
80 sgc2.noiseThresshold = -30
85 config.clearSummary = 0
86 config.audioName$ = ""
89 mainPage.saveAudio$ = ""
91 sgc_ToneProt.minimumPitch = 40
92 sgc_ToneProt.maximumPitch = 600
94 # Platform dependent settings
95 if macintosh or windows
96 config.displayChar = 1
97 config.displayNumbers = 0
99 if windows and endsWith(build_SHA$, " XP")
100 config.displayChar = 0
101 config.displayNumbers = 0
104 config.displayChar = 0
105 config.displayNumbers = 1
109 samplingFrequency = 44100
111 samplingFrequency = 44100
113 samplingFrequency = 44100
118 config.language$ = "EN"
119 config.register = 249
120 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
121 preferencesLogDir$ = "'preferencesAppDir$'/log"
122 preferencesTableDir$ = "'preferencesAppDir$'/Data"
123 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
124 localWordlistDir$ = sgc2wordlists$
126 # Global word lists must be installed BY THE ADMINISTRATOR
127 # This means, create the directory 'globalwordlists$' and
128 # fill it with wordlist directories containing wordlist.Table
129 # and audio files. They can be copied from a local directory.
130 globalwordlists$ = ""
131 globaltablelists$ = ""
134 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
135 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
136 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
138 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
139 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
140 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
142 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
143 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
144 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
146 sgc2.synthesizer = -1
147 # Define a Praat TTS command that will set sgc2.synthesizer
148 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
149 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
150 sgc2.alignedTextGrid = -1
152 ##############################################################
154 config.shuffleLists = 1
156 defaultFont$ = "Helvetica"
159 # Set up directories if they do not exist already
160 call set_up_directories
162 # Get saved preferences
163 call read_preferences ""
166 speakCommandDir$ = "'preferencesAppDir$'/TTS"
167 speakCommandFile$ = ""
170 # Set inital language
171 call set_language 'config.language$'
174 call load_word_list "'localWordlistDir$'" 0
176 # Set up evaluation table
177 initialiseSGC2.toneevaluation_table$ = ""
178 call initialize_toneevaluation_tables
183 ###############################################################
185 # Obligatory procedures
187 ###############################################################
188 # Initialize Demo Window
189 procedure init_window
191 demo Line width... 'defaultLineWidth'
193 call set_font_size 'defaultFontSize'
196 # If there is no recorded sound, try to read stored recording
197 if recordedSound$ = "" and sgc.saveAudio$ <> ""
199 select Table 'wordlist$'
200 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
201 sgc.pinyin$ = Get value... 'te.currentWord' Pinyin
202 .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
203 if fileReadable(.outputName$)
204 te.recordedSound = Read from file: .outputName$
205 recordedSound$ = selected$("Sound")
208 # Set config.audioName
209 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
210 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
216 # Set mainPage.saveAudio$ grade
217 call set_grade_display 'sgc.pinyin$'
221 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
222 call set_window_title 'buttons$' '.windowTitle$'
223 # Display the word-list
225 # Wipe screen and draw background
226 call wipeArea 'wipeContourArea$'
227 if config.showBackground
228 call draw_background Background
230 # Define buttons in a table
236 call draw_tone_contour
238 call paint_saveAudio_light
243 # Make sure all Preferences directories are available
244 procedure set_up_directories
245 .dirPath$ = "'preferencesAppDir$'"
246 createDirectory(.dirPath$)
247 .dirPath$ > '.dirPath$'/directory.txt
248 createDirectory("'.dirPath$'/wordlists")
249 .dirPath$ > '.dirPath$'/wordlists/directory.txt
250 createDirectory("'.dirPath$'/pitchmodels")
251 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
252 createDirectory("'.dirPath$'/log")
253 .dirPath$ > '.dirPath$'/log/directory.txt
254 createDirectory("'.dirPath$'/TTS")
255 .dirPath$ > '.dirPath$'/TTS/directory.txt
259 # Retrieve and store setting between sessions
261 procedure read_preferences .preferencesFile$
262 if not fileReadable(.preferencesFile$)
263 .preferencesFile$ = preferencesAppFile$
265 if fileReadable(.preferencesFile$)
266 Read from file... 'preferencesAppFile$'
267 .preferenceTable$ = selected$("Table")
268 .numPrefKeys = Get number of rows
269 for .row to .numPrefKeys
270 .variableName$ = Get value... '.row' Key
271 if variableExists(.variableName$)
272 .variableValue = Get value... '.row' Value
273 if .variableValue <> undefined
274 '.variableName$' = '.variableValue'
276 elsif variableExists(.variableName$+"$")
277 .variableValue$ = Get value... '.row' Value
278 .variableName$ = .variableName$+"$"
279 '.variableName$' = "'.variableValue$'"
283 select Table '.preferenceTable$'
288 procedure write_preferences .preferencesFile$
289 Create Table with column names... Preferences 0 Key Value
290 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
291 .preferencesFile$ = preferencesAppFile$
295 for .tableNum from 1 to 2
301 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
303 select Table '.table$'
304 .numTableRows = Get number of rows
305 for .tablerow to .numTableRows
306 select Table '.table$'
307 .label$ = Get value... '.tablerow' Label
308 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
309 .keyName$ = .variableName$
311 if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
313 elsif variableExists(.variableName$)
314 .value = '.variableName$'
316 elsif variableExists(.variableName$ + "$")
317 .variableName$ = .variableName$ + "$"
318 .value$ = '.variableName$'
319 elsif index(.variableName$, "_") > 0
320 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
321 .keyName$ = .variableName$
322 select Table Preferences
323 .prefRow = Search column... Key '.keyName$'
325 if variableExists(.variableName$)
326 .value = '.variableName$'
328 elsif variableExists(.variableName$ + "$")
329 .variableName$ = .variableName$ + "$"
330 .value$ = '.variableName$'
336 select Table Preferences
339 Set string value... '.row' Key '.keyName$'
340 Set string value... '.row' Value '.value$'
345 # Some extra settings
346 select Table Preferences
349 Set string value... '.row' Key wordlistDir
350 Set string value... '.row' Value 'localWordlistDir$'
352 select Table Preferences
355 Set string value... '.row' Key wordlistName
356 Set string value... '.row' Value 'wordlistName$'
358 select Table Preferences
359 Write to table file... '.preferencesFile$'
364 ###############################################################
366 # TTS (speech synthesis). Place commands in user info
369 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
370 # speakCommandFile$ = ""
372 # Make sure sgc2.synthesizer is set!
373 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
374 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
375 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
379 if sgc2.synthesizer > 0
380 select sgc2.synthesizer
382 sgc2.synthesizer = -1
384 # Note, the TTScommand must set sgc2.synthesizer!
385 if config.strict$ = "'sgc.highestLevel'"
386 'sgc2.advancedTTScommand$'
388 'sgc2.normalTTScommand$'
392 .ttscommand$ = "speak"
396 .ttscommand$ = "espeak"
400 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
404 # Global TTS command has precedence
405 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
406 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
407 if config.synthesis$ = "_DISABLED_"
409 config.synthesis$ = "eSpeak"
413 # Local TTS command, if there is no global one
414 # Command does not exist yet, create it if eSpeak is installed
415 # Make sure to quote the path!
416 if speakCommandFile$ = ""
417 # Autodetect synthesizer command
418 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
419 # .command_path$ = ""
421 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
422 # .command_path$ < 'speakCommandDir$'/command_path.txt
424 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
425 # .command_path$ < 'speakCommandDir$'/command_path.txt
427 # if fileReadable(.ttscommand$)
428 # .command_path$ = .ttscommand$
431 # # Remove any newlines
432 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
434 # # Command path found
435 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
436 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
437 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
438 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
439 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
442 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
443 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
445 deleteFile("'speakCommandDir$'/command_path.txt")
448 # Close off (other TTS maybe a later option)
449 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
450 config.synthesis$ = "eSpeak"
454 ###############################################################
459 # initialiseSGC2.toneevaluation_table$ = ""
460 procedure initialize_toneevaluation_tables
462 if initialiseSGC2.toneevaluation_table$ != ""
463 select Table 'initialiseSGC2.toneevaluation_table$'
466 initialiseSGC2.toneevaluation_table$ = ""
468 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
469 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
470 Rename: "Performance"
473 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Time
475 initialiseSGC2.toneevaluation_table$ = "Performance"
476 call get_toneevaluation_row Total
479 procedure get_toneevaluation_row .pinyin$
480 select Table 'initialiseSGC2.toneevaluation_table$'
481 .row = Search column... Pinyin '.pinyin$'
484 .row = Get number of rows
485 Set string value... '.row' Pinyin '.pinyin$'
486 Set numeric value... '.row' Correct 0
487 Set numeric value... '.row' Wrong 0
488 Set numeric value... '.row' Total 0
489 Set numeric value... '.row' High 0
490 Set numeric value... '.row' Low 0
491 Set numeric value... '.row' Wide 0
492 Set numeric value... '.row' Narrow 0
493 Set numeric value... '.row' Unknown 0
494 Set numeric value... '.row' Commented 0
495 Set numeric value... '.row' Time 0
497 .row = Search column... Pinyin '.pinyin$'
501 procedure increment_toneevaluation_in_row .row .column$
502 select Table 'initialiseSGC2.toneevaluation_table$'
503 .tmp = Get value... '.row' '.column$'
505 Set numeric value... '.row' '.column$' '.tmp'
509 Set string value: .row, "Time", .dateTime$
513 # Update existing performance file with toneevaluation
514 procedure update_toneevaluation_file
515 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
516 select Table 'initialiseSGC2.toneevaluation_table$'
517 Write to table file... 'sgc.savePerf$'
521 procedure increment_toneevaluation_value .pinyin$ .column$
522 call get_toneevaluation_row '.pinyin$'
523 .row = get_toneevaluation_row.row
524 call increment_toneevaluation_in_row '.row' '.column$'
526 call get_toneevaluation_row Total
527 .row = get_toneevaluation_row.row
528 call increment_toneevaluation_in_row '.row' '.column$'
531 procedure add_feedback_to_toneevaluation .table$
532 select Table '.table$'
533 .line1$ = Get value... 1 Text
534 .line2$ = Get value... 2 Text
535 .label$ = Get value... 3 Text
540 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
541 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
544 call increment_toneevaluation_value 'sgc.pinyin$' Total
545 if .tones$ = .recognized$
546 call increment_toneevaluation_value 'sgc.pinyin$' Correct
548 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
550 if index_regex(.label$, "(Correct|Wrong)") <= 0
551 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
552 call increment_toneevaluation_value 'sgc.pinyin$' Commented
555 # Update performance table when SaveAudio is on
557 call update_toneevaluation_file
561 procedure setGrade .grade$
562 select Table 'wordlist$'
563 if recordedSound$ <> "" and sgc.pinyin$ <> ""
564 call get_toneevaluation_row 'sgc.pinyin$'
565 .row = get_toneevaluation_row.row
566 select Table 'initialiseSGC2.toneevaluation_table$'
567 .col = Get column index... Grade
569 Insert column... 2 Grade
572 Set numeric value: .row, "Grade", '.grade$'
573 .numRows = Get number of rows
576 for .r from 2 to .numRows
577 .tmp = Get value: .r, "Grade"
585 .meanGrade = .sumGrade / .sumN
587 Set numeric value: 1, "Grade", .meanGrade
589 # Store current performance table
590 call update_toneevaluation_file
594 # Set mainPage.saveAudio$ to Grade
595 procedure set_grade_display .pinyin$
596 mainPage.saveAudio$ = ""
597 select Table 'initialiseSGC2.toneevaluation_table$'
599 .col = Get column index: "Grade"
600 if .pinyin$ <> "" and .col > 0
601 call get_toneevaluation_row '.pinyin$'
602 .row = get_toneevaluation_row.row
604 .grade$ = Get value: .row, "Grade"
605 if index_regex(.grade$, "[0-9]") <= 0
611 mainPage.saveAudio$ = .grade$
615 ###############################################################
617 # Miscelaneous procedures
619 ###############################################################
622 procedure synthesize_sound .pinyin$
624 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
625 .command$ < 'speakCommandFile$'
626 .command$ = replace$(.command$, "'newline$'", " ", 0)
628 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
630 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
632 system_nocheck call '.command$' "'.pinyin$'"'
634 elsif sgc2.synthesizer > 0
635 select sgc2.synthesizer
636 Play text... '.pinyin$'
640 procedure align_recordedSound .pinyin$
641 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
642 select Sound 'recordedSound$'
643 .recordedTextGrid = To TextGrid... pinyin
644 Set interval text... 1 1 '.pinyin$'
645 select sgc2.synthesizer
646 plus Sound 'recordedSound$'
647 plus .recordedTextGrid
648 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
649 Rename... AlignedTextGrid
650 select .recordedTextGrid