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$ = ""
171 sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
173 sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
175 sgc.useAlternativeRecorder = 0
178 # Set inital language
179 call set_language 'config.language$'
182 call load_word_list "'localWordlistDir$'" 0
184 # Set up evaluation table
185 initialiseSGC2.toneevaluation_table$ = ""
186 call initialize_toneevaluation_tables
191 ###############################################################
193 # Obligatory procedures
195 ###############################################################
196 # Initialize Demo Window
197 procedure init_window
199 demo Line width... 'defaultLineWidth'
201 call set_font_size 'defaultFontSize'
204 # If there is no recorded sound, try to read stored recording
205 if recordedSound$ = "" and sgc.saveAudio$ <> ""
207 select Table 'wordlist$'
208 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
209 sgc.pinyin$ = Get value... 'te.currentWord' Pinyin
210 .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
211 if fileReadable(.outputName$)
212 te.recordedSound = Read from file: .outputName$
213 recordedSound$ = selected$("Sound")
216 # Set config.audioName
217 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
218 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
224 # Set mainPage.saveAudio$ grade
225 call set_grade_display 'sgc.pinyin$'
229 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
230 call set_window_title 'buttons$' '.windowTitle$'
231 # Display the word-list
233 # Wipe screen and draw background
234 call wipeArea 'wipeContourArea$'
235 if config.showBackground
236 call draw_background Background
238 # Define buttons in a table
244 call draw_tone_contour
246 call paint_saveAudio_light
251 # Make sure all Preferences directories are available
252 procedure set_up_directories
253 .dirPath$ = "'preferencesAppDir$'"
254 createDirectory(.dirPath$)
255 .dirPath$ > '.dirPath$'/directory.txt
256 createDirectory("'.dirPath$'/wordlists")
257 .dirPath$ > '.dirPath$'/wordlists/directory.txt
258 createDirectory("'.dirPath$'/pitchmodels")
259 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
260 createDirectory("'.dirPath$'/log")
261 .dirPath$ > '.dirPath$'/log/directory.txt
262 createDirectory("'.dirPath$'/TTS")
263 .dirPath$ > '.dirPath$'/TTS/directory.txt
267 # Retrieve and store setting between sessions
269 procedure read_preferences .preferencesFile$
270 if not fileReadable(.preferencesFile$)
271 .preferencesFile$ = preferencesAppFile$
273 if fileReadable(.preferencesFile$)
274 Read from file... 'preferencesAppFile$'
275 .preferenceTable$ = selected$("Table")
276 .numPrefKeys = Get number of rows
277 for .row to .numPrefKeys
278 .variableName$ = Get value... '.row' Key
279 if variableExists(.variableName$)
280 .variableValue = Get value... '.row' Value
281 if .variableValue <> undefined
282 '.variableName$' = '.variableValue'
284 elsif variableExists(.variableName$+"$")
285 .variableValue$ = Get value... '.row' Value
286 .variableName$ = .variableName$+"$"
287 '.variableName$' = "'.variableValue$'"
291 select Table '.preferenceTable$'
296 procedure write_preferences .preferencesFile$
297 Create Table with column names... Preferences 0 Key Value
298 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
299 .preferencesFile$ = preferencesAppFile$
303 for .tableNum from 1 to 2
309 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
311 select Table '.table$'
312 .numTableRows = Get number of rows
313 for .tablerow to .numTableRows
314 select Table '.table$'
315 .label$ = Get value... '.tablerow' Label
316 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
317 .keyName$ = .variableName$
319 if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
321 elsif variableExists(.variableName$)
322 .value = '.variableName$'
324 elsif variableExists(.variableName$ + "$")
325 .variableName$ = .variableName$ + "$"
326 .value$ = '.variableName$'
327 elsif index(.variableName$, "_") > 0
328 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
329 .keyName$ = .variableName$
330 select Table Preferences
331 .prefRow = Search column... Key '.keyName$'
333 if variableExists(.variableName$)
334 .value = '.variableName$'
336 elsif variableExists(.variableName$ + "$")
337 .variableName$ = .variableName$ + "$"
338 .value$ = '.variableName$'
344 select Table Preferences
347 Set string value... '.row' Key '.keyName$'
348 Set string value... '.row' Value '.value$'
353 # Some extra settings
354 select Table Preferences
357 Set string value... '.row' Key wordlistDir
358 Set string value... '.row' Value 'localWordlistDir$'
360 select Table Preferences
363 Set string value... '.row' Key wordlistName
364 Set string value... '.row' Value 'wordlistName$'
366 select Table Preferences
367 Write to table file... '.preferencesFile$'
372 ###############################################################
374 # TTS (speech synthesis). Place commands in user info
377 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
378 # speakCommandFile$ = ""
380 # Make sure sgc2.synthesizer is set!
381 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
382 sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
383 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
387 if sgc2.synthesizer > 0
388 select sgc2.synthesizer
390 sgc2.synthesizer = -1
392 # Note, the TTScommand must set sgc2.synthesizer!
393 if config.strict$ = "'sgc.highestLevel'"
394 'sgc2.advancedTTScommand$'
396 'sgc2.normalTTScommand$'
400 .ttscommand$ = "speak"
404 .ttscommand$ = "espeak"
408 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
412 # Global TTS command has precedence
413 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
414 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
415 if config.synthesis$ = "_DISABLED_"
417 config.synthesis$ = "eSpeak"
421 # Local TTS command, if there is no global one
422 # Command does not exist yet, create it if eSpeak is installed
423 # Make sure to quote the path!
424 if speakCommandFile$ = ""
425 # Autodetect synthesizer command
426 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
427 # .command_path$ = ""
429 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
430 # .command_path$ < 'speakCommandDir$'/command_path.txt
432 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
433 # .command_path$ < 'speakCommandDir$'/command_path.txt
435 # if fileReadable(.ttscommand$)
436 # .command_path$ = .ttscommand$
439 # # Remove any newlines
440 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
442 # # Command path found
443 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
444 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
445 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
446 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
447 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
450 # if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
451 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
453 deleteFile("'speakCommandDir$'/command_path.txt")
456 # Close off (other TTS maybe a later option)
457 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
458 config.synthesis$ = "eSpeak"
462 # Alternative audio recorder
463 procedure set_up_recorder
474 # Global TTS command has precedence
475 sgc.useAlternativeRecorder = 0
476 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
477 createDirectory(sgc.scratchAudioDir$)
478 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
479 sgc.useAlternativeRecorder = 1
480 elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
481 createDirectory(sgc.scratchAudioDir$)
482 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
483 sgc.useAlternativeRecorder = 1
487 ###############################################################
492 # initialiseSGC2.toneevaluation_table$ = ""
493 procedure initialize_toneevaluation_tables
495 if initialiseSGC2.toneevaluation_table$ != ""
496 select Table 'initialiseSGC2.toneevaluation_table$'
499 initialiseSGC2.toneevaluation_table$ = ""
501 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
502 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
503 Rename: "Performance"
504 # Temporary cludge to handle old files
505 .column = Get column index: "Level"
507 select sgc2.performanceTable
508 Append column: "Level"
512 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
514 initialiseSGC2.toneevaluation_table$ = "Performance"
515 call get_toneevaluation_row Total
518 procedure get_toneevaluation_row .pinyin$
519 select Table 'initialiseSGC2.toneevaluation_table$'
520 .row = Search column... Pinyin '.pinyin$'
523 .row = Get number of rows
524 Set string value... '.row' Pinyin '.pinyin$'
525 Set numeric value... '.row' Correct 0
526 Set numeric value... '.row' Wrong 0
527 Set numeric value... '.row' Total 0
528 Set numeric value... '.row' High 0
529 Set numeric value... '.row' Low 0
530 Set numeric value... '.row' Wide 0
531 Set numeric value... '.row' Narrow 0
532 Set numeric value... '.row' Unknown 0
533 Set numeric value... '.row' Commented 0
534 Set numeric value... '.row' Level 0
535 Set numeric value... '.row' Time 0
537 .row = Search column... Pinyin '.pinyin$'
541 procedure increment_toneevaluation_in_row .row .column$
542 select Table 'initialiseSGC2.toneevaluation_table$'
543 .tmp = Get value... '.row' '.column$'
545 Set numeric value... '.row' '.column$' '.tmp'
548 Set string value: .row, "Level", config.strict$
552 Set string value: .row, "Time", .dateTime$
556 # Update existing performance file with toneevaluation
557 procedure update_toneevaluation_file
558 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
559 select Table 'initialiseSGC2.toneevaluation_table$'
560 Write to table file... 'sgc.savePerf$'
564 procedure increment_toneevaluation_value .pinyin$ .column$
565 call get_toneevaluation_row '.pinyin$'
566 .row = get_toneevaluation_row.row
567 call increment_toneevaluation_in_row '.row' '.column$'
569 call get_toneevaluation_row Total
570 .row = get_toneevaluation_row.row
571 call increment_toneevaluation_in_row '.row' '.column$'
574 procedure add_feedback_to_toneevaluation .table$
575 select Table '.table$'
576 .line1$ = Get value... 1 Text
577 .line2$ = Get value... 2 Text
578 .label$ = Get value... 3 Text
583 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
584 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
587 call increment_toneevaluation_value 'sgc.pinyin$' Total
588 if .tones$ = .recognized$
589 call increment_toneevaluation_value 'sgc.pinyin$' Correct
591 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
593 if index_regex(.label$, "(Correct|Wrong)") <= 0
594 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
595 call increment_toneevaluation_value 'sgc.pinyin$' Commented
598 # Update performance table when SaveAudio is on
600 call update_toneevaluation_file
604 procedure setGrade .grade$
605 select Table 'wordlist$'
606 if recordedSound$ <> "" and sgc.pinyin$ <> ""
607 call get_toneevaluation_row 'sgc.pinyin$'
608 .row = get_toneevaluation_row.row
609 select Table 'initialiseSGC2.toneevaluation_table$'
610 .col = Get column index... Grade
612 Insert column... 2 Grade
615 Set numeric value: .row, "Grade", '.grade$'
616 .numRows = Get number of rows
619 for .r from 2 to .numRows
620 .tmp = Get value: .r, "Grade"
628 .meanGrade = .sumGrade / .sumN
630 Set numeric value: 1, "Grade", .meanGrade
632 # Store current performance table
633 call update_toneevaluation_file
637 # Set mainPage.saveAudio$ to Grade
638 procedure set_grade_display .pinyin$
639 mainPage.saveAudio$ = ""
640 select Table 'initialiseSGC2.toneevaluation_table$'
642 .col = Get column index: "Grade"
643 if .pinyin$ <> "" and .col > 0
644 call get_toneevaluation_row '.pinyin$'
645 .row = get_toneevaluation_row.row
647 .grade$ = Get value: .row, "Grade"
648 if index_regex(.grade$, "[0-9]") <= 0
654 mainPage.saveAudio$ = .grade$
658 ###############################################################
660 # Miscelaneous procedures
662 ###############################################################
665 procedure synthesize_sound .pinyin$
667 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
668 .command$ < 'speakCommandFile$'
669 .command$ = replace$(.command$, "'newline$'", " ", 0)
671 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
673 system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
675 system_nocheck call '.command$' "'.pinyin$'"'
677 elsif sgc2.synthesizer > 0
678 select sgc2.synthesizer
679 Play text... '.pinyin$'
683 procedure align_recordedSound .pinyin$
684 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
685 select Sound 'recordedSound$'
686 .recordedTextGrid = To TextGrid... pinyin
687 Set interval text... 1 1 '.pinyin$'
688 select sgc2.synthesizer
689 plus Sound 'recordedSound$'
690 plus .recordedTextGrid
691 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
692 Rename... AlignedTextGrid
693 select .recordedTextGrid