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