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$ = ""
88 sgc_ToneProt.minimumPitch = 40
89 sgc_ToneProt.maximumPitch = 600
91 # Platform dependent settings
92 if macintosh or windows
93 config.displayChar = 1
94 config.displayNumbers = 0
96 if windows and endsWith(build_SHA$, " XP")
97 config.displayChar = 0
98 config.displayNumbers = 0
101 config.displayChar = 0
102 config.displayNumbers = 1
106 samplingFrequency = 44100
108 samplingFrequency = 44100
110 samplingFrequency = 44100
115 config.language$ = "EN"
116 config.register = 249
117 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
118 preferencesLogDir$ = "'preferencesAppDir$'/log"
119 preferencesTableDir$ = "'preferencesAppDir$'/Data"
120 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
121 localWordlistDir$ = sgc2wordlists$
123 # Global word lists must be installed BY THE ADMINISTRATOR
124 # This means, create the directory 'globalwordlists$' and
125 # fill it with wordlist directories containing wordlist.Table
126 # and audio files. They can be copied from a local directory.
127 globalwordlists$ = ""
128 globaltablelists$ = ""
131 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
132 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
133 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
135 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
136 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
137 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
139 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
140 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
141 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
143 sgc2.synthesizer = -1
144 # Define a Praat TTS command that will set sgc2.synthesizer
145 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
146 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
147 sgc2.alignedTextGrid = -1
149 ##############################################################
151 config.shuffleLists = 1
153 defaultFont$ = "Helvetica"
156 # Set up directories if they do not exist already
157 call set_up_directories
159 # Get saved preferences
160 call read_preferences ""
163 speakCommandDir$ = "'preferencesAppDir$'/TTS"
164 speakCommandFile$ = ""
167 # Set inital language
168 call set_language 'config.language$'
171 call load_word_list "'localWordlistDir$'" 0
173 # Set up evaluation table
174 initialiseSGC2.toneevaluation_table$ = ""
175 call initialize_toneevaluation_tables
180 ###############################################################
182 # Obligatory procedures
184 ###############################################################
185 # Initialize Demo Window
186 procedure init_window
188 demo Line width... 'defaultLineWidth'
190 call set_font_size 'defaultFontSize'
193 # If there is no recorded sound, try to read stored recording
194 if recordedSound$ = "" and sgc.saveAudio$ <> ""
196 select Table 'wordlist$'
197 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
198 .pinyin$ = Get value... 'te.currentWord' Pinyin
199 .outputName$ = "'sgc.saveAudio$'/'.pinyin$'.wav"
200 if fileReadable(.outputName$)
201 te.recordedSound = Read from file: .outputName$
202 recordedSound$ = selected$("Sound")
205 # Set config.audioName
206 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/]+/?$", "", 0)
207 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
214 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
215 call set_window_title 'buttons$' '.windowTitle$'
216 # Display the word-list
218 # Wipe screen and draw background
219 call wipeArea 'wipeContourArea$'
220 if config.showBackground
221 call draw_background Background
223 # Define buttons in a table
229 call draw_tone_contour
231 call paint_saveAudio_light
236 # Make sure all Preferences directories are available
237 procedure set_up_directories
238 .dirPath$ = "'preferencesAppDir$'"
239 createDirectory(.dirPath$)
240 .dirPath$ > '.dirPath$'/directory.txt
241 createDirectory("'.dirPath$'/wordlists")
242 .dirPath$ > '.dirPath$'/wordlists/directory.txt
243 createDirectory("'.dirPath$'/pitchmodels")
244 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
245 createDirectory("'.dirPath$'/log")
246 .dirPath$ > '.dirPath$'/log/directory.txt
247 createDirectory("'.dirPath$'/TTS")
248 .dirPath$ > '.dirPath$'/TTS/directory.txt
252 # Retrieve and store setting between sessions
254 procedure read_preferences .preferencesFile$
255 if not fileReadable(.preferencesFile$)
256 .preferencesFile$ = preferencesAppFile$
258 if fileReadable(.preferencesFile$)
259 Read from file... 'preferencesAppFile$'
260 .preferenceTable$ = selected$("Table")
261 .numPrefKeys = Get number of rows
262 for .row to .numPrefKeys
263 .variableName$ = Get value... '.row' Key
264 if variableExists(.variableName$)
265 .variableValue = Get value... '.row' Value
266 if .variableValue <> undefined
267 '.variableName$' = '.variableValue'
269 elsif variableExists(.variableName$+"$")
270 .variableValue$ = Get value... '.row' Value
271 .variableName$ = .variableName$+"$"
272 '.variableName$' = "'.variableValue$'"
276 select Table '.preferenceTable$'
281 procedure write_preferences .preferencesFile$
282 Create Table with column names... Preferences 0 Key Value
283 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
284 .preferencesFile$ = preferencesAppFile$
288 for .tableNum from 1 to 2
294 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
296 select Table '.table$'
297 .numTableRows = Get number of rows
298 for .tablerow to .numTableRows
299 select Table '.table$'
300 .label$ = Get value... '.tablerow' Label
301 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
302 .keyName$ = .variableName$
304 if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
306 elsif variableExists(.variableName$)
307 .value = '.variableName$'
309 elsif variableExists(.variableName$ + "$")
310 .variableName$ = .variableName$ + "$"
311 .value$ = '.variableName$'
312 elsif index(.variableName$, "_") > 0
313 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
314 .keyName$ = .variableName$
315 select Table Preferences
316 .prefRow = Search column... Key '.keyName$'
318 if variableExists(.variableName$)
319 .value = '.variableName$'
321 elsif variableExists(.variableName$ + "$")
322 .variableName$ = .variableName$ + "$"
323 .value$ = '.variableName$'
329 select Table Preferences
332 Set string value... '.row' Key '.keyName$'
333 Set string value... '.row' Value '.value$'
338 # Some extra settings
339 select Table Preferences
342 Set string value... '.row' Key wordlistDir
343 Set string value... '.row' Value 'localWordlistDir$'
345 select Table Preferences
348 Set string value... '.row' Key wordlistName
349 Set string value... '.row' Value 'wordlistName$'
351 select Table Preferences
352 Write to table file... '.preferencesFile$'
357 ###############################################################
359 # TTS (speech synthesis). Place commands in user info
362 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
363 # speakCommandFile$ = ""
365 # Make sure sgc2.synthesizer is set!
366 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
367 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
368 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
372 if sgc2.synthesizer > 0
373 select sgc2.synthesizer
375 sgc2.synthesizer = -1
377 # Note, the TTScommand must set sgc2.synthesizer!
379 'sgc2.advancedTTScommand$'
381 'sgc2.normalTTScommand$'
385 .ttscommand$ = "speak"
389 .ttscommand$ = "espeak"
393 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
397 # Global TTS command has precedence
398 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
399 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
400 if config.synthesis$ = "_DISABLED_"
402 config.synthesis$ = "eSpeak"
406 # Local TTS command, if there is no global one
407 # Command does not exist yet, create it if eSpeak is installed
408 # Make sure to quote the path!
409 if speakCommandFile$ = ""
410 # Autodetect synthesizer command
411 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
412 # .command_path$ = ""
414 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
415 # .command_path$ < 'speakCommandDir$'/command_path.txt
417 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
418 # .command_path$ < 'speakCommandDir$'/command_path.txt
420 # if fileReadable(.ttscommand$)
421 # .command_path$ = .ttscommand$
424 # # Remove any newlines
425 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
427 # # Command path found
428 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
429 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
430 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
431 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
432 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
435 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
436 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
438 deleteFile("'speakCommandDir$'/command_path.txt")
441 # Close off (other TTS maybe a later option)
442 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
443 config.synthesis$ = "eSpeak"
447 ###############################################################
452 # initialiseSGC2.toneevaluation_table$ = ""
453 procedure initialize_toneevaluation_tables
455 if initialiseSGC2.toneevaluation_table$ != ""
456 select Table 'initialiseSGC2.toneevaluation_table$'
459 initialiseSGC2.toneevaluation_table$ = ""
461 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
462 sgc2.performanceTable = Read from file: sgc.savePerf$
463 Rename: "Performance"
466 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Time
468 initialiseSGC2.toneevaluation_table$ = "Performance"
469 call get_toneevaluation_row Total
472 procedure get_toneevaluation_row .pinyin$
473 select Table 'initialiseSGC2.toneevaluation_table$'
474 .row = Search column... Pinyin '.pinyin$'
477 .row = Get number of rows
478 Set string value... '.row' Pinyin '.pinyin$'
479 Set numeric value... '.row' Correct 0
480 Set numeric value... '.row' Wrong 0
481 Set numeric value... '.row' Total 0
482 Set numeric value... '.row' High 0
483 Set numeric value... '.row' Low 0
484 Set numeric value... '.row' Wide 0
485 Set numeric value... '.row' Narrow 0
486 Set numeric value... '.row' Unknown 0
487 Set numeric value... '.row' Commented 0
488 Set numeric value... '.row' Time 0
490 .row = Search column... Pinyin '.pinyin$'
494 procedure increment_toneevaluation_in_row .row .column$
495 select Table 'initialiseSGC2.toneevaluation_table$'
496 .tmp = Get value... '.row' '.column$'
498 Set numeric value... '.row' '.column$' '.tmp'
502 Set string value: .row, "Time", .dateTime$
506 procedure increment_toneevaluation_value .pinyin$ .column$
507 call get_toneevaluation_row '.pinyin$'
508 .row = get_toneevaluation_row.row
509 call increment_toneevaluation_in_row '.row' '.column$'
511 call get_toneevaluation_row Total
512 .row = get_toneevaluation_row.row
513 call increment_toneevaluation_in_row '.row' '.column$'
516 procedure add_feedback_to_toneevaluation .table$
517 select Table '.table$'
518 .line1$ = Get value... 1 Text
519 .line2$ = Get value... 2 Text
520 .label$ = Get value... 3 Text
525 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
526 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
529 call increment_toneevaluation_value 'sgc.pinyin$' Total
530 if .tones$ = .recognized$
531 call increment_toneevaluation_value 'sgc.pinyin$' Correct
533 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
535 if index_regex(.label$, "(Correct|Wrong)") <= 0
536 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
537 call increment_toneevaluation_value 'sgc.pinyin$' Commented
540 # Update performance table when SaveAudio is on
541 if sgc.saveAudioOn and sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
542 select Table 'initialiseSGC2.toneevaluation_table$'
543 Write to table file... 'sgc.savePerf$'
547 procedure setGrade .grade$
548 select Table 'wordlist$'
549 if recordedSound$ <> "" and sgc.pinyin$ <> ""
550 call get_toneevaluation_row 'sgc.pinyin$'
551 .row = get_toneevaluation_row.row
552 select Table 'initialiseSGC2.toneevaluation_table$'
553 .col = Get column index... Grade
555 Insert column... 2 Grade
558 Set numeric value: .row, "Grade", '.grade$'
559 .numRows = Get number of rows
562 for .r from 2 to .numRows
563 .tmp = Get value: .r, "Grade"
571 .meanGrade = .sumGrade / .sumN
573 Set numeric value: 1, "Grade", .meanGrade
575 # Store current performance table
576 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
577 select Table 'initialiseSGC2.toneevaluation_table$'
578 Write to table file... 'sgc.savePerf$'
584 ###############################################################
586 # Miscelaneous procedures
588 ###############################################################
591 procedure synthesize_sound .pinyin$
593 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
594 .command$ < 'speakCommandFile$'
595 .command$ = replace$(.command$, "'newline$'", " ", 0)
597 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
599 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
601 system_nocheck call '.command$' "'.pinyin$'"'
603 elsif sgc2.synthesizer > 0
604 select sgc2.synthesizer
605 Play text... '.pinyin$'
609 procedure align_recordedSound .pinyin$
610 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
611 select Sound 'recordedSound$'
612 .recordedTextGrid = To TextGrid... pinyin
613 Set interval text... 1 1 '.pinyin$'
614 select sgc2.synthesizer
615 plus Sound 'recordedSound$'
616 plus .recordedTextGrid
617 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
618 Rename... AlignedTextGrid
619 select .recordedTextGrid