Made grading language independent
[sgc2.git] / InitialiseSGC2.praat
blobfa992cc696b98922f7e62e5de0faf24d23006839
2 # SpeakGoodChinese 2.0
3
4 # Intializing Praat script
6 #     SpeakGoodChinese: InitializeSGC2.praat defines and sets the global variables
7 #     and loads general SGC2 code 
8 #     
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
13
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.
18
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.
23
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
27
29 # Do not store these variables as preferences
30 sgc.nonPreferences$ = " config.savePerf config.openPerf config.clearSummary config.audioName "
32 # Information for logging activity
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 evaluationTablePrefix$ = "Evaluation"
40 evaluationTableName$ = ""
41 performanceTableName$ = "Performance"
43 # Define canvas
44 viewportMargin = 5
45 yWordlist = 11
46 yFeedback = yWordlist + 5
47 yPinyin = yFeedback + 10
48 yContour = yPinyin + 15
49 wipeContourArea$ = "demo Paint rectangle... White 20 80 'yContour' 100"
50 wipePinyinArea$ = "demo Paint rectangle... White 20 80 'yPinyin' 'yContour'"
51 wipeFeedbackArea$ = "demo Paint rectangle... White 0 100 'yFeedback' 'yPinyin'"
52 wipeWordlistArea$ = "demo Paint rectangle... White 20 80 'yWordlist' 'yFeedback'"
54 # Pop-Up window colors
55 sgc2.popUp_bordercolor$ = "{0.5,0.5,1}"
56 sgc2.popUp_backgroundcolor$ = "{0.9,0.9,1}"
58 # Initialize parameters
59 alertText$ = ""
60 sgc.currentWord = 1
61 sgc.currentWordlist = -1
62 sgc.numberOfWords = 0
63 sgc.currentWordNum = 1
64 sgc.numberOfDisplayedWords = 0
65 sgc.recordedSound = 0
66 sgc.saveAudioOn = 0
67 sgc.writeAll = 0
68 sgc.customLanguage$ = ""
69 pinyin$ = ""
70 sgc.pinyin$ = ""
71 character$ = ""
72 buttons$ = ""
73 config$ = ""
74 wordlistNum = 1
75 wordlistName$ = ""
76 wordlist$ = ""
77 sgc.failedAttempts = 0
78 mainPage.play = -1
79 config.deleteWordlist = -1
80 config.displayNumbers = 1
81 config.displayPinyin = 1
82 config.displayChar = 0
83 config.displayTrans = 1
84 config.useSoundExample = 1
85 config.synthesis$ = "_DISABLED_"
86 config.input$ = "Microphone"
87 config.showBackground = 1
88 sgc2.noiseThresshold = -30
89 config.strict$ = "1"
90 sgc.highestLevel = 3
91 config.voice = 4
92 config.voice$ = "f4"
93 config.voicesTTS$ = "default f1 f2 f3 f4 f5 m1 m2 m3 m4 m5 m6 m7"
94 sgc.lastVoice = 12
95 sgc.nativeOSXTTScommand$ = "say -vTing-Ting  --rate=95"
96 sgc.nativeLinuxTTScommand$ = "spd-say -l zh -t female1 -r -60"
97 #sgc.nativeWindowsTTScommand$ = "mshta.exe vbscript:Execute(""CreateObject(""""SAPI.SpVoice"""").Speak """"<voice gender='female' xml:lang='zh-CN'><rate speed='-5'>$$$</rate></voice>"""":Close"")"
98 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"")"
99 sgc.nativeTTScommand$ = ""
100 config.savePerf = 0
101 config.openPerf = 0
102 config.clearSummary = 0
103 config.audioName$ = ""
104 sgc.savePerf$ = ""
105 sgc.saveAudio$ = ""
106 mainPage.saveAudio$ = ""
107 sgc.allWordLists = -1
109 sgc_ToneProt.minimumPitch = 40
110 sgc_ToneProt.maximumPitch = 600
112 # Platform dependent settings
113 if macintosh or windows
114         config.displayChar = 1
115         config.displayNumbers = 0
116 endif
117 if windows and endsWith(build_SHA$, " XP")
118         config.displayChar = 0
119         config.displayNumbers = 0
120 endif
121 if unix
122         config.displayChar = 0
123         config.displayNumbers = 1
124 endif
126 if unix
127         samplingFrequency = 44100
128 elsif macintosh
129         samplingFrequency = 44100
130 elsif windows
131         samplingFrequency = 44100
132 endif
133 recordingTime = 2
135 # Be careful: Logging and Replaying can have side-effects
136 # sgc2.logging$ = "call saveLogOfActivity"
137 sgc2.logging$ = "# "
138 # When this is not "# ", the command 'replaySGC2Log$' will be run!
139 if not variableExists("replaySGC2Log$")
140         replaySGC2Log$ = "# "
141 endif
143 config.language$ = "EN"
144 config.register = 249
145 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
146 preferencesLogDir$ = "'preferencesAppDir$'/log"
147 preferencesTableDir$ = "'preferencesAppDir$'/GUI"
148 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
149 localWordlistDir$ = sgc2wordlists$
151 # Global word lists must be installed BY THE ADMINISTRATOR
152 # This means, create the directory 'globalwordlists$' and
153 # fill it with wordlist directories containing wordlist.Table
154 # and audio files. They can be copied from a local directory.
155 globalwordlists$ = ""
156 globaltablelists$ = ""
157 globalTTSdir$ = ""
158 if unix
159         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
160         globaltablelists$ = "/etc/'sgc2.demoAppName$'/GUI"      
161         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
162 elsif macintosh
163         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
164         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/GUI"
165         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
166 elsif windows
167         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
168         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/GUI"
169         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
170 endif
171 sgc2.synthesizer = -1
172 sgc.alignedTextGrid = -1
174 ##############################################################
176 config.shuffleLists = 1
177 config.adaptiveLists = 0
178 defaultFontSize = 12
179 defaultFont$ = "Helvetica"
180 defaultLineWidth = 1
182 # Set up directories if they do not exist already
183 call set_up_directories
185 # Get saved preferences
186 call read_preferences ""
188 # Set up TTS system
189 speakCommandDir$ = "'preferencesAppDir$'/TTS"
190 speakCommandFile$ = ""
191 # Define a Praat TTS command that will set sgc2.synthesizer
192 call set_TTS_parameters
193 call set_up_TTS
194 sgc.recordCommandFile$ = ""
195 sgc.playCommandFile$ = ""
196 if windows
197         sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
198 else
199         sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
200 endif
201 sgc.useAlternativeRecorder = 0
202 call set_up_recorder
203 sgc.useAlternativePlayer = 0
204 call set_up_player
206 # Set inital language
207 call set_language 'config.language$'
209 # Get the word-list
210 call load_word_list "'localWordlistDir$'" 0
212 # Set up evaluation table
213 initialiseSGC2.toneevaluation_table$ = ""
214 call initialize_toneevaluation_tables
216 # Draw inital window
217 call init_window
219 ###############################################################
221 # Obligatory procedures
223 ###############################################################
224 # Initialize Demo Window
225 procedure init_window
226     demo Erase all
227         demo Line width... 'defaultLineWidth'
228         demo 'defaultFont$'
229         call set_font_size 'defaultFontSize'
230         demo Black
231         
232         # If there is no recorded sound, try to read stored recording
233         if recordedSound$ = "" and sgc.saveAudio$ <> ""
234                 sgc.pinyin$ = ""
235                 select sgc.currentWordlist
236                 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
237                         call readPinyin 'sgc.currentWord'
238                         sgc.pinyin$ = readPinyin.pinyin$
239                         sgc.character$ = readPinyin.character$
240                         .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
241                         if fileReadable(.outputName$)
242                                 sgc.recordedSound = Read from file: .outputName$
243                                 recordedSound$ = selected$("Sound")
244                                 call recognizeTone
245                                 # See feedback on earl;ier recorded words
246                                 if fileReadable("'sgc.saveAudio$'/TestSpeakGoodChinese2.txt")
247                                         call write_feedback Feedback
248                                 else
249                                         call write_grade 'sgc.pinyin$'
250                                 endif
251                                 select Table Feedback
252                                 Remove
253                                 
254                                 # Set config.audioName
255                                 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
256                                 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
257                                 
258                         endif
259                 endif
260         endif
261         
262         # Set mainPage.saveAudio$ grade
263         call set_grade_display 'sgc.pinyin$'
264         
265         # Update screen
266         call reset_viewport
267         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
268         call set_window_title 'buttons$' '.windowTitle$'
269         # Display the word-list
270         call write_word_list
271     # Wipe screen and draw background
272     call wipeArea 'wipeContourArea$'
273         if config.showBackground
274                 call draw_background Background
275         endif
276     # Define buttons in a table
277     call init_buttons
278     # Set Play button
279         call Set_Play_Button
280     
281         # Draw the contour
282         call draw_tone_contour
283         # SaveAudio light
284         call paint_saveAudio_light
285         # Set play button
286         
287         sgc.failedAttempts = 0
288 endproc
290 # Make sure all Preferences directories are available
291 procedure set_up_directories
292         .dirPath$ = "'preferencesAppDir$'"
293         createDirectory(.dirPath$)
294         .dirPath$ > '.dirPath$'/directory.txt
295         createDirectory("'.dirPath$'/wordlists")
296         .dirPath$ > '.dirPath$'/wordlists/directory.txt
297         createDirectory("'.dirPath$'/pitchmodels")
298         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
299         createDirectory("'.dirPath$'/log")
300         .dirPath$ > '.dirPath$'/log/directory.txt
301         createDirectory("'.dirPath$'/TTS")
302         .dirPath$ > '.dirPath$'/TTS/directory.txt
303 endproc
306 # Retrieve and store setting between sessions
308 procedure read_preferences .preferencesFile$
309         if not fileReadable(.preferencesFile$)
310                 .preferencesFile$ = preferencesAppFile$
311         endif
312         if fileReadable(.preferencesFile$)
313                 Read from file... 'preferencesAppFile$'
314                 .preferenceTable$ = selected$("Table")
315                 .numPrefKeys = Get number of rows
316                 for .row to .numPrefKeys
317                         .variableName$ = Get value... '.row' Key
318                         if variableExists(.variableName$)
319                                 .variableValue = Get value... '.row' Value
320                                 if .variableValue <> undefined
321                                         '.variableName$' = '.variableValue'
322                                 endif
323                         elsif variableExists(.variableName$+"$")
324                                 .variableValue$ = Get value... '.row' Value
325                                 .variableName$ = .variableName$+"$"
326                                 '.variableName$' = "'.variableValue$'"
327                         endif
328                 endfor
329                 
330                 select Table '.preferenceTable$'
331                 Remove
332         endif
333 endproc
335 procedure write_preferences .preferencesFile$
336         Create Table with column names... Preferences 0 Key Value
337         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
338                 .preferencesFile$ = preferencesAppFile$
339         endif
340         .row = 0
341         
342         for .tableNum from 1 to 2
343                 if .tableNum = 1
344                         .table$ = "Config"
345                 elsif .tableNum = 2
346                         .table$ = "MainPage"
347                 endif
348                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
350                 select Table '.table$'
351                 .numTableRows = Get number of rows
352                 for .tablerow to .numTableRows
353                         select Table '.table$'
354                         .label$ = Get value... '.tablerow' Label
355                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
356                         .keyName$ = .variableName$
357                         .value$ = ""
358                         if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
359                                 .value$ = ""
360                         elsif variableExists(.variableName$)
361                                 .value = '.variableName$'
362                                 .value$ = "'.value'"
363                         elsif variableExists(.variableName$ + "$")
364                                 .variableName$ = .variableName$ + "$"
365                                 .value$ = '.variableName$'
366                         elsif index(.variableName$, "_") > 0
367                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
368                                 .keyName$ = .variableName$
369                                 select Table Preferences
370                                 .prefRow = Search column... Key '.keyName$'
371                                 if .prefRow <= 0
372                                         if variableExists(.variableName$)
373                                            .value = '.variableName$'
374                                            .value$ = "'.value'"
375                                         elsif variableExists(.variableName$ + "$")
376                                            .variableName$ = .variableName$ + "$"
377                                            .value$ = '.variableName$'
378                                         endif
379                                 endif
380                         endif
382                         if .value$ <> ""
383                                 select Table Preferences
384                                 Append row
385                                 .row += 1
386                                 Set string value... '.row' Key '.keyName$'
387                                 Set string value... '.row' Value '.value$'
388                         endif
389                 endfor
390         endfor
391         
392         # Some extra settings
393         select Table Preferences
394         Append row
395         .row += 1
396         Set string value... '.row' Key wordlistDir
397         Set string value... '.row' Value 'localWordlistDir$'
399         select Table Preferences
400         Append row
401         .row += 1
402         Set string value... '.row' Key wordlistName
403         Set string value... '.row' Value 'wordlistName$'
405         select Table Preferences
406         Write to table file... '.preferencesFile$'
407         Remove
409 endproc
411 ###############################################################
413 # TTS (speech synthesis). Place commands in user info
415 # Prerequisites
416 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
417 # speakCommandFile$ = ""
419 # Make sure sgc2.synthesizer is set!
420 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
421         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
422         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
423 endproc
425 procedure set_up_TTS
426         if sgc2.synthesizer > 0
427                 select sgc2.synthesizer
428                 Remove
429                 sgc2.synthesizer = -1
430         endif
431         # Note, the TTScommand must set sgc2.synthesizer!
432         if config.strict$ = "'sgc.highestLevel'"
433                 'sgc2.advancedTTScommand$'
434         else
435                 'sgc2.normalTTScommand$'
436         endif
438         if macintosh
439                 .ttscommand$ = "speak"
440                 .osName$ = "OSX"
441                 .quote$ = ""
442         elsif unix
443                 .ttscommand$ = "espeak"
444                 .osName$ = "UNIX"
445                 .quote$ = ""
446         elsif windows
447                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
448                 .osName$ = "WIN"
449                 .quote$ = """"
450         endif
451         # Global TTS command has precedence
452         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
453                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
454                 if config.synthesis$ = "_DISABLED_"
455                         # Default is ON
456                         config.synthesis$ = "eSpeak"
457                 endif
458         endif
460         # Local TTS command, if there is no global one
461         # Command does not exist yet, create it if eSpeak is installed
462         # Make sure to quote the path!
463         if speakCommandFile$ = ""
464                 # Autodetect synthesizer command
465 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
466 #                       .command_path$ = ""
467 #                       if macintosh
468 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
469 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
470 #                       elsif unix
471 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
472 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
473 #                       elsif windows
474 #                               if fileReadable(.ttscommand$)
475 #                                       .command_path$ = .ttscommand$
476 #                               endif
477 #                       endif
478 #                       # Remove any newlines
479 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
481 #                       # Command path found
482 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
483 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
484 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
485 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
486 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
487 #                       endif
488 #               endif
489                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
490                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
491                 endif
492                 deleteFile("'speakCommandDir$'/command_path.txt")
493         endif
494         
495         # Close off (other TTS maybe a later option)
496         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
497                 config.synthesis$ = "eSpeak"
498         endif
499 endproc
501 procedure set_TTS_parameters
502         if config.voice > 0
503                 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+\s+){'config.voice'}(\S+)(\s+.*)?$", "\2", 0)
504         else
505                 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+)(\s+.*)?$", "\1", 0)
506         endif
507         if .voice$ <> "" and index_regex(.voice$, "\s") <= 0
508                 if .voice$ = "default"
509                         config.voice$ = "*"
510                 else
511                         config.voice$ = replace_regex$(.voice$, ".", "\u&", 0)
512                 endif
513                 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin '.voice$' 10000 0.01 50 100 95 no IPA"
514                 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin '.voice$' 10000 0.01 50 70 125 no IPA"
515         endif
516         if macintosh and fileReadable("/usr/bin/say")
517                 sgc.nativeTTScommand$ = sgc.nativeOSXTTScommand$
518         elsif unix and fileReadable("/usr/bin/spd-say")
519                 sgc.nativeTTScommand$ = sgc.nativeLinuxTTScommand$
520         elsif windows
521                 sgc.nativeTTScommand$ = sgc.nativeWindowsTTScommand$
522         endif
523 endproc
525 # Alternative audio recorder
526 procedure set_up_recorder
527         if macintosh
528                 .osName$ = "OSX"
529                 .quote$ = ""
530         elsif unix
531                 .osName$ = "UNIX"
532                 .quote$ = ""
533         elsif windows
534                 .osName$ = "WIN"
535                 .quote$ = """"
536         endif
537         # Global TTS command has precedence
538         sgc.useAlternativeRecorder = 0
539         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
540                 createDirectory(sgc.scratchAudioDir$)
541                 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
542                 sgc.useAlternativeRecorder = 1
543         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
544                 createDirectory(sgc.scratchAudioDir$)
545                 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
546                 sgc.useAlternativeRecorder = 1
547         endif
548 endproc
550 # Alternative audio player
551 procedure set_up_player
552         if macintosh
553                 .osName$ = "OSX"
554                 .quote$ = ""
555         elsif unix
556                 .osName$ = "UNIX"
557                 .quote$ = ""
558         elsif windows
559                 .osName$ = "WIN"
560                 .quote$ = """"
561         endif
562         # Global TTS command has precedence
563         sgc.useAlternativePlayer = 0
564         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
565                 createDirectory(sgc.scratchAudioDir$)
566                 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
567                 sgc.useAlternativePlayer = 1
568         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
569                 createDirectory(sgc.scratchAudioDir$)
570                 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
571                 sgc.useAlternativePlayer = 1
572         endif
573 endproc
575 ###############################################################
577 # Evaluation tables
579 # Prerequisites
580 # initialiseSGC2.toneevaluation_table$ = ""
581 procedure initialize_toneevaluation_tables
582         .invalidperformanceTable = 0
583         .oldPerformanceTable = -1
584         # Mark old table for removal
585         if initialiseSGC2.toneevaluation_table$ != ""
586                 .oldPerformanceTable = sgc2.performanceTable
587                 sgc2.performanceTable = -1
588         endif
589         initialiseSGC2.toneevaluation_table$ = ""
590         # Get the name of the table
591         call get_evaluation_table_labels 'config.language$'
592         performanceTableName$ = eval.performance$
593         
594         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
595                 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
596                 Rename: performanceTableName$
597                 .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$'"
598                 .numPerfColumns = 14
599                 # Check to make sure all columns are initialized, but only if Pinyin exists!
600                 select sgc2.performanceTable
601                 .numColumns = Get number of columns
602                 # Find Pinyin column on 1
603                 if index("Pinyin 拼音 ピンイン", eval.pinyin$) > 0
604                         .column = Get column index: "Pinyin"
605                         if .column <= 0
606                                 .column = Get column index: "拼音"
607                         endif
608                         if .column <= 0
609                                 .column = Get column index: "ピンイン"
610                         endif
611                 else
612                         .column = Get column index: eval.pinyin$
613                 endif
614                 if .column != 1 or .numColumns != .numPerfColumns
615                         # Set ERROR and remove file read
616                         .invalidperformanceTable = 1
617                         sgc2.performanceTable = .oldPerformanceTable
618                         .oldPerformanceTable = -1
619                 else
620                         .nameList$ = .columnNameList$
621                         for .col to .numColumns
622                                 .columnNameList$ = replace_regex$(.columnNameList$, "^\s+", "", 1)
623                                 .columnHeader$ = extractWord$(.columnNameList$, "")
624                                 .columnNameList$ = replace_regex$(.columnNameList$, "^\s*'.columnHeader$'\s*", "", 1)
625                                 select sgc2.performanceTable
626                                 Set column label (index): .col, .columnHeader$
627                         endfor
628                 endif
630                 # Performance table has been read. Set the wordlist to the first row
631                 select sgc2.performanceTable
632                 .numWords = Get number of rows
633                 .row = 1
634                 wordlistName$ = ""
635                 .wordlistLabel$ = eval.wordlist$
636                 .col = Get column index: .wordlistLabel$
637                 if .col <= 0
638                         .wordlistLabel$ = "Wordlist"
639                         .col = Get column index: .wordlistLabel$
640                 endif
641                 while .col > 0 and .row <= .numWords and (wordlistName$ = "" or index_regex(wordlistName$, "[^\?\- ]") <= 0)
642                         wordlistName$ = Get value: .row, .wordlistLabel$
643                         .row += 1
644                 endwhile
645                 
646                 call load_word_list: localWordlistDir$, 0
647         else
648                 # Create new table
649                 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$'
650         endif
651         initialiseSGC2.toneevaluation_table$ = performanceTableName$
652         call get_toneevaluation_row 'eval.total$'
653         
654         # Remove marked old table
655         if .oldPerformanceTable > 0
656                 select .oldPerformanceTable
657                 Remove
658                 .oldPerformanceTable = -1
659         endif
660 endproc
662 procedure get_toneevaluation_row .pinyin$
663         select sgc2.performanceTable
664         # Catch and rename Totals. Make sure it works when you stay within a new language
665         if index("Total Totaal 总计 合計", .pinyin$) > 0
666                 .row = Search column: eval.pinyin$, "Total"
667                 if .row < 1
668                         .row = Search column: eval.pinyin$, "Totaal"
669                 endif
670                 if .row < 1
671                         .row = Search column: eval.pinyin$, "总计"
672                 endif
673                 if .row < 1
674                         .row = Search column: eval.pinyin$, "合計"
675                 endif
676                 if .row > 0
677                         Set string value: .row,  eval.pinyin$, .pinyin$
678                 endif
679         else
680                 .row = Search column... "'eval.pinyin$'" '.pinyin$'
681         endif
682         if .row < 1
683                 Append row
684                 .row = Get number of rows
685                 Set string value... '.row' "'eval.pinyin$'" '.pinyin$'
686                 Set numeric value... '.row' 'eval.correct$' 0
687                 Set numeric value... '.row' 'eval.wrong$' 0
688                 Set numeric value... '.row' 'eval.total$' 0
689                 Set numeric value... '.row' 'eval.high$' 0
690                 Set numeric value... '.row' 'eval.low$' 0
691                 Set numeric value... '.row' 'eval.wide$' 0
692                 Set numeric value... '.row' 'eval.narrow$' 0
693                 Set numeric value... '.row' 'eval.unknown$' 0
694                 Set numeric value... '.row' 'eval.commented$' 0
695                 Set numeric value... '.row' 'eval.level$' 0
696                 Set numeric value... '.row' 'eval.time$' 0
697                 Set string value... '.row' 'eval.wordlist$' 'wordlistName$'
698                 Sort rows... 'eval.pinyin$'
699         .row = Search column... "'eval.pinyin$'" '.pinyin$'
700         endif
701 endproc
703 procedure increment_toneevaluation_in_row .row .column$
704         select sgc2.performanceTable
705         .tmp = Get value... '.row' '.column$'
706         if .tmp = undefined
707                 .tmp = 0
708         endif
709         .tmp += 1
710         Set numeric value... '.row' '.column$' '.tmp'
711         
712         # Set Level
713         Set string value: .row, eval.level$, config.strict$
714         
715         # Set time stamp and wordlist
716         .dateTime$ = date$()
717         Set string value: .row, eval.time$, .dateTime$
718         Set string value: .row, eval.wordlist$, wordlistName$
719         
720 endproc
722 # Update existing performance file with toneevaluation
723 procedure update_toneevaluation_file
724         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
725                 select sgc2.performanceTable
726                 Write to table file... 'sgc.savePerf$'
727         endif
728 endproc
730 procedure increment_toneevaluation_value .pinyin$ .column$
731         call get_toneevaluation_row '.pinyin$'
732         .row = get_toneevaluation_row.row
733         call increment_toneevaluation_in_row '.row' '.column$'
734         
735         call get_toneevaluation_row 'eval.total$'
736         .row = get_toneevaluation_row.row
737         call increment_toneevaluation_in_row '.row' '.column$'
738 endproc
740 procedure add_feedback_to_toneevaluation .table$
741     select Table '.table$'
742     .line1$ = Get value... 1 Text
743     .line2$ = Get value... 2 Text
744     .label$ = Get value... 3 Text
745         if .label$ = "6"
746                 .label$ = "'eval.unknown$'"
747         endif
748         
749         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
750         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
751         
752         # Set evaluation
753         call increment_toneevaluation_value 'sgc.pinyin$' 'eval.total$'
754         if .tones$ = .recognized$
755                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.correct$'
756                 .result = 1
757         else
758                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.wrong$'
759                 .result = 0
760         endif
761         if index_regex(.label$, "(Correct|Wrong)") <= 0
762                 if .label$ = "Correct"
763                         .label$ = eval.correct$
764                 else
765                         .label$ = eval.wrong$
766                 endif
767                 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
768                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.commented$'
769                 .result = 0
770         endif
771         
772         # Update performance table when SaveAudio is on
773         if sgc.saveAudioOn
774                 call update_toneevaluation_file
775         endif
776 endproc
778 procedure setGrade .grade$
779         select sgc.currentWordlist
780         if recordedSound$ <> "" and sgc.pinyin$ <> ""           
781                 call get_toneevaluation_row 'sgc.pinyin$'
782                 .row = get_toneevaluation_row.row
783                 select sgc2.performanceTable
784                 .col = Get column index: eval.grade$
785                 if .col <= 0
786                         Insert column: 2, eval.grade$
787                 endif
788                 .gradeNum = '.grade$'
789                 if .gradeNum = 0
790                         .gradeNum = 10
791                 endif
793                 Set numeric value: .row, eval.grade$, .gradeNum
794                 .numRows = Get number of rows
795                 .sumGrade = 0
796                 .sumN = 0
797                 for .r from 2 to .numRows
798                         .tmp = Get value: .r, eval.grade$
799                         if .tmp <> undefined
800                                 .sumGrade += .tmp
801                                 .sumN += 1
802                         endif
803                 endfor
804                 .meanGrade = -1
805                 if .sumN > 0
806                         .meanGrade = .sumGrade / .sumN
807                 endif
808                 
809                 call get_evaluation_text '.language$' Total
810                 .totalTxt$ = get_evaluation_text.text$
811                 call get_toneevaluation_row '.totalTxt$'
812                 .row = get_toneevaluation_row.row
813                 Set numeric value: .row , eval.grade$, .meanGrade
814                 
815                 # Store current performance table
816                 call update_toneevaluation_file
817         endif
818 endproc
820 procedure getGrade .pinyin$
821         .grade = -1
822         if .pinyin$ <> ""
823                 call get_toneevaluation_row '.pinyin$'
824                 .row = get_toneevaluation_row.row
825                 
826                 select sgc2.performanceTable
827                 .gradeLabel$ = eval.grade$
828                 .col = Get column index: eval.grade$
829                 if .col <= 0
830                         .col = Get column index: "Grade"
831                         .gradeLabel$ = "Grade"
832                 endif
833                 if .col <= 0
834                         Insert column... 2 Grade
835                 else
836                         .grade = Get value: .row, .gradeLabel$
837                         if .grade = undefined
838                                 .grade = -1
839                         endif   
840                 endif
841         endif
842 endproc
844 # Set mainPage.saveAudio$ to Grade
845 procedure set_grade_display .pinyin$
846         mainPage.saveAudio$ = ""
847         select sgc2.performanceTable
848         .grade$ = ""
849         .col = Get column index: eval.grade$
850         if .pinyin$ <> "" and .col > 0
851                 call get_toneevaluation_row '.pinyin$'
852                 .row = get_toneevaluation_row.row
853                 if .row > 0
854                         .grade$ = Get value: .row, eval.grade$
855                         if index_regex(.grade$, "[0-9]") <= 0
856                                 .grade$ = ""
857                         endif
858                 endif
859         endif
860         if .grade$ <> ""
861                 mainPage.saveAudio$ = .grade$
862         endif
863 endproc
864         
865 ###############################################################
867 # Miscelaneous procedures
869 ###############################################################
871 # TTS synthesis
872 procedure synthesize_sound .pinyin$ .character$
873         .pinyin$ = replace_regex$(.pinyin$, "(^""|""$)", "", 0)
874         .character$ = replace_regex$(.character$, "(^""|""$)", "", 0)
875         .pinyinText$ = replace_regex$(.pinyin$, "0", "5", 0)
876         .pinyinExpanded$ = replace_regex$(.pinyin$, "([0-5])", " \1 - ", 0)
877         .pinyinExpanded$ = replace_regex$(.pinyinExpanded$, " - $", "", 0)
878         
879         if .character$ = "-" or .character$ = "" or .character$ = "?"
880                 .character$ = .pinyinText$
881         endif
882         .currentCommand$ = ""
883         # Files need more checks!
884         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
885                 .currentCommand$ < 'speakCommandFile$'
886                 .currentCommand$ = replace$(.currentCommand$, "'newline$'", " ", 0)
887                 if index(.currentCommand$, "$$$") and .character$ <> .pinyinText$
888                         # Find the character representation
889                         .currentCommand$ = replace$(.currentCommand$, "$$$", .character$, 0)
890                 elsif index(.currentCommand$, "&&&")
891                         # Find the character representation
892                         .currentCommand$ = replace$(.currentCommand$, "&&&", .pinyinText$, 0)
893                 elsif index(sgc.nativeTTScommand$, "@@@")
894                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "@@@", .pinyinExpanded$, 0)
895                 else
896                         .currentCommand$ = .currentCommand$ + " ""'.pinyinText$'"""
897                 endif
898                 if macintosh
899                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.currentCommand$''
900                 elsif unix
901                         system_nocheck bash -rc -- ''.currentCommand$''
902                 elsif windows
903                         runSystem_nocheck: .currentCommand$
904                 endif
905         elsif sgc.nativeTTScommand$ <> "" and config.voice <= 0
906                 # Get the pinyin text on the correct place
907                 if index(sgc.nativeTTScommand$, "$$$")
908                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "$$$", .character$, 0)
909                 elsif index(sgc.nativeTTScommand$, "&&&")
910                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "&&&", .pinyinText$, 0)
911                 elsif index(sgc.nativeTTScommand$, "@@@")
912                         .currentCommand$ = replace$(sgc.nativeTTScommand$, "@@@", .pinyinExpanded$, 0)
913                 else
914                         .currentCommand$ = sgc.nativeTTScommand$ + " ""'.pinyinText$'"""
915                 endif
916                 runSystem_nocheck: .currentCommand$
917         elsif sgc2.synthesizer > 0
918                 select sgc2.synthesizer
919                 Play text... '.pinyin$'
920         endif
921 endproc
923 procedure align_recordedSound .pinyin$
924         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
925                 select Sound 'recordedSound$'
926                 .recordedTextGrid = To TextGrid... pinyin
927                 Set interval text... 1 1 '.pinyin$'
928                 select sgc2.synthesizer
929                 plus Sound 'recordedSound$'
930                 plus .recordedTextGrid
931                 sgc.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
932                 Rename... AlignedTextGrid
933                 select .recordedTextGrid
934                 Remove
935         endif
936 endproc