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 # Generalizing the wordlist code
33 sgcwordlist.word$ = "Pinyin"
34 sgcwordlist.graph$ = "Character"
35 sgcwordlist.transl$ = "Translation"
36 sgcwordlist.audio$ = "Sound"
38 sgcwordlist.ipa$ = "IPA"
39 sgcwordlist.gender$ = "Gender"
41 sgcwordlist.f0$ = "F0"
42 sgcwordlist.f1$ = "F1"
43 sgcwordlist.f2$ = "F2"
44 sgcwordlist.f3$ = "F3"
47 # Information for logging activity
48 currentDate$ = date$()
49 dateYear$ = right$(currentDate$, length(currentDate$) - rindex(currentDate$, " "))
50 logtimeStamp$ = replace_regex$(currentDate$, "[^a-zA-Z0-9\-_]", "-", 0)
51 currentLogDirectory$ = ""
52 feedbackTablePrefix$ = "Feedback"
53 feedbackTableName$ = ""
54 evaluationTablePrefix$ = "Evaluation"
55 evaluationTableName$ = ""
56 performanceTableName$ = "Performance"
61 yFeedback = yWordlist + 5
62 yPinyin = yFeedback + 10
63 yContour = yPinyin + 15
64 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
65 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
66 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
67 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
69 # Pop-Up window colors
70 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
71 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
73 # Initialize parameters
76 sgc.currentWordlist = -1
78 sgc.currentWordNum = 1
79 sgc.numberOfDisplayedWords = 0
83 sgc.customLanguage$ = ""
92 sgc.failedAttempts = 0
94 config.deleteWordlist = -1
95 config.displayNumbers = 1
96 config.displayPinyin = 1
97 config.displayChar = 0
98 config.displayTrans = 1
99 config.useSoundExample = 1
100 config.synthesis$ = "_DISABLED_"
101 config.input$ = "Microphone"
102 config.showBackground = 1
103 sgc2.noiseThresshold = -30
108 config.voicesTTS$ = "default f1 f2 f3 f4 f5 m1 m2 m3 m4 m5 m6 m7"
110 sgc.nativeOSXTTScommand$ = "say -vTing-Ting --rate=95"
111 sgc.nativeLinuxTTScommand$ = "spd-say -l zh -t female1 -r -60"
112 #sgc.nativeWindowsTTScommand$ = "mshta.exe vbscript:Execute(""CreateObject(""""SAPI.SpVoice"""").Speak """"<voice gender='female' xml:lang='zh-CN'><rate speed='-5'>$$$</rate></voice>"""":Close"")"
113 sgc.nativeWindowsTTScommand$ = "mshta.exe vbscript:Execute(""CreateObject(""""SAPI.SpVoice"""").Speak """"<voice gender='female' xml:lang='zh-CN'><rate speed='-5'><pron sym='@@@'/></rate></voice>"""":Close"")"
114 sgc.nativeTTScommand$ = ""
117 config.clearSummary = 0
118 config.audioName$ = ""
121 mainPage.saveAudio$ = ""
122 sgc.allWordLists = -1
124 sgc_ToneProt.minimumPitch = 40
125 sgc_ToneProt.maximumPitch = 600
127 # Platform dependent settings
128 if macintosh or windows
129 config.displayChar = 1
130 config.displayNumbers = 0
132 if windows and endsWith(build_SHA$, " XP")
133 config.displayChar = 0
134 config.displayNumbers = 0
137 config.displayChar = 0
138 config.displayNumbers = 1
142 samplingFrequency = 44100
144 samplingFrequency = 44100
146 samplingFrequency = 44100
150 # Be careful: Logging and Replaying can have side-effects
151 # sgc2.logging$ = "call saveLogOfActivity"
153 # When this is not "# ", the command 'replaySGC2Log$' will be run!
154 if not variableExists("replaySGC2Log$")
155 replaySGC2Log$ = "# "
158 config.language$ = "EN"
159 config.register = 249
160 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
161 preferencesLogDir$ = "'preferencesAppDir$'/log"
162 preferencesTableDir$ = "'preferencesAppDir$'/GUI"
163 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
164 localWordlistDir$ = sgc2wordlists$
166 # Global word lists must be installed BY THE ADMINISTRATOR
167 # This means, create the directory 'globalwordlists$' and
168 # fill it with wordlist directories containing wordlist.Table
169 # and audio files. They can be copied from a local directory.
170 globalwordlists$ = ""
171 globaltablelists$ = ""
174 globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
175 globaltablelists$ = "/etc/'sgc2.demoAppName$'/GUI"
176 globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"
178 globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
179 globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/GUI"
180 globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"
182 globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
183 globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/GUI"
184 globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
186 sgc2.synthesizer = -1
187 sgc.alignedTextGrid = -1
189 ##############################################################
191 config.shuffleLists = 1
192 config.adaptiveLists = 0
194 defaultFont$ = "Helvetica"
197 # Set up directories if they do not exist already
198 call set_up_directories
200 # Get saved preferences
201 call read_preferences ""
204 speakCommandDir$ = "'preferencesAppDir$'/TTS"
205 speakCommandFile$ = ""
206 # Define a Praat TTS command that will set sgc2.synthesizer
207 call set_TTS_parameters
209 sgc.recordCommandFile$ = ""
210 sgc.playCommandFile$ = ""
212 sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
214 sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
216 sgc.useAlternativeRecorder = 0
218 sgc.useAlternativePlayer = 0
221 # Set inital language
222 call set_language 'config.language$'
225 call load_word_list "'localWordlistDir$'" 0
227 # Set up evaluation table
228 initialiseSGC2.toneevaluation_table$ = ""
229 call initialize_toneevaluation_tables
234 ###############################################################
236 # Obligatory procedures
238 ###############################################################
239 # Initialize Demo Window
240 procedure init_window
242 demo Line width... 'defaultLineWidth'
244 call set_font_size 'defaultFontSize'
247 # If there is no recorded sound, try to read stored recording
248 if recordedSound$ = "" and sgc.saveAudio$ <> ""
250 select sgc.currentWordlist
251 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
252 call readPinyin 'sgc.currentWord'
253 sgc.pinyin$ = readPinyin.pinyin$
254 sgc.character$ = readPinyin.character$
255 .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
256 if fileReadable(.outputName$)
257 sgc.recordedSound = Read from file: .outputName$
258 recordedSound$ = selected$(sgcwordlist.audio$)
260 # See feedback on earlier recorded words
261 if fileReadable("'sgc.saveAudio$'/TestSpeakGoodChinese2.txt")
262 call write_feedback Feedback
264 call write_grade 'sgc.pinyin$'
266 select Table Feedback
269 # Set config.audioName
270 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
271 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
277 # Set mainPage.saveAudio$ grade
278 call set_grade_display 'sgc.pinyin$'
279 call write_grade 'sgc.pinyin$'
283 .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
284 call set_window_title 'buttons$' '.windowTitle$'
285 # Display the word-list
287 # Wipe screen and draw background
288 call wipeArea 'wipeContourArea$'
289 if config.showBackground
290 call draw_background Background
292 # Define buttons in a table
298 call draw_tone_contour
300 call paint_saveAudio_light
303 sgc.failedAttempts = 0
306 # Make sure all Preferences directories are available
307 procedure set_up_directories
308 .dirPath$ = "'preferencesAppDir$'"
309 createDirectory(.dirPath$)
310 .dirPath$ > '.dirPath$'/directory.txt
311 createDirectory("'.dirPath$'/wordlists")
312 .dirPath$ > '.dirPath$'/wordlists/directory.txt
313 createDirectory("'.dirPath$'/pitchmodels")
314 .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
315 createDirectory("'.dirPath$'/log")
316 .dirPath$ > '.dirPath$'/log/directory.txt
317 createDirectory("'.dirPath$'/TTS")
318 .dirPath$ > '.dirPath$'/TTS/directory.txt
322 # Retrieve and store setting between sessions
324 procedure read_preferences .preferencesFile$
325 if not fileReadable(.preferencesFile$)
326 .preferencesFile$ = preferencesAppFile$
328 if fileReadable(.preferencesFile$)
329 Read from file... 'preferencesAppFile$'
330 .preferenceTable$ = selected$("Table")
331 .numPrefKeys = Get number of rows
332 for .row to .numPrefKeys
333 .variableName$ = Get value... '.row' Key
334 if variableExists(.variableName$)
335 .variableValue = Get value... '.row' Value
336 if .variableValue <> undefined
337 '.variableName$' = '.variableValue'
339 elsif variableExists(.variableName$+"$")
340 .variableValue$ = Get value... '.row' Value
341 .variableName$ = .variableName$+"$"
342 '.variableName$' = "'.variableValue$'"
346 select Table '.preferenceTable$'
351 procedure write_preferences .preferencesFile$
352 Create Table with column names... Preferences 0 Key Value
353 if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
354 .preferencesFile$ = preferencesAppFile$
358 for .tableNum from 1 to 2
364 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
366 select Table '.table$'
367 .numTableRows = Get number of rows
368 for .tablerow to .numTableRows
369 select Table '.table$'
370 .label$ = Get value... '.tablerow' Label
371 .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
372 .keyName$ = .variableName$
374 if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
376 elsif variableExists(.variableName$)
377 .value = '.variableName$'
379 elsif variableExists(.variableName$ + "$")
380 .variableName$ = .variableName$ + "$"
381 .value$ = '.variableName$'
382 elsif index(.variableName$, "_") > 0
383 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1)
384 .keyName$ = .variableName$
385 select Table Preferences
386 .prefRow = Search column... Key '.keyName$'
388 if variableExists(.variableName$)
389 .value = '.variableName$'
391 elsif variableExists(.variableName$ + "$")
392 .variableName$ = .variableName$ + "$"
393 .value$ = '.variableName$'
399 select Table Preferences
402 Set string value... '.row' Key '.keyName$'
403 Set string value... '.row' Value '.value$'
408 # Some extra settings
409 select Table Preferences
412 Set string value... '.row' Key wordlistDir
413 Set string value... '.row' Value 'localWordlistDir$'
415 select Table Preferences
418 Set string value... '.row' Key wordlistName
419 Set string value... '.row' Value 'wordlistName$'
421 select Table Preferences
422 Write to table file... '.preferencesFile$'
427 ###############################################################
429 # TTS (speech synthesis). Place commands in user info
432 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
433 # speakCommandFile$ = ""
435 # Make sure sgc2.synthesizer is set!
436 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
437 sgc2.synthesizer = Create SpeechSynthesizer: .language$, .voice$
438 Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
442 if sgc2.synthesizer > 0
443 select sgc2.synthesizer
445 sgc2.synthesizer = -1
447 # Note, the TTScommand must set sgc2.synthesizer!
448 if config.strict$ = "'sgc.highestLevel'"
449 'sgc2.advancedTTScommand$'
451 'sgc2.normalTTScommand$'
455 .ttscommand$ = "speak"
459 .ttscommand$ = "espeak"
463 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
467 # Global TTS command has precedence
468 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
469 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
470 if config.synthesis$ = "_DISABLED_"
472 config.synthesis$ = "eSpeak"
476 # Local TTS command, if there is no global one
477 # Command does not exist yet, create it if eSpeak is installed
478 # Make sure to quote the path!
479 if speakCommandFile$ = ""
480 # Autodetect synthesizer command
481 # if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
482 # .command_path$ = ""
484 # system_nocheck PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
485 # .command_path$ < 'speakCommandDir$'/command_path.txt
487 # system_nocheck PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
488 # .command_path$ < 'speakCommandDir$'/command_path.txt
490 # if fileReadable(.ttscommand$)
491 # .command_path$ = .ttscommand$
494 # # Remove any newlines
495 # .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
497 # # Command path found
498 # if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
499 # deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
500 # fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
501 # ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
502 # speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
505 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
506 speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
508 deleteFile("'speakCommandDir$'/command_path.txt")
511 # Close off (other TTS maybe a later option)
512 if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
513 config.synthesis$ = "eSpeak"
517 procedure set_TTS_parameters
519 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+\s+){'config.voice'}(\S+)(\s+.*)?$", "\2", 0)
521 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+)(\s+.*)?$", "\1", 0)
523 if .voice$ <> "" and index_regex(.voice$, "\s") <= 0
524 if .voice$ = "default"
527 config.voice$ = replace_regex$(.voice$, ".", "\u&", 0)
529 sgc2.normalTTScommand$ = "call create_default_TTS ""Chinese (Mandarin)"" '.voice$' 10000 0.01 50 100 95 no IPA"
530 sgc2.advancedTTScommand$ = "call create_default_TTS ""Chinese (Mandarin)"" '.voice$' 10000 0.01 50 70 125 no IPA"
532 if macintosh and fileReadable("/usr/bin/say")
533 sgc.nativeTTScommand$ = sgc.nativeOSXTTScommand$
534 elsif unix and fileReadable("/usr/bin/spd-say")
535 sgc.nativeTTScommand$ = sgc.nativeLinuxTTScommand$
537 sgc.nativeTTScommand$ = sgc.nativeWindowsTTScommand$
541 # Alternative audio recorder
542 procedure set_up_recorder
553 # Global TTS command has precedence
554 sgc.useAlternativeRecorder = 0
555 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
556 createDirectory(sgc.scratchAudioDir$)
557 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
558 sgc.useAlternativeRecorder = 1
559 elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
560 createDirectory(sgc.scratchAudioDir$)
561 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
562 sgc.useAlternativeRecorder = 1
566 # Alternative audio player
567 procedure set_up_player
578 # Global TTS command has precedence
579 sgc.useAlternativePlayer = 0
580 if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
581 createDirectory(sgc.scratchAudioDir$)
582 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
583 sgc.useAlternativePlayer = 1
584 elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
585 createDirectory(sgc.scratchAudioDir$)
586 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
587 sgc.useAlternativePlayer = 1
591 ###############################################################
596 # initialiseSGC2.toneevaluation_table$ = ""
597 procedure initialize_toneevaluation_tables
598 .invalidperformanceTable = 0
599 .oldPerformanceTable = -1
600 # Mark old table for removal
601 if initialiseSGC2.toneevaluation_table$ != ""
602 .oldPerformanceTable = sgc2.performanceTable
603 sgc2.performanceTable = -1
605 initialiseSGC2.toneevaluation_table$ = ""
606 # Get the name of the table
607 call get_evaluation_table_labels 'config.language$'
608 performanceTableName$ = eval.performance$
610 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
611 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
612 Rename: performanceTableName$
613 .columnNameList$ = "'eval.pinyin$' 'eval.grade$' 'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'"
615 # Check to make sure all columns are initialized, but only if Pinyin exists!
616 select sgc2.performanceTable
617 .numColumns = Get number of columns
618 # Find Pinyin column on 1
619 if index("Pinyin 拼音 ピンイン", eval.pinyin$) > 0
620 .column = Get column index: sgcwordlist.word$
622 .column = Get column index: "拼音"
625 .column = Get column index: "ピンイン"
628 .column = Get column index: eval.pinyin$
630 # This is not a valid Performance table, remove it
631 if .column != 1 or .numColumns != .numPerfColumns
632 select sgc2.performanceTable
635 # Set ERROR and remove file read
636 .invalidperformanceTable = 1
637 sgc2.performanceTable = .oldPerformanceTable
638 .oldPerformanceTable = -1
641 # ALWAYS make sure the column headers are translated!
642 select sgc2.performanceTable
643 .numColumns = Get number of columns
644 .nameList$ = .columnNameList$
645 for .col to .numColumns
646 .nameList$ = replace_regex$(.nameList$, "^\s+", "", 1)
647 .columnHeader$ = extractWord$(.nameList$, "")
648 .nameList$ = replace_regex$(.nameList$, "^\s*'.columnHeader$'\s*", "", 1)
649 select sgc2.performanceTable
650 Set column label (index): .col, .columnHeader$
653 # Performance table has been read. Set the wordlist to the first row
654 select sgc2.performanceTable
655 .numWords = Get number of rows
658 .wordlistLabel$ = eval.wordlist$
659 .col = Get column index: .wordlistLabel$
661 .wordlistLabel$ = "Wordlist"
662 .col = Get column index: .wordlistLabel$
664 while .col > 0 and .row <= .numWords and (wordlistName$ = "" or index_regex(wordlistName$, "[^\?\- ]") <= 0)
665 wordlistName$ = Get value: .row, .wordlistLabel$
669 call load_word_list: localWordlistDir$, 0
671 # If this is a graded list, look for the audio
672 if config.audioName$ = ""
673 # Check if there is a grade
674 select sgc2.performanceTable
675 .grade$ = Get value: get_toneevaluation_row.row, eval.grade$
676 # It is graded! Look for the audio
677 if index_regex(.grade$, "[\d]") > 0
679 select sgc2.performanceTable
680 .numRows = Get number of rows
682 select sgc2.performanceTable
683 .pinyin$ = Get value: .row, eval.pinyin$
684 .strings = Create Strings as file list: "Files", "'sgc.saveAudio$'/'.pinyin$'.*"
685 .numAudio = Get number of strings
693 .fileName$ = right$(sgc.savePerf$, length(sgc.savePerf$) - index_regex(sgc.savePerf$, "[/\\][^/\\]+$"))
694 .fileName$ = replace_regex$(.fileName$, "(?i\.tsv$)", "", 0)
695 call findLabel Config $AudioName
698 .openDialogue$ = Get value... '.row' Helptext
699 call convert_praat_to_latin1 '.openDialogue$'
700 .openDialogue$ = convert_praat_to_latin1.text$
702 sgc.saveAudio$ = chooseDirectory$ (.fileName$+": "+.openDialogue$)
709 sgc2.performanceTable = Create Table with column names... "'performanceTableName$'" 0 'eval.pinyin$' 'eval.grade$' 'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'
711 initialiseSGC2.toneevaluation_table$ = performanceTableName$
712 call get_toneevaluation_row 'eval.total$'
714 # Remove marked old table
715 if .oldPerformanceTable > 0
716 select .oldPerformanceTable
718 .oldPerformanceTable = -1
722 procedure get_toneevaluation_row .pinyin$
723 select sgc2.performanceTable
724 .colName$ = Get column label: 1
725 # Catch and rename Totals. Make sure it works when you stay within a new language
726 if index("Total Totaal 总计 合計", .pinyin$) > 0
727 .row = Search column: .colName$, "Total"
729 .row = Search column: .colName$, "Totaal"
732 .row = Search column: .colName$, "总计"
735 .row = Search column: .colName$, "合計"
738 Set string value: .row, .colName$, .pinyin$
741 .row = Search column: .colName$ , .pinyin$
745 .row = Get number of rows
746 Set string value: .row, eval.pinyin$, .pinyin$
747 Set numeric value... '.row' 'eval.correct$' 0
748 Set numeric value... '.row' 'eval.wrong$' 0
749 Set numeric value... '.row' 'eval.total$' 0
750 Set numeric value... '.row' 'eval.high$' 0
751 Set numeric value... '.row' 'eval.low$' 0
752 Set numeric value... '.row' 'eval.wide$' 0
753 Set numeric value... '.row' 'eval.narrow$' 0
754 Set numeric value... '.row' 'eval.unknown$' 0
755 Set numeric value... '.row' 'eval.commented$' 0
756 Set numeric value... '.row' 'eval.level$' 0
757 Set numeric value... '.row' 'eval.time$' 0
758 Set string value: .row, eval.wordlist$, wordlistName$
759 Sort rows... 'eval.pinyin$'
760 .row = Search column: eval.pinyin$, .pinyin$
764 procedure increment_toneevaluation_in_row .row .column$
765 select sgc2.performanceTable
766 .tmp = Get value... '.row' '.column$'
771 Set numeric value... '.row' '.column$' '.tmp'
774 Set string value: .row, eval.level$, config.strict$
776 # Set time stamp and wordlist
778 Set string value: .row, eval.time$, .dateTime$
779 Set string value: .row, eval.wordlist$, wordlistName$
783 # Update existing performance file with toneevaluation
784 procedure update_toneevaluation_file
785 if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
786 select sgc2.performanceTable
787 Write to table file... 'sgc.savePerf$'
791 procedure increment_toneevaluation_value .pinyin$ .column$
792 call get_toneevaluation_row '.pinyin$'
793 .row = get_toneevaluation_row.row
794 call increment_toneevaluation_in_row '.row' '.column$'
796 call get_toneevaluation_row 'eval.total$'
797 .row = get_toneevaluation_row.row
798 call increment_toneevaluation_in_row '.row' '.column$'
801 procedure add_feedback_to_toneevaluation .table$
802 select Table '.table$'
803 .line1$ = Get value... 1 Text
804 .line2$ = Get value... 2 Text
805 .label$ = Get value... 3 Text
807 .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
808 .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
812 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.total$'
813 if .tones$ = .recognized$
814 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.correct$'
817 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.wrong$'
819 if index_regex(.label$, "(Correct|Wrong)") <= 0
820 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.commented$'
824 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.unknown$'
827 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.low$'
830 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.high$'
832 if .label$ = "Narrow"
833 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.narrow$'
836 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.wide$'
840 # Update performance table when SaveAudio is on
842 call update_toneevaluation_file
846 procedure setGrade .grade$
847 select sgc.currentWordlist
848 if recordedSound$ <> "" and sgc.pinyin$ <> ""
849 call get_toneevaluation_row 'sgc.pinyin$'
850 .row = get_toneevaluation_row.row
851 select sgc2.performanceTable
852 .col = Get column index: eval.grade$
854 Insert column: 2, eval.grade$
856 .gradeNum = '.grade$'
861 Set numeric value: .row, eval.grade$, .gradeNum
862 .numRows = Get number of rows
865 for .r from 2 to .numRows
866 .tmp = Get value: .r, eval.grade$
874 .meanGrade = .sumGrade / .sumN
877 call get_evaluation_text '.language$' Total
878 .totalTxt$ = get_evaluation_text.text$
879 call get_toneevaluation_row '.totalTxt$'
880 .row = get_toneevaluation_row.row
881 Set numeric value: .row , eval.grade$, .meanGrade
883 # Store current performance table
884 call update_toneevaluation_file
888 procedure getGrade .pinyin$
891 call get_toneevaluation_row '.pinyin$'
892 .row = get_toneevaluation_row.row
894 select sgc2.performanceTable
895 .gradeLabel$ = eval.grade$
896 .col = Get column index: eval.grade$
898 .col = Get column index: "Grade"
899 .gradeLabel$ = "Grade"
902 Insert column... 2 Grade
904 .grade = Get value: .row, .gradeLabel$
905 if .grade = undefined
912 # Set mainPage.saveAudio$ to Grade
913 procedure set_grade_display .pinyin$
914 mainPage.saveAudio$ = ""
915 select sgc2.performanceTable
917 .col = Get column index: eval.grade$
918 if .pinyin$ <> "" and .col > 0
919 call get_toneevaluation_row 'eval.total$'
920 .row = get_toneevaluation_row.row
922 .grade$ = Get value: .row, eval.grade$
923 if index_regex(.grade$, "[0-9]") <= 0
929 .gradeNum = '.grade$'
930 mainPage.saveAudio$ = fixed$(.gradeNum, 1)
934 ###############################################################
936 # Miscelaneous procedures
938 ###############################################################
941 procedure synthesize_sound .pinyin$ .character$
942 .pinyin$ = replace_regex$(.pinyin$, "(^""|""$)", "", 0)
943 .character$ = replace_regex$(.character$, "(^""|""$)", "", 0)
944 .pinyinText$ = replace_regex$(.pinyin$, "0", "5", 0)
945 .pinyinExpanded$ = replace_regex$(.pinyin$, "([0-5])", " \1 - ", 0)
946 .pinyinExpanded$ = replace_regex$(.pinyinExpanded$, " - $", "", 0)
948 if .character$ = "-" or .character$ = "" or .character$ = "?"
949 .character$ = .pinyinText$
951 .currentCommand$ = ""
952 # Files need more checks!
953 if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
954 .currentCommand$ < 'speakCommandFile$'
955 .currentCommand$ = replace$(.currentCommand$, "'newline$'", " ", 0)
956 if index(.currentCommand$, "$$$") and .character$ <> .pinyinText$
957 # Find the character representation
958 .currentCommand$ = replace$(.currentCommand$, "$$$", .character$, 0)
959 elsif index(.currentCommand$, "&&&")
960 # Find the character representation
961 .currentCommand$ = replace$(.currentCommand$, "&&&", .pinyinText$, 0)
962 elsif index(sgc.nativeTTScommand$, "@@@")
963 .currentCommand$ = replace$(sgc.nativeTTScommand$, "@@@", .pinyinExpanded$, 0)
965 .currentCommand$ = .currentCommand$ + " ""'.pinyinText$'"""
968 system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.currentCommand$''
970 system_nocheck bash -rc -- ''.currentCommand$''
972 runSystem_nocheck: .currentCommand$
974 elsif sgc.nativeTTScommand$ <> "" and config.voice <= 0
975 # Get the pinyin text on the correct place
976 if index(sgc.nativeTTScommand$, "$$$")
977 .currentCommand$ = replace$(sgc.nativeTTScommand$, "$$$", .character$, 0)
978 elsif index(sgc.nativeTTScommand$, "&&&")
979 .currentCommand$ = replace$(sgc.nativeTTScommand$, "&&&", .pinyinText$, 0)
980 elsif index(sgc.nativeTTScommand$, "@@@")
981 .currentCommand$ = replace$(sgc.nativeTTScommand$, "@@@", .pinyinExpanded$, 0)
983 .currentCommand$ = sgc.nativeTTScommand$ + " ""'.pinyinText$'"""
985 runSystem_nocheck: .currentCommand$
986 elsif sgc2.synthesizer > 0
987 select sgc2.synthesizer
988 Play text... '.pinyin$'
992 procedure align_recordedSound .pinyin$
993 if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
994 select Sound 'recordedSound$'
995 .recordedTextGrid = To TextGrid... pinyin
996 Set interval text... 1 1 '.pinyin$'
997 select sgc2.synthesizer
998 plus Sound 'recordedSound$'
999 plus .recordedTextGrid
1000 sgc.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
1001 Rename... AlignedTextGrid
1002 select .recordedTextGrid