Updated external audio recorder
[sgc2.git] / InitialiseSGC2.praat
blobc376e6a3bc3862fc92ecadf071f8750cbe8a333c
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 te.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 if windows
171         sgc.scratchAudioDir$ = "'preferencesAppDir$'\audio\"
172 else
173         sgc.scratchAudioDir$ = "/tmp/SpeakGoodChinese/"
174 endif
175 sgc.useAlternativeRecorder = 0
176 call set_up_recorder
178 # Set inital language
179 call set_language 'config.language$'
181 # Get the word-list
182 call load_word_list "'localWordlistDir$'" 0
184 # Set up evaluation table
185 initialiseSGC2.toneevaluation_table$ = ""
186 call initialize_toneevaluation_tables
188 # Draw inital window
189 call init_window
191 ###############################################################
193 # Obligatory procedures
195 ###############################################################
196 # Initialize Demo Window
197 procedure init_window
198     demo Erase all
199         demo Line width... 'defaultLineWidth'
200         demo 'defaultFont$'
201         call set_font_size 'defaultFontSize'
202         demo Black
203         
204         # If there is no recorded sound, try to read stored recording
205         if recordedSound$ = "" and sgc.saveAudio$ <> ""
206                 sgc.pinyin$ = ""
207                 select Table 'wordlist$'
208                 if te.currentWord > 0 and te.currentWord <= te.numberOfWords
209                         sgc.pinyin$ = Get value... 'te.currentWord' Pinyin
210                         .outputName$ = "'sgc.saveAudio$'/'sgc.pinyin$'.wav"
211                         if fileReadable(.outputName$)
212                                 te.recordedSound = Read from file: .outputName$
213                                 recordedSound$ = selected$("Sound")
214                                 call recognizeTone
215                                 
216                                 # Set config.audioName
217                                 .tmp$ = replace_regex$(sgc.saveAudio$, "[^/\\]+[/\\]?$", "", 0)
218                                 config.audioName$ = replace$(sgc.saveAudio$, .tmp$, "", 0)
219                                 
220                         endif
221                 endif
222         endif
223         
224         # Set mainPage.saveAudio$ grade
225         call set_grade_display 'sgc.pinyin$'
226         
227         # Update screen
228         call reset_viewport
229         .windowTitle$ = replace$(wordlistName$, "_", " ", 0)
230         call set_window_title 'buttons$' '.windowTitle$'
231         # Display the word-list
232         call write_word_list
233     # Wipe screen and draw background
234     call wipeArea 'wipeContourArea$'
235         if config.showBackground
236                 call draw_background Background
237         endif
238     # Define buttons in a table
239     call init_buttons
240     # Set Play button
241         call Set_Play_Button
242     
243         # Draw the contour
244         call draw_tone_contour
245         # SaveAudio light
246         call paint_saveAudio_light
247         # Set play button
248         
249 endproc
251 # Make sure all Preferences directories are available
252 procedure set_up_directories
253         .dirPath$ = "'preferencesAppDir$'"
254         createDirectory(.dirPath$)
255         .dirPath$ > '.dirPath$'/directory.txt
256         createDirectory("'.dirPath$'/wordlists")
257         .dirPath$ > '.dirPath$'/wordlists/directory.txt
258         createDirectory("'.dirPath$'/pitchmodels")
259         .dirPath$ > '.dirPath$'/pitchmodels/directory.txt
260         createDirectory("'.dirPath$'/log")
261         .dirPath$ > '.dirPath$'/log/directory.txt
262         createDirectory("'.dirPath$'/TTS")
263         .dirPath$ > '.dirPath$'/TTS/directory.txt
264 endproc
267 # Retrieve and store setting between sessions
269 procedure read_preferences .preferencesFile$
270         if not fileReadable(.preferencesFile$)
271                 .preferencesFile$ = preferencesAppFile$
272         endif
273         if fileReadable(.preferencesFile$)
274                 Read from file... 'preferencesAppFile$'
275                 .preferenceTable$ = selected$("Table")
276                 .numPrefKeys = Get number of rows
277                 for .row to .numPrefKeys
278                         .variableName$ = Get value... '.row' Key
279                         if variableExists(.variableName$)
280                                 .variableValue = Get value... '.row' Value
281                                 if .variableValue <> undefined
282                                         '.variableName$' = '.variableValue'
283                                 endif
284                         elsif variableExists(.variableName$+"$")
285                                 .variableValue$ = Get value... '.row' Value
286                                 .variableName$ = .variableName$+"$"
287                                 '.variableName$' = "'.variableValue$'"
288                         endif
289                 endfor
290                 
291                 select Table '.preferenceTable$'
292                 Remove
293         endif
294 endproc
296 procedure write_preferences .preferencesFile$
297         Create Table with column names... Preferences 0 Key Value
298         if index_regex(.preferencesFile$, "[a-zA-Z0-9]") <= 0
299                 .preferencesFile$ = preferencesAppFile$
300         endif
301         .row = 0
302         
303         for .tableNum from 1 to 2
304                 if .tableNum = 1
305                         .table$ = "Config"
306                 elsif .tableNum = 2
307                         .table$ = "MainPage"
308                 endif
309                 .varPrefix$ = replace_regex$(.table$, "^(.)", "\l\1", 0)
311                 select Table '.table$'
312                 .numTableRows = Get number of rows
313                 for .tablerow to .numTableRows
314                         select Table '.table$'
315                         .label$ = Get value... '.tablerow' Label
316                         .variableName$ = .varPrefix$+"."+(replace_regex$(.label$, "^(.)", "\l\1", 0))
317                         .keyName$ = .variableName$
318                         .value$ = ""
319                         if sgc.nonPreferences$ <> "" and index(sgc.nonPreferences$, " '.variableName$' ")
320                                 .value$ = ""
321                         elsif variableExists(.variableName$)
322                                 .value = '.variableName$'
323                                 .value$ = "'.value'"
324                         elsif variableExists(.variableName$ + "$")
325                                 .variableName$ = .variableName$ + "$"
326                                 .value$ = '.variableName$'
327                         elsif index(.variableName$, "_") > 0
328                                 .variableName$ = left$(.variableName$, rindex(.variableName$, "_") - 1) 
329                                 .keyName$ = .variableName$
330                                 select Table Preferences
331                                 .prefRow = Search column... Key '.keyName$'
332                                 if .prefRow <= 0
333                                         if variableExists(.variableName$)
334                                            .value = '.variableName$'
335                                            .value$ = "'.value'"
336                                         elsif variableExists(.variableName$ + "$")
337                                            .variableName$ = .variableName$ + "$"
338                                            .value$ = '.variableName$'
339                                         endif
340                                 endif
341                         endif
343                         if .value$ <> ""
344                                 select Table Preferences
345                                 Append row
346                                 .row += 1
347                                 Set string value... '.row' Key '.keyName$'
348                                 Set string value... '.row' Value '.value$'
349                         endif
350                 endfor
351         endfor
352         
353         # Some extra settings
354         select Table Preferences
355         Append row
356         .row += 1
357         Set string value... '.row' Key wordlistDir
358         Set string value... '.row' Value 'localWordlistDir$'
360         select Table Preferences
361         Append row
362         .row += 1
363         Set string value... '.row' Key wordlistName
364         Set string value... '.row' Value 'wordlistName$'
366         select Table Preferences
367         Write to table file... '.preferencesFile$'
368         Remove
370 endproc
372 ###############################################################
374 # TTS (speech synthesis). Place commands in user info
376 # Prerequisites
377 # speakCommandDir$ = "'preferencesAppDir$'/TTS"
378 # speakCommandFile$ = ""
380 # Make sure sgc2.synthesizer is set!
381 procedure create_default_TTS .language$ .voice$ .samplefreq .gap .pitch_adj .pitch_range .words_per_minute .use_data$ .espeak_phones$
382         sgc2.synthesizer = Create SpeechSynthesizer... '.language$' '.voice$'
383         Set speech output settings... .samplefreq .gap .pitch_adj .pitch_range .words_per_minute '.use_data$' '.espeak_phones$'
384 endproc
386 procedure set_up_TTS
387         if sgc2.synthesizer > 0
388                 select sgc2.synthesizer
389                 Remove
390                 sgc2.synthesizer = -1
391         endif
392         # Note, the TTScommand must set sgc2.synthesizer!
393         if config.strict$ = "'sgc.highestLevel'"
394                 'sgc2.advancedTTScommand$'
395         else
396                 'sgc2.normalTTScommand$'
397         endif
399         if macintosh
400                 .ttscommand$ = "speak"
401                 .osName$ = "OSX"
402                 .quote$ = ""
403         elsif unix
404                 .ttscommand$ = "espeak"
405                 .osName$ = "UNIX"
406                 .quote$ = ""
407         elsif windows
408                 .ttscommand$ = "C:/Program Files/eSpeak/command_line/espeak.exe"
409                 .osName$ = "WIN"
410                 .quote$ = """"
411         endif
412         # Global TTS command has precedence
413         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_command.txt")
414                 speakCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_command.txt"
415                 if config.synthesis$ = "_DISABLED_"
416                         # Default is ON
417                         config.synthesis$ = "eSpeak"
418                 endif
419         endif
421         # Local TTS command, if there is no global one
422         # Command does not exist yet, create it if eSpeak is installed
423         # Make sure to quote the path!
424         if speakCommandFile$ = ""
425                 # Autodetect synthesizer command
426 #               if not fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
427 #                       .command_path$ = ""
428 #                       if macintosh
429 #                               system_nocheck  PATH=${PATH}:/opt/local/bin; bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
430 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
431 #                       elsif unix
432 #                               system_nocheck  PATH=${PATH};bash -c -- 'which '.ttscommand$' > "'speakCommandDir$'/command_path.txt"'
433 #                               .command_path$ < 'speakCommandDir$'/command_path.txt
434 #                       elsif windows
435 #                               if fileReadable(.ttscommand$)
436 #                                       .command_path$ = .ttscommand$
437 #                               endif
438 #                       endif
439 #                       # Remove any newlines
440 #                       .command_path$ = replace$(.command_path$, "'newline$'", "", 0)
442 #                       # Command path found
443 #                       if sgc2.synthesizer <= 0 and .command_path$ <> "" and fileReadable(.command_path$)
444 #                               deleteFile("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
445 #                               fileappend "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"  
446 #                               ...'.quote$''.ttscommand$''.quote$' -v zh+f4 -s 100 'newline$'
447 #                               speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
448 #                       endif
449 #               endif
450 #               if fileReadable("'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt")
451                         speakCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_eSpeak_command.txt"
452                 endif
453                 deleteFile("'speakCommandDir$'/command_path.txt")
454         endif
455         
456         # Close off (other TTS maybe a later option)
457         if sgc2.synthesizer > 0 or (speakCommandFile$ <> "" and fileReadable(speakCommandFile$))
458                 config.synthesis$ = "eSpeak"
459         endif
460 endproc
462 # Alternative audio recorder
463 procedure set_up_recorder
464         if macintosh
465                 .osName$ = "OSX"
466                 .quote$ = ""
467         elsif unix
468                 .osName$ = "UNIX"
469                 .quote$ = ""
470         elsif windows
471                 .osName$ = "WIN"
472                 .quote$ = """"
473         endif
474         # Global TTS command has precedence
475         sgc.useAlternativeRecorder = 0
476         if fileReadable("'globalTTSdir$'/TTS_'.osName$'_record_command.txt")
477                 createDirectory(sgc.scratchAudioDir$)
478                 sgc.recordCommandFile$ = "'globalTTSdir$'/TTS_'.osName$'_record_command.txt"
479                 sgc.useAlternativeRecorder = 1
480         elsif fileReadable("'speakCommandDir$'/TTS_'.osName$'_record_command.txt")
481                 createDirectory(sgc.scratchAudioDir$)
482                 sgc.recordCommandFile$ = "'speakCommandDir$'/TTS_'.osName$'_record_command.txt"
483                 sgc.useAlternativeRecorder = 1
484         endif
485 endproc
487 ###############################################################
489 # Evaluation tables
491 # Prerequisites
492 # initialiseSGC2.toneevaluation_table$ = ""
493 procedure initialize_toneevaluation_tables
494         # Remove old table
495         if initialiseSGC2.toneevaluation_table$ != ""
496                 select Table 'initialiseSGC2.toneevaluation_table$'
497                 Remove
498         endif
499         initialiseSGC2.toneevaluation_table$ = ""
500         
501         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$)
502                 sgc2.performanceTable = Read Table from tab-separated file: sgc.savePerf$
503                 Rename: "Performance"
504                 # Temporary cludge to handle old files
505                 .column = Get column index: "Level"
506                 if .column <= 0
507                         select sgc2.performanceTable
508                         Append column: "Level"
509                 endif
510         else
511                 # Create new table
512                 sgc2.performanceTable = Create Table with column names... Performance 0 Pinyin Correct Wrong Total High Low Wide Narrow Unknown Commented Level Time
513         endif
514         initialiseSGC2.toneevaluation_table$ = "Performance"
515         call get_toneevaluation_row Total
516 endproc
518 procedure get_toneevaluation_row .pinyin$
519         select Table 'initialiseSGC2.toneevaluation_table$'
520     .row = Search column... Pinyin '.pinyin$'
521         if .row < 1
522                 Append row
523                 .row = Get number of rows
524                 Set string value... '.row' Pinyin '.pinyin$'
525                 Set numeric value... '.row' Correct 0
526                 Set numeric value... '.row' Wrong 0
527                 Set numeric value... '.row' Total 0
528                 Set numeric value... '.row' High 0
529                 Set numeric value... '.row' Low 0
530                 Set numeric value... '.row' Wide 0
531                 Set numeric value... '.row' Narrow 0
532                 Set numeric value... '.row' Unknown 0
533                 Set numeric value... '.row' Commented 0
534                 Set numeric value... '.row' Level 0
535                 Set numeric value... '.row' Time 0
536                 Sort rows... Pinyin
537         .row = Search column... Pinyin '.pinyin$'
538         endif
539 endproc
541 procedure increment_toneevaluation_in_row .row .column$
542         select Table 'initialiseSGC2.toneevaluation_table$'
543         .tmp = Get value... '.row' '.column$'
544         .tmp += 1
545         Set numeric value... '.row' '.column$' '.tmp'
546         
547         # Set Level
548         Set string value: .row, "Level", config.strict$
549         
550         # Set time stamp
551         .dateTime$ = date$()
552         Set string value: .row, "Time", .dateTime$
553         
554 endproc
556 # Update existing performance file with toneevaluation
557 procedure update_toneevaluation_file
558         if sgc.savePerf$ <> "" and fileReadable(sgc.savePerf$) and initialiseSGC2.toneevaluation_table$ <> ""
559                 select Table 'initialiseSGC2.toneevaluation_table$'
560                 Write to table file... 'sgc.savePerf$'
561         endif
562 endproc
564 procedure increment_toneevaluation_value .pinyin$ .column$
565         call get_toneevaluation_row '.pinyin$'
566         .row = get_toneevaluation_row.row
567         call increment_toneevaluation_in_row '.row' '.column$'
568         
569         call get_toneevaluation_row Total
570         .row = get_toneevaluation_row.row
571         call increment_toneevaluation_in_row '.row' '.column$'
572 endproc
574 procedure add_feedback_to_toneevaluation .table$
575     select Table '.table$'
576     .line1$ = Get value... 1 Text
577     .line2$ = Get value... 2 Text
578     .label$ = Get value... 3 Text
579         if .label$ = "6"
580                 .label$ = "Unknown"
581         endif
582         
583         .tones$ = replace_regex$(left$(.line1$, rindex(.line1$, ":")), "[^0-9]", "", 0)
584         .recognized$ = replace_regex$(right$(.line1$, length(.line1$)-rindex(.line1$, ":")), "[^0-9]", "", 0)
585         
586         # Set evaluation
587         call increment_toneevaluation_value 'sgc.pinyin$' Total
588         if .tones$ = .recognized$
589                 call increment_toneevaluation_value 'sgc.pinyin$' Correct
590         else
591                 call increment_toneevaluation_value 'sgc.pinyin$' Wrong
592         endif
593         if index_regex(.label$, "(Correct|Wrong)") <= 0
594                 call increment_toneevaluation_value 'sgc.pinyin$' '.label$'
595                 call increment_toneevaluation_value 'sgc.pinyin$' Commented
596         endif
597         
598         # Update performance table when SaveAudio is on
599         if sgc.saveAudioOn
600                 call update_toneevaluation_file
601         endif
602 endproc
604 procedure setGrade .grade$
605         select Table 'wordlist$'
606         if recordedSound$ <> "" and sgc.pinyin$ <> ""           
607                 call get_toneevaluation_row 'sgc.pinyin$'
608                 .row = get_toneevaluation_row.row
609                 select Table 'initialiseSGC2.toneevaluation_table$'
610                 .col = Get column index... Grade
611                 if .col <= 0
612                         Insert column... 2 Grade
613                 endif
614                         
615                 Set numeric value: .row, "Grade", '.grade$'
616                 .numRows = Get number of rows
617                 .sumGrade = 0
618                 .sumN = 0
619                 for .r from 2 to .numRows
620                         .tmp = Get value: .r, "Grade"
621                         if .tmp <> undefined
622                                 .sumGrade += .tmp
623                                 .sumN += 1
624                         endif
625                 endfor
626                 .meanGrade = -1
627                 if .sumN > 0
628                         .meanGrade = .sumGrade / .sumN
629                 endif
630                 Set numeric value: 1, "Grade", .meanGrade
631                 
632                 # Store current performance table
633                 call update_toneevaluation_file
634         endif
635 endproc
637 # Set mainPage.saveAudio$ to Grade
638 procedure set_grade_display .pinyin$
639         mainPage.saveAudio$ = ""
640         select Table 'initialiseSGC2.toneevaluation_table$'
641         .grade$ = ""
642         .col = Get column index: "Grade"
643         if .pinyin$ <> "" and .col > 0
644                 call get_toneevaluation_row '.pinyin$'
645                 .row = get_toneevaluation_row.row
646                 if .row > 0
647                         .grade$ = Get value: .row, "Grade"
648                         if index_regex(.grade$, "[0-9]") <= 0
649                                 .grade$ = ""
650                         endif
651                 endif
652         endif
653         if .grade$ <> ""
654                 mainPage.saveAudio$ = .grade$
655         endif
656 endproc
657         
658 ###############################################################
660 # Miscelaneous procedures
662 ###############################################################
664 # TTS synthesis
665 procedure synthesize_sound .pinyin$
666         .command$ = ""
667         if speakCommandFile$ <> "" and fileReadable(speakCommandFile$)
668                 .command$ < 'speakCommandFile$'
669                 .command$ = replace$(.command$, "'newline$'", " ", 0)
670                 if macintosh
671                         system_nocheck PATH=${PATH}:/usr/local/bin:/opt/local/bin; bash -rc -- ''.command$' "'.pinyin$'"'
672                 elsif unix
673                         system_nocheck bash -rc -- ''.command$' "'.pinyin$'"'
674                 elsif windows
675                         system_nocheck call '.command$' "'.pinyin$'"'
676                 endif
677         elsif sgc2.synthesizer > 0
678                 select sgc2.synthesizer
679                 Play text... '.pinyin$'
680         endif
681 endproc
683 procedure align_recordedSound .pinyin$
684         if 0 and sgc2.synthesizer > 0 and recordedSound$ <> ""
685                 select Sound 'recordedSound$'
686                 .recordedTextGrid = To TextGrid... pinyin
687                 Set interval text... 1 1 '.pinyin$'
688                 select sgc2.synthesizer
689                 plus Sound 'recordedSound$'
690                 plus .recordedTextGrid
691                 sgc2.alignedTextGrid = nowarn noprogress To TextGrid (align,trim)... 1 1 1 -35 0.1 0.1 0.08
692                 Rename... AlignedTextGrid
693                 select .recordedTextGrid
694                 Remove
695         endif
696 endproc