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