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