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