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