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