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$ = ""
39 performanceTableName$ = "Performance"
44 yFeedback = yWordlist + 5
45 yPinyin = yFeedback + 10
46 yContour = yPinyin + 15
47 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
48 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
49 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
50 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
52 # Pop-Up window colors
53 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
54 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
56 # Initialize parameters
71 sgc.failedAttempts = 0
73 config.deleteWordlist = -1
74 config.displayNumbers = 1
75 config.displayPinyin = 1
76 config.displayChar = 0
77 config.displayTrans = 1
78 config.useSoundExample = 1
79 config.synthesis$ = "_DISABLED_"
80 config.input$ = "Microphone"
81 config.showBackground = 1
82 sgc2.noiseThresshold = -30
87 config.clearSummary = 0
88 config.audioName$ = ""
91 mainPage.saveAudio$ = ""
93 sgc_ToneProt.minimumPitch = 40
94 sgc_ToneProt.maximumPitch = 600
96 # Platform dependent settings
97 if macintosh or windows
98 config.displayChar = 1
99 config.displayNumbers = 0
101 if windows and endsWith(build_SHA$, " XP")
102 config.displayChar = 0
103 config.displayNumbers = 0
106 config.displayChar = 0
107 config.displayNumbers = 1
111 samplingFrequency = 44100
113 samplingFrequency = 44100
115 samplingFrequency = 44100
120 config.language$ = "EN"
121 config.register = 249
122 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
123 preferencesLogDir$ = "'preferencesAppDir$'/log"
124 preferencesTableDir$ = "'preferencesAppDir$'/Data"
125 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
126 localWordlistDir$ = sgc2wordlists$
128 # Global word lists must be installed BY THE ADMINISTRATOR
129 # This means, create the directory 'globalwordlists$' and
130 # fill it with wordlist directories containing wordlist.Table
131 # and audio files. They can be copied from a local directory.
132 globalwordlists$ = ""
133 globaltablelists$ = ""
136 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
137 globaltablelists$ = "/etc/'sgc2.demoAppName$'/Data"
138 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
140 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
141 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/Data"
142 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
144 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
145 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/Data"
146 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
148 sgc2.synthesizer = -1
149 # Define a Praat TTS command that will set sgc2.synthesizer
150 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 100 95 no IPA"
151 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin f4 10000 0.01 50 70 125 no IPA"
152 sgc.alignedTextGrid = -1
154 ##############################################################
156 config.shuffleLists = 1
157 config.adaptiveLists = 0
159 defaultFont$ = "Helvetica"
162 # Set up directories if they do not exist already
163 call set_up_directories
165 # Get saved preferences
166 call read_preferences ""
169 speakCommandDir$ = "'preferencesAppDir$'/TTS"
170 speakCommandFile$ = ""
172 sgc.recordCommandFile$ = ""
173 sgc.playCommandFile$ = ""
175 sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
177 sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
179 sgc.useAlternativeRecorder = 0
181 sgc.useAlternativePlayer = 0
184 # Set inital language
185 call set_language 'config.language$'
188 call load_word_list "'localWordlistDir$'" 0
190 # Set up evaluation table
191 initialiseSGC2.toneevaluation_table$ = ""
192 call initialize_toneevaluation_tables
197 ###############################################################
199 # Obligatory procedures
201 ###############################################################
202 # Initialize Demo Window
203 procedure init_window
205 demo Line width... 'defaultLineWidth'
207 call set_font_size 'defaultFontSize'
210 # If there is no recorded sound, try to read stored recording
211 if recordedSound$ = "" and sgc.saveAudio$ <> ""
213 select Table 'wordlist$'
214 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
215 call readPinyin 'sgc.currentWord'
216 sgc.pinyin$ = readPinyin.pinyin$
217 .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
218 if fileReadable(.outputName$)
219 sgc.recordedSound = Read from file: .outputName$
220 recordedSound$ = selected$("Sound")
222 # See feedback on earl;ier recorded words
223 if fileReadable("'sgc.saveAudio$'/TestSpeakGoodChinese2.txt")
224 call write_feedback Feedback
226 select Table Feedback
229 # Set config.audioName
230 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
231 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
237 # Set mainPage.saveAudio$ grade
238 call set_grade_display 'sgc.pinyin$'
242 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
243 call set_window_title 'buttons$' '.windowTitle$'
244 # Display the word-list
246 # Wipe screen and draw background
247 call wipeArea 'wipeContourArea$'
248 if config.showBackground
249 call draw_background Background
251 # Define buttons in a table
257 call draw_tone_contour
259 call paint_saveAudio_light
262 sgc.failedAttempts = 0
265 # Make sure all Preferences directories are available
266 procedure set_up_directories
267 .dirPath$ = "'preferencesAppDir$'"
268 createDirectory(.dirPath$)
269 .dirPath$ > '.dirPath$'/directory.txt
270 createDirectory("'.dirPath$'/wordlists")
271 .dirPath$ > '.dirPath$'/wordlists/directory.txt
272 createDirectory("'.dirPath$'/pitchmodels")
273 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
274 createDirectory("'.dirPath$'/log")
275 .dirPath$ > '.dirPath$'/log/directory.txt
276 createDirectory("'.dirPath$'/TTS")
277 .dirPath$ > '.dirPath$'/TTS/directory.txt
281 # Retrieve and store setting between sessions
283 procedure read_preferences .preferencesFile$
284 if not fileReadable(.preferencesFile$)
285 .preferencesFile$ = preferencesAppFile$
287 if fileReadable(.preferencesFile$)
288 Read from file... 'preferencesAppFile$'
289 .preferenceTable$ = selected$("Table")
290 .numPrefKeys = Get number of rows
291 for .row to .numPrefKeys
292 .variableName$ = Get value... '.row' Key
293 if variableExists(.variableName$)
294 .variableValue = Get value... '.row' Value
295 if .variableValue <> undefined
296 '.variableName$' = '.variableValue'
298 elsif variableExists(.variableName$+"$")
299 .variableValue$ = Get value... '.row' Value
300 .variableName$ = .variableName$+"$"
301 '.variableName$' = "'.variableValue$'"
305 select Table '.preferenceTable$'
310 procedure write_preferences .preferencesFile$
311 Create Table with column names... Preferences 0 Key Value
312 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
313 .preferencesFile$ = preferencesAppFile$
317 for .tableNum from 1 to 2
323 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
325 select Table '.table$'
326 .numTableRows = Get number of rows
327 for .tablerow to .numTableRows
328 select Table '.table$'
329 .label$ = Get value... '.tablerow' Label
330 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
331 .keyName$ = .variableName$
333 if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
335 elsif variableExists(.variableName$)
336 .value = '.variableName$'
338 elsif variableExists(.variableName$ + "$")
339 .variableName$ = .variableName$ + "$"
340 .value$ = '.variableName$'
341 elsif index(.variableName$, "_") > 0
342 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
343 .keyName$ = .variableName$
344 select Table Preferences
345 .prefRow = Search column... Key '.keyName$'
347 if variableExists(.variableName$)
348 .value = '.variableName$'
350 elsif variableExists(.variableName$ + "$")
351 .variableName$ = .variableName$ + "$"
352 .value$ = '.variableName$'
358 select Table Preferences
361 Set string value... '.row' Key '.keyName$'
362 Set string value... '.row' Value '.value$'
367 # Some extra settings
368 select Table Preferences
371 Set string value... '.row' Key wordlistDir
372 Set string value... '.row' Value 'localWordlistDir$'
374 select Table Preferences
377 Set string value... '.row' Key wordlistName
378 Set string value... '.row' Value 'wordlistName$'
380 select Table Preferences
381 Write to table file... '.preferencesFile$'
386 ###############################################################
388 # TTS (speech synthesis). Place commands in user info
391 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
392 # speakCommandFile$ = ""
394 # Make sure sgc2.synthesizer is set!
395 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
396 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
397 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
401 if sgc2.synthesizer > 0
402 select sgc2.synthesizer
404 sgc2.synthesizer = -1
406 # Note, the TTScommand must set sgc2.synthesizer!
407 if config.strict$ = "'sgc.highestLevel'"
408 'sgc2.advancedTTScommand$'
410 'sgc2.normalTTScommand$'
414 .ttscommand$ = "speak"
418 .ttscommand$ = "espeak"
422 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
426 # Global TTS command has precedence
427 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
428 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
429 if config.synthesis$ = "_DISABLED_"
431 config.synthesis$ = "eSpeak"
435 # Local TTS command, if there is no global one
436 # Command does not exist yet, create it if eSpeak is installed
437 # Make sure to quote the path!
438 if speakCommandFile$ = ""
439 # Autodetect synthesizer command
440 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
441 # .command_path$ = ""
443 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
444 # .command_path$ < 'speakCommandDir$'/command_path.txt
446 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
447 # .command_path$ < 'speakCommandDir$'/command_path.txt
449 # if fileReadable(.ttscommand$)
450 # .command_path$ = .ttscommand$
453 # # Remove any newlines
454 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
456 # # Command path found
457 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
458 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
459 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
460 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
461 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
464 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
465 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
467 deleteFile("'speakCommandDir$'/command_path.txt")
470 # Close off (other TTS maybe a later option)
471 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
472 config.synthesis$ = "eSpeak"
476 # Alternative audio recorder
477 procedure set_up_recorder
488 # Global TTS command has precedence
489 sgc.useAlternativeRecorder = 0
490 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
491 createDirectory(sgc.scratchAudioDir$)
492 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
493 sgc.useAlternativeRecorder = 1
494 elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
495 createDirectory(sgc.scratchAudioDir$)
496 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
497 sgc.useAlternativeRecorder = 1
501 # Alternative audio player
502 procedure set_up_player
513 # Global TTS command has precedence
514 sgc.useAlternativePlayer = 0
515 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
516 createDirectory(sgc.scratchAudioDir$)
517 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
518 sgc.useAlternativePlayer = 1
519 elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
520 createDirectory(sgc.scratchAudioDir$)
521 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
522 sgc.useAlternativePlayer = 1
526 ###############################################################
531 # initialiseSGC2.toneevaluation_table$ = ""
532 procedure initialize_toneevaluation_tables
534 if initialiseSGC2.toneevaluation_table$ != ""
535 select sgc2.performanceTable
538 initialiseSGC2.toneevaluation_table$ = ""
539 # Get the name of the table
540 call get_feedback_text 'config.language$' Performance
541 call convert_praat_to_latin1 'get_feedback_text.text$'
542 performanceTableName$ = convert_praat_to_latin1.text$
545 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
546 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
547 Rename: performanceTableName$
548 # Check to make sure all columns are initialized, but only if Pinyin exists!
549 select sgc2.performanceTable
550 .column = Get column index: "Pinyin"
552 select sgc2.performanceTable
554 sgc2.performanceTable = Create Table with column names... "'performanceTableName$'" 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
556 .columnNameList$ = "Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time"
557 while index_regex(.columnNameList$, "\S")
558 .columnNameList$ = replace_regex$(.columnNameList$, "^\s+", "", 1)
559 .columnHeader$ = extractWord$(.columnNameList$, "")
560 .columnNameList$ = replace_regex$(.columnNameList$, "^\s*'.columnHeader$'\s*", "", 1)
562 select sgc2.performanceTable
563 .column = Get column index: .columnHeader$
565 select sgc2.performanceTable
566 Append column: .columnHeader$
572 sgc2.performanceTable = Create Table with column names... "'performanceTableName$'" 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
574 initialiseSGC2.toneevaluation_table$ = performanceTableName$
575 call get_toneevaluation_row Total
578 procedure get_toneevaluation_row .pinyin$
579 select sgc2.performanceTable
580 .row = Search column... Pinyin '.pinyin$'
583 .row = Get number of rows
584 Set string value... '.row' Pinyin '.pinyin$'
585 Set numeric value... '.row' Correct 0
586 Set numeric value... '.row' Wrong 0
587 Set numeric value... '.row' Total 0
588 Set numeric value... '.row' High 0
589 Set numeric value... '.row' Low 0
590 Set numeric value... '.row' Wide 0
591 Set numeric value... '.row' Narrow 0
592 Set numeric value... '.row' Unknown 0
593 Set numeric value... '.row' Commented 0
594 Set numeric value... '.row' Level 0
595 Set numeric value... '.row' Time 0
597 .row = Search column... Pinyin '.pinyin$'
601 procedure increment_toneevaluation_in_row .row .column$
602 select sgc2.performanceTable
603 .tmp = Get value... '.row' '.column$'
608 Set numeric value... '.row' '.column$' '.tmp'
611 Set string value: .row, "Level", config.strict$
615 Set string value: .row, "Time", .dateTime$
619 # Update existing performance file with toneevaluation
620 procedure update_toneevaluation_file
621 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
622 select sgc2.performanceTable
623 Write to table file... 'sgc.savePerf$'
627 procedure increment_toneevaluation_value .pinyin$ .column$
628 call get_toneevaluation_row '.pinyin$'
629 .row = get_toneevaluation_row.row
630 call increment_toneevaluation_in_row '.row' '.column$'
632 call get_toneevaluation_row Total
633 .row = get_toneevaluation_row.row
634 call increment_toneevaluation_in_row '.row' '.column$'
637 procedure add_feedback_to_toneevaluation .table$
638 select Table '.table$'
639 .line1$ = Get value... 1 Text
640 .line2$ = Get value... 2 Text
641 .label$ = Get value... 3 Text
646 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
647 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
650 call increment_toneevaluation_value 'sgc.pinyin$' Total
651 if .tones$ = .recognized$
652 call increment_toneevaluation_value 'sgc.pinyin$' Correct
655 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
658 if index_regex(.label$, "(Correct|Wrong)") <= 0
659 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
660 call increment_toneevaluation_value 'sgc.pinyin$' Commented
664 # Update performance table when SaveAudio is on
666 call update_toneevaluation_file
670 procedure setGrade .grade$
671 select Table 'wordlist$'
672 if recordedSound$ <> "" and sgc.pinyin$ <> ""
673 call get_toneevaluation_row 'sgc.pinyin$'
674 .row = get_toneevaluation_row.row
675 select sgc2.performanceTable
676 .col = Get column index... Grade
678 Insert column... 2 Grade
681 Set numeric value: .row, "Grade", '.grade$'
682 .numRows = Get number of rows
685 for .r from 2 to .numRows
686 .tmp = Get value: .r, "Grade"
694 .meanGrade = .sumGrade / .sumN
696 Set numeric value: 1, "Grade", .meanGrade
698 # Store current performance table
699 call update_toneevaluation_file
703 # Set mainPage.saveAudio$ to Grade
704 procedure set_grade_display .pinyin$
705 mainPage.saveAudio$ = ""
706 select sgc2.performanceTable
708 .col = Get column index: "Grade"
709 if .pinyin$ <> "" and .col > 0
710 call get_toneevaluation_row '.pinyin$'
711 .row = get_toneevaluation_row.row
713 .grade$ = Get value: .row, "Grade"
714 if index_regex(.grade$, "[0-9]") <= 0
720 mainPage.saveAudio$ = .grade$
724 ###############################################################
726 # Miscelaneous procedures
728 ###############################################################
731 procedure synthesize_sound .pinyin$
733 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
734 .command$ < 'speakCommandFile$'
735 .command$ = replace$(.command$, "'newline$'", " ", 0)
737 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
739 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
741 system_nocheck call '.command$' "'.pinyin$'"'
743 elsif sgc2.synthesizer > 0
744 select sgc2.synthesizer
745 Play text... '.pinyin$'
749 procedure align_recordedSound .pinyin$
750 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
751 select Sound 'recordedSound$'
752 .recordedTextGrid = To TextGrid... pinyin
753 Set interval text... 1 1 '.pinyin$'
754 select sgc2.synthesizer
755 plus Sound 'recordedSound$'
756 plus .recordedTextGrid
757 sgc.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
758 Rename... AlignedTextGrid
759 select .recordedTextGrid