Changed behavior of buttons with variable labels
[sgc2.git] / InitialiseSGC2.praat
blobbe88b389efb9552a29471f67dae216a720c9b78f
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.nativeTTScommand$ = ""
98 config.savePerf = 0
99 config.openPerf = 0
100 config.clearSummary = 0
101 config.audioName$ = ""
102 sgc.savePerf$ = ""
103 sgc.saveAudio$ = ""
104 mainPage.saveAudio$ = ""
105 sgc.allWordLists = -1
107 sgc_ToneProt.minimumPitch = 40
108 sgc_ToneProt.maximumPitch = 600
110 # Platform dependent settings
111 if macintosh or windows
112         config.displayChar = 1
113         config.displayNumbers = 0
114 endif
115 if windows and endsWith(build_SHA$, " XP")
116         config.displayChar = 0
117         config.displayNumbers = 0
118 endif
119 if unix
120         config.displayChar = 0
121         config.displayNumbers = 0
122 endif
124 if unix
125         samplingFrequency = 44100
126 elsif macintosh
127         samplingFrequency = 44100
128 elsif windows
129         samplingFrequency = 44100
130 endif
131 recordingTime = 2
133 # Be careful: Logging and Replaying can have side-effects
134 # sgc2.logging$ = "call saveLogOfActivity"
135 sgc2.logging$ = "# "
136 # When this is not "# ", the command 'replaySGC2Log$' will be run!
137 if not variableExists("replaySGC2Log$")
138         replaySGC2Log$ = "# "
139 endif
141 config.language$ = "EN"
142 config.register = 249
143 preferencesAppFile$ = preferencesAppDir$+"/sgc2rc.txt"
144 preferencesLogDir$ = "'preferencesAppDir$'/log"
145 preferencesTableDir$ = "'preferencesAppDir$'/GUI"
146 sgc2wordlists$ = "'preferencesAppDir$'/wordlists"
147 localWordlistDir$ = sgc2wordlists$
149 # Global word lists must be installed BY THE ADMINISTRATOR
150 # This means, create the directory 'globalwordlists$' and
151 # fill it with wordlist directories containing wordlist.Table
152 # and audio files. They can be copied from a local directory.
153 globalwordlists$ = ""
154 globaltablelists$ = ""
155 globalTTSdir$ = ""
156 if unix
157         globalwordlists$ = "/etc/'sgc2.demoAppName$'/wordlists"
158         globaltablelists$ = "/etc/'sgc2.demoAppName$'/GUI"      
159         globalTTSdir$ = "/etc/'sgc2.demoAppName$'/TTS"  
160 elsif macintosh
161         globalwordlists$ = "/Library/Preferences/'sgc2.demoAppName$'/wordlists"
162         globaltablelists$ = "/Library/Preferences/'sgc2.demoAppName$'/GUI"
163         globalTTSdir$ = "/Library/Preferences/'sgc2.demoAppName$'/TTS"  
164 elsif windows
165         globalwordlists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/wordlists"
166         globaltablelists$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/GUI"
167         globalTTSdir$ = "C:/Documents and Settings/All Users/Application Data/'sgc2.demoAppName$'/TTS"
168 endif
169 sgc2.synthesizer = -1
170 sgc.alignedTextGrid = -1
172 ##############################################################
174 config.shuffleLists = 1
175 config.adaptiveLists = 0
176 defaultFontSize = 12
177 defaultFont$ = "Helvetica"
178 defaultLineWidth = 1
180 # Set up directories if they do not exist already
181 call set_up_directories
183 # Get saved preferences
184 call read_preferences ""
186 # Set up TTS system
187 speakCommandDir$ = "'preferencesAppDir$'/TTS"
188 speakCommandFile$ = ""
189 # Define a Praat TTS command that will set sgc2.synthesizer
190 call set_TTS_parameters
191 call set_up_TTS
192 sgc.recordCommandFile$ = ""
193 sgc.playCommandFile$ = ""
194 if windows
195         sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
196 else
197         sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
198 endif
199 sgc.useAlternativeRecorder = 0
200 call set_up_recorder
201 sgc.useAlternativePlayer = 0
202 call set_up_player
204 # Set inital language
205 call set_language 'config.language$'
207 # Get the word-list
208 call load_word_list "'localWordlistDir$'" 0
210 # Set up evaluation table
211 initialiseSGC2.toneevaluation_table$ = ""
212 call initialize_toneevaluation_tables
214 # Draw inital window
215 call init_window
217 ###############################################################
219 # Obligatory procedures
221 ###############################################################
222 # Initialize Demo Window
223 procedure init_window
224     demo Erase all
225         demo Line width... 'defaultLineWidth'
226         demo 'defaultFont$'
227         call set_font_size 'defaultFontSize'
228         demo Black
229         
230         # If there is no recorded sound, try to read stored recording
231         if recordedSound$ = "" and sgc.saveAudio$ <> ""
232                 sgc.pinyin$ = ""
233                 select sgc.currentWordlist
234                 if sgc.currentWord > 0 and sgc.currentWord <= sgc.numberOfWords
235                         call readPinyin 'sgc.currentWord'
236                         sgc.pinyin$ = readPinyin.pinyin$
237                         .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
238                         if fileReadable(.outputName$)
239                                 sgc.recordedSound = Read from file: .outputName$
240                                 recordedSound$ = selected$("Sound")
241                                 call recognizeTone
242                                 # See feedback on earl;ier recorded words
243                                 if fileReadable("'sgc.saveAudio$'/TestSpeakGoodChinese2.txt")
244                                         call write_feedback Feedback
245                                 endif
246                                 select Table Feedback
247                                 Remove
248                                 
249                                 # Set config.audioName
250                                 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
251                                 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
252                                 
253                         endif
254                 endif
255         endif
256         
257         # Set mainPage.saveAudio$ grade
258         call set_grade_display 'sgc.pinyin$'
259         
260         # Update screen
261         call reset_viewport
262         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
263         call set_window_title 'buttons$' '.windowTitle$'
264         # Display the word-list
265         call write_word_list
266     # Wipe screen and draw background
267     call wipeArea 'wipeContourArea$'
268         if config.showBackground
269                 call draw_background Background
270         endif
271     # Define buttons in a table
272     call init_buttons
273     # Set Play button
274         call Set_Play_Button
275     
276         # Draw the contour
277         call draw_tone_contour
278         # SaveAudio light
279         call paint_saveAudio_light
280         # Set play button
281         
282         sgc.failedAttempts = 0
283 endproc
285 # Make sure all Preferences directories are available
286 procedure set_up_directories
287         .dirPath$ = "'preferencesAppDir$'"
288         createDirectory(.dirPath$)
289         .dirPath$ > '.dirPath$'/directory.txt
290         createDirectory("'.dirPath$'/wordlists")
291         .dirPath$ > '.dirPath$'/wordlists/directory.txt
292         createDirectory("'.dirPath$'/pitchmodels")
293         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
294         createDirectory("'.dirPath$'/log")
295         .dirPath$ > '.dirPath$'/log/directory.txt
296         createDirectory("'.dirPath$'/TTS")
297         .dirPath$ > '.dirPath$'/TTS/directory.txt
298 endproc
301 # Retrieve and store setting between sessions
303 procedure read_preferences .preferencesFile$
304         if not fileReadable(.preferencesFile$)
305                 .preferencesFile$ = preferencesAppFile$
306         endif
307         if fileReadable(.preferencesFile$)
308                 Read from file... 'preferencesAppFile$'
309                 .preferenceTable$ = selected$("Table")
310                 .numPrefKeys = Get number of rows
311                 for .row to .numPrefKeys
312                         .variableName$ = Get value... '.row' Key
313                         if variableExists(.variableName$)
314                                 .variableValue = Get value... '.row' Value
315                                 if .variableValue <> undefined
316                                         '.variableName$' = '.variableValue'
317                                 endif
318                         elsif variableExists(.variableName$+"$")
319                                 .variableValue$ = Get value... '.row' Value
320                                 .variableName$ = .variableName$+"$"
321                                 '.variableName$' = "'.variableValue$'"
322                         endif
323                 endfor
324                 
325                 select Table '.preferenceTable$'
326                 Remove
327         endif
328 endproc
330 procedure write_preferences .preferencesFile$
331         Create Table with column names... Preferences 0 Key Value
332         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
333                 .preferencesFile$ = preferencesAppFile$
334         endif
335         .row = 0
336         
337         for .tableNum from 1 to 2
338                 if .tableNum = 1
339                         .table$ = "Config"
340                 elsif .tableNum = 2
341                         .table$ = "MainPage"
342                 endif
343                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
345                 select Table '.table$'
346                 .numTableRows = Get number of rows
347                 for .tablerow to .numTableRows
348                         select Table '.table$'
349                         .label$ = Get value... '.tablerow' Label
350                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
351                         .keyName$ = .variableName$
352                         .value$ = ""
353                         if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
354                                 .value$ = ""
355                         elsif variableExists(.variableName$)
356                                 .value = '.variableName$'
357                                 .value$ = "'.value'"
358                         elsif variableExists(.variableName$ + "$")
359                                 .variableName$ = .variableName$ + "$"
360                                 .value$ = '.variableName$'
361                         elsif index(.variableName$, "_") > 0
362                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
363                                 .keyName$ = .variableName$
364                                 select Table Preferences
365                                 .prefRow = Search column... Key '.keyName$'
366                                 if .prefRow <= 0
367                                         if variableExists(.variableName$)
368                                            .value = '.variableName$'
369                                            .value$ = "'.value'"
370                                         elsif variableExists(.variableName$ + "$")
371                                            .variableName$ = .variableName$ + "$"
372                                            .value$ = '.variableName$'
373                                         endif
374                                 endif
375                         endif
377                         if .value$ <> ""
378                                 select Table Preferences
379                                 Append row
380                                 .row += 1
381                                 Set string value... '.row' Key '.keyName$'
382                                 Set string value... '.row' Value '.value$'
383                         endif
384                 endfor
385         endfor
386         
387         # Some extra settings
388         select Table Preferences
389         Append row
390         .row += 1
391         Set string value... '.row' Key wordlistDir
392         Set string value... '.row' Value 'localWordlistDir$'
394         select Table Preferences
395         Append row
396         .row += 1
397         Set string value... '.row' Key wordlistName
398         Set string value... '.row' Value 'wordlistName$'
400         select Table Preferences
401         Write to table file... '.preferencesFile$'
402         Remove
404 endproc
406 ###############################################################
408 # TTS (speech synthesis). Place commands in user info
410 # Prerequisites
411 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
412 # speakCommandFile$ = ""
414 # Make sure sgc2.synthesizer is set!
415 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
416         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
417         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
418 endproc
420 procedure set_up_TTS
421         if sgc2.synthesizer > 0
422                 select sgc2.synthesizer
423                 Remove
424                 sgc2.synthesizer = -1
425         endif
426         # Note, the TTScommand must set sgc2.synthesizer!
427         if config.strict$ = "'sgc.highestLevel'"
428                 'sgc2.advancedTTScommand$'
429         else
430                 'sgc2.normalTTScommand$'
431         endif
433         if macintosh
434                 .ttscommand$ = "speak"
435                 .osName$ = "OSX"
436                 .quote$ = ""
437         elsif unix
438                 .ttscommand$ = "espeak"
439                 .osName$ = "UNIX"
440                 .quote$ = ""
441         elsif windows
442                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
443                 .osName$ = "WIN"
444                 .quote$ = """"
445         endif
446         # Global TTS command has precedence
447         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
448                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
449                 if config.synthesis$ = "_DISABLED_"
450                         # Default is ON
451                         config.synthesis$ = "eSpeak"
452                 endif
453         endif
455         # Local TTS command, if there is no global one
456         # Command does not exist yet, create it if eSpeak is installed
457         # Make sure to quote the path!
458         if speakCommandFile$ = ""
459                 # Autodetect synthesizer command
460 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
461 #                       .command_path$ = ""
462 #                       if macintosh
463 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
464 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
465 #                       elsif unix
466 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
467 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
468 #                       elsif windows
469 #                               if fileReadable(.ttscommand$)
470 #                                       .command_path$ = .ttscommand$
471 #                               endif
472 #                       endif
473 #                       # Remove any newlines
474 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
476 #                       # Command path found
477 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
478 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
479 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
480 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
481 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
482 #                       endif
483 #               endif
484                 if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
485                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
486                 endif
487                 deleteFile("'speakCommandDir$'/command_path.txt")
488         endif
489         
490         # Close off (other TTS maybe a later option)
491         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
492                 config.synthesis$ = "eSpeak"
493         endif
494 endproc
496 procedure set_TTS_parameters
497         if config.voice > 0
498                 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+\s+){'config.voice'}(\S+)(\s+.*)?$", "\2", 0)
499         else
500                 .voice$ = replace_regex$(config.voicesTTS$, "^(\S+)(\s+.*)?$", "\1", 0)
501         endif
502         if .voice$ <> "" and index_regex(.voice$, "\s") <= 0
503                 if .voice$ = "default"
504                         config.voice$ = "*"
505                 else
506                         config.voice$ = replace_regex$(.voice$, ".", "\u&", 0)
507                 endif
508                 sgc2.normalTTScommand$ = "call create_default_TTS Mandarin '.voice$' 10000 0.01 50 100 95 no IPA"
509                 sgc2.advancedTTScommand$ = "call create_default_TTS Mandarin '.voice$' 10000 0.01 50 70 125 no IPA"
510         endif
511         if macintosh and fileReadable("/usr/bin/say")
512                 sgc.nativeTTScommand$ = sgc.nativeOSXTTScommand$
513         elsif unix and fileReadable("/usr/bin/spd-say")
514                 sgc.nativeTTScommand$ = sgc.nativeLinuxTTScommand$
515         endif
516 endproc
518 # Alternative audio recorder
519 procedure set_up_recorder
520         if macintosh
521                 .osName$ = "OSX"
522                 .quote$ = ""
523         elsif unix
524                 .osName$ = "UNIX"
525                 .quote$ = ""
526         elsif windows
527                 .osName$ = "WIN"
528                 .quote$ = """"
529         endif
530         # Global TTS command has precedence
531         sgc.useAlternativeRecorder = 0
532         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
533                 createDirectory(sgc.scratchAudioDir$)
534                 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
535                 sgc.useAlternativeRecorder = 1
536         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
537                 createDirectory(sgc.scratchAudioDir$)
538                 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
539                 sgc.useAlternativeRecorder = 1
540         endif
541 endproc
543 # Alternative audio player
544 procedure set_up_player
545         if macintosh
546                 .osName$ = "OSX"
547                 .quote$ = ""
548         elsif unix
549                 .osName$ = "UNIX"
550                 .quote$ = ""
551         elsif windows
552                 .osName$ = "WIN"
553                 .quote$ = """"
554         endif
555         # Global TTS command has precedence
556         sgc.useAlternativePlayer = 0
557         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_play_command.txt")
558                 createDirectory(sgc.scratchAudioDir$)
559                 sgc.playCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_play_command.txt"
560                 sgc.useAlternativePlayer = 1
561         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_play_command.txt")
562                 createDirectory(sgc.scratchAudioDir$)
563                 sgc.playCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_play_command.txt"
564                 sgc.useAlternativePlayer = 1
565         endif
566 endproc
568 ###############################################################
570 # Evaluation tables
572 # Prerequisites
573 # initialiseSGC2.toneevaluation_table$ = ""
574 procedure initialize_toneevaluation_tables
575         # Remove old table
576         if initialiseSGC2.toneevaluation_table$ != ""
577                 select sgc2.performanceTable
578                 Remove
579         endif
580         initialiseSGC2.toneevaluation_table$ = ""
581         # Get the name of the table
582         call get_evaluation_table_labels 'config.language$'
583         performanceTableName$ = eval.performance$
584         
585         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
586                 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
587                 Rename: performanceTableName$
588                 # Check to make sure all columns are initialized, but only if Pinyin exists!
589                 select sgc2.performanceTable
590                 .column = Get column index: eval.pinyin$
591                 if .column <= 0
592                         select sgc2.performanceTable
593                         Remove
594                         sgc2.performanceTable = Create Table with column names... "'performanceTableName$'" 0 'eval.pinyin$' 'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'
595                 endif
596                 .columnNameList$ = "'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'"
597                 while index_regex(.columnNameList$, "\S")
598                         .columnNameList$ = replace_regex$(.columnNameList$, "^\s+", "", 1)
599                         .columnHeader$ = extractWord$(.columnNameList$, "")
600                         .columnNameList$ = replace_regex$(.columnNameList$, "^\s*'.columnHeader$'\s*", "", 1)
601                         # Initialize header
602                         select sgc2.performanceTable
603                         .column = Get column index: .columnHeader$
604                         if .column <= 0
605                                 select sgc2.performanceTable
606                                 Append column: .columnHeader$
607                         endif
608                         
609                 endwhile
610                 
611                 # Performance table has been read. Set the wordlist to the first row
612                 select sgc2.performanceTable
613                 wordlistName$ = Get value: 1, eval.wordlist$
614                 call load_word_list: localWordlistDir$, 0
615         else
616                 # Create new table
617                 sgc2.performanceTable = Create Table with column names... "'performanceTableName$'" 0 'eval.pinyin$' 'eval.correct$' 'eval.wrong$' 'eval.total$' 'eval.high$' 'eval.low$' 'eval.wide$' 'eval.narrow$' 'eval.unknown$' 'eval.commented$' 'eval.level$' 'eval.time$' 'eval.wordlist$'
618         endif
619         initialiseSGC2.toneevaluation_table$ = performanceTableName$
620         call get_toneevaluation_row 'eval.total$'
621 endproc
623 procedure get_toneevaluation_row .pinyin$
624         select sgc2.performanceTable
625     .row = Search column... "'eval.pinyin$'" '.pinyin$'
626         if .row < 1
627                 Append row
628                 .row = Get number of rows
629                 Set string value... '.row' "'eval.pinyin$'" '.pinyin$'
630                 Set numeric value... '.row' 'eval.correct$' 0
631                 Set numeric value... '.row' 'eval.wrong$' 0
632                 Set numeric value... '.row' 'eval.total$' 0
633                 Set numeric value... '.row' 'eval.high$' 0
634                 Set numeric value... '.row' 'eval.low$' 0
635                 Set numeric value... '.row' 'eval.wide$' 0
636                 Set numeric value... '.row' 'eval.narrow$' 0
637                 Set numeric value... '.row' 'eval.unknown$' 0
638                 Set numeric value... '.row' 'eval.commented$' 0
639                 Set numeric value... '.row' 'eval.level$' 0
640                 Set numeric value... '.row' 'eval.time$' 0
641                 Set string value... '.row' 'eval.wordlist$' 'wordlistName$'
642                 Sort rows... 'eval.pinyin$'
643         .row = Search column... "'eval.pinyin$'" '.pinyin$'
644         endif
645 endproc
647 procedure increment_toneevaluation_in_row .row .column$
648         select sgc2.performanceTable
649         .tmp = Get value... '.row' '.column$'
650         if .tmp = undefined
651                 .tmp = 0
652         endif
653         .tmp += 1
654         Set numeric value... '.row' '.column$' '.tmp'
655         
656         # Set Level
657         Set string value: .row, eval.level$, config.strict$
658         
659         # Set time stamp and wordlist
660         .dateTime$ = date$()
661         Set string value: .row, eval.time$, .dateTime$
662         Set string value: .row, eval.wordlist$, wordlistName$
663         
664 endproc
666 # Update existing performance file with toneevaluation
667 procedure update_toneevaluation_file
668         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
669                 select sgc2.performanceTable
670                 Write to table file... 'sgc.savePerf$'
671         endif
672 endproc
674 procedure increment_toneevaluation_value .pinyin$ .column$
675         call get_toneevaluation_row '.pinyin$'
676         .row = get_toneevaluation_row.row
677         call increment_toneevaluation_in_row '.row' '.column$'
678         
679         call get_toneevaluation_row 'eval.total$'
680         .row = get_toneevaluation_row.row
681         call increment_toneevaluation_in_row '.row' '.column$'
682 endproc
684 procedure add_feedback_to_toneevaluation .table$
685     select Table '.table$'
686     .line1$ = Get value... 1 Text
687     .line2$ = Get value... 2 Text
688     .label$ = Get value... 3 Text
689         if .label$ = "6"
690                 .label$ = "'eval.unknown$'"
691         endif
692         
693         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
694         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
695         
696         # Set evaluation
697         call increment_toneevaluation_value 'sgc.pinyin$' 'eval.total$'
698         if .tones$ = .recognized$
699                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.correct$'
700                 .result = 1
701         else
702                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.wrong$'
703                 .result = 0
704         endif
705         if index_regex(.label$, "(Correct|Wrong)") <= 0
706                 if .label$ = "Correct"
707                         .label$ = eval.correct$
708                 else
709                         .label$ = eval.wrong$
710                 endif
711                 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
712                 call increment_toneevaluation_value 'sgc.pinyin$' 'eval.commented$'
713                 .result = 0
714         endif
715         
716         # Update performance table when SaveAudio is on
717         if sgc.saveAudioOn
718                 call update_toneevaluation_file
719         endif
720 endproc
722 procedure setGrade .grade$
723         select sgc.currentWordlist
724         if recordedSound$ <> "" and sgc.pinyin$ <> ""           
725                 call get_toneevaluation_row 'sgc.pinyin$'
726                 .row = get_toneevaluation_row.row
727                 select sgc2.performanceTable
728                 .col = Get column index... Grade
729                 if .col <= 0
730                         Insert column... 2 Grade
731                 endif
732                         
733                 Set numeric value: .row, "Grade", '.grade$'
734                 .numRows = Get number of rows
735                 .sumGrade = 0
736                 .sumN = 0
737                 for .r from 2 to .numRows
738                         .tmp = Get value: .r, "Grade"
739                         if .tmp <> undefined
740                                 .sumGrade += .tmp
741                                 .sumN += 1
742                         endif
743                 endfor
744                 .meanGrade = -1
745                 if .sumN > 0
746                         .meanGrade = .sumGrade / .sumN
747                 endif
748                 Set numeric value: 1, "Grade", .meanGrade
749                 
750                 # Store current performance table
751                 call update_toneevaluation_file
752         endif
753 endproc
755 # Set mainPage.saveAudio$ to Grade
756 procedure set_grade_display .pinyin$
757         mainPage.saveAudio$ = ""
758         select sgc2.performanceTable
759         .grade$ = ""
760         .col = Get column index: "Grade"
761         if .pinyin$ <> "" and .col > 0
762                 call get_toneevaluation_row '.pinyin$'
763                 .row = get_toneevaluation_row.row
764                 if .row > 0
765                         .grade$ = Get value: .row, "Grade"
766                         if index_regex(.grade$, "[0-9]") <= 0
767                                 .grade$ = ""
768                         endif
769                 endif
770         endif
771         if .grade$ <> ""
772                 mainPage.saveAudio$ = .grade$
773         endif
774 endproc
775         
776 ###############################################################
778 # Miscelaneous procedures
780 ###############################################################
782 # TTS synthesis
783 procedure synthesize_sound .pinyin$
784         .command$ = ""
785         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
786                 .command$ < 'speakCommandFile$'
787                 .command$ = replace$(.command$, "'newline$'", " ", 0)
788                 if macintosh
789                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
790                 elsif unix
791                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
792                 elsif windows
793                         system_nocheck call '.command$' "'.pinyin$'"
794                 endif
795         elsif sgc.nativeTTScommand$ <> "" and config.voice <= 0
796                 .text$ = replace_regex$(.pinyin$, "0$", "''", 0)
797                 system_nocheck 'sgc.nativeTTScommand$' "'.text$'"
798         elsif sgc2.synthesizer > 0
799                 select sgc2.synthesizer
800                 Play text... '.pinyin$'
801         endif
802 endproc
804 procedure align_recordedSound .pinyin$
805         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
806                 select Sound 'recordedSound$'
807                 .recordedTextGrid = To TextGrid... pinyin
808                 Set interval text... 1 1 '.pinyin$'
809                 select sgc2.synthesizer
810                 plus Sound 'recordedSound$'
811                 plus .recordedTextGrid
812                 sgc.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
813                 Rename... AlignedTextGrid
814                 select .recordedTextGrid
815                 Remove
816         endif
817 endproc