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$ = ""
169 sgc.recordCommandFile$ = ""
170 sgc.playCommandFile$ = ""
172 sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
174 sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
176 sgc.useAlternativeRecorder = 0
178 sgc.useAlternativePlayer = 0
181 # Set inital language
182 call set_language 'config.language$'
185 call load_word_list "'localWordlistDir$'" 0
187 # Set up evaluation table
188 initialiseSGC2.toneevaluation_table$ = ""
189 call initialize_toneevaluation_tables
194 ###############################################################
196 # Obligatory procedures
198 ###############################################################
199 # Initialize Demo Window
200 procedure init_window
202 demo Line width... 'defaultLineWidth'
204 call set_font_size 'defaultFontSize'
207 # If there is no recorded sound, try to read stored recording
208 if recordedSound$ = "" and sgc.saveAudio$ <> ""
210 select Table 'wordlist$'
211 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
212 sgc.pinyin$ = Get value... 'te.currentWord' Pinyin
213 .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
214 if fileReadable(.outputName$)
215 sgc.recordedSound = Read from file: .outputName$
216 recordedSound$ = selected$("Sound")
219 # Set config.audioName
220 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
221 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
227 # Set mainPage.saveAudio$ grade
228 call set_grade_display 'sgc.pinyin$'
232 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
233 call set_window_title 'buttons$' '.windowTitle$'
234 # Display the word-list
236 # Wipe screen and draw background
237 call wipeArea 'wipeContourArea$'
238 if config.showBackground
239 call draw_background Background
241 # Define buttons in a table
247 call draw_tone_contour
249 call paint_saveAudio_light
254 # Make sure all Preferences directories are available
255 procedure set_up_directories
256 .dirPath$ = "'preferencesAppDir$'"
257 createDirectory(.dirPath$)
258 .dirPath$ > '.dirPath$'/directory.txt
259 createDirectory("'.dirPath$'/wordlists")
260 .dirPath$ > '.dirPath$'/wordlists/directory.txt
261 createDirectory("'.dirPath$'/pitchmodels")
262 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
263 createDirectory("'.dirPath$'/log")
264 .dirPath$ > '.dirPath$'/log/directory.txt
265 createDirectory("'.dirPath$'/TTS")
266 .dirPath$ > '.dirPath$'/TTS/directory.txt
270 # Retrieve and store setting between sessions
272 procedure read_preferences .preferencesFile$
273 if not fileReadable(.preferencesFile$)
274 .preferencesFile$ = preferencesAppFile$
276 if fileReadable(.preferencesFile$)
277 Read from file... 'preferencesAppFile$'
278 .preferenceTable$ = selected$("Table")
279 .numPrefKeys = Get number of rows
280 for .row to .numPrefKeys
281 .variableName$ = Get value... '.row' Key
282 if variableExists(.variableName$)
283 .variableValue = Get value... '.row' Value
284 if .variableValue <> undefined
285 '.variableName$' = '.variableValue'
287 elsif variableExists(.variableName$+"$")
288 .variableValue$ = Get value... '.row' Value
289 .variableName$ = .variableName$+"$"
290 '.variableName$' = "'.variableValue$'"
294 select Table '.preferenceTable$'
299 procedure write_preferences .preferencesFile$
300 Create Table with column names... Preferences 0 Key Value
301 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
302 .preferencesFile$ = preferencesAppFile$
306 for .tableNum from 1 to 2
312 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
314 select Table '.table$'
315 .numTableRows = Get number of rows
316 for .tablerow to .numTableRows
317 select Table '.table$'
318 .label$ = Get value... '.tablerow' Label
319 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
320 .keyName$ = .variableName$
322 if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
324 elsif variableExists(.variableName$)
325 .value = '.variableName$'
327 elsif variableExists(.variableName$ + "$")
328 .variableName$ = .variableName$ + "$"
329 .value$ = '.variableName$'
330 elsif index(.variableName$, "_") > 0
331 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
332 .keyName$ = .variableName$
333 select Table Preferences
334 .prefRow = Search column... Key '.keyName$'
336 if variableExists(.variableName$)
337 .value = '.variableName$'
339 elsif variableExists(.variableName$ + "$")
340 .variableName$ = .variableName$ + "$"
341 .value$ = '.variableName$'
347 select Table Preferences
350 Set string value... '.row' Key '.keyName$'
351 Set string value... '.row' Value '.value$'
356 # Some extra settings
357 select Table Preferences
360 Set string value... '.row' Key wordlistDir
361 Set string value... '.row' Value 'localWordlistDir$'
363 select Table Preferences
366 Set string value... '.row' Key wordlistName
367 Set string value... '.row' Value 'wordlistName$'
369 select Table Preferences
370 Write to table file... '.preferencesFile$'
375 ###############################################################
377 # TTS (speech synthesis). Place commands in user info
380 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
381 # speakCommandFile$ = ""
383 # Make sure sgc2.synthesizer is set!
384 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
385 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
386 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
390 if sgc2.synthesizer > 0
391 select sgc2.synthesizer
393 sgc2.synthesizer = -1
395 # Note, the TTScommand must set sgc2.synthesizer!
396 if config.strict$ = "'sgc.highestLevel'"
397 'sgc2.advancedTTScommand$'
399 'sgc2.normalTTScommand$'
403 .ttscommand$ = "speak"
407 .ttscommand$ = "espeak"
411 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
415 # Global TTS command has precedence
416 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
417 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
418 if config.synthesis$ = "_DISABLED_"
420 config.synthesis$ = "eSpeak"
424 # Local TTS command, if there is no global one
425 # Command does not exist yet, create it if eSpeak is installed
426 # Make sure to quote the path!
427 if speakCommandFile$ = ""
428 # Autodetect synthesizer command
429 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
430 # .command_path$ = ""
432 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
433 # .command_path$ < 'speakCommandDir$'/command_path.txt
435 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
436 # .command_path$ < 'speakCommandDir$'/command_path.txt
438 # if fileReadable(.ttscommand$)
439 # .command_path$ = .ttscommand$
442 # # Remove any newlines
443 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
445 # # Command path found
446 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
447 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
448 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
449 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
450 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
453 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
454 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
456 deleteFile("'speakCommandDir$'/command_path.txt")
459 # Close off (other TTS maybe a later option)
460 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
461 config.synthesis$ = "eSpeak"
465 # Alternative audio recorder
466 procedure set_up_recorder
477 # Global TTS command has precedence
478 sgc.useAlternativeRecorder = 0
479 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
480 createDirectory(sgc.scratchAudioDir$)
481 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
482 sgc.useAlternativeRecorder = 1
483 elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
484 createDirectory(sgc.scratchAudioDir$)
485 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
486 sgc.useAlternativeRecorder = 1
490 # Alternative audio player
491 procedure set_up_player
502 # Global TTS command has precedence
503 sgc.useAlternativePlayer = 0
504 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
505 createDirectory(sgc.scratchAudioDir$)
506 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
507 sgc.useAlternativePlayer = 1
508 elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
509 createDirectory(sgc.scratchAudioDir$)
510 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
511 sgc.useAlternativePlayer = 1
515 ###############################################################
520 # initialiseSGC2.toneevaluation_table$ = ""
521 procedure initialize_toneevaluation_tables
523 if initialiseSGC2.toneevaluation_table$ != ""
524 select Table 'initialiseSGC2.toneevaluation_table$'
527 initialiseSGC2.toneevaluation_table$ = ""
529 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
530 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
531 Rename: "Performance"
532 # Temporary cludge to handle old files
533 .column = Get column index: "Level"
535 select sgc2.performanceTable
536 Append column: "Level"
540 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
542 initialiseSGC2.toneevaluation_table$ = "Performance"
543 call get_toneevaluation_row Total
546 procedure get_toneevaluation_row .pinyin$
547 select Table 'initialiseSGC2.toneevaluation_table$'
548 .row = Search column... Pinyin '.pinyin$'
551 .row = Get number of rows
552 Set string value... '.row' Pinyin '.pinyin$'
553 Set numeric value... '.row' Correct 0
554 Set numeric value... '.row' Wrong 0
555 Set numeric value... '.row' Total 0
556 Set numeric value... '.row' High 0
557 Set numeric value... '.row' Low 0
558 Set numeric value... '.row' Wide 0
559 Set numeric value... '.row' Narrow 0
560 Set numeric value... '.row' Unknown 0
561 Set numeric value... '.row' Commented 0
562 Set numeric value... '.row' Level 0
563 Set numeric value... '.row' Time 0
565 .row = Search column... Pinyin '.pinyin$'
569 procedure increment_toneevaluation_in_row .row .column$
570 select Table 'initialiseSGC2.toneevaluation_table$'
571 .tmp = Get value... '.row' '.column$'
573 Set numeric value... '.row' '.column$' '.tmp'
576 Set string value: .row, "Level", config.strict$
580 Set string value: .row, "Time", .dateTime$
584 # Update existing performance file with toneevaluation
585 procedure update_toneevaluation_file
586 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
587 select Table 'initialiseSGC2.toneevaluation_table$'
588 Write to table file... 'sgc.savePerf$'
592 procedure increment_toneevaluation_value .pinyin$ .column$
593 call get_toneevaluation_row '.pinyin$'
594 .row = get_toneevaluation_row.row
595 call increment_toneevaluation_in_row '.row' '.column$'
597 call get_toneevaluation_row Total
598 .row = get_toneevaluation_row.row
599 call increment_toneevaluation_in_row '.row' '.column$'
602 procedure add_feedback_to_toneevaluation .table$
603 select Table '.table$'
604 .line1$ = Get value... 1 Text
605 .line2$ = Get value... 2 Text
606 .label$ = Get value... 3 Text
611 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
612 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
615 call increment_toneevaluation_value 'sgc.pinyin$' Total
616 if .tones$ = .recognized$
617 call increment_toneevaluation_value 'sgc.pinyin$' Correct
619 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
621 if index_regex(.label$, "(Correct|Wrong)") <= 0
622 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
623 call increment_toneevaluation_value 'sgc.pinyin$' Commented
626 # Update performance table when SaveAudio is on
628 call update_toneevaluation_file
632 procedure setGrade .grade$
633 select Table 'wordlist$'
634 if recordedSound$ <> "" and sgc.pinyin$ <> ""
635 call get_toneevaluation_row 'sgc.pinyin$'
636 .row = get_toneevaluation_row.row
637 select Table 'initialiseSGC2.toneevaluation_table$'
638 .col = Get column index... Grade
640 Insert column... 2 Grade
643 Set numeric value: .row, "Grade", '.grade$'
644 .numRows = Get number of rows
647 for .r from 2 to .numRows
648 .tmp = Get value: .r, "Grade"
656 .meanGrade = .sumGrade / .sumN
658 Set numeric value: 1, "Grade", .meanGrade
660 # Store current performance table
661 call update_toneevaluation_file
665 # Set mainPage.saveAudio$ to Grade
666 procedure set_grade_display .pinyin$
667 mainPage.saveAudio$ = ""
668 select Table 'initialiseSGC2.toneevaluation_table$'
670 .col = Get column index: "Grade"
671 if .pinyin$ <> "" and .col > 0
672 call get_toneevaluation_row '.pinyin$'
673 .row = get_toneevaluation_row.row
675 .grade$ = Get value: .row, "Grade"
676 if index_regex(.grade$, "[0-9]") <= 0
682 mainPage.saveAudio$ = .grade$
686 ###############################################################
688 # Miscelaneous procedures
690 ###############################################################
693 procedure synthesize_sound .pinyin$
695 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
696 .command$ < 'speakCommandFile$'
697 .command$ = replace$(.command$, "'newline$'", " ", 0)
699 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
701 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
703 system_nocheck call '.command$' "'.pinyin$'"'
705 elsif sgc2.synthesizer > 0
706 select sgc2.synthesizer
707 Play text... '.pinyin$'
711 procedure align_recordedSound .pinyin$
712 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
713 select Sound 'recordedSound$'
714 .recordedTextGrid = To TextGrid... pinyin
715 Set interval text... 1 1 '.pinyin$'
716 select sgc2.synthesizer
717 plus Sound 'recordedSound$'
718 plus .recordedTextGrid
719 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
720 Rename... AlignedTextGrid
721 select .recordedTextGrid