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