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